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

(Refer to class diagram 6-1.) Code a constructor for the Account class that assi

ID: 3565266 • Letter: #

Question

(Refer to class diagram 6-1.) Code a constructor for the Account class that assigns values to the instance variables based on the values that are passed to it. The parameters of this constructor should be given the same names as the instance variables.

(Refer to class diagram 6-1.) Declare the Account class and its instance variables. Then, code a constructor that assigns default values to these variables. The default value you assign to the customer variable should prevent a NullPointerException. Write this code as concisely as possible. (Refer to class diagram 6-1.) Code a constructor for the Account class that assigns values to the instance variables based on the values that are passed to it. The parameters of this constructor should be given the same names as the instance variables.

Explanation / Answer

public class Account

{

private int number;
private Customer customer;
private String type;
private double balance;
// constructor that assigns default values to the variables
public Account() {
this.number = 0;
this.customer = new Customer();
this.type = "";
this.balance = 0.0;
}
// constructor for the Account class that assigns values to the instance variables based on the values that are passed to it.
public Account(int number, Customer customer, String type, double balance) {
this.number = number;
this.customer = customer;
this.type = type;
this.balance = balance;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getFormattedBalance(){
return String.valueOf(balance);
}

public String getCustomerName(){
return this.customer.name;
}

public void creditAccount(double amount){
this.balance = this.balance + amount;
}

public void debitAccount(double amount){
this.balance = this.balance - amount;
}

}