cse 1020:using objects
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
CSE 1020:Using Objects
Mark Shtern
1-1
Summary
• Read API• Method binding and overloading• Development process• Input validation• Assert
1-2
Utility Class
• Features are static• No Instantiation • Memory Diagram (Fig 3.12 and Fig 3.14)• Advantages of Utility Class– Simplicity– Suitability
1-3
Objects
• An Abstraction View• An API View
1-4
The Life of an Object
• The Birth of an Object– Locate the Class– Declare a Reference– Instantiate the Class– Assign the Reference
1-5
Object at Work
• 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
1-6
AnswerFraction f;f = new Fraction(8,6)output.println(f.getNumerator());output.println(f.toProperString())
Object at Work
• 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.
1-7
Answerfraction f = new Fraction (8,6);output.println(f.toString());f.Separator = ‘\u00f7’;output.println(f.toString());
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()) ;
1-8
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);
1-9
Equality of References
output.println(f1 == f2);output.println(f4 == f5);
1-10
Equality of Objects
output.println(f1.equals(f4));output.println(f4.equals(f2));
1-11
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
1-12
The Death of an Object
Fraction x = new Fraction(3,5);Fraction y=x;y = new Fraction(4,7);x=null;
1-13
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);
1-14
Predict its output
Fraction f = new Fraction(5,3);f.setDenominator(25);output.println(f.getNumerator());
1-15
Answer1
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
1-16
Summary
• Object (attributes, methods, states and identity)
• Object Life Time• Reference vs Object• Attribute Privacy
1-17
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
1-18
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
1-19
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
1-20
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
1-21
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);
1-22
Object with static Features (predict its output)
Fraction f = new Fraction(3,2);output.println(f.toProperString());f.isQuoted = false;output.println(f.toProperString());
1-23
Answer “1 1/2”1 1/2
Object with static Features (predict its output)
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());
1-24
Answer1 1/22 1/2
Object with final Feature
• Final fields are made static:– Saves memory– Access without having to create instance first
1-25
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.
1-26
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
1-27
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());
1-28