question archive It is difficult to make a budget that spans several years, because prices are not stable

It is difficult to make a budget that spans several years, because prices are not stable

Subject:Computer SciencePrice:2.87 Bought7

It is difficult to make a budget that spans several years, because prices are not stable. If your company needs 200 pencils per year, you cannot simply use this year's price as the cost of pencils two years from now. Due to inflation the cost is likely to be higher than it is today. Write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years from now that the item will be purchased, and the rate of inflation. The program then outputs the estimated cost of the item after the specified period. Have the user enter the inflation rate as a percentage, like 5.6 (percent). Your program should then convert the percent to a fraction, like 0.056, and should use a loop to estimate the price adjusted for inflation.
 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer,

// Inflation.java

import java.util.Scanner;
import java.text.NumberFormat;

/**
* Computes the estimated cost of an item after inflation
**/

public class Inflation {
   
    public static void main(String[] args) {
       
        // Make a Scanner to read data from the console
        Scanner console = new Scanner(System.in);
       
        // Read in the current item cost, years, and inflation rate
        System.out.println("Enter cost of item:");
        double cost = console.nextDouble();
        System.out.println("Enter number of (whole) years until the item is purchased:");
        int years = console.nextInt();
        System.out.println("Enter the inflation rate as a percentage (e.g., 5.6):");
        double inflationRate = console.nextDouble();       
       
        // Convertes percent to fractional value
        inflationRate = inflationRate / 100;
       
        //taking assumtion as
        //for the first year the cost is same as given cost,
        //so calculating after second year onwards
        //final cost after specified number of years
        for(int i = 2; i <= years; i++){
            cost += cost * inflationRate;
        }
       
        // Make a NumberFormat for printing the final cost
        NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
       
        System.out.println("The estimated final cost of the item will be " +
        moneyFormat.format(cost));
    }   
}


run-single:

Enter cost of item: 150

Enter number of (whole) years until the item is purchased: 5

Enter the inflation rate as a percentage (e.g., 5.6): 6.5

The estimated final cost of the item will be $192.97

 

Related Questions