a. (Refer to class diagram 7-1.) Declare the Account class and its instance vari
ID: 3677886 • Letter: A
Question
a. (Refer to class diagram 7-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 (hint: do not assign a default value of null to the customer variable). Write this code as concisely as possible.
b. (Refer to class diagram 7-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.
c. (Refer to class diagram 7-1.) Write the code for the getType method. This method should simply return the value of the type instance variable.
d. (Refer to class diagram 7-1.) Write the code for the creditAccount and debitAccount methods. The creditAccount method should add the amount that’s passed to it to the balance instance variable, and the debitAccount method should subtract the amount that’s passed to it from the balance instance variable.
e. (Refer to class diagram 7-1.) Code a statement that calls the getFormattedBalance method and stores the String object that’s returned in a variable named formattedBalance. Assume that an Account object named account has already been created in the calling class.
Explanation / Answer
//Account.java
public class Account {
//private members of class Account
private int number;
private Customer customer;
private String type;
private double balance;
//Default constructor
public Account() {
number=0;
customer=new Customer();
type="";
balance=0.0;
}
//Parametrized constructor
public Account(int number, Customer customer, String
type,double balance) {
this.number=number;
this.customer=customer;
this.type=type;
this.balance=balance;
}
public void setNumber(int number){
this.number=number;
}
public int getNumber(){
return number;
}
public void setCustomer(Customer customer){
this.customer=customer;
}
public Customer getCustomer(){
return customer;
}
public void setType(String type){
this.type=type;
}
public String getType(){
return type;
}
public void setBalance(double balance){
this.balance=balance;
}
public double getBalance(){
return balance;
}
public String getFormatBalance(){
return String.format("%s$=%5.2f",
"Formatted Balance",balance);
}
public String getCustomerName(){
return customer.getCustomerName();
}
public void creditAccount(double amount){
balance=balance+amount;
}
public void debitAccount(double amount){
balance=balance-amount;
}
}
--------------------------------------------------------------------------------------------------------------------------------
//Customer.java
public class Customer {
//private members of class
private String firstName;
private String lastName;
private int id;
//default constructor
public Customer() {
firstName="";
lastName="";
id=0;
}
//parametrized cosntructor
public Customer(String firstName, String lastName, int id) {
this.firstName=firstName;
this.lastName=lastName;
this.id=id;
}
//Returns the customer name
public String getCustomerName()
{
return firstName+""+lastName;
}
//Returns the string representaion of Customer
@Override
public String toString() {
return "Id :"+id+" Name :"+firstName+""+lastName;
}
}
--------------------------------------------------------------------------------------------------------------------------------
/**
* The java program AccountDriver that creates
* an instance of Account and prints the details
* of the Account object
* */
//AccountDriver.java
public class AccountDriver {
public static void main(String[] args) {
//Create an instance of Customer
Customer customer=new Customer("Mrs. ", "Johnson", 1);
//Create Account class
Account act=new Account(1000, customer, "Savings", 1500);
//print number
System.out.println("Account Number :"+act.getNumber());
//print customer name
System.out.println("Name : "+act.getCustomerName());
//print type
System.out.println("Type:"+act.getType());
//print formatted balance
System.out.println(act.getFormatBalance());
}
}
--------------------------------------------------------------------------------------------------------------------------------
Sample Run:
Account Number :1000
Name : Mrs. Johnson
Type:Savings
Formatted Balance$=1500.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.