Question

Write a program that takes two numbers from the Java console representing, respectively, an investment and...

Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest rate is an annual rate and is compounded annually.

This is what I have so far- the program runs just fine I'm just having a hard time figuring out how to convert the future value into US dollars. Right now I'm getting back a long scientific number, so please help me figure out how to convert :)

import java.util.*;
public class calculate {
public static double getFutureValue(int investment,double interestRate,int year)
{
return Math.pow(investment*(1+interestRate),year);
}
public static void main(String[] args) {
   int investment;
   double dollars;
double interestRate;
Scanner keyboard = new Scanner(System.in);
  
System.out.println("Enter the investment > ");
investment = keyboard.nextInt();

System.out.println("Enter the interest rate > " );
interestRate = keyboard.nextDouble();
  
System.out.println("With an investment of $"+investment);

  
System.out.println("at an interest rate of "+interestRate+"% compounded annually:");
  
double futureValue = getFutureValue(investment,interestRate,5);
System.out.println("The future value after 5 years is : "+futureValue);

futureValue = getFutureValue(investment,interestRate,10);
System.out.println("The future value after 10 years is : "+futureValue);

futureValue = getFutureValue(investment,interestRate,20);
System.out.println("The future value after 20 years is : "+futureValue);
  


}
}

Homework Answers

Answer #1

Note:

1) I made changes according to the requirement.

2) I have noticed some errors in the calculation part.So made changes in that area.

3) I formatted the output limiting to two decimal places.

Just run the program .If you find any thing wrong just tell me I will make changes.Thank You.

____________________

Calculate.java

import java.text.DecimalFormat;
import java.util.*;

public class Calculate {
   //This method will calculate the future value based on compound interest
   public static double getFutureValue(int investment, double interestRate,
           int year) {
       return (investment * Math.pow ((1+ interestRate / 12 ) , year*12));

   }

   public static void main(String[] args) {
       //Declaring the variables
       int investment;
       double dollars;
       double interestRate;
      
       //Scanner class object is used to get the inputs entered by the user
       Scanner keyboard = new Scanner(System.in);
      
       //DecimalFormat Object is used To format the output
       DecimalFormat df=new DecimalFormat("#.##");

       //getting the investment entered by te user
       System.out.print("Enter the investment : $");
       investment = keyboard.nextInt();

      
       //getting the investment rate entered by the user
       System.out.print("Enter the interest rate : %");
       interestRate = keyboard.nextDouble();

       System.out.println("With an investment of $" + investment);

       System.out.println("at an interest rate of " + df.format(interestRate*100)
               + "% compounded annually:");

       double futureValue = getFutureValue(investment, interestRate, 5);
       //Displaying the amount after 5 years
       System.out
               .println("The future value after 5 years is : $" + df.format(futureValue));

       futureValue = getFutureValue(investment, interestRate, 10);
      
       //Displaying the amount after 10 years
       System.out.println("The future value after 10 years is : $"
               + df.format(futureValue));

       futureValue = getFutureValue(investment, interestRate, 20);
      
       //Displaying the amount after 20 years
       System.out.println("The future value after 20 years is : $"
               + df.format(futureValue));

   }
}

______________________

output:

Enter the investment : $650
Enter the interest rate : %0.068
With an investment of $650
at an interest rate of 6.8% compounded annually:
The future value after 5 years is : $912.34
The future value after 10 years is : $1280.56
The future value after 20 years is : $2522.82

____________Thank You

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else {...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Download the attached .java file. Run it, become familiar with its processes. Your task is to...
Download the attached .java file. Run it, become familiar with its processes. Your task is to turn TemperatureConversion into GUI based program. it should, at the least, perform similar functions as their text output versions. The key factor to remember is that the workings should remain the same (some tweaks may be necessary) between text and GUI programs, while the means pf visual presentation and user interaction changes. You must properly document, comment, indent, space, and structure both programs. import...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...