1 / 28

CSE 1020:Using Objects

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.

bendek
Télécharger la présentation

CSE 1020:Using Objects

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. CSE 1020:Using Objects Mark Shtern

  2. Summary Read API Method binding and overloading Development process Input validation Assert

  3. Utility Class • Features are static • No Instantiation • Memory Diagram (Fig 3.12 and Fig 3.14) • Advantages of Utility Class • Simplicity • Suitability

  4. Objects An Abstraction View An API View

  5. The Life of an Object • The Birth of an Object • Locate the Class • Declare a Reference • Instantiate the Class • Assign the Reference

  6. 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

  7. 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.

  8. 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()) ;

  9. 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);

  10. Equality of References output.println(f1 == f2); output.println(f4 == f5);

  11. Equality of Objects output.println(f1.equals(f4)); output.println(f4.equals(f2));

  12. 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

  13. The Death of an Object Fraction x = new Fraction(3,5); Fraction y=x; y = new Fraction(4,7); x=null;

  14. 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);

  15. Predict its output Answer 1 Fraction f = new Fraction(5,3); f.setDenominator(25); output.println(f.getNumerator());

  16. 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

  17. Summary Object (attributes, methods, states and identity) Object Life Time Reference vs Object Attribute Privacy

  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 true true

  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 false

  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)); • Replace the comment with one statement such that output is 3 false true

  21. 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

  22. 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);

  23. 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());

  24. 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());

  25. Object with final Feature • Final fields are made static: • Saves memory • Access without having to create instance first

  26. 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.

  27. 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

  28. 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());

More Related