question archive Java program because prices are not stable

Java program because prices are not stable

Subject:Computer SciencePrice:4.87 Bought7

Java program 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. Because of inflation, the cost is likely to be higher than it is today.

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, such as 5.6 (percent). Your program should then convert the percent to a fraction, such as 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:
import java.util.Scanner;
import java.text.NumberFormat;

public class Main {
  public static void main(String[] args) {
    double cost, inflation;
    int years;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the cost :");
    cost = input.nextDouble();
    System.out.print("Enter the number of years:");
    years = input.nextInt();
System.out.println("Enter the inflation rate as a percentage:");
inflation = input.nextDouble();
double inflationRate = inflation*0.01;
    for (int i = 0; i < years; i++)
      cost += cost * inflationRate;
    cost = (int) (cost * 100) / 100.0;
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
System.out.println("The estimated final cost of the item will be " +moneyFormat.format(cost));
  }
}

Step-by-step explanation

In the main function, we will get the cost and years as :

Scanner input = new Scanner(System.in);
    System.out.print("Enter the cost :");
    cost = input.nextDouble();
    System.out.print("Enter the number of years:");
    years = input.nextInt();

After this we will ask the user to enter the inflation rate as percent and then multiply the number with 0.01 to get the inflationRate and then, we will format the cost using the NumberFormat as:

double inflationRate = inflation*0.01;
    for (int i = 0; i < years; i++)
      cost += cost * inflationRate;
    cost = (int) (cost * 100) / 100.0;
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
System.out.println("The estimated final cost of the item will be " +moneyFormat.format(cost));

Output:

please use this google drive link to download the answer file.

https://drive.google.com/file/d/15cEPVjTTAjUdxcDBeB5GGEH2-ZM4xfR_/view?usp=sharing

note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.

https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link