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

4.15 Project 3 Project Outcomes: Read user input and validate the value uses bra

ID: 3725476 • Letter: 4

Question

4.15 Project 3

Project Outcomes:

Read user input and validate the value

uses branch control structure

uses loop control structure

follows standard acceptable programming practices.

uses javadoc to for the class

Prep Readings:

Chapter 1 - 4

Project Requirements:

Write a Java program named InterestCalculator.java that calculates the value of a bank account based on starting principle, interest rate and time.

The program prompt the user for

Number of quarters to display (int) that is greater than zero and less or equal to 10.

Starting balance (double) that is greater than zero.

Interest rate (double) that is greater than zero and less than or equal to 20%

Display the information the user entered and ask if it is correct. If so continue, if not ask for all the information again (loop and half example in Addition Iteration examples).

Calculate the ending balance for each quarter and display the results in a table similar to below, the formula for quarterly interest is balance * interestRate / 100 * .25 where balance is balance from last quarter (current balance) and interestRate is annual interest rate from user input. The new balance will be the old balance plus the calculated quarterly interest.

If the user enters a value outside the boundaries listed above, prompt them with an error message and ask for the value again. Please find the corresponding error messages in sample run 2.

Sample run 1: input/output with all correct inputs

Sample run 2: input/output with some bad inputs

Both samples are from either jGrasp or command line execution of the program. You will not see user input when you run this code in zybook.

Other instruction

Copy the user prompt and error message from the sample run to avoid output format mismatch.

Use printf to print table rows with data. Display to the hundreds for all money amount.

Develop this program on your PC and upload the InterestCalculator.java file to the project 3 dropbox on elearning.

Your program should be compilable and giving generally correct input/output on your PC before you test run your code on this page.

You will get 80% if you pass all tests on this page. If you can not pass all tests here, there will be an automatic 20% off before your code is manually graded on the .java file uploaded to elearning. There will be another 20% off if your code is not compilable.

The rest of the grade (20%) will be graded on elearning on your coding style and javadoc.

Information for running on your code on this page

input to be used in develop mode for sample run 1:

Output you will see in develop mode with above input:

input to be used in develop mode for sample run 2:

Output you will see in develop mode with above input:

Explanation / Answer

InterestCalculator.java

import java.util.Scanner;

public class InterestCalculator {

public static void main(String[] args) {

//Declaring variables

double principal, endingBalance, interestEarned, rate;

int noOfQuarters;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

/* This while loop continues to execute

* until the user enters 'Y' or 'y'

*/

while (true) {

/* This while loop continues to execute

* until the user enters a valid number

*/

while (true) {

//Getting the input entered by the user

System.out.print("Enter number of quarters from 1 to 10:");

noOfQuarters = sc.nextInt();

if (noOfQuarters < 1 || noOfQuarters > 10) {

System.out.println("Number of quarters must be between 1 and 10 inclusive.");

continue;

} else

break;

}

/* This while loop continues to execute

* until the user enters a valid number

*/

while (true) {

System.out.print("Enter the beginning principal balance greater than 0:");

principal = sc.nextDouble();

if (principal <= 0) {

System.out.println("Beginning balance must be greater than zero.");

continue;

} else

break;

}

/* This while loop continues to execute

* until the user enters a valid number

*/

while (true) {

System.out.print("Enter the interest rate percentage without the percent sign greater than 0% and less than/equal to 20%:");

rate = sc.nextDouble();

if (rate < 1 || rate > 20) {

System.out.println("Interest rate must be between 1 and 20 inclusive.");

continue;

} else

break;

}

System.out.println("You entered a principal balance of $" + principal + " for " + noOfQuarters + " quarters at " + rate + "% interest.");

System.out.print("Is this correct? (y/n)");

char ch = sc.next(".").charAt(0);

if (ch == 'Y' || ch == 'y')

break;

else

continue;

}

//Displaying the table

System.out.println("Quarter Beginning Interest Ending");

System.out.println("Number Balance Earned Balance");

for (int i = 1; i <= noOfQuarters; i++) {

endingBalance = principal + principal * (rate / 100) * 0.25;

interestEarned = endingBalance - principal;

System.out.printf("%d $%.2f $%.2f $%.2f ", i, principal, interestEarned, endingBalance);

principal = endingBalance;

}

}

}

_____________________

Output:

Enter number of quarters from 1 to 10:8

Enter the beginning principal balance greater than 0:1000

Enter the interest rate percentage without the percent sign greater than 0% and less than/equal to 20%:5.25

You entered a principal balance of $1000.0 for 8 quarters at 5.25% interest.

Is this correct? (y/n)y

Quarter Beginning Interest Ending

Number Balance Earned Balance

1 $1000.00 $13.13 $1013.13

2 $1013.13 $13.30 $1026.42

3 $1026.42 $13.47 $1039.89

4 $1039.89 $13.65 $1053.54

5 $1053.54 $13.83 $1067.37

6 $1067.37 $14.01 $1081.38

7 $1081.38 $14.19 $1095.57

8 $1095.57 $14.38 $1109.95

___________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote