Objectives

Objectives include:

  • Use local variables.
  • Use arithmetic expressions.
  • Use Scanner to input values.
  • Use a class constant.
  • Use for loops.

Tasks

Write a program that prints

UTSA - Summer 2021 - CS1083 - Prj 1 - written by YOURNAME - ABC123

note that you should change your name and your abc123. Your program also:

1. Prints a table showing the value of an account with simple interest added annually. The program should ask the user to enter the original value of the account, the annual interest rate, and the number of years that should be calculated.

2. Prints a table showing the value of an account with simple interest added quarterly. The program needs to ask the user to enter the original value of the account, the annual interest rate, and the number of years that should be calculated.

Here is an example of what your output should look like (user input is in bold and underlined): see image.

Sometimes, a calculation might print out a lot of digits. This is because of roundoff errors when you use doubles. You can use the printf method as discussed in class.

Details

The Console

You will need to use Scanner to obtain input from the keyboard. You should declare a class constant of type Scanner named CONSOLE at the beginning of your class; you should store new Scanner(System.in) in CONSOLE.

Table for Simple Interest Calculated Annually

Suppose the original amount of money is p, the annual interest rate is r, and the number of years is y. Then, using simple interest, the value v after y years is:

v = p * (1 + y * r / 100)

In the first part of the program, you should allow the user to enter three numbers, p, r, and the maximum value for y. p and r should be doubles. The table should show the results for different values of y, from 0 to the maximum value. For example, the user might want to print the values from year 0 to year 10.

After obtaining values from the user, your program should print a header for each column, and a line for each value of y and b. Use tab characters (\t) to align the columns. See the example output shown above.

You can use a for loop that counts from 0 to the maximum value for y. Inside the loop, you should calculate v and print out one line.

Table for Simple Interest Calculated Quarterly

Suppose the original amount of money is p, the annual interest rate is r, and it is quarter q in year y. That is, in any given year, you will have a 1st quarter, 2nd quarter, 3rd quarter, and a 4th quarter, so q should store the value 1, 2, 3 or 4. Then, using simple interest, the value v after quarter q in year y is:

v = p * (1 + (y - 1 + q / 4) * r / 100)

The (y - 1 + q / 4) is a bit of trick. At the end of quarter q of year y, a total of y - 1 years have passed plus q quarters. For example, at the end of the 3rd quarter in the 1st year, a total of 0 years plus 3 quarters have passed. [A similar example is that if you are a freshman, then this is your 1st year of college, but you haven't have a full year of college yet.]

For the second part of the program, you should print the result of v for different values of y and q.

Just like the first part of the program, in the second one, you should allow the user to enter three numbers, p, r, and the maximum value for y. p and r should be doubles. The table should show the results for different values of y, from 1 to the maximum value. For example, the user might want to print the quarterly values from year 1 to year 7.

Then, you should print a line for each value of y, from 1 to its maximum value. The first column should be the value of y. The following columns should be the value of v after quarter q, for different values of q.

Let's consider the pseudocode for this part of the program step-by-step.

Pseudocode for Printing the Table for Simple Interest

1. Obtain values from the user.
2. Print headers.
3. Print the values in the table.

The headers for the columns need to be printed in the right order with the appropriate spacing.

Pseudocode for Printing Headers

1. Print "Year" and a tab.
2. For each value of q from 1 to 4:
A. Print "Q", q, " Value", and a tab (maybe two tabs).
3. Print a newline.

Printing the values in the table requires a double loop. You will get fewer points if do this without a double loop.

Pseudocode for Printing the Values in the Table

1. For each value of y from 1 up to its maximum value:
A. Print y and a tab.
B. For each value of q from 1 to 4:
a. Calculate v.
b. Print v and a tab (maybe two tabs).
C. Print a newline.

The columns should be correctly lined up at least for the above examples, assuming no roundoff errors.

Avoiding Roundoff

If you want to avoid seeing roundoff errors (and get two digits after the decimal point) when printing a dollar value, replace:

System.out.print(v);

with:

System.out.printf("%7.2f", v);
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.