280 likes | 426 Views
CSE 1020:Using Objects. Mark Shtern. Summary. Read API Method binding and overloading Development process Input validation Assert. Utility Class. Features are static No Instantiation Memory Diagram (Fig 3.12 and Fig 3.14) Advantages of Utility Class Simplicity Suitability.
E N D
CSE 1020:Using Objects Mark Shtern
Summary Read API Method binding and overloading Development process Input validation Assert
Utility Class • Features are static • No Instantiation • Memory Diagram (Fig 3.12 and Fig 3.14) • Advantages of Utility Class • Simplicity • Suitability
Objects An Abstraction View An API View
The Life of an Object • The Birth of an Object • Locate the Class • Declare a Reference • Instantiate the Class • Assign the Reference
Object at Work Answer Fraction f; f = new Fraction(8,6) output.println(f.getNumerator()); output.println(f.toProperString()) Write a code fragment that creates a fraction object that corresponds to 8/6 and then outputs the results of invoking getNumerator and toProperString on it
Object at Work Answer fraction f = new Fraction (8,6); output.println(f.toString()); f.Separator = ‘\u00f7’; output.println(f.toString()); Write a code fragment that creates fraction object that corresponds to 8/6 and then outputs the results of invoking toString method on it, once with the default separator and once with a different separator.
The Object and its Reference Fraction f1; f1 = new Fraction(6,3); Fraction f2; f2 = f1; f2 = new Fraction(5,8); f2 = null; output.println(f2.getNumerator()) ;
Objects’ Equality Fraction f1 = new Fraction(3,5); Fraction f2 = f1; Fraction f3 = new Fraction(2,7); Fraction f4 = new Fraction(6,10); Fraction f5 = f4; output.println(f1 == f2); output.println(f4 == f5);
Equality of References output.println(f1 == f2); output.println(f4 == f5);
Equality of Objects output.println(f1.equals(f4)); output.println(f4.equals(f2));
Obligatory Methods toString() returns a string that represents the current object equals() indicates whether some other object is "equal to" this one hashCode() returns an integer (hash code value) for the object. This method is supported for the benefit of containers
The Death of an Object Fraction x = new Fraction(3,5); Fraction y=x; y = new Fraction(4,7); x=null;
Object’s state • State • Determine the state of objects • Change the state of objects • Accessors • public typeX getX(); • public boolean isVisible(); • Mutators • public void setX(typeX value);
Predict its output Answer 1 Fraction f = new Fraction(5,3); f.setDenominator(25); output.println(f.getNumerator());
Attribute Privacy Neither an accessor nor a mutator: zero visibility An accessor and no mutator: read only access A mutator and no accessor: write only access An accessor and a mutator:read/write access
Summary Object (attributes, methods, states and identity) Object Life Time Reference vs Object Attribute Privacy
Example 4.5 • Consider the following fragment Fraction f = new Fraction(1,2); Fraction g = f; // Place mystery statement here output.println(f.getNumerator()); output.println(f==g); output.println(e.equals(g)); • Replace the comment with one statement such that output is 3 true true
Example 4.5 • Consider the following fragment Fraction f = new Fraction(1,2); Fraction g = f; // Place mystery statement here output.println(f.getNumerator()); output.println(f==g); output.println(e.equals(g)); • Replace the comment with one statement such that output is 3 false false
Example 4.5 • Consider the following fragment Fraction f = new Fraction(1,2); Fraction g = f; // Place mystery statement here output.println(f.getNumerator()); output.println(f==g); output.println(e.equals(g)); • Replace the comment with one statement such that output is 3 false true
Example 4.5 • Consider the following fragment Fraction f = new Fraction(1,2); Fraction g = f; // Place mystery statement here output.println(f.getNumerator()); output.println(f==g); output.println(e.equals(g)); • If possible, replace the comment with one statement such that output is 3 true false
Example 4.6 Examine the following fragment, and predict its output for various user inputs: Fraction f = new Fraction(7,10); output.print(“Enter a separator...”); boolean ok = f.setSeparator(input.nextLine().charAt(0)); output.println(ok); output.println(f);
Object with static Features (predict its output) Answer “1 1/2” 1 1/2 Fraction f = new Fraction(3,2); output.println(f.toProperString()); f.isQuoted = false; output.println(f.toProperString());
Object with static Features (predict its output) Answer 1 1/2 2 1/2 Fraction f = new Fraction(3,2); f.isQuoted = true; Fraction g = new Fraction(5,2); g.isQuoted = true; output.println(f.toProperString()); output.println(g.toProperString());
Object with final Feature • Final fields are made static: • Saves memory • Access without having to create instance first
Ex4.15 Create an instance of the Random class, a member of the java.util package. Generate two random integers and output the them. Why was nextInt method overloaded? Modify your program so that it also generates two double and boolean values.
Ex 4.20 • Create an instance, soap, of type.lib.Item with the following state: • Item Number: “001” • Item Name: “The ABC Chicken Soup” • Sale Price: $9.75 per unit • Assume store order two batches of these soup items: • 100 units and has overall cost of $500 • 50 units and has overall cost of $400 • Simulate these two orders in your program by using purchase method • Output the following information for this item: • Number, Name, Stock (quantity on hand) and cost price per unit
Ex 4.21 • Continue previous program by processing two more transactions: • Sells 20 units of soup at the posted sale price using sell method • Sells 10 units for a total of $80 • You program must generate two output as following: • output.println(soup.getUnitPrice()); • output.println(soup.getSales()/soup.getSoldQy());