Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* Design a SavingsAccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings

/* Design a SavingsAccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly intererst to the balance. The monthly interest rate is the annual interest rate divided by 12. To add the montly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance.

Test the class in a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following: a. Ask the user for the amount deposited into the account during the month. Use the class method to add this amount to the account balance. b. Ask the user for the amount withdrawn from the account during the month. Use the class method to subtract this amount from the account balance. c. Use the class method to calculate the monthly interest. After the last iteration, the program should display the ending balance,the total amount of deposits, the total amount of withdrawals, and the total interest earned. ****************************************************************** Use notepad or another text editor to create a text file named Deposits.txt. The file should contain the following numbers, one per line: 100.00 124.00 78.92 37.55 Next, create a text file named Withdrawals.txt. The file should contain the following numbers, one per line: 29.88 110.00 27.52 50.00 12.90 The numbers in the Deposits.txt file are the amounts of deposits that were made to a savings account during the month, and the numbers in the Withdrawals.txt file are the amounts of withdrawals that were made during the month. Write a program that creates an instance of the SavingsAccount class that you wrote in Programming Challenge 12. The starting balance for the object is 500.00. The program should read the values from the Deposits.txt file and the Withdrawals.txt file and use the object's method to add them to the acount balance. The program should call the class method to calculate the montly interest, and then display the ending balance and the total interest earned */

Okay so here's what I have. It works in the editor, but when i upload the assignment to the drop box and click on it, it gives me errors.

import java.util.*; import java.io.*; import java.util.Scanner;

public class DepositWithdrawal2 { public double balance,annual,month,deposits,withdraws,earned; //balance method sets the balance to b public void Balance(double b) { balance=b; } //deposit method calculates the deposit public void deposit(double a) { balance=balance+a; deposits=deposits+a; } //calculates the withdraw public void withdraw(double a) { balance=balance-a; withdraws=withdraws+a; } //calculates the interet earned public void LastInterest() { double interest,month; month=annual/12/100; interest=month*balance; balance=balance+interest; earned=earned+interest; } //sets the interest rate i to annual interest rate public void setInterestRate(double i) { annual=i; } //returns the balance public double getBalance() { return balance; } //returns deposits public double getDeposits() { return deposits; } //returns withdraws public double getWithdrawals() { return withdraws; } // returns the interest rate earned public double getInterest() { return earned; } }

class BankAccount1

{

public static void main(String[] args) { double balance,interest,withdraw,deposit; int months,i; Scanner keyboard=new Scanner(System.in); //create the savings account object SavingsAccount account1 = new SavingsAccount(); //get the starting balance from user System.out.println("Enter the starting balance: "); balance=keyboard.nextDouble(); //constructor with the starting balance account1.Balance(balance); //get interest rate from user System.out.println("Enter the annual interest rate: "); interest=keyboard.nextDouble(); //sets interest rate to rate entered by user account1.setInterestRate(interest); //get the amount of months the account has been opened from user System.out.println("Enter how many months the account has been open: "); months=keyboard.nextInt(); //get the amount deposited in each month the account has been open from the user for(i=0;i

System.out.printf("Total deposited $ %.2f ",account1.getDeposits()); System.out.printf("Total withdrawn $ %.2f ",account1. getWithdrawals()); System.out.printf("Interest earned $ %.2f ",account1.getInterest()); System.out.printf("Ending balance $%.2f ",account1.getBalance() ); } }

//************************************************************************************************************* //Second Savings account for part 2

class BankAccount2 { public static void main (String [] args) throws IOException { double amount; double interestRate; double balance = 500.00; Scanner keyboard = new Scanner(System.in); SavingsAccount account2 = new SavingsAccount(); //get annual interest rate from user System.out.println("Enter annual interest rate: "); interestRate = keyboard.nextDouble(); //call annual interest rate method account2.setInterestRate(interestRate); //open deposit file File file1 = new File("Deposits.txt"); Scanner dFile = new Scanner (file1); //open withdrawal file File file2 = new File ("Withdrawals.txt"); Scanner wFile = new Scanner(file2); //read values from the deposits file while(dFile.hasNext()) { amount = dFile.nextDouble(); //call deposit method account2.deposit(amount); } //read the values from teh withdrawals file while(wFile.hasNext()) { amount = wFile.nextDouble(); //call withdraw method account2.withdraw(amount); } //call to calculate monthly interest rate account2.LastInterest(); //display data System.out.printf("Total deposited $ %.2f ",account2.getDeposits()); System.out.printf("Total withdrawn $ %.2f ",account2. getWithdrawals()); System.out.printf("Interest earned $ %.2f ",account2.getInterest()); System.out.printf("Ending balance $%.2f ",account2.getBalance() ); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

When does an employer have to submit payroll tax payments?

Answered: 1 week ago

Question

2. To compare the costs of alternative training programs.

Answered: 1 week ago