You must write one Main class containing a main method . For each question, exce
ID: 670942 • Letter: Y
Question
You must write one Main class containing a main method. For each question, except the last, you must write another method in your Main class. Your main method must show each of your others methods running on some sensible example values. Comment your methods appropriately.
Submit your code, together with the results of running it on sensible example values.
Question : Write a main program that reads from the keyboard: • A double, P, representing the principal of the investment (i.e. the initial amount of the investment). • An integer, N, representing the term of an investment (i.e. the number of years for the investment). • A double, R, representing the rate of return of the investment per year as a percentage (e.g: an input of 25.5 means you earn 25.5% interest per year). Write a method interest that returns the interest earned (as a double) given an initial investment I (a double), and an interest rate R (a double). Use this method to show, for each year of the investment, the value of the investment at the beginning of the year, the interest earned that year, and the value of the investment at the end of the year.
( JAVA code)
Explanation / Answer
package mani;
import java.util.Scanner;
public class Main{
public static double interest(double p,double rate){
double i=(p*rate)/100;
return i;
}
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the principal of the investment: ");
double p=scan.nextDouble();
System.out.print("Enter the no. of years of investment: ");
int n=scan.nextInt();
System.out.print("rate of interest: ");
double rate=scan.nextDouble();
double inter;
for(int i=1;i<=n;i++){
System.out.println("Initial amount at the start of "+i+" year");
inter=interest(p,rate);
System.out.println("Interest at the end of "+i+" year: "+inter);
p=p+inter;
System.out.println("value of the investment at the end of "+i+" year");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.