1. Analyze the following code(Multiple answers) (explain/ show your work): class
ID: 3566728 • Letter: 1
Question
1. Analyze the following code(Multiple answers) (explain/ show your work):
class Test {
private double i;
public Test(double i) {
this.t();
this.i = i;
}
public Test() {
System.out.println("Default constructor");
this(1);
}
public void t() {
System.out.println("Invoking t");
}
}
2. What is the printout for the third println statement (in red ink) in the main method? ( explain/show your work)
public class Foo {
static int i = 0;
static int j = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("i + j is " + i + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
B. j is 1
C. j is 2
D. j is 3
3. Which of the following statements are true about an immutable object?
A. The contents of an immutable object cannot be modified.
B. All properties of an immutable object must be private.
C. All properties of an immutable object must be of primitive types.
D. An object type property in an immutable object must also be immutable.
E. An immutable object contains no mutator methods.
4. Which of the following can be placed in the blank line in the following code? Please explain.
public class Test {
private static int id;
public static void m1() {
_____.id = 45;
}
}
A. this
B. Test
5. The Account Class
Problem Description:
(The Account class) Design a class named Account that contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and print the balance, the monthly interest, and the date when this account was created.
The Account class) Design a class named Account that contains:
A private int data field named id for the account (default 0).
A private double data field named balance for the account (default 0).
A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the account was created.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named withdraw that withdraws a specified amount from the account.
A method named deposit that deposits a specified amount to the account.
Draw the UML diagram for the class. Implement the class. Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and print the balance, the monthly interest, and the date when this account was created.
Analysis:
(Describe the problem including input and output in your own words.)
?
?
?
?
?
?
?
?
?
?
Design:
(Draw an UML class diagram for the Account class.)
?
?
?
?
?
?
?
?
?
?
?
?
?
?
Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts)
?
public class Account {
public static void main (String[] args) {
Account account = new Account(1122, 20000);
Account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
Class Account {
// Implement the class here
}
?
?
?
?
?
?
?
Testing: (Describe how you test this program)
?
?
?
?
?
?
?
?
?
Submit the following items:
1. Compile, Run, and Submit to Blackboard the Account.java source file (you must submit the program regardless whether it complete or incomplete, correct or incorrect)
2. Submit this Project Account Class file with your own analysis, design (UML class diagram), and Testing description.
Explanation / Answer
Answer 1:Third option is correct because,In Test() method this(1) should be the first statement in the method because constructor call should be the first statement in the constructor.
Answer 2:First option is correct because,j is mentioned as static variable so it got initialised to j=0 and will print j as 0.
Answer 3:A option is correct because,immutable objects are objects whose state cannot be changed.
Answer 4:B option is correct because,the class variables can be called with the class name itself and this should not be used in static content.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.