1 / 13

CIS 270—Application Development II

CIS 270—Application Development II. Chapter 8—Classes and Objects: A Deeper Look. 8.2.1 Time Class Case Study. Two classes: Time1 (Fig 8.1) and Time1Test (Fig. 8.2) Time1 has three private instance variables and three public methods (a.k.a., public services or the public __________).

rmcgee
Download Presentation

CIS 270—Application Development II

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. CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look

  2. 8.2.1 Time Class Case Study • Two classes: Time1 (Fig 8.1) and Time1Test (Fig. 8.2) • Time1 has three private instance variables and three public methods (a.k.a., public services or the public __________). • Time1 has a default ___________ that sets instance variables to 0. • Note the code hour = ((h>=0 && h<24) ? h:0); • It means if(h>=0 && h<24) hour=h; else hour=0; • The Time1 object will always have __________ data (always in range), but not necessarily correct data. interface constructor consistent

  3. 8.2.2 Time Class Case Study • Note the code String.format(“%02d:%02d:%02d", hour, minute, second); in the Time1 class. • format is a method of the __________ class • The %02d means “format is leading 0, two decimal integers” (this formatted output is new in J2SE 5.0) • The three formats (in “”) will be applied to hour, minute, second • Likewise, note the code String.format( "%d:%02d:%02d %s", ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), minute, second, ( hour < 12 ? "AM" : "PM" ) ); • The hour % 12 means hour ______ 12 String mod

  4. 8.2.3 Time Class Case Study • Note Time1 time = new Time1(); in the Time1Test class. • Variable time is assigned the address of a ________ object (time is a reference variable). • The code System.out.println( time.toUniversalString() ); prints the string returned when the object time calls the method toUniversalString() in the class _________. • The code time.setTime( 13, 27, 6 ); is the object time calling the setTime method in Time1. • The class MemberAccessTest in Fig. 8.3 shows that a client cannot directly access _________ members. Time1 Time1 private

  5. 8.4 The this Reference • Note the file ThisTest.java in Fig. 8.4. • ThisTest.java has two classes (but only one can be public, which contains the ________ method here). • Compiling ThisTest.java will create two _______ files. • Parameter names for a constructor should NOT be the same as the instance variable (______) names. • Variables in a method with the same names as fields will shadow (hide) the fields. • In that case, you will need to refer to fields with the keyword _______. • Accessing members within the same class does not require the this reference (see Line 35). main .class field this

  6. 8.5 Overloaded Constructors • Note class Time2 in Fig. 8.5. • A class can have several (__________) constructors if the ________ (parameter list) of each is different. • public Time2() is a no-__________ constructor. • this can be used as the first statement in a constructor’s body to call another constructor. • This use of this allows for code ______ with less maintenance if a constructor needs to be changed. • The methods setTime, toUniversalString, and toString use set and get methods instead of accessing instance variables directly. overloaded signature argument reuse

  7. 8.6 Default and No-Argument Constructors • If a class has no constructors, the compiler creates a default constructor that sets numeric fields to 0, Boolean fields to false, and reference fields to ____. • If a class has constructors, the compiler will not create a default constructor. The class will need a no-argument constructor. • A no-argument constructor with no _____ will set fields to their default values. • A constructor is identified by Java because it has the same name as the class and has no _______ type. null body return

  8. 8.7 Set and Get Methods • Classes provide public methods to allow clients to set and get ________ instance variables. • Set and get methods should only be used as needed. • Set methods are also called mutator methods and get methods are called _________ methods. • Set and get methods make private fields accessible, but not _______. • Get methods can hide the actual data format from the client when they return formatted data. • Set methods can protect fields from careless attempts to modify values by using ______ checking. private accessor public validity

  9. 8.8 Composition • A data member of a class can be a reference to an ______ of another class. • This is known as composition or a _____ relationship. • Note that the Employee class in Fig. 8.8 uses composition. • Employeehas two Date objects as instance variables. • In UML (Unified _________ Language), composition would be expressed as follows: object has-a Modeling Employee Date

  10. 8.9 Enumerations • An enumeration is a special kind of class which declares a set of _________. • enum types are final and __________. • Do not try to create an object of type enum. • An enum can have instance variables, constructors, and methods, much like an ordinary class. • Enhanced for loop (new in J2SE 5.0) • int scores[] = { 87, 93, 92, 95, 88, 90 }; • int var = 0; • for(int var : scores) // var holds each array value • totalScore += var; • See Fig. 8.10 and Fig. 8.11 for example of enum. constants static

  11. 8.11 static Class Members • A static field (class variable) is shared by all objects. • Example: numOfItems in a class called Item: • public static int numberOfItems = 0; • In the constructor Item, include the code numberOfItems++; • A publicstatic field can be directly accessed by an object or with the _______ name: • anItem.numberOfItems • Item.numberOfItems • A ________ static field can be accessed by calling a publicstatic method using the class name: • Item.aMethod() • See Fig. 8.12 and Fig. 8.13 for examples. class private

  12. 8.12-13 static Import and final Instance Variables • static Import • By using code like import static java.lang.Math.*; , you can access the static methods of a class without using the class name. • For example, sqrt(900.0) instead of ______.sqrt(900.0) • finalInstance Variables • Principle of _______ Privilege: code should be granted only the amount of privilege and access that it needs. • Instance variables can be made final—they can only be set in a constructor and cannot be changed otherwise. • private final int INCREMENT; Math Least

  13. 8.14-15 Some OO Concepts • Software reuse • Java is a language, but it is also a ___________ for accessing the Java API (thousands of prewritten classes) • Software can be created by using prewritten components • Data abstraction and encapsulation • Information ______: classes hide their implementation from clients • Data abstraction: clients focus on data, not on implementation details • An __________ data type (ADT) has data representation and operations that manipulate the data • A class is an ADT—it has data and operations framework hiding abstract

More Related