160 likes | 246 Views
Intro to CS – Honors I Class Types and Object References. Georgios Portokalidis gportoka@stevens.edu. A Name and A Value. Remember the Java p rimitive types byte, short, int , long, float, double, boolean , char And class types Like the class Dog we defined in previous lectures
E N D
Intro to CS – Honors IClass Types and Object References Georgios Portokalidis gportoka@stevens.edu
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? Data 1 Variables are used to store data Data 2 Data 3 Data are stored in memory Data 1 [] accesses the contents of an address Data 2 Data 3 A1 A2 A3 Each piece of data has an address
Primitive Data Types The names of primitive variables directly access the contents stored in them Memory VariableName1 Data 1 A1 VariableName2 Data 2 A2 VariableName3 Data 3 A3
Class Data Types • They are declared the same, but … The names of class variables can refer to an object or not VariableName1 Not an object Memory VariableName2 VariableName3 A1 Data 1 A2 Data 2 A3 Data 3 Object This variables are referencesto objects A variable’s data refer can refer to an object
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(); Memory A1 Data 1 scooby A reference pointing nowhere Object A new object is created
Species - name: String - population: int - growthRate: double + setSpecies(String newName, intnewPopulation, double newGrowthRate): void + getName(): String + getPopulation(): int + getGrowthRate( ): growthRate + writeOutput(): void 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();
References • So both variables klingonSpecies and earthSpecies store references to Species objects • What does the == then do on references? earthSpecies klingonSpecies So class type variables essentially contain memory addresses SpeciesFourthTryklingonSpecies = new SpeciesFourthTry(); SpeciesFourthTryearthSpecies = 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."); Species object The address is always called a reference in Java
Actually Comparing Two Objects • How do we compare strings? • You can define an equals() method Always name the method testing for equality equals 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.");
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 }
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); }
Class Types are Passed Using call-by-reference • Let’s introduce a new Species class method Which object is updated? public booleaneatSpecies(Species unluckySpecies) { unluckySpecies.population = 0; this.growthRate+= 5; } Why is this accessible? Species tyrannosaurus = new Species(), elephant = new Species(); tyrannosaurus.setSpecies(“Dinosaur", 10, 15); elephant.setSpecies(“Mamal", 11, 2); tyrannosaurs.eatSpecies(elephant); elephant.writeOutput();
Lost Objects Species tyrannosaurus = new Species(), elephant = new Species(); tyrannosaurus.setSpecies(“Dinosaur", 10, 15); elephant.setSpecies(“Mamal", 11, 2); tyrannosaurus = elephant;
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