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

Stuck on for loops for this computer science lab. The assigment is Read in start

ID: 3735060 • Letter: S

Question

Stuck on for loops for this computer science lab. The assigment is

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.

The code I have so far is

package Labs;

import java.util.Scanner;

public class Lab08 {

   public static void main(String[] args) {
  
   //Create scanner
       Scanner getInfo = new Scanner(System.in);
      
   //variables
       double accountAmount, monthlyInterestRate;
       double yearlyInterest, oneYear, accountBalance;
      
   //read in values print statement
       System.out.println("Please enter amount in account ");
       accountAmount = getInfo.nextDouble();
      
   //read in values for amount in account and monthly interest rate (yearly interest rate / 12 months  
       System.out.println("Enter yearly interest in floating point ");
       yearlyInterest = getInfo.nextDouble();
       System.out.println("Enter in the amount of months in a year ");
      >       
      
   //close scanner  
       getInfo.close();
  
   //print statement for interest rate method and calculation for interest in account
       monthlyInterestRate = calcMonthlyDoubles(yearlyInterest, oneYear);
       System.out.println("The monthly interest is "+monthlyInterestRate);
       accountBalance = calcInterestDoubles(accountAmount, monthlyInterestRate);
       System.out.println("Account balance after interest rate is "+ accountBalance);
      
       }//end of main method
      
       public static double calcMonthlyDoubles(double yearlyInterest, double oneYear) {
       double result;
       result = yearlyInterest / oneYear;
       return result;
       }//end of calculateMonthly method
      

       public static double calcInterestDoubles(double accountAmount, double monthlyInterestRate) {
       double result;
       result = accountAmount + monthlyInterestRate*accountAmount;
       return result;
       }//end of calcInterest method
      
   }

Explanation / Answer

Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

package Labs;

import java.util.Scanner;

public class Lab08 {

public static void main(String[] args) {

//Create scanner
Scanner getInfo = new Scanner(System.in);

//variables
double accountAmount, monthlyInterest;
double yearlyInterest;
int months;

//read in values for amount in account and yearly interest and no. of months
System.out.println("Please enter starting amount in account: ");
accountAmount = getInfo.nextDouble();

System.out.println("Enter yearly interest rate (E.g. 0.12 for 12%): ");
yearlyInterest = getInfo.nextDouble();
System.out.println("Enter the number of months to calculate interest: ");
months = getInfo.nextInt();

//close scanner   
getInfo.close();

//calculate monthlyinterest
monthlyInterest = calculateMonthly(yearlyInterest);

//use for loop to add monthly interest for specified no. of months
for(int i= 1; i <= months; i++)
{
accountAmount = calcInterest(accountAmount, monthlyInterest);
}

System.out.println("The monthly interest is "+monthlyInterest);
System.out.println("Account balance after "+ months + " months is "+ accountAmount);

}//end of main method

public static double calculateMonthly(double yearlyInterest) {
double result;
result = yearlyInterest / 12;
return result;
}//end of calculateMonthly method


public static double calcInterest(double accountAmount, double monthlyInterestRate) {
double result;
result = accountAmount + monthlyInterestRate*accountAmount;
return result;
}//end of calcInterest method

}


output
=====
Please enter starting amount in account:
100
Enter yearly interest rate (E.g. 0.12 for 12%):
0.12
Enter the number of months to calculate interest:
4
The monthly interest is 0.01
Account balance after 4 months is 104.060401