In: Computer Science
The goal of Java program(s) implemented as part of this
assignment is:
1. To use for, while, do-while loops to solve the given
problem(s)
2. Write custom method(s)
Note:
Be sure to document your code thoroughly. In other words, you must
comment
each pertinent line of code to briefly state what it is intending
to do.
Requirement-1
In the Requirement-1 you will create a program named
‘ComputeCompoundInterest, that will compute compound interest using
the
formula as follows:
amount = principal * ( 1 + rate )numYears
(a) Ask the user to enter the following 3 data elements:
(i) The ‘Principal’
(ii) The ‘Interest Rate’
(iii) The ‘Number of Years’
(b) You will write the following 3 custom methods, each using a
different loop
type to compute and print compound interest using the above
mentioned
formula:
(i) “computeWhile”
(ii) “computeDoWhile”
(iii) “computeFor”
March 2, 2019
page 3
(c) Each method will take 3 parameters, as mentioned in (a) above,
with the
right type, and will not be returning anything. So be sure the
method
signature is defined the right way. Hint: return value should be
‘void’
(d) Each of the 3 methods should be called/invoked from the main()
method of
your class
(e) The output from each of the 3 methods should look like the
following. The
number of rows of output will depend on the number of years entered
by
the user input. Sample output should like the following:
Output from ‘while’ loop:
Year 1 Amount: $xxx
Year 2 Amount: $yyy
…..
…..
Output from ‘do-while’ loop:
Year 1 Amount: $xxx
Year 2 Amount: $yyy
…..
…..
Output from ‘for’ loop:
Year 1 Amount: $xxx
Year 2 Amount: $yyy
…..
…..
Note:
• Please add a blank line after the output from each method is
completed
Java Code:
Output Snapshot:
Text Code:
import java.io.*;
import java.util.Scanner; // Importing Scanner class for user
input
public class Main
{
/* Main */
public static void main(String[] args) {
/* Creating scanner class object to take input from
user */
Scanner userInput = new Scanner(System.in);
/* Prompting user to input Principal Amount */
System.out.print("\nEnter Principal Amount :$");
double principal = userInput.nextDouble(); // Storing Double value
from user
/* Prompting user to input Interest Rate */
System.out.print("\nEnter Interest Rate (0.00 - 1.00) :");
double interestRate = userInput.nextDouble(); // Storing Double
value from user
/* Prompting user to input Number of Years */
System.out.print("\nEnter Number of Years :");
int numOfYear = userInput.nextInt(); // Storing Integer value from
user
/* Calling methods to calculate compound interest */
computeWhile(principal,
interestRate, numOfYear);
computeDoWhile(principal,
interestRate, numOfYear);
computeFor(principal, interestRate,
numOfYear);
}
/*
* Method Name : computeWhile
* Input Arguments : principal : principal amount to
calculate compound interest
interestRate : interest rate on the principal
amount
numOfYear : number of years to calculate compound
interest
* Output Argument : void
* Description : To calculate compound interest using
while loop and display Output
on output screen
*/
public static void computeWhile(double principal,
double interestRate, int numOfYear){
System.out.println("\nOutput from ‘while’
loop:");
int i=1; // loop counter initialisation
while(i<=numOfYear){ // when counter value is less
than or equal to numOfYear
/* Calculating amount using compound interest formula
*/
double amount =
principal*Math.pow((1+interestRate),i);
/* Displaying the output for each year */
System.out.println("\t Year " + i + " Amount: $" +
amount);
/* Incrementing loop counter */
i++;
}
}
/*
* Method Name : computeDoWhile
* Input Arguments : principal : principal amount to
calculate compound interest
interestRate : interest rate on the principal
amount
numOfYear : number of years to calculate compound
interest
* Output Argument : void
* Description : To calculate compound interest using
do-while loop and display Output
on output screen
*/
public static void computeDoWhile(double principal,
double interestRate, int numOfYear){
System.out.println("\nOutput from ‘do-while’
loop:");
/* Calculating amount using compound interest formula
*/
int i=1;
do{
/* Calculating amount using compound interest formula
*/
double amount =
principal*Math.pow((1+interestRate),i);
/* Displaying the output for each year */
System.out.println("\t Year " + i + " Amount: $" +
amount);
/* Incrementing loop counter */
i++;
}while(i<=numOfYear);
}
/*
* Method Name : computeFor
* Input Arguments : principal : principal amount to
calculate compound interest
interestRate : interest rate on the principal
amount
numOfYear : number of years to calculate compound
interest
* Output Argument : void
* Description : To calculate compound interest using
for loop and display Output
on output screen
*/
public static void computeFor(double principal, double
interestRate, int numOfYear){
System.out.println("\nOutput from ‘for’ loop:");
for(int i=1; i<=numOfYear; i++){
/* Calculating amount using compound interest formula
*/
double amount =
principal*Math.pow((1+interestRate),i);
/* Displaying the output for each year */
System.out.println("\t Year " + i + " Amount: $" +
amount);
}
}
}
Explanation: