Question

(JAVA) Write a program that deals with inflation, which is essentially the rising cost of general goods over time.

IN JAVA

Write a 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 need to test for valid inputs and invalid inputs, such as negative price, negative year, and negative interest rate.

(( IN JAVA ONLY ))

3 1
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Inflation {

        public static void main(String[] args) {
                Scanner in = new Scanner(System.in);

                System.out.print("Enter the current price of the item: $");
                double currentPrice = in.nextDouble();

                System.out.print("Enter the number of years: ");
                int numYears = in.nextInt();
                System.out.print("Enter the inflation rate as a percentage: ");
                double rate = in.nextDouble();
                
                int y = numYears;
                while(y-- > 0) {
                        currentPrice = currentPrice + currentPrice * rate / 100.0;
                }
                
                System.out.printf("After " + numYears + " years, the price will be $%.2f\n", currentPrice);
                
                in.close();
        }

}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
(JAVA) Write a program that deals with inflation, which is essentially the rising cost of general goods over time.
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • java; programing language Write a program to calculate the price of a purchase. It should ask...

    java; programing language Write a program to calculate the price of a purchase. It should ask the user to enter the name of the item bought (like sweater, apple, …), number of items bought (4, 5 lbs,…), price per item ($40.50, $1.99 per pound,…), and the sales tax rate (8.35% for example). It should calculate the subtotal (price*number of items), sales tax (subtotal*sales tax), and total price (subtotal+ sales tax). It should print all the information in a receipt form

  • In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST...

    In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST use GUI, that means all user input must be through the windows. The "first window" is the window on the top left of the following picture. Add 5 more items to be purchased. Search is accessed from second window, top right. Thw third window is the bottom left, and the fourth window is the bottom right. The program MUST CONTAIN EVERYTHING IN THE PICTURES....

  • You are to write a program IN C++ that asks the user to enter an item's...

    You are to write a program IN C++ that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: if the an item's wholesale cost is 5.00 and its markup percentage is 100%, then the item's retail price is 10.00 If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50 Program design specs. You should have 5...

  • Price Markup Write a program that asks the user to enter an item’s wholesale cost and...

    Price Markup Write a program that asks the user to enter an item’s wholesale cost and its markup percentage. It should then display the item’s retail price. For example: If an item’s wholesale cost is 5.00 and its markup percentage is 100%, then the item’s retail price is 10.00. If an item’s wholesale cost is 5.00 and its markup percentage is 50%, then the item’s retail price is 7.50. The program should have a function named calculateRetail that receives the...

  • Write a C++ program that calculates the discount of the original price of an item and...

    Write a C++ program that calculates the discount of the original price of an item and displays the new price, and the total amount of savings. This program should have a separate header (discount.h), implementation (discount.cpp) and application files (main.cpp). The program must also have user input: the user must be prompted to enter data (the original price, and the percent off as a percentage). There must also be a validation, for example: Output: “Enter the original price of the...

  • JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow...

    JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...

  • 1. Markup Write a program that asks the user to enter an item's wholesale cost and...

    1. Markup Write a program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: . If an item's wholesale cost is $5.00 and its markup percentage is 100 percent, then . If an item's wholesale cost is $5.00 and its markup percentage is 50 percent, then The program should have a function named calculateReta1l that receives the wholesale the item's retail price is $10.00. the...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT