Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 Fill in the third line of this SquareTester program so that it prints

ID: 3846213 • Letter: Q

Question

Question 1

Fill in the third line of this SquareTester program so that it prints out the expected outcome. Remember that the expected outcome should be the string "Expected: " followed by the expected answer (remember the single space that goes between them).

public class SquareTester

{

    public static void main(String[] args)

    {

        // Step 1: declare and initialize a variable mySquare as an

        // instance of a Square class with a side length of 6

        // Step 2: print out the area of the object referenced by the

        // variable mySquare using the getArea method

        // Step 3: print the expected outcome

                 

   }

}

Question 2

Given this constructor, fill in the blank to complete its definition.

/**

* Constructs a bank account with a given balance.

* @param initialBalance The initial balance.

*/

public BankAccount(double          

)

{

    balance = initialBalance;

}

Question 3

Assume the method below has been added to the BankAccount class.

public void transfer(BankAccount source, double amount)

{

   balance = balance + amount;

   source.balance = source.balance – amount;

}

What will be output from the following statements that use the revised BankAccount class?

BankAccount first = new BankAccount(100.0);

BankAccount second = new BankAccount(300.0);

first.transfer(second, 50.0);

System.out.println(first.getBalance() + " " + second.getBalance());

Question 4

What will be output from the following statements that use the BankAccount class?

BankAccount first = new BankAccount(100.0);

first.deposit(50.0);

BankAccount second = new BankAccount(first.getBalance());

first.deposit(50.0);

BankAccount third = new BankAccount(first.getBalance());

first.deposit(50.0);

System.out.println(first.getBalance() + " " + second.getBalance() + " " + third.getBalance());

Question 5

Which statement is true about the following constructor of the BankAccount class?

public BankAccount(double balance)

{

   this.balance = balance;

}

The code has a logic error.

The code sets the instance variable balance to the parameter variable balance.

The code has a syntax error.

You can't have an instance variable and a parameter variable with the same name.

The code has a logic error.

The code sets the instance variable balance to the parameter variable balance.

Explanation / Answer

Question 1:
Answer: System.out.println("Expected: "+mySquare);


Question 2:
Answer: initialBalance


Question 3:
Answer: first.getBalance() gives 150.0
        second.getBalance() gives 250.0
Therefore,output will be 150.0 250.0


Question 4:

answer: 250.0 150.0 200.0

question 5:
answer: The code sets the instance variable balance to the parameter variable balance.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote