Lesson16
-
Upload
cybernaut -
Category
Technology
-
view
940 -
download
2
Transcript of Lesson16
Anonymous objects
It is possible to instantiate an object without a name.
public void melloJello(Circle cirA) osborne.melloJello(new Circle(5) ); The code, new Circle(5), instantiates the
object; however, in the region of the calling code it doesn’t have a name.
Lesson 16
Private methods can only be accessed from within the class itself.
Declaring and instantiating an object
Normally when we instantiate an object, we do it in one line of code:
Circle cir1 = new Circle(3.0); However, it can be done in two lines: Circle cir1; //Here, cir1 is merely declared to
be of type Circle cir1 = new Circle(3.0); //Here, it is finally
instantiated.
Setting two objects equal Circle cir1 = new Circle(5.3); //cir1 has a radius of 5.3 We will now demonstrate how to declare a cir2 object,
but not to instantiate it. Then in another line of code, set it equal to cir1: Circle cir2; //cir2 has only been declared to be of typeCircle cir2 = cir1; //cir2 and cir1 now refer to the same
object. There is only one object. It simply has two references to it.
Thus, cir2.area( ) returns exactly the same as cir1.area( )….and cir1.radius is exactly the same as cir2.radius,…etc.
Determining if two objects are equal
System.out.println(cir1 = = cir2);Circle cir1 = new Circle(11); Circle cir2 = new Circle(11);System.out.println(cir1 = = cir2); //false, in spite of the fact they both have a
radius of 11
System.out.println( cir1.equals(cir2) ); //false.
(cir1.equals(cir2) ) is equivalent to (cir1 = = cir2).
Circle class inherits the equals method from a superclass Object and simply compares to see if we are referring to the same object.
If the programmer who created the Circle class created an equals method for it, then that overrides the inherited method and compares the contents of the two objects (likely the radii). In this case, the println above would print a true since the contents of the two objects are the same (they both have a radius of 11).
Strings
String s1 = “Hello”; String s2 = “Hello”; //s1 and s2 are String
constants System.out.println(s1 = = s2); // prints true
The String constant pool
String literals are stored as String constants in a separate memory area called theString constant pool.
When object s1 is created “Hello” it is placed in the String constant pool with the reference s1 pointing to it. Then, for efficiency, when the reference (variable) s2 is created, Java checks the pool to see If the String constant being specified for s2 is already there. Since it is in this case, s2 also points to “Hello” stored in the String constant pool.
Physically, s1 and s2 are two separate String object references, but logically they are pointing to the same object in theString constant pool. So, in (s1 = = s2) from the code above we see that both s1 and s2 are referencing the same object, and a true is returned.
Exercises for Lesson 16
Problems 1 – 5 refer to the following code (assume that equals is not an explicit, method of this class):
MoonRock myRock = new MoonRock(3, “Xeon”);MoonRock yourRock = new MoonRock(2,
“Kryptonite”);MoonRock ourRock = new MoonRock(3, “Xeon”);MoonRock theRock;theRock = ourRock;1. Does theRock.equals(ourRock) return a true or
false?
1.true
MoonRock myRock = new MoonRock(3, “Xeon”);MoonRock yourRock = new MoonRock(2, “Kryptonite”);MoonRock ourRock = new MoonRock(3, “Xeon”);MoonRock theRock;theRock = ourRock;2. Does theRock.equals(yourRock) return a true or
false?3. Does theRock.equals(myRock) return a true or false?4. Does myRock = = ourRock return a true or false? 5. Does myRock.equals(yourRock) return a true or
false?
2-5. All False
Problems 6 – 11 refer to the following code: public class Weenie{public Weenie( ){ . . . }public String method1(int jj){ . . . }private void method2(String b){ . . . }public int method3( ){ . . . }public double x;public int y;private String z;}Now suppose from within a different class we instantiate a Weenie
object,oscarMayer. All of the code in questions 6 – 11 is assumed to be in this otherclass.
6. Is int zz = oscarMayer.method1(4); legal? If not, why?
6. Is int zz = oscarMayer.method1(4); legal? If not, why?
No, method1 returns a String and we are trying to store it in xx, an int.
6. Is int zz = oscarMayer.method1(4); legal? If not, why?
7. Is oscarMayer.method2(“Hello”); legal? If not, why?
8. Is int cv = oscarMayer.method3( ); legal? If not, why?
9. Is int cv = oscarMayer.method3(14); legal? If not, why?
10. Is oscarMayer.z = “hotdog”; legal? If not, why?
11. Assume the following code is inside method1:method2(“BarBQ”); Is this legal? If not, why?
6. Is int zz = oscarMayer.method1(4); legal? If not, why? No, method1 returns a String and we are trying to store it in zz, an
int.Answers 7. Is oscarMayer.method2(“Hello”); legal? If not, why? No, method2 is private8. Is int cv = oscarMayer.method3( ); legal? If not, why? Yes9. Is int cv = oscarMayer.method3(14); legal? If not, why? No, method3 is not expecting to receive a parameter…yet we’resending a
14.10. Is oscarMayer.z = “hotdog”; legal? If not, why? No, z is private.11. Assume the following code is inside method1:method2(“BarBQ”); Is this legal? If not, why? Yes, we can access a private method from within its class.
12. Instantiate an object called surferDude from the Surfer class using two separate lines of code. One line should declare the object and the other line should instantiate it. (Assume no parameters are sent to the constructor.)
12. Instantiate an object called surferDude from the Surfer class using two separate lines of code. One line should declare the object and the other line should instantiate it. (Assume no parameters are sent to the constructor.)
Surfer surferDude;surferDude = new Surfer( );
13. Which of the following is correct? (Assume beco is an object with a method (method33) that receives a Circle paramater.)
a.Circle cir5 = new Circle(10); beco.method33(cir5);b. beco.method33( new Circle(10) );c. Both a and b
13. Which of the following is correct? (Assume beco is an object with a method (method33) that receives a Circle paramater.)
a.Circle cir5 = new Circle(10); beco.method33(cir5);b. beco.method33( new Circle(10) );c. Both a and b
14. What is the value of balance after the following transactions?//refer to the BankAccount class you created on p. 15-7
BankAccount acc = new BankAccount(10, “Sally”); acc.deposit(5000); acc.withdraw(acc.balance / 2);
14. What is the value of balance after the following transactions?//refer to the BankAccount class you created on p 15-7
BankAccount acc = new BankAccount(10, “Sally”); acc.deposit(5000); acc.withdraw(acc.balance / 2); 2505
15. What’s wrong with the following code?BankAccount b;
b.deposit(1000);
15. What’s wrong with the following code?BankAccount b;b.deposit(1000);
b was never instantiated
16. What’s wrong with the following code?BankAccount b new BankAccount(32.75, “Melvin”);
b = new BankAccount(1000,”Bob”); //ok to assign a new object to b b.deposit(“A thousand dollars”); //Wrong, deposit receives a double, not a String
16. What’s wrong with the following codeBankAccount b new BankAccount(32.75, “Melvin”); //= sign missing between b and newb = new BankAccount(1000,”Bob”); //ok to assign a new object to bb.deposit(“A thousand dollars”); //Wrong, deposit receives a double, not a String
17. What is printed in the following? String myString = “Yellow”; String yourString = “Yellow”; String hisString = new String(“Yellow”); String ourString = myString;System.out.println(myString = = yourString); System.out.println(myString = = ourString); System.out.println(myString.equals(yourString)); System.out.println(myString.equals(ourString)); System.out.println( myString = = hisString );
17. What is printed in the following? String myString = “Yellow”; String yourString = “Yellow”; String hisString = new String(“Yellow”); String ourString = myString;System.out.println(myString = = yourString); // true…both references to
same objectSystem.out.println(myString = = ourString); //true…both references to
same objectSystem.out.println(myString.equals(yourString)); //true…contents are
sameSystem.out.println(myString.equals(ourString)); //true…contents are sameSystem.out.println( myString = = hisString ); //false… different objects