11:43 oo AT&T; 91%, public asu.edu ASU CSE 110 Assignment #8 Due date/Time: Frid
ID: 3827623 • 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 balanceExplanation / Answer
import java.io.*;
//Class definition
public class FileReadWrite
{
//Instance variables
double startingBalance;
double annualInterest;
double totalDeposit;
double totalWithdrawl;
double totalInterest;
double finalBalance;
double balance;
//Main method
public static void main(String ss[])throws IOException
{
//Class FileReadWrite object created
FileReadWrite frw = new FileReadWrite();
//Calls acceptData() method to accept data
frw.acceptData();
//Calls writeData() method to write data
frw.writeData();
}//End of main method
//Method to accept data
void acceptData()throws IOException
{
//InputStreamReader class object created
InputStreamReader isr = new InputStreamReader(System.in);
//BufferedReader class object created
BufferedReader br = new BufferedReader(isr);
//Accepts Starting balance
System.out.println("Enter the stating balance on the account: $");
//Converts the string data to double
startingBalance = Double.parseDouble(br.readLine());
//Accepts annual interest rate
System.out.println("Enter the annual interest rate on the account (e.g. 0.4): ");
//Converts the string data to double
annualInterest = Double.parseDouble(br.readLine());
balance = startingBalance;
double deposit = 0, withdraw = 0;
//Loops three times to accept data for three months
for(int x = 1; x <= 3; x++)
{
//Displays month number
System.out.println(" Month " + x);
do
{
//Accepts monthly deposit
System.out.println("Total deposit for this month: $");
//Converts the string data to double
deposit = Double.parseDouble(br.readLine());
if(deposit >= 0)
break;
else
System.out.println("Negative numbers not allowed. Please reenter.");
}while(true);
totalDeposit += deposit;
balance += deposit;
do
{
//Accepts monthly withdrawal
System.out.println("Total withdrawals for this month: $");
//Converts the string data to double
withdraw = Double.parseDouble(br.readLine());
if(withdraw >= 0)
break;
else
System.out.println("Negative numbers not allowed. Please reenter.");
}while(true);
totalWithdrawl += withdraw;
balance -= withdraw;
//Calculates monthly interest
double interest = (annualInterest / 12) * balance;
balance += interest;
totalInterest += interest;
}//End of loop
}//End of method
//To decimal align
public static String printDouble(Double number)
{
//Generates a string format with two decimal places
String numberString = String.format("%4.2f", number);
//Find out the difference total space eight from number format length
int empty = 8 - numberString.length();
//Provides space based on the length
String emptyString = new String(new char[empty]).replace('', ' ');
//Returns number formated string concatenated with space
return (emptyString + numberString);
}//End of method
//Method to write data
void writeData()
{
//File class object created
File file = new File ("result.txt");
//PrintWriter object initialized to null
PrintWriter pw = null;
//Try block begins
try
{
//PrintWriter object created to write onto the file name specified with file object
pw = new PrintWriter (file);
//Writes data to file
pw.printf("Quarterly Savings Account Statement");
//For new line
pw.println();
pw.printf("Stating Balance: $%s", printDouble(startingBalance));
pw.println();
pw.printf("Total Deposit: + $%s", printDouble(totalDeposit));
pw.println();
pw.printf("Total Withdrawal: - $%s", printDouble(totalWithdrawl));
pw.println();
pw.printf("Total Deposit: + $%s", printDouble(totalInterest));
pw.println();
pw.printf("Ending Balance: $%s", printDouble((startingBalance + totalDeposit + totalInterest) - totalWithdrawl));
}//End of try block
//Catch to handle file not found exception
catch (FileNotFoundException e)
{
e.printStackTrace();
}//End of catch
//Close printWriter
pw.close();
}//End of method
}//End of class
Output File Contents:
Quarterly Savings Account Statement
Stating Balance: $ 5000.00
Total Deposit: + $ 2059.75
Total Withdrawal: - $ 1333.40
Total Deposit: + $ 47.92
Ending Balance: $ 5774.27
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.