Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Good morning everyone! I seem to be having a lot of difficulty figuring out how

ID: 3572633 • Letter: G

Question

Good morning everyone! I seem to be having a lot of difficulty figuring out how to do this Java programming assignment. Would someone mind helping me out? The assignment is as follows:

Write a Java program that deals with inflation, which is essentially the rising cost of general goods over time. That is, the price of goods, such as a packet of peanuts, goes up as time goes by. So, you will 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, and the rate of inflation. The output is the estimated cost of the item after that number of years, using the given inflation rate. The user enters the inflation rate as a percentage, for example 4.5. You will have to convert the percentage to a fraction (like 0.045), and then use a loop to estimate the item's price adjusted for inflation. Note that this is similar to computing compound interest on a credit card account or a mortgage. Also note that you must check each of the values provided by the user to make sure that they are reasonable. Finally, you have to print out the price with exactly two places after the decimal (for the cents) after your calculations are done. To adjust the price for inflation, you need to increase the price by the inflation rate each year.

For example, if you have an item that is initially $10, with inflation rate of 10%, the adjusted prices will be: • After 1 year: $10.00 (1 + 0.10) = $11.00 • After 2 years: $11.00 (1 + 0.10) = $12.10 • After 3 years: $12.10 (1 + 0.10) = $13.31 …

In other words, to calculate the price after another year, you have to use the value from the current year, NOT the original price. To do this, you must use a loop. An example of what your program should output:

Enter the current price of the item: $10

Enter the number of years: 3

Enter the inflation rate as a percentage: 10

After 3 years, the price will be $13.31

You have been supplied JUnit tests for some simple valid examples, as well as negative price, negative year, and negative interest rate.

Thanks in advance, everyone! I greatly appreciate it!

Explanation / Answer

import java.text.DecimalFormat;

import java.util.Scanner;

public class Inflation {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println(" Enter initial cost");

       double initial_cost = in.nextDouble();

       System.out.println(" Enter number of years");

       int years = in.nextInt();

       System.out.println(" Enter inflation cost");

       double inflation_cost = in.nextDouble();

       find_price(initial_cost, years, inflation_cost);

   }

   private static void find_price(double initial_cost, int years, double inflation_cost) {

       double value = initial_cost * Math.pow((1 + (inflation_cost / 100)), years);

       System.out.println("Price after "+years+" is "+Double.parseDouble(new DecimalFormat(".##").format(value)));

      

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote