Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

11:43 oo AT&T; 91%, public asu.edu ASU CSE 110 Assignment #8 Due date/Time: Frid

ID: 3826179 • Letter: 1

Question

11:43 oo AT&T; 91%, public asu.edu ASU CSE 110 Assignment #8 Due date/Time: Friday, April 28, 2017 at 5:30pm What this Assignment is About: Leam to read in data by using InputStreamReader & BufferedReader classes. Leam to use PrintWriter class to write the output to a text file Leam to use printfo method to format the output Note: for this lab, you are NOT allowed to use Scanner class to read in data! Coding Guidelines for All Labs/Assignments oou will be graded on this) Give identifiers semantic meaning and make them easy to read examples numStudents, grossPay, etc), Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is upper case)for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. Use white space to make your program more readable. Use comments properly before or after the ending brace of classes, methods, and blocks to identify to which block it belongs. Assignment description In this assignment, write a program that calculates the balance of a saving account at the end ofa three-month period. It should ask the user for the starting balance and the annual interest rate. A loop should then iterate once for every month in the period, performing the following steps: A) Ask the user for the total amount deposited into the account during that month and add it to the balance. Do not accept negative numbers. B) Ask the user for the total amount withdrawn from the account during that month and subtract it from the blance. Do not accept negative numbers or numbers greater than the balance after the deposits for the month have been added in. C) Calculate the interest for that month. The monthly interest rate is the annual interest rate divided by 12. Multiply the monthly interest rate by the average of that month's starting and ending balance to get the interest amount for the month. This amount should be added to the balance. After the last iteration, the program should display a report and save it inside a file called result.tu that includes the following information: starting balance at the beginning of the three-month period total deposits made during the three months total withdrawals made during the three months total interest posted to the account during the three months final balance

Explanation / Answer

package com.cse110;

public class SavingAccount {

   // declaration
   private double annualInterestRate, balance, accumulativeInterest,
           accumulativeDeposits = 0, accumulativeWithdrawals = 0;

   /**
   * default constructor to set initial balance
   */
   public SavingAccount() {
       // TODO Auto-generated constructor stub
       balance = 0.0;

   }

   /**
   * constructor to set the balance
   *
   * @param balance
   */
   public SavingAccount(double balance) {
       this.balance = balance;
   }

   /**
   * method to add the monthly interest to the balance
   *
   */
   public void addMonthlyInterest() {
       accumulativeInterest += balance * (annualInterestRate / 12);
       balance += balance * (annualInterestRate / 12);
   }

   /**
   * @return the annualInterestRate
   */
   public double getAnnualInterestRate() {
       return annualInterestRate;
   }

   /**
   * @return the balance
   */
   public double getBalance() {
       return balance;
   }

   /**
   * @return the accumulativeInterest
   */
   public double getAccumulativeInterest() {
       return accumulativeInterest;
   }

   /**
   * @return the accumulativeDeposits
   */
   public double getAccumulativeDeposits() {
       return accumulativeDeposits;
   }

   /**
   * @return the accumulativeWithdrawals
   */
   public double getAccumulativeWithdrawals() {
       return accumulativeWithdrawals;
   }

   /**
   * @param annualInterestRate
   * the annualInterestRate to set
   */
   public void setAnnualInterestRate(double annualInterestRate) {
       this.annualInterestRate = annualInterestRate;
   }

   /**
   * @param balance
   * the balance to set
   */
   public void setBalance(double balance) {
       this.balance = balance;
   }

   /**
   * @param accumulativeInterest
   * the accumulativeInterest to set
   */
   public void setAccumulativeInterest(double accumulativeInterest) {
       this.accumulativeInterest = accumulativeInterest;
   }

   /**
   * @param accumulativeDeposits
   * the accumulativeDeposits to set
   */
   public void setAccumulativeDeposits(double accumulativeDeposits) {
       this.accumulativeDeposits = accumulativeDeposits;
   }

