CSE 1020:Using Objects
description
Transcript of CSE 1020:Using Objects
![Page 1: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/1.jpg)
CSE 1020:Using Objects
Mark Shtern
1-1
![Page 2: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/2.jpg)
Summary
• Read API• Method binding and overloading• Development process• Input validation• Assert
1-2
![Page 3: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/3.jpg)
Utility Class
• Features are static• No Instantiation • Memory Diagram (Fig 3.12 and Fig 3.14)• Advantages of Utility Class– Simplicity– Suitability
1-3
![Page 4: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/4.jpg)
Objects
• An Abstraction View• An API View
1-4
![Page 5: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/5.jpg)
The Life of an Object
• The Birth of an Object– Locate the Class– Declare a Reference– Instantiate the Class– Assign the Reference
1-5
![Page 6: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/6.jpg)
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())
![Page 7: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/7.jpg)
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());
![Page 8: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/8.jpg)
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
![Page 9: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/9.jpg)
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
![Page 10: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/10.jpg)
Equality of References
output.println(f1 == f2);output.println(f4 == f5);
1-10
![Page 11: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/11.jpg)
Equality of Objects
output.println(f1.equals(f4));output.println(f4.equals(f2));
1-11
![Page 12: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/12.jpg)
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
![Page 13: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/13.jpg)
The Death of an Object
Fraction x = new Fraction(3,5);Fraction y=x;y = new Fraction(4,7);x=null;
1-13
![Page 14: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/14.jpg)
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
![Page 15: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/15.jpg)
Predict its output
Fraction f = new Fraction(5,3);f.setDenominator(25);output.println(f.getNumerator());
1-15
Answer1
![Page 16: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/16.jpg)
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
![Page 17: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/17.jpg)
Summary
• Object (attributes, methods, states and identity)
• Object Life Time• Reference vs Object• Attribute Privacy
1-17
![Page 18: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/18.jpg)
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
![Page 19: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/19.jpg)
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
![Page 20: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/20.jpg)
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
![Page 21: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/21.jpg)
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
![Page 22: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/22.jpg)
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
![Page 23: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/23.jpg)
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
![Page 24: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/24.jpg)
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
![Page 25: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/25.jpg)
Object with final Feature
• Final fields are made static:– Saves memory– Access without having to create instance first
1-25
![Page 26: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/26.jpg)
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
![Page 27: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/27.jpg)
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
![Page 28: CSE 1020:Using Objects](https://reader036.fdocuments.in/reader036/viewer/2022062422/56813d66550346895da74266/html5/thumbnails/28.jpg)
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