1 / 10

Day double 11.0

Day double 11.0. 4.3 Objects and References. Exercise:. Species - name: String

cian
Download Presentation

Day double 11.0

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. Day double 11.0 4.3 Objects and References

  2. Exercise: Species - name: String - population: int- growthRate: double+ readInput(): void+ writeOutput(): void+ projectedPopulation(int years): int+ set(String newName, intnewPopulation, double newGrowthRate): void+ getName(): String + getPopulation(): int+ getGrowthRate(): double+ equals(Species otherObject): boolean

  3. Reference • The memory address of where an object is called a reference to the object

  4. Reviewing comparing primitives int x = 5; int y = 6; if (x == y) { System.out.println(“They are equal.”);} else{ System.out.println(“They aren’t equal.”);}

  5. Reviewing comparing primitives String x = “Bob”; String y = “Chucky”; if (x == y) { System.out.println(“They are equal.”);} else{ System.out.println(“They aren’t equal.”);}

  6. Comparing objects *See handout

  7. Comparing objects Can we use == to compare objects? Or should we use .equals to compare objects? Let’s experiment!

  8. Comparing objects Create objects s1 and s2 for the class studentInfotry this:

  9. if (s1 == s2)System.out.println(“Match with ==.” ); else System.out.println(“Doesn’t match with ==.”); // versus if (s1.equals(s2)) System.out.println(Match with the method equals.”); else System.out.println(“Doesn’t match with method equals.”);

  10. Boolean comparison of objects public boolean equals(Species otherObject){ return ((name.equalsIgnoreCase(otherObject.name)) && (population == otherObject.population) && (growthRate == otherObject.growthRate)); }

More Related