(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.
codes (Account.java, ComparableAccount.java, and Assignment7_Part2.java)
See the grading rubric for details
======== 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.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:
(Enable the Account class comparable & cloneable) @ Create ComparableAccount class inheritanc...
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 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 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 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 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 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 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 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 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 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...