1 / 11

INF 523Q

INF 523Q. Chapter 5: Enhancing Classes (Examples). Num.java. //******************************************************************** // Num.java Author: Lewis and Loftus // Represents a single integer as an object.

Download Presentation

INF 523Q

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

More Related