Question

(Enable the Account class comparable & cloneable)

@ Create ComparableAccount class inheritance from the Account class, and implemented the comparable and cloneable interfaces. Override the compareTo method to compare the balance of two accounts. Print the Account ID, balance and dataCreated information from the toString() method.

Implements the cloneable interface and override the clone method to perform a deep copy on the dateCreated field.

Write a driver program to create one array contains 5 ComparableAcccount objects (account #: 1001-1005, initial balance=$1000). Make deposit $500 to each account, and withdraw $100 - $500 for each account. Clone all 5 accounts (before sort) into a new array - use a for-loop.

Print out each object info from the cloned array, and use the “==“ operator, and compareTo() to compare the dateCreated field of each object from both arrays. Use sort method on the original array (or ArrayList), and print out object info.

  • For an array, use Arrays.sort(array); for an ArrayList, use Collections.sort(arrayList).
  • codes (Account.java, ComparableAccount.java, and Assignment7_Part2.java)

See the grading rubric for details

  • Suggested UML

Account **Make the dataCreated variable as protected in Account class protected: dateCreate ComparableAccount Java lang Clone

  • Sample output

Clone Array from elements Account ID: 1001 Balance 1400.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1002 Balance

======== Account.java ================

public class Account
{
private int id;
private double balance;
private Date dateCreated;
private static double annualInterestRate;
private double initialBalance;

public Account ()
{
id = 0;
balance = 0;
initialBalance = 0;
dateCreated = new Date();
annualInterestRate = 0.0;
}

public Account (int id, double balance)
{
this.id = id;
this.balance = balance;
initialBalance = balance;
dateCreated = new Date();
annualInterestRate = 0.0;
}

// id
public void setId(int id)
{
this.id = id;
}

public int getId()
{
return id;
}

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

public double getBalance()
{
return balance;
}

public double getInitialBalance()
{
return initialBalance;
}

// annual
public static double getAnnualInterestRate()
{
return annualInterestRate;
}

public static void setAnnualInterestRate(double annualInterest)
{
Account.annualInterestRate = annualInterest;
}

// date
public Date getDateCreated()
{
return dateCreated;
}

public double getMonthlyInterest()
{
return this.balance * annualInterestRate / 1200;
}

// w,d
public void withdraw(double amount)
{
if (amount <= balance) {
balance -= amount;
}
}

public void deposit(double amount)
{
if (amount > 0) {
balance += amount;
}
}
}

Account **Make the dataCreated variable as protected in Account class protected: dateCreate ComparableAccount Java lang Cloneable + ComparableAccount(id: int, balance: double) + toString0: String @Override + compareTo0: int @Override + clone0: Object @Override Java.lang Comparable
Clone Array from elements Account ID: 1001 Balance 1400.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1002 Balance: 1300.00 Date Created: Wed Nov 18 12:15:20 PST 2015 Account ID: 1003 Balance 1200.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1004 Balance 1100.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1005 Balance : 1000.00 Date Created: ฝึed Nov 18 12:15:20 PST 2015 CloneArray.dateCeated--OrignalArray.dateCreates [0] isfalse CloneArray.dateCeated-OrignalArray.datecreates [1] is: false CloneArray.dateCeated--OrignalArray.dateCreates [2] is false CloneArray.dateCeated--OrignalArray.dateCreates [3] is false CloneArray.dateCeated-OrignalArray.dateCreates [4] is false CloneArray.dateCeated.compareTo (OrignalArray.dateCreates [O]) is:0 CloneArray.dateCeated.compareTo (OrignalArray.dateCreates [1]) is:0 CloneArray.dateCeated . compareTo (Orignal Array . dateCreates [2]) is : Ο CloneArray.dateCeated.compareTo (OrignalArray.dateCreates [3]) is:0 CloneArray.dateCeated.compareTo (OrignalArray.dateCreates [4]) is: 0 Before Sorting Account ID: 1001 Balance 1400.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1002 Balance: 1300.00 Date Created: Wed Nov 18 12:15:20 PST 2015 Account ID: 1003 Balance: 1200.00 Date Created: Wed Nov 18 12:15:20 PST 2015 Account ID: 1004 Balance 1100.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1005 Balance: 1000.00 Date Created : ฝึed Nov 18 12:15:20 PST 2015 After Sorting Account ID: 1005 Balance: 1000.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1004 Balance: 1100.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1003 Balance 1200.00 Date Created Wed Nov 18 12:15:20 PST 2015 Account ID: 1002 Balance: 1300.00 Date Created: Wed Nov 18 12:15:20 PST 2015 Account ID: 1001 Balance: 1400.00 Date Created Wed Nov 18 12:15:20 PST 2015
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Account.java:


import java.util.Date;


public class Account {
    public int id;
    public double balance;
    protected Date dateCreated;
    private static double annualInterestRate;
    private double initialBalance;

    public Account ()
    {
    id = 0;
    balance = 0;
    initialBalance = 0;
    dateCreated = new Date();
    annualInterestRate = 0.0;
    }

    public Account (int id, double balance)
    {
    this.id = id;
    this.balance = balance;
    initialBalance = balance;
    dateCreated = new Date();
    annualInterestRate = 0.0;
    }

    // id
    public void setId(int id)
    {
    this.id = id;
    }

    public int getId()
    {
    return id;
    }

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

    public double getBalance()
    {
    return balance;
    }

    public double getInitialBalance()
    {
    return initialBalance;
    }

    // annual
    public static double getAnnualInterestRate()
    {
    return annualInterestRate;
    }

    public static void setAnnualInterestRate(double annualInterest)
    {
    Account.annualInterestRate = annualInterest;
    }

    // date
    public Date getDateCreated()
    {
    return dateCreated;
    }

    public double getMonthlyInterest()
    {
    return this.balance * annualInterestRate / 1200;
    }

    // w,d
    public void withdraw(double amount)
    {
    if (amount <= balance) {
        balance -= amount;
    }
    }

    public void deposit(double amount)
    {
    if (amount > 0) {
    balance += amount;
    }
    }
}

ComparableAccount.java:

import java.util.Date;

//class ComparableAccount inherit Account class
//and implements the interface Comparable and Cloneable
public class ComparableAccount extends Account implements Comparable<ComparableAccount>,Cloneable{
  
    ComparableAccount(int id,double balance){
        //pass to super class(Account class) constructor
        super(id,balance);
    }
  
    //override the toString method to display the field details
    @Override
    public String toString(){
        return ("Account ID: "+this.id+" Balance: "+this.balance+" Date Created: "+getDateCreated());
    }
  
    //Override comapreTo method
    //This mentod will compare the balance of two object
    //returns 1 or -1 when not equal
    //returns 0 when equal
    @Override
    public int compareTo(ComparableAccount obj) {
        if(this.balance>obj.balance)
            return 1;
        else if(this.balance<obj.balance)
            return -1;
        else
             return 0;
                  
    }
  
    //This method will deep clone tha dateCreated field
    @Override
    public Object clone()throws CloneNotSupportedException{
        //clone
        ComparableAccount account=(ComparableAccount)super.clone();
        account.dateCreated=(Date) this.getDateCreated().clone();
        return account;
    }


}

Assignment7_Part2.java:


import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Assignment7_Part2 {

  
    public static void main(String[] args) {
        //create array of object
        ComparableAccount[] OriginalArray=new ComparableAccount[5];
        int id=1001;
        double k=100;
        for(int j=0;j<5;j++){
            //initialize the objects
            OriginalArray[j]=new ComparableAccount(id,1000);
            //deposit 500
            OriginalArray[j].deposit(500);
            //withdraw k amount
            OriginalArray[j].withdraw(k);
            id++;
            k+=100;
            //print object detail
            System.out.println(OriginalArray[j].toString());
        }
        //create cloneArray of element 5
        ComparableAccount[] CloneArray=new ComparableAccount[5];
        for (int j=0;j<5;j++){
          
            try {
                //for each element in CloneArray create a clone of OriginalArray
                CloneArray[j]=(ComparableAccount)OriginalArray[j].clone();
                //check whether datecreated value is same for CloneArray and OriginalArray
                //the value will be false as they refer to different object
                boolean check=(CloneArray[j].getDateCreated()==OriginalArray[j].getDateCreated());
                //returns false asthey refer to different object
                System.out.println("CloneArray.dateCreated==OriginalArray.dateCreates["+j+"] is : "+ check);
              
            } catch (CloneNotSupportedException ex) {
                Logger.getLogger(Assignment7_Part2.class.getName()).log(Level.SEVERE, null, ex);
            }
          
         
        }
        //for all the element in cloneArray compare them to OriginalArray
         for (int j=0;j<5;j++){
         
                System.out.println("CloneArray.dateCreated.compareTo(OriginalArray["+j+")] is : "+CloneArray[j].compareTo(OriginalArray[j]));
             
          
         
        }
         //print original array befor sorting
         System.out.println("Before Sorting");
         for(int j=0;j<5;j++){
             System.out.println(OriginalArray[j].toString());
         }
         //sort originalarray
         Arrays.sort(OriginalArray);
         System.out.println("After Sorting");
         //display the original Array after sorting
         for(int j=0;j<5;j++){
             System.out.println(OriginalArray[j].toString());
         }
      
      
      
    }
  
}

Output:

Account ID: 1001 Balance: 1400.0 Date Created Mon Mar 25 15: 42:45 IST 2019 Account ID: 1002 Balance: 1300.0 Date Created Mon

Add a comment
Know the answer?
Add Answer to:
(Enable the Account class comparable & cloneable) @ Create ComparableAccount class inheritanc...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • Unable to find out why am I getting: Account.java:85: error: reached end of file while parsing...

    Unable to find out why am I getting: Account.java:85: error: reached end of file while parsing import java.util.*; class Account{ private int id; private double balance; private double annualInterestRate; private Date dateCreated; public Account() { id = 0; balance = 0; annualInterestRate = 0; dateCreated = new Date(); } public Account(int id1, double balance1) { id =id1; balance = balance1; dateCreated = new Date(); } public void setId(int id1) { id=id1; } public void setBalance(double balance1) { balance = balance1;...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (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...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • Hello, I'm very new to programming and I'm running into a problem with the system.out.println function...

    Hello, I'm very new to programming and I'm running into a problem with the system.out.println function in the TestAccount Class pasted below. Account Class: import java.util.Date; public class Account {    private int accountId;    private double accountBalance, annualInterestRate;    private Date dateCreated;    public Account(int id, double balance) { id = accountId;        balance = accountBalance; dateCreated = new Date();    }    public Account() { dateCreated = new Date();    }    public int getId() { return...

  • the language i wan used in C# is visual basic.Create a console program that contains the...

    the language i wan used in C# is visual basic.Create a console program that contains the following ·         A Class named Account with the following properties: o   public int Id { get; set; } o   public string Firstname { get; set; } o   public string Lastname { get; set; } o   public double Balance { get; set; } o   public DateTime CreationDate { get; set; } o   Create a constructor that initializes all of the properties. o   Create a ToString...

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

  • 11.3 (Subclasses of Account) In Programming Exercise 9.7, the Account class was defined to model a...

    11.3 (Subclasses of Account) In Programming Exercise 9.7, the Account class was defined to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and with- draw funds. Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Draw the UML diagram for the classes and implement them. Write a test program that creates objects of...

  • •create a new savings account object (for choice 2). •ask the user to enter an initial...

    •create a new savings account object (for choice 2). •ask the user to enter an initial balance which should be greater than 50. If the entered amount is less than 50, the program will ask the user to enter another amount. This continues until an amount greater than 50 is entered. •Set the balance of the savings account object by calling the setBalance() method. •Ask the user to deposit an amount. Deposit that amount and display the balance of the...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT