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
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...
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 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...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
Question 2: write a java program    A Hypermarket makes an offer for its customers according...
Question 2: write a java program    A Hypermarket makes an offer for its customers according to the number of items to be bought as follows: Number of purchased items Discount percentage Less than 25 2.5% 25 – 50 5.0% 51 – 100 6.25% Greater than 100 7.5% The hypermarket also adds 2% city taxes to the purchases after the sale discount. Reads from the user the number of items to be purchased and the item price. Calculates and prints...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT