Question

Problem a (PA5a.java) Write a program that deals with inflation, which is essentially the rising cost...

Problem a (PA5a.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: (The numbers marked with blue color are the user’s input.) 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.

******Please make sure to add an error output message for the negative prices, years, and interest rates instead of having the program run the calculations with negative numbers

Homework Answers

Answer #1

Below is the code to find inflation for product given the number of years and inflation rate. It displays increased price for every year. The code is well commented to explain all the steps. It gets user input and program stops if values entered are negative integers.

//Code starts here

import java.util.Scanner;
public class Main
{
  
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x=1;
double f_price=0;
//Get item price
System.out.print("Enter the current price of the item: $");
double c_price = input.nextDouble();

//If single input is invalid there is no need to get other user input
//exit the program
if (c_price<=0) {
System.out.println("The current price must be at least 0!");
System.exit(0);
}
//Get number of years
System.out.print("Enter the number of years: ");
int years = input.nextInt();
  
//Exit if years value is negative
if (years<=0) {
System.out.println("The number of years must be at least 0!");
System.exit(0);
}
//Get inflation rate
System.out.print("Enter the inflation rate as a percentage: ");
double infr = input.nextDouble();
//Exit if inflation rate value is negative
if (infr<=0) {
System.out.println("The inflation rate must be at least 0!");
System.exit(0);
}
//Get rate by dividing by 100
double infrd = infr/100;
f_price = c_price; //Set the initial price to the price input by user
while ( x <= years) { // Loop from 1 to number of years
f_price = f_price * (1+infrd);
//We need to multiply updated price with (1+rate) to get updated price for every year
  
//Display value for each year.
System.out.printf("After %d years, the price will be $%.2f%n", x, f_price);
x++;
}
}
}

//Code ends here

Output

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
In java how do you write an inflation calculator. Input should be amount, rate of inflation...
In java how do you write an inflation calculator. Input should be amount, rate of inflation and number of years. Output should be the future value of the amount. Example Output: Enter the current cost of the item: 1000 Enter the expected inflation rate per year (Enter as a percentage and do not include the percent sign): 6 Enter the whole number of years in the future to project cost: 10 At 6.00% inflation per year, the cost in 10...
Write code in JAVA Write a program that will output a right triangle based on user...
Write code in JAVA Write a program that will output a right triangle based on user specified input height (int) and specified input symbol (char).The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches specified input height. Output a space after each user-specified character, including after the line's last user-specified character. Hint: Use a nested for loop. Ex:If the input...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
Write a Python program which calculates the average of the numbers entered by the user through...
Write a Python program which calculates the average of the numbers entered by the user through the keyboard. Use an interactive loop and ask the user at each iteration if he/she wants to enter more numbers. At the end dispay the average on the screen. Using built-in library functions for finding average is not allowed. Sample output of the program: Enter a number > 23 More numbers (yes or no)? y Enter a number > 4 Do you have more...
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Write a program (C language) that will read the number of a month and will print...
Write a program (C language) that will read the number of a month and will print the number of days in the month. Ignore leap years. Use 28 days for February. Have the program runs in a continuous loop, allowing the user to enter a month number, see that number of days, and repeat. Use month number = 0 to exit the loop and the program. Program must meet the following criteria: 1.Your name and the name of the program...
Problem 1 (Defining products and Creating a Menu) Write a program called a2_p1.py that asks a...
Problem 1 (Defining products and Creating a Menu) Write a program called a2_p1.py that asks a shop owner for product information and creates a menu system that allows customers to purchase multiple products of varying quantities. The program should start by asking the shop owner’s name (string) and company name (string). Next it should ask for three (3) products (strings), their prices (int – not ideal, but for now we will keep it this way), and the number of products...
WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's...
WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's profit (or loss) within a certain period of time. 1) First, the program need to get user's name, the stock's code, number of shares and the price he/she purchased the stock, it also asks the user for the price he/she sold the stock later on. The program then compute the following: The amount of money the customer paid for the stock. The amount of...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT