1. Write a program that includes a SavingsAccount class. Data members: firstName
ID: 3565405 • Letter: 1
Question
1. Write a program that includes a SavingsAccount class.
Data members:
firstName (string)
lastName (string)
savingBalance
annualInterestRate (static)
objectnumber (1,2,3...) const
Function members:
Default constructor - allows objects to be created without arguments. Set the names
to the empty string and the balance to zero
Constructor that allows an object to be created with name arguments only.
Set the balance to zero.
Constructor that allows an object to be created with name arguments and
a beginning balance
(All constructors will initialize annual interest rate to 5.0% and provide a serial number to each object.)
setName
get Name const
setBalance
getBalance const
setInterestRate static
getInterestRate static
getNumber const
calculateNewBalance (monthly rate = annualInterestRate / 12)
Destructor (Display message to indicate that the object no longer exists)
2. Write a driver program to test the class. Perform the following tasks in this order:
Declare at least 3 objects:
Saver1: Margaret Olson with a balance of $2,000
Saver2: Debra Baxter with no balance
Saver3: object without name or balance
Set Baxter balance to $5,000
Set Saver3 name to Arturo Ortiz and the balance to $10,000
Calculate the new monthly balance for each customer
(new balance = old balance + monthly interest earned)
Display all the data for all of the objects
Set the annual interest rate to 10.0%
Calculate the new monthly balance for each customer again
Display all the data for all of the objects
3. A header file will be used to declare the class. Other header files may be used if you wish. A separate .cpp file will be used to implement the class member functions. Hence, your program will consist of at least one header file and at least 2 .cpp files.
Explanation / Answer
package savingaccount;
import java.util.*;
import java.io.*;
public class Savingaccount {
// private String owner;
private double startingbalance;
private double currentbalance;
private double interestrate;
private double lastinterest;
public Savingaccount(double balance, double apr){ //constructor
balance = startingbalance;
apr = interestrate;
}
//Savingaccount suhaag = new Savingaccount(0.0, 20000); //constuctor is called by new object suhaag
public double getstartingBalance(){
return startingbalance;
}
public double getcurrentBalance(){ //get current balance
return currentbalance;
}
public double getinterestrate(){ //gets interest
return interestrate;
}
public double getlastinterestrate(){
return lastinterest;
}
public double setinterest(){ //sets interest
return interestrate;
}
public void withdrawal(double w){
currentbalance = currentbalance - w;
}
public void deposit(double d){
currentbalance = currentbalance + d;
}
public void addInterest(){
double monthlyinterestrate = interestrate/12;
lastinterest = monthlyinterestrate * currentbalance;
currentbalance = currentbalance + lastinterest;
}
public static void main(String[]args)
throws FileNotFoundException
{
double beginningbalance;
double accountbalance;
double deposit;
double withdraw;
double interestpaid;
//Create input file objects
Scanner inFile = new Scanner(new FileReader("transactions.txt"));
beginningbalance = inFile.nextDouble();
System.out.print("File opened ");
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the annual interest rate?");
interestpaid = keyboard.nextDouble();
//Savingaccounts object
deposit = inFile.nextDouble();
withdraw = inFile.nextDouble();
accountbalance = deposit - withdraw;
Savingaccount sunny = new Savingaccount(beginningbalance, interestpaid);
sunny.getstartingBalance(); //balance before adding interest
sunny.addInterest(); //add interest
sunny.getlastinterestrate(); //get last interest rate
System.out.println("Methods working");
System.out.println("Starting Balance " + sunny.getstartingBalance());
System.out.println("Account balance $" + sunny.getcurrentBalance());
System.out.println("Total interest $" + sunny.getlastinterestrate());
System.out.println("Interest rate " + sunny.getinterestrate());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.