Question

Can I get the answers in Java, please? Write an application that prompts a user for...

Can I get the answers in Java, please?

Write an application that prompts a user for the number of years the user has until retirement and the amount of money the user can save annually. If the user enters a 0 or a negative number for either value, reprompt the user until valid entries are made. Display the amount of money the user will have at retirement.

The total amount of money the user will have can be calculated with the following equation:

total=numYears∗amountSaved

Example input:

0
5
-2
45

Example output:

How many years until retirement?
Years cannot be 0 or negative. Reenter years: 
How much can you save annually?
Amount cannot be 0 or negative. Reenter amount to save annually: 
If you save $45 for 5 years, you will have $225
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Retirement

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

       

        int numYears;

       

        System.out.println("How many years until retirement?");

       

        // get user input

        numYears = sc.nextInt();

       

        // infinite loop

        while(true)

        {          

            // if input is correct

            if( numYears > 0 )

                break;

           

            System.out.print("Years cannot be 0 or negative. Reenter years: ");

           

            // get user input

            numYears = sc.nextInt();

        }

       

        int amountSaved;

       

        System.out.println("\nHow much can you save annually?");

        

        // get user input

        amountSaved = sc.nextInt();

       

        // infinite loop

        while(true)

        {          

            // if input is correct

            if( amountSaved > 0 )

                break;

           

           System.out.print("Amount cannot be 0 or negative. Reenter amount to save annually: ");

           

            // get user input

            amountSaved = sc.nextInt();

        }

       

        // calculate total amount saved

        int total = numYears * amountSaved;

       

        System.out.println("If you saved $" + amountSaved + " for " + numYears + " years, you will hava $" + total);

    }

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
Can I get the answers in Java, please? Write an application that prompts a user for...
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
  • I need help with the following assignment: Write an application named DailyTemps that continuously prompts a...

    I need help with the following assignment: Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from -20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message: Valid temperatures range from -20 to 130. Please reenter temperature. Before the program ends, display the...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • JAVA 2. (8 points) Write a code segment which prompts the user to enter the price...

    JAVA 2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not...

  • (java) Write a While loop that prompts the user for only even numbers. Keep prompting the...

    (java) Write a While loop that prompts the user for only even numbers. Keep prompting the user to enter even numbers until the user enters an odd number. After the loop ends, print the sum of the numbers. Do not include that last odd number. You should not store the numbers in an array, just keep track of the sum. Make sure to use a break statement in your code. You may assume that a java.util.Scanner object named input has...

  • Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit...

    Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit 1. For Erase-ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase function is as follow: void Erase( int a[ ], int * N, int * Search-Element) The function Erase should remove all occurrences of Search-Element from the array al). Note that array a[ ] is loaded with integer numbers entered by the user through the keyboard. N...

  • Write a Java application that prompts the user for pairs of inputs of a product number...

    Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold (this is two separate prompts for input values). You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows: Product 1    1...

  • in java Write an application that: 1. Prompts the user for a number of trials to...

    in java Write an application that: 1. Prompts the user for a number of trials to perform (I will call this "N"). This operation should be type-safe for integers. 2. Simulates N trials of rolling two dice and finding the sum. 3. Uses an array to keep track of how many times each outcome has been rolled (you do not need to keep track of the actual rolls themselves). 4. Computes the percentage of the total number of rolls for...

  • c# language Description: Design and implement a C# application that allows the user to enter a...

    c# language Description: Design and implement a C# application that allows the user to enter a number N. It returns to the user the result of the Fibonacci of N. The Fibonacci of a number N is calculated as follows: Fibonacci (0) 0 Fibonacci (1) - Fibonacci (n) - Fibonacci (n-1) + Fibonacci (n-2) Requirements Your Application should have the following: • An exception class called FibException that is thrown when the user enters a negative value of N •...

  • Write a program called CountCoins.java that prompts the user for the input file name (you can...

    Write a program called CountCoins.java that prompts the user for the input file name (you can copy the getInputScanner() method given in the ProcessFile assignment) then reads the file. The file contains a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be “pennies” (1 cent each), “nickels” (5 cents each), “dimes” (10 cents each), or “quarters” (25 cents each), case-insensitively. Add up the cash values of...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

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