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 =...
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...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the range between −25 and 25 THIS IS MY CODE! please edit this...Thank you import java.util.Scanner; public class Array { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] integs = new int[51]; System.out.println("Enter integers 0-50 [enter -1 to quit]: "); int nums = scan.nextInt(); while (nums != -1 && nums>=0 && nums<=50) { integs[nums]++; nums = scan.nextInt(); } for...
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...
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...
2). Question with methods use scanner (Using Java): You are going to invest the amount, then...
2). Question with methods use scanner (Using Java): You are going to invest the amount, then you going to compute that amount with annual interest rate. 1) Prompt the user to enter student id, student name, student major 2) Declare variables consistent with names that you are prompting and relate them to the scanner 3) Declare the amount invested in college, prompt the user to ask for amount and declare variable and scanner to input 4) Declare the annual interest...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class Test { int a, b; Test(int i, int j) { a = i; b = j; } void meth(Test s) { s.a *= 2; s.b /= 2; } } class demo { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("Before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("After call: " + ob.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. 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 {...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT