Question

In: Computer Science

Create a Java Program to calculate compound interest. This can all be done in the main()...

Create a Java Program to calculate compound interest.

This can all be done in the main() method.

  1. Create a double variable named currentBalance.
  2. Create a double variable named newBalance.
  3. Create a final double variable named INTEREST_RATE and assign 1.05 to it.
  4. Create a int variable named yearCount.
  5. Prompt for the "Number of years to invest" and store the input into the yearCount variable.
  6. Prompt for the "Amount to Invest" and store this in the currentBalance variable.
  7. Assign currentBalance to newBalance.
  8. Using a 'FOR' loop, create and display the balance for each year. Additional variables may be required ( such as for the current year being displayed)
    1. newBalance= newBalance * INTEREST_RATE;
    2. Review the FOR loop example in Chapter 6 -Section 6.3 page 92
    3. Format the displayed balance to show 2 decimal places only.

Example:

Number of years to invest:5
Amount to Invest:1000

Year 1 balance =  1050.00
Year 2 balance =  1102.50
Year 3 balance =  1157.63
Year 4 balance =  1215.51
Year 5 balance =  1276.28


   Display the message "You have doubled your investment" and terminate the loop if/when you have more than doubled your initial investment.

Solutions

Expert Solution

Answer:

I have written the below Java program based on your requirements.

The below code has no-error and it is working perfectly.

I have also attached the Output Screenshot that I got by running the below program.

Just save the below Java file as Main.java and then Compile & run.

***********************************************************************************************************

Output:

*********************************************************************************************************************

Code:

Main.java

import java.io.*;
import java.util.*;

public class Main
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
      
       double currentBalance, newBalance;
       final double INTEREST_RATE=1.05;
       int yearCount;
      
       System.out.print("\nNumber of years to invest:");
       yearCount=sc.nextInt();
      
       System.out.print("Amount to Invest:");
       currentBalance=sc.nextDouble();
      
       newBalance=currentBalance;
      
       System.out.println();
      
       for(int i=1;i<=yearCount;i++)
       {
           newBalance=newBalance*INTEREST_RATE;
           if(newBalance>=currentBalance*2)
           {
               System.out.println("You have doubled your investment");
               break;
           }
           System.out.println("Year "+i+" balance = "+String.format("%.2f", newBalance));
       }
   }
}


Related Solutions

TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It...
TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It demonstrates that a TreeSet eliminates duplicates and is ordered. Create a Random object with a seed of 5. Create an ArrayList numAL of type Integer Populate numAL with 10 numbers randomly selected from 0 to 6 Print out numAL Create a TreeSet numTS of type Integer Create an Iterator alIter from numAl and use it to add all the elements of numAL in numTS....
In java Create a program to calculate interest rate and total balance - the program consists...
In java Create a program to calculate interest rate and total balance - the program consists of (a) A class that encapsulates the interest rate and total balance calculation Constructor should accept initial balance and interest rate Methods: Returns interest rate Returns total balance Set interest rate Set initial balance (b) A test program that uses class from step (A) to perform the following: Set interest rate to 5% and initial balance to 1000 Print interest amount and total balance...
The following program can be done all in the main method. It demonstrates that a TreeSet...
The following program can be done all in the main method. It demonstrates that a TreeSet eliminates duplicates and is ordered. Create a Random object with a seed of 5. Create an ArrayList numAL of type Integer Populate numAL with 10 numbers randomly selected from 0 to 6 Print out numAL Create a TreeSet numTS of type Integer Create an Iterator alIter from numAl and use it to add all the elements of numAL in numTS. Print out numTS
Problem 1: In java Create a program to calculate interest rate and total balance - the...
Problem 1: In java Create a program to calculate interest rate and total balance - the program consists of (a) A class that encapsulates the interest rate and total balance calculation Constructor should accept initial balance and interest rate Methods: Returns interest rate Returns total balance Set interest rate Set initial balance (b) A test program that uses class from step (A) to perform the following: Set interest rate to 5% and initial balance to 1000 Print interest amount and...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
create a program in java that will evaluate a logical expression (compound proposition) based on the...
create a program in java that will evaluate a logical expression (compound proposition) based on the given truth values of individual propositional variables. The logical expression may include the logical AND or the logical OR operators. The NOT operator will be included in the variable names itself. So, a proposition such as ¬a would appear as na in the logical expression. Here is an example of a logical expression using proper notation: a ∧ b ∨ ¬c ∨ d This...
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
Calculate Interest in a Java program. If you know the balance and the annual percentage interest...
Calculate Interest in a Java program. If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula: interest = balance * (annualInterestRate/1200) Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month. Create a scanner Prompt the user to enter a name and create variable for scanner Prompt the user to enter a balance and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT