Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS

Similar presentations


Presentation on theme: "Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS"— Presentation transcript:

1 Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS

2 A Name and A Value Remember the Java primitive types ◦byte, short, int, long, float, double, boolean, char And class types ◦Like the class Dog we defined in previous lectures Declaring a variable of any type is always the same ◦TYPE VariableName1, VariableName2; But not all names are the same! Back to basics: What is a variable, what is its purpose? Where are data stored? How are data referenced/accessed? Variables are used to store data Data 2 Data 1 Data 3 Data 2 Data 1 Data 3 Data are stored in memory A1 A2 A3 Each piece of data has an address [] accesses the contents of an address

3 Primitive Data Types VariableName1 VariableName2 VariableName3 The names of primitive variables directly access the contents stored in them Data 2 Data 1 Data 3 A1 A2 A3 Memory

4 Object Class Data Types They are declared the same, but … VariableName1 VariableName2 VariableName3 The names of class variables can refer to an object or not Data 2 Data 1 Data 3 A1 A2 A3 Memory Not an object A variable’s data refer can refer to an object This variables are references to objects

5 What Does This Mean For Me? When you declare a variable of a class type ◦Example: Dog scooby; When you create a new object of type Dog ◦Example: new Dog(); When an object is assigned to variable ◦Example: scooby = new Dog(); scooby A1 Memory Data 1 A reference pointing nowhere A new object is created Object

6 Species klingonSpecies, earthSpecies; klingonSpecies.setSpecies("Klingon ox", 10, 15); earthSpecies.setSpecies("Black rhino", 11, 2); earthSpecies = klingonSpecies; earthSpecies.setSpecies("Elephant", 100, 12); System.out.println("earthSpecies:"); earthSpecies.writeOutput(); System.out.println("klingonSpecies:"); klingonSpecies.writeOutput(); - name: String - population: int - growthRate: double Species + setSpecies(String newName, int newPopulation, double newGrowthRate): void + getName(): String + getPopulation(): int + getGrowthRate( ): growthRate + writeOutput(): void

7 References So both variables klingonSpecies and earthSpecies store references to Species objects What does the == then do on references? klingonSpeciesearthSpecies Species object So class type variables essentially contain memory addresses The address is always called a reference in Java SpeciesFourthTry klingonSpecies = new SpeciesFourthTry(); SpeciesFourthTry earthSpecies = new SpeciesFourthTry(); klingonSpecies.setSpecies("Klingon ox", 10, 15); earthSpecies.setSpecies("Klingon ox", 10, 15); if (klingonSpecies == earthSpecies) System.out.println("They are EQUAL."); else System.out.println("They are NOT equal.");

8 Actually Comparing Two Objects How do we compare strings? You can define an equals() method public boolean equals(Species otherObject) { return (this.name.equalsIgnoreCase(otherObject.name)) && (this.population == otherObject.population) && (this.growthRate == otherObject.growthRate); } if (klingonSpecies.equals(earthSpecies)) System.out.println("They are EQUAL."); else System.out.println("They are NOT equal."); Always name the method testing for equality equals

9 Other Methods Returning boolean Use names that make sense in English when used with if..else statements, while loops, etc. public Boolean isExtinct() { return population == 0; } if (s1.isExtinct()) { // Yes, it’s extinct }

10 Class Types are Passed Using call-by-reference Defining method parameters of primitive types is equivalent to declaring a local variable public boolean equals(Species otherObject) { otherObject = new Species(); return (this.name.equalsIgnoreCase(otherObject.name)) && (this.population == otherObject.population) && (this.growthRate == otherObject.growthRate); }

11 Class Types are Passed Using call-by-reference Let’s introduce a new Species class method public boolean eatSpecies(Species unluckySpecies) { unluckySpecies.population = 0; this.growthRate += 5; } Species tyrannosaurus = new Species(), elephant = new Species(); tyrannosaurus.setSpecies(“Dinosaur", 10, 15); elephant.setSpecies(“Mamal", 11, 2); tyrannosaurs.eatSpecies(elephant); elephant.writeOutput(); Which object is updated? Why is this accessible?

12

13 Lost Objects Species tyrannosaurus = new Species(), elephant = new Species(); tyrannosaurus.setSpecies(“Dinosaur", 10, 15); elephant.setSpecies(“Mamal", 11, 2); tyrannosaurus = elephant;

14 Unit Testing Testing our program with different inputs or even all possible inputs is good! ◦However not possible for larger programs Test smaller parts of your program/class individually ◦AKA Unit Testing Species testSpecies = new Species(); // Test the setSpecies method testSpecies.setSpecies("Tribbles", 100, 50); if (testSpecies.getName().equals("Tribbles") && (testSpecies.getPopulation() == 100) && (testSpecies.getGrowthRate() >= 49.99) && (testSpecies.getGrowthRate() <= 50.01)) { System.out.println("Pass: setSpecies test."); } else { System.out.println("FAIL: setSpecies test."); } Since getGrowthRate returns a double we should not attempt testSpecies. getGrowthRate() == 50

15

16


Download ppt "Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS"

Similar presentations


Ads by Google