1 / 31

Lecture 2 Review of classes

Lecture 2 Review of classes. Richard Gesick. The toString Method. Returns a String representing the data of an object Clients can call toString explicitly by coding the method call. Clients can call toString implicitly by using an object reference where a String is expected.

odell
Download Presentation

Lecture 2 Review of classes

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. Lecture 2Review of classes Richard Gesick

  2. The toString Method • Returns a String representing the data of an object • Clients can call toString explicitly by coding the method call. • Clients can call toStringimplicitly by using an object reference where a String is expected. • Example client code: Auto compact = new Auto( ); // instantiate an object // explicit toString call System.out.println( compact.toString( ) ); // implicit toString call System.out.println( compact );

  3. The toString API

  4. Auto Class toString Method public String toString( ) { DecimalFormatgallonsFormat=new DecimalFormat("#0.0"); return "Model: " + model + "; miles driven: " + milesDriven + "; gallons of gas: " + gallonsFormat.format( gallonsOfGas ); }

  5. The equals Method Determines if the data in another object is equal to the data in this object. Example client code using Auto references auto1 and auto2: if ( auto1.equals( auto2 ) ) System.out.println( "auto1 equals auto2" );

  6. The instanceof Operator Because the equals method’s parameter is of type Object, we need to determine if the parameter is an Auto object. We can use the instanceofoperator to determine if an object reference refers to an object of a particular class. Syntax: objectReferenceinstanceofClassName evaluates to true if objectReferenceis of ClassNametype; false otherwise.

  7. Auto Class equals Method public boolean equals( Object o ) { // if o is not an Auto object, return false if ( ! ( o instanceof Auto ) ) return false; else { Auto objAuto = ( Auto ) o; if ( model.equals( objAuto.model ) && milesDriven == objAuto.milesDriven && Math.abs( gallonsOfGas– objAuto.gallonsOfGas) < 0.0001 ) return true; else return false; } }

  8. static Variables • Also called class variables • One copy of a static variable is created per class. • static variables are not associated with an object. • static constants are often declared as public. • To define a static variable, include the keyword static in its definition: Syntax: accessSpecifier static dataTypevariableName…; Example: public static intcountAutos = 0;

  9. static Methods • Also called class methods • Often defined to access and change static variables • static methods cannot access instance variables: • static methods are associated with the class, not with any object. • static methods do not have access to the implicit parameter this.

  10. Rules for static and Non-static Methods

  11. Reusable Graphical Objects In Chapter 4, the applet code and the Astronaut were tightly coupled; we couldn't draw the Astronaut without running the applet. We want to separate the Astronaut from the applet.

  12. The Astronaut Class To separate the Astronaut from the applet, we define an Astronaut class. • The starting x and y values become instance variables, along with a new scaling factor to allow the client to draw Astronauts of different sizes. • We move the code for drawing the Astronaut into a draw method, with this API: public void draw( Graphics g ) The client will call draw from the paint method of the applet, passing the graphics context object.

  13. The Astronaut Client Applet • The applet instantiates Astronaut objects in init • The init method of an applet is called automatically, once, when the applet starts executing. It is used to perform initialization, such as instantiating objects. • The applet calls draw from the paint method for each Astronaut object.

  14. enum Types • Special class definition designed to increase the readability of code • Allows you to define a set of objects that apply names to ordered sets Examples of ordered sets: • Days of the week • Months of the year • Playing cards

  15. enum Types Built into java.lang (no import statement needed) Syntax: enumEnumName { obj1, obj2,… objn }; Example: enumDays { Sun, Mon, Tue,Wed,Thurs,Fri,Sat}; A constant object is instantiated for each name in the list. Thus, each name is a reference to an object of type Days.

  16. Using an enum Object Referring to an enum object reference: Syntax: EnumType.enumObject Example: Days.Mon Declaring an object reference of an enum type Syntax: EnumTypereferenceName; Example: Days d; // d is initially null d = Days.Thurs;

  17. Useful enum Methods

  18. More Useful enum Methods

  19. Using enum Objects with switch Using enum objects for case constants makes the code more readable. For the case constant, use the enum object reference without the enum type Example: case Fri:

  20. Creating Packages • A package is a collection of related classes that can be imported into a program. • Packages allow reuse of classes without needing to place the class in the same directory/folder as the other source files. • To include a class in a package, precede the class definition with the package statement: package packageName;

  21. Naming Packages To avoid name collisions, which can occur when multiple programmers define packages, we use this naming convention: Use the reverse of the domain name, excluding "www". • For example, for a domain name: www.jbpub.com the package name would begin with: com.jbpub then add the package name: com.jbpub.af

  22. A Reusable Class For example, we can create a class that provides type-safe reading of input from the console that can be reused by our programs . /** Type-Safe Input Using Scanner * Anderson, Franceschi */ package com.jbpub.af; // package name import java.util.Scanner; public class ConsoleIn { // rest of class definition

  23. Create the Directory Structure For the package com.jbpub.af, we create three directories. We place ConsoleIn.java into the afdirectory and compile it.

  24. Modify the CLASSPATH The CLASSPATH environment variable tells the compiler where to look for packages. • Set the CLASSPATH to include the directory/folder in which you created the com directory for your package. • On Windows, if com is created in My Documents, the CLASSPATH might be: .;c:\documents and settings\user\My Documents • On Linux, if com is created in myClasses in your home directory, the CLASSPATH might be: .;/usr/local/java/jre/lib;/home/user/myClasses

  25. Client Use of Package To reuse the classes in a package, the client imports the package. import com.jbpub.af.ConsoleIn;

  26. Javadoc Documentation • The Java class library documentation on Oracle's website (www.oracle.com/technetwork/java) helps us determine how to instantiate objects and call methods for the classes. • This documentation was generated using Javadoc, a tool provided in the Java Development Toolkit (JDK). • We can also use Javadoc to generate web pages that provide documentation on our class's fields and methods.

  27. To Use Javadoc • We need to add Javadoc comments and special tags to our classes. • Javadoc comments begin with /** and end with */ (Note that this is similar to a Java block comment, but with an extra * in the opening syntax.) Example: /** Auto class * Anderson, Franceschi */

  28. Block Tags Identify parameters and return values HTML tags can be used in the descriptions • For example, <BR> to insert a new line

  29. Sample equals Method Documentation /** * equals method:<BR> * Compares the fields of two Auto objects * @param a1 an Auto object * @return true if this object’s fields have * the same values as a1’s fields; else false */ public boolean equals( Object a1 ) { // equals method body }

  30. Executing Javadoc • javadoc.exe is located in the bin directory of the Java JDK • To generate documentation for a class: javadoc ClassName.java Example: javadoc Auto.java • To generate documentation for all classes in a directory: javadoc *.java

  31. Sample Javadoc Documentation

More Related