Presentation is loading. Please wait.

Presentation is loading. Please wait.

INF 523Q Chapter 5: Enhancing Classes (Examples).

Similar presentations


Presentation on theme: "INF 523Q Chapter 5: Enhancing Classes (Examples)."— Presentation transcript:

1 INF 523Q Chapter 5: Enhancing Classes (Examples)

2 Num.java b //******************************************************************** b // Num.java Author: Lewis and Loftus b // Represents a single integer as an object. b //******************************************************************** b class Num b { b private int value; b // Sets up the new Num object, storing an initial value. b public Num (int update) b { b value = update; b } b // Sets the stored value to the newly specified value. b public void setValue (int update) b { b value = update; b } b // Returns the stored integer value as a string. b public String toString () b { b return value + ""; } }

3 ParameterTester.java b //******************************************************************** b // ParameterTester.java Author: Lewis and Loftus b // Demonstrates the effects of passing various types of parameters. b //******************************************************************** b class ParameterTester b { b // Modifies the parameters, printing their values before and after making the changes. b public void changeValues (int f1, Num f2, Num f3) b { b System.out.println ("Before changing the values:"); b System.out.println ("f1\tf2\tf3"); b System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); b f1 = 999; b f2.setValue(888); b f3 = new Num (777); b System.out.println ("After changing the values:"); b System.out.println ("f1\tf2\tf3"); b System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); b }

4 ParameterPassing.java b //****************************************************************** b // ParameterPassing.java Author: Lewis and Loftus b // Demonstrates the effects of passing various types of parameters. b //****************************************************************** b class ParameterPassing b { b // Sets up three variables (one primitive and two objects) to serve as actual parameters b // to the changeValues method. Prints their values before and after calling the method. b public static void main (String[] args) b { b ParameterTester tester = new ParameterTester(); b int a1 = 111; b Num a2 = new Num (222); b Num a3 = new Num (333); b System.out.println ("Before calling changeValues:"); b System.out.println ("a1\ta2\ta3"); b System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); b tester.changeValues (a1, a2, a3); b System.out.println ("After calling changeValues:"); b System.out.println ("a1\ta2\ta3"); b System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); } }

5 MyClass.java b //****************************************************************** b // MyClass.java Author: Lewis and Loftus b // Demonstrates the use of the static modifier. b //****************************************************************** b b class MyClass b { b private static int count = 0; b // Counts the number of instances created. b public MyClass () b { b count++; b } b // Returns the number of instances of this class that have been created. b public static int getCount () b { b return count; b }

6 CountInstances.java b //****************************************************************** b // CountInstances.java Author: Lewis and Loftus b // Demonstrates the use of the static modifier. b //****************************************************************** b class CountInstances b { b // Creates several MyClass objects and prints the number of b // objects that were created. b public static void main (String[] args) b { b MyClass obj; b for (int scan=1; scan <= 10; scan++) b obj = new MyClass(); b System.out.println ("Objects created: " + MyClass.getCount()); b }

7 Speaker.java b //****************************************************************** b // Speaker.java Author: Lewis and Loftus b // Demonstrates the declaration of an interface. b //****************************************************************** b interface Speaker b { b public void speak (); b public void announce (String str); b }

8 Philosopher.java b //****************************************************************** b // Philosopher.java Author: Lewis and Loftus b // Demonstrates the implementation of an interface. b //****************************************************************** b b class Philosopher implements Speaker b { b private String philosophy; b b // Establishes this philosopher's philosophy. b public Philosopher (String philosophy) b { b this.philosophy = philosophy; b } b b // Prints this philosophers's philosophy. b public void speak () b { b System.out.println (philosophy); }

9 Philosopher.java (cont.) b b // Prints the specified announcement. b public void announce (String announcement) b { b System.out.println (announcement); b } b b // Prints this philosophers's philosophy multiple times. b public void pontificate () b { b for (int count=1; count <= 5; count++) b System.out.println (philosophy); b }

10 Dog.java b //****************************************************************** b // Dog.java Author: Lewis and Loftus b // Demonstrates the implementation of an interface. b //****************************************************************** b b class Dog implements Speaker b { b // Prints this dog's philosophy. b public void speak () b { b System.out.println ("woof"); b } b b // Prints this dog's philosophy and the specified announcement. b public void announce (String announcement) b { b System.out.println ("woof: " + announcement); b }

11 Talking.java b //****************************************************************** b // Talking.java Author: Lewis and Loftus b // Demonstrates the use of an interface for polymorphic references. b //****************************************************************** b class Talking b { b // Instantiates two objects using an interface reference and invokes one of the b // common methods. Then casts the interface reference into a class reference b // to invoke its unique method. b public static void main (String[] args) b { b Speaker current; b current = new Dog(); b current.speak(); b current = new Philosopher ("I think, therefore I am."); b current.speak(); b ((Philosopher) current).pontificate(); b }


Download ppt "INF 523Q Chapter 5: Enhancing Classes (Examples)."

Similar presentations


Ads by Google