package test: import java.util.Scanner; public class IntrestRate { public static
ID: 3738480 • Letter: P
Question
package test:
import java.util.Scanner;
public class IntrestRate
{
public static void main(String[] args)
{
double saving,interestRate,first,second,third,fourth; // variable declaration
Scanner sc = new Scanner(System.in); // Scanner declaration
System.out.print("Enter initial saving value:");
saving=sc.nextDouble(); // Accept savings
System.out.print("Enter interest rate:");
interestRate=sc.nextDouble(); // Accept interest Rate
first = saving + (interestRate/100/12*saving) ; // account after 1 month
second = first + (interestRate/12/100*first);// account after 2 months
third = second + (interestRate/12/100*second);// account after 3 months
fourth = third + (interestRate/12/100*third); // account after 4 months
System.out.print("Account after 4 months is "+fourth);
System.out.println();
}
}?
*******************************
This problem requires modification of the program above using methods in it.
Read in starting value in account, yearly interest rate and number of months
– Create method called calculateMonthly that gets yearly interest rate and calculates monthly rate and assign it to a monthly interest rate variable
– Create a method called CalcInterest and pass the current value in account and monthly interest rate. It should return new amount in the savings account .
– Create loop that executes the number of months and calls the method each month returning the new value in the account
– When loop is done print the amount in the account
– Test it for 4 months with starting value of $100 and 12% yearly rate. Should get same result as previous lab
– Also test it for 3 months with 0% yearly interest rate.
programing language Java.
Explanation / Answer
import java.util.*;
public class interest {
public static void main(String args[]){
Scanner console=new Scanner(System.in);
System.out.println("Enter Starting value in account::");
double startVal=console.nextDouble();
System.out.println("Enter Yearly interest::");
double inte=console.nextDouble();
System.out.println("Enter number of months::");
int mnts=console.nextInt();
interest obj=new interest();
double monInt=obj.calculateMonthly(inte);
for(int i=0;i<mnts;i++)
{
double newAmt=obj.calcInterest(startVal, monInt);
System.out.println("Amount after "+(i+1)+" months is:: "+newAmt);
startVal=newAmt;
}
}
public double calculateMonthly(double yearInt)
{
double monInt=yearInt/12;
return monInt;
}
public double calcInterest(double start,double mint)
{
double newAmt=start+(mint/100*start);
return newAmt;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.