Q6. Given class BankAccount on the last page of the test : 6a . What gets printe
ID: 3856558 • Letter: Q
Question
Q6. Given class BankAccount on the last page of the test :
6a. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount(60 , “Jennifer”);
BankAccount b = new BankAccount( 70 , “Hala”);
6b. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount();
a.update(23,”Nora”);
System.out.println(a.toString());
6c. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount();
a.update(270,”Clinton”);
BankAccount b = a;
b.update(89,”Trump”);
a.printMe() ;
6d. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount();
a.setAccount(”Normal”);
a.setBalance(12.3);
System.out.println(a.printMe() );
BankAccount b = new BankAccount();
b.setAccount2(”Randy”);
b.setBalance2(15.9);
System.out.println(b.printMe() );
Explanation / Answer
6.a.What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount(60 , “Jennifer”);
BankAccount b = new BankAccount( 70 , “Hala”);
1. A new object 'a' is created and the parameters are assigned to members of object ‘ a’
a.member1=60, a.member2='jennifer'
2.A new object 'b' is created and the parameters are assigned to members of object ‘ b’
b.member1=70 b.member2='Hala'
--------------------------------------------------------------------------------------------------
6.b. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount();
a.update(23,”Nora”);
System.out.println(a.toString());
In step1 a new object ‘a’ is created with default values.
Default values for integer is zero(0), and string is”NULL”.
The second statement update the object members with parameters passed to method update( ) as follows:
a.member1=23
a.member2='Nora'
in step 3 It prints the message specified in method toString();
6c. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount();
a.update(270,”Clinton”);
BankAccount b = a;
b.update(89,”Trump”);
a.printMe() ;
Answer
6c. What gets printed after we execute the following code (4 pts)
BankAccount a = new BankAccount();
a.update(270,”Clinton”);
BankAccount b = a;
b.update(89,”Trump”);
a.printMe() ;
These statements initially creates an object ‘a’ with default values.
In step 2, the members of object ‘a’ are updated to
a.member1=270
a.member2=”clinton”
In step3 a new reference variable ‘b’ is created and it is set to point object ‘a’
In step 4 the contents of object ‘a ‘ is updated with new values
b.member1=89
b.member2=’Trump’
In step 5 When a function printMe() is called using object a, it prints the contents of object a
Like a.member1=89
a.member2=”Trump”
__________________________________________________
6.d. BankAccount a = new BankAccount();
a.setAccount(”Normal”);
a.setBalance(12.3);
System.out.println(a.printMe() );
BankAccount b = new BankAccount();
b.setAccount2(”Randy”);
b.setBalance2(15.9);
System.out.println(b.printMe() );
In step1 a new object ‘a’ is created with default values
I n step 2 it assigns account type=”NORMAL”
In step 3 it assigns balance as 12.3 as follows;
a.Account_Type=”Normal”
a.Balance=12.3
in step4 the contents of PrintMe is printed associated with object ‘a’
in step 5 a new object ‘b’ is created with default values
in step 6 it assigns account type=”Randy”
In step 7 it assigns balance as 15.9 as follows;
b.Account_Type=”Randy”
b.Balance=15.9
In step 8 the contents of PrintMe is printed associated with object ‘b’
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.