String str1 = (enter a string); String str1 = (enter a string); String str2 = (enter.

12
String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter a string”); if (str1 == str2) if (str1 == str2) System.out.println (“equal input”); System.out.println (“equal input”); the boolean expression the boolean expression if (str1 == str2) if (str1 == str2) will never be true !! will never be true !! You must use the String method You must use the String method String String equals(String) equals(String) to to compare Strings. compare Strings. == will only evaluate to true if two references refer == will only evaluate to true if two references refer to the EXACT same object to the EXACT same object CORRECT: CORRECT: if(str1.equals(str2) ) if(str1.equals(str2) ) System.out.println (“equal input”); System.out.println (“equal input”);

description

public class Dog { private String name; private String name; private String breed; private String breed; private int age; private int age; public Dog(String aName, String aBreed, int anAge){ public Dog(String aName, String aBreed, int anAge){ name = aName; name = aName; breed = aBreed; breed = aBreed; age = anAge; age = anAge; } public String getName (){ public String getName (){ return name; return name; } public String getBreed() { public String getBreed() { return breed; return breed; }}

Transcript of String str1 = (enter a string); String str1 = (enter a string); String str2 = (enter.

Page 1: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

String str1 = JOptionPane.showInputDialog(“enter a string”);String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter a string”);String str2 = JOptionPane.showInputDialog(“enter a string”);

if (str1 == str2) if (str1 == str2) System.out.println (“equal input”);System.out.println (“equal input”);

the boolean expression the boolean expression if (str1 == str2)if (str1 == str2) will never be true !!will never be true !!

You must use the String method You must use the String method String String equals(String)equals(String) to compare to compare Strings.Strings.

== will only evaluate to true if two references refer== will only evaluate to true if two references refer to the EXACT same objectto the EXACT same object

CORRECT:CORRECT: if(str1.equals(str2) )if(str1.equals(str2) ) System.out.println (“equal input”);System.out.println (“equal input”);

Page 2: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

Suppose we have a Dog class:Suppose we have a Dog class:

DogDog

-name:String-name:String-breed:String-breed:String-age:int-age:int

+Dog(String n, String br,int a)+Dog(String n, String br,int a)+String getName()+String getName()+String getBreed()+String getBreed()+String toString()+String toString()

Page 3: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

public class Dog {public class Dog { private String name;private String name; private String breed;private String breed; private int age;private int age;

public Dog(String aName, String aBreed, int anAge){public Dog(String aName, String aBreed, int anAge){ name = aName;name = aName; breed = aBreed;breed = aBreed; age = anAge;age = anAge; }}

public String getName (){public String getName (){ return name;return name; }}

public String getBreed() {public String getBreed() { return breed;return breed; }}}}

Page 4: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

You would might use it as …You would might use it as …

public class DogUser{public class DogUser{ public static void main (String [] args) {public static void main (String [] args) { Dog myDog = new Dog(“Spot”,”German Shephard”, 4);Dog myDog = new Dog(“Spot”,”German Shephard”, 4); String thename = JOptionPane.showInputDialog(“Enter dog name”);String thename = JOptionPane.showInputDialog(“Enter dog name”); String thebreed = JOptionPane.showInputDialog(“Enter breed”);String thebreed = JOptionPane.showInputDialog(“Enter breed”); int theage= Integer.parseInt(JOptionPane.showInputDialog(“Age?”));int theage= Integer.parseInt(JOptionPane.showInputDialog(“Age?”)); Dog theDog = new Dog(thename,thebreed,theage);Dog theDog = new Dog(thename,thebreed,theage);

if (myDog == theDog)if (myDog == theDog) System.out.println(“our dogs have same characteristics”);System.out.println(“our dogs have same characteristics”); elseelse System.out.println(“dogs are not similar”);System.out.println(“dogs are not similar”); }}// if user entered Spot, German Shepard and 4 this program would output// if user entered Spot, German Shepard and 4 this program would output// dogs are not similiar// dogs are not similiar }}

Page 5: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

This is because == compares the contents of the two operands for This is because == compares the contents of the two operands for equality..equality..

and myDog’s contents are NOT the same as theDog. and myDog’s contents are NOT the same as theDog.

Page 6: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

Need to provide a way for Dog’s to be compared…… Need to provide a way for Dog’s to be compared…… add an equals method to the Dog class so that the comparison of add an equals method to the Dog class so that the comparison of

two dog object can be made.two dog object can be made.

We would like to be able to say:We would like to be able to say:

if (myDog.equals(theDog) )if (myDog.equals(theDog) ) System.out.println(“our dogs have same characteristics”);System.out.println(“our dogs have same characteristics”); elseelse System.out.println(“dogs are not similar”);System.out.println(“dogs are not similar”);

Page 7: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

The method would look like this:The method would look like this://In order to override the equals method inherited from//In order to override the equals method inherited from// the Object class, we want the parameter to be of// the Object class, we want the parameter to be of// type Object// type Object

public boolean equals (Object obj) {public boolean equals (Object obj) { if (age == obj.age &&name.equals(obj.name) && if (age == obj.age &&name.equals(obj.name) && (breed.equals(obj.breed) )(breed.equals(obj.breed) ) return true;return true; elseelse return false;return false; }}

//This is the general idea, but it will not compile//This is the general idea, but it will not compile

Page 8: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

public boolean equals (Object obj) {public boolean equals (Object obj) { Dog dobj = (Dog) obj;Dog dobj = (Dog) obj; if (age == if (age == ddobj.age &&name.equals(obj.age &&name.equals(ddobj.name) && obj.name) && (breed.equals((breed.equals(ddobj.breed) )obj.breed) ) return true;return true; elseelse return false;return false; }}

//obj is of type Object//obj is of type Object// an Object doesn’t have an age, a Dog does// an Object doesn’t have an age, a Dog does

//we must cast the Object object to be of type Dog//we must cast the Object object to be of type Dog

//this is okay, all objects are of type Object thru inheritance//this is okay, all objects are of type Object thru inheritance

Page 9: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

This will compile and run, but if a call is made :This will compile and run, but if a call is made : if ( adog.equals( mycar) )if ( adog.equals( mycar) )

And the parameter is NOT a Dog object, the cast will cause And the parameter is NOT a Dog object, the cast will cause the program to crash ….the program to crash ….

So we will protect for this caseSo we will protect for this case

public boolean equals (Object obj) {public boolean equals (Object obj) { if (obj instanceof Dog) {if (obj instanceof Dog) { Dog dobj = (Dog) obj;Dog dobj = (Dog) obj; if (age == if (age == ddobj.age &&name.equals(obj.age &&name.equals(ddobj.name) && obj.name) && (breed.equals((breed.equals(ddobj.breed) )obj.breed) ) return true;return true; elseelse return false;return false; }} else else return false;return false; }}

Page 10: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

public class Dog {public class Dog { private String name;private String name; private String breed;private String breed; private int age;private int age;

public Dog(String aName, String aBreed, int anAge){public Dog(String aName, String aBreed, int anAge){ name = aName;name = aName; breed = aBreed;breed = aBreed; age = anAge;age = anAge; }} public String getName (){public String getName (){ return name;return name; }}

public String getBreed() {public String getBreed() { return breed;return breed; } } //add toString//add toString public String toString(){public String toString(){ return name + “ “ + breed + “:” + “is” + age;return name + “ “ + breed + “:” + “is” + age; }}}}

Page 11: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

public class DogUser{public class DogUser{ public static void main (String [] args) {public static void main (String [] args) { Dog myDog = new Dog(“Spot”,”German Shephard”, 4);Dog myDog = new Dog(“Spot”,”German Shephard”, 4); updateDog(myDog);updateDog(myDog); System.out.println(myDog);System.out.println(myDog); }}

public static void updateDog(Dog aDog){public static void updateDog(Dog aDog){ String thename = JOptionPane.showInputDialog(“Enter dog String thename = JOptionPane.showInputDialog(“Enter dog

name”);name”); String thebreed = JOptionPane.showInputDialog(“Enter String thebreed = JOptionPane.showInputDialog(“Enter

breed”);breed”); int theage= int theage=

Integer.parseInt(JOptionPane.showInputDialog(“Age?”));Integer.parseInt(JOptionPane.showInputDialog(“Age?”)); aDog = new Dog(thename,thebreed,theage);aDog = new Dog(thename,thebreed,theage); }}}}// What would you expect this program to output, if the user // What would you expect this program to output, if the user

typed:typed: “ “Rover” “French Poodle” 15Rover” “French Poodle” 15

}}

Page 12: String str1 =  (enter a string); String str1 =  (enter a string); String str2 =  (enter.

public class DogUser{public class DogUser{ public static void main (String [] args) {public static void main (String [] args) { Dog myDog = new Dog(“Spot”,”German Shephard”, 4);Dog myDog = new Dog(“Spot”,”German Shephard”, 4); updateDog(myDog);updateDog(myDog); System.out.println(myDog);System.out.println(myDog); }}

public static void updateDog(Dog aDog){public static void updateDog(Dog aDog){ String thename = JOptionPane.showInputDialog(“Enter dog String thename = JOptionPane.showInputDialog(“Enter dog

name”);name”); String thebreed = JOptionPane.showInputDialog(“Enter String thebreed = JOptionPane.showInputDialog(“Enter

breed”);breed”); int theage= int theage=

Integer.parseInt(JOptionPane.showInputDialog(“Age?”));Integer.parseInt(JOptionPane.showInputDialog(“Age?”)); aDog = new Dog(thename,thebreed,theage);aDog = new Dog(thename,thebreed,theage); }}}}Output will be:Output will be: Spot German SHephard: is 4hat would you expect Spot German SHephard: is 4hat would you expect

this}this}

Why??Why?? Java parameter passing is CALL BY VALUE.Java parameter passing is CALL BY VALUE.