Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter."— Presentation transcript:

1 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 if (str1 == str2) will never be true !! the boolean expression if (str1 == str2) will never be true !! You must use the String method String equals(String) to compare Strings. You must use the String method String equals(String) to 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 objectCORRECT: if(str1.equals(str2) ) if(str1.equals(str2) ) System.out.println (“equal input”); System.out.println (“equal input”);

2  Suppose we have a Dog class: Dog -name:String-breed:String-age:int +Dog(String n, String br,int a) +String getName() +String getBreed() +String toString()

3 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; }}

4 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”); else else 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 // dogs are not similiar }

5 This is because == compares the contents of the two operands for equality.. and myDog’s contents are NOT the same as theDog.

6 Need to provide a way for Dog’s to be compared…… add an equals method to the Dog class so that the comparison of two dog object can be made. add an equals method to the Dog class so that the comparison of two dog object can be made. 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”); else else System.out.println(“dogs are not similar”); System.out.println(“dogs are not similar”);

7 The method would look like this: //In order to override the equals method inherited from // the Object class, we want the parameter to be of // 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; else else return false; return false; } //This is the general idea, but it will not compile

8 public boolean equals (Object obj) { public boolean equals (Object obj) { Dog dobj = (Dog) obj; Dog dobj = (Dog) obj; if (age == dobj.age &&name.equals(dobj.name) && if (age == dobj.age &&name.equals(dobj.name) && (breed.equals(dobj.breed) ) (breed.equals(dobj.breed) ) return true; return true; else else return false; return false; } //obj is of type Object // an Object doesn’t have an age, a Dog does //we must cast the Object object to be of type Dog //this is okay, all objects are of type Object thru inheritance

9 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 the program to crash …. So 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 == dobj.age &&name.equals(dobj.name) && if (age == dobj.age &&name.equals(dobj.name) && (breed.equals(dobj.breed) ) (breed.equals(dobj.breed) ) return true; return true; else else return false; return false; } else else return false; return false; }

10 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; }}

11 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 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?”)); aDog = new Dog(thename,thebreed,theage); aDog = new Dog(thename,thebreed,theage); }} // What would you expect this program to output, if the user typed: “Rover” “French Poodle” 15 “Rover” “French Poodle” 15}

12 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 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?”)); aDog = new Dog(thename,thebreed,theage); aDog = new Dog(thename,thebreed,theage); }} Output will be: Spot German SHephard: is 4hat would you expect this} Spot German SHephard: is 4hat would you expect this}Why?? Java parameter passing is CALL BY VALUE. Java parameter passing is CALL BY VALUE.


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

Similar presentations


Ads by Google