   /**
   * @param accumulativeWithdrawals
   * the accumulativeWithdrawals to set
   */
   public void setAccumulativeWithdrawals(double accumulativeWithdrawals) {
       this.accumulativeWithdrawals = accumulativeWithdrawals;
   }

   /**
   *
   * method to withdraw the amount from balance
   *
   * @param w
   */
   public void withdraw(double w) {
       balance -= w;
       accumulativeWithdrawals += w;
   }

   /**
   * method to deposit the amount to balance
   *
   * @param d
   */
   public void deposit(double d) {
       balance += d;
       accumulativeDeposits += d;
   }

   /**
   * method to get the annual interest
   *
   * @return
   */
   public double getMonthlyInterest() {
       return balance * (annualInterestRate / 12);
   }

}

package com.cse110;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
* @author
*
*/
public class Assignment8 {

   /**
   * @param args
   */
   public static void main(String[] args) {

       final int MONTH_PERIOD = 3;
       try {

           // create an InputStreamReader object to read from keyboard
           InputStreamReader ir = new InputStreamReader(System.in);

           // create BufferedReader obj
           BufferedReader br = new BufferedReader(ir);

           // create printwriter object to write data to the file result.txt
           // file
           PrintWriter pw = new PrintWriter(new File("result.txt"));
           double startingBalance, interestRate;

           System.out.print("Enter the starting balance on the account: $");
           startingBalance = Double.parseDouble(br.readLine());

           SavingAccount savingAccount = new SavingAccount(startingBalance);

           System.out.print("Enter the anual interest rate on the account: $");
           interestRate = Double.parseDouble(br.readLine());

           savingAccount.setAnnualInterestRate(interestRate);

           for (int count = 1; count <= MONTH_PERIOD; count++) {
               // print the month to the console
               System.out.println(" MONTH " + count + "");

               // read the amount to deposit to balance from the console

               System.out.printf("Total deposits for this month: $");
               savingAccount.deposit(Double.parseDouble(br.readLine()));

               // read the amount to withdraw from balance from the console
               System.out.printf("Total withdrawals for this month: $");
               savingAccount.withdraw(Double.parseDouble(br.readLine()));

               savingAccount.addMonthlyInterest();
           }

           System.out.println(" Quaterly Savings Account Statement");

           System.out.printf(" Starting balance: $ %.2f", startingBalance);

           System.out.printf(" Total deposits: + $%.2f",
                   savingAccount.getAccumulativeDeposits());
           System.out.printf(" Total withdrawals: - $%.2f",
                   savingAccount.getAccumulativeWithdrawals());
           System.out.printf(" Total interest: + $%.2f",
                   savingAccount.getAccumulativeInterest());
           System.out.println(" ----------");
           System.out.printf(" Ending balance: $%.2f",
                   savingAccount.getBalance());

           // writing data to file
           pw.write(" Starting balance: $ " + startingBalance);
           pw.write(" Total deposits: + $"
                   + savingAccount.getAccumulativeDeposits());
           pw.write(" Total withdrawals: - $"
                   + savingAccount.getAccumulativeWithdrawals());
           pw.write(" Total interest: + $"
                   + savingAccount.getAccumulativeInterest());
           pw.write(" ----------");
           pw.write(" Ending balance: $" + savingAccount.getBalance());
           pw.flush();
       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

OUTPUT:

Enter the starting balance on the account: $5000
Enter the anual interest rate on the account: $0.035

MONTH 1
Total deposits for this month: $780.5
Total withdrawals for this month: $472.75

MONTH 2
Total deposits for this month: $550.25
Total withdrawals for this month: $510

MONTH 3
Total deposits for this month: $729
Total withdrawals for this month: $350.65

Quaterly Savings Account Statement

Starting balance:   $ 5000.00
Total deposits:       + $2059.75
Total withdrawals:   - $1333.40
Total interest:       + $47.92
----------

Ending balance:       $5774.27

result.txt


Starting balance:   $ 5000.0
Total deposits:       + $2059.75
Total withdrawals:   - $1333.4
Total interest:       + $47.917057303114156
                   ----------
Ending balance:       $5774.267057303115