Question: Purpose Calculate savings using methods. This assignment builds on Assignment #2. Due Date Per the Course at a Glance. Can be resubmitted. Submissions Via ZyLabs.
Purpose Calculate savings using methods. This assignment builds on Assignment #2.
Due Date Per the Course at a Glance. Can be resubmitted.
Submissions Via ZyLabs.
Java Topics methods, if statements, calculating with percentages.
References Textbook use the index for relevant topics.
Specification
This program is an upgrade to Programming Assignment #2 to use methods. We will discuss a revisit of Assignment #1, Interactive Average with Methods, as a model.
Write a Java program and methods in the order listed below.
Declare the variables as type double for the gross pay, savings rate, IRA investment rate, savings amount, IRA investment amount and the total of the two amounts inside the main method and use them as appropriate when you call methods.
Call a void method that will output an explanation of what the program does. The explanation is this statement:
This program calculates savings amounts using methods.
2. Input these three numbers: gross pay, savings rate and IRA investment rate. Use one method to read and return a value so you will call this method three times. When a number is input, ensure it is greater than 0. Note: use the System.out.print style of Assignment #2 to prompt for input, not JOptionPane as in Assignment #3. Use the prompts exactly as shown below in step 4. If an input value is 0 or less, use this message:
Please enter a value greater than 0.
and prompt again for the value you are requesting.
Calculate the savings amount and IRA investment amounts using one method for both calculations. The method will have two parameters, the gross pay and the respective percentage rate. In the main program, use a third variable to calculate the total of these two amounts.
Use a void method to output, with the messages below: the three values that were inputted, the savings amount, IRA investment amount, and the total of the two amounts.
Note that this method requires six parameters. Output all decimal results to one or two places, depending on whether the result is a rate or a dollar amount, respectively. Using a gross pay of $10,000, a savings rate of 10% and an IRA rate of 5%, the messages are:
Enter the gross pay: 10000
Enter the savings rate %: 10
Enter the IRA rate: 5
Gross pay: 10,000.00
Savings rate: 10.0%
Savings amount: 1,000.00
IRA rate: 5.0%
IRA amount: 500.00
Total of amounts: 1,500.00
As before, include comments programmer identification, assignment number, section, purpose, explanation for all variables, parameters, and methods, and the vocabulary word and quote. See the documentation standards.
/**
Interactive Average Program with Methods
Accompanies Programming Assignment #5
Using methods, this program asks the user to input two real numbers,
calculates the average of these numbers, and prints the results.
Use strings as parameters to getOneNumber
Methods used:
explain() - explain the program to the user
getNum() - get numbers from the user
calcAvg() - calculate the average of the numbers
outputResults()- output the numbers and their average
Version of your Integrated Development Environment (IDE), computer and OS
New vocabulary word and its meaning
Inspirational quote not religious or political along with the source and
the persons year of birth [and death] written as, for example, (1912 1987)
or, if the person is still alive, (b. 1949)
*/
import java.util.Scanner; // For console input
public class InteractiveAverageMethods_AdaptedForAsgn05 {
// Note that the Scanner an Toolkit objects are preceeded by 'static' and
// appear before the main program. This gives them a global scope.
static Scanner console = new Scanner(System.in);
static Toolkit tools = new Toolkit();
static final int NUMBER_WIDTH = 0; // # of spaces to display numbers
public static void main (String [] args) {
double num1 = 0.0; // First input value
double num2 = 0.0; // Second input value
double grossPay = 0.0;
double savingsRate = 0.0;
double iraRate = 0.0;
double average = 0.0; // Average of the input values
// Explain the program to the user
explain();
// Input the numbers
grossPay = getOneNumber("gross pay");
savingsRate = getOneNumber("savings rate");
iraRate = getOneNumber("IRA rate");
// Determine the average of the two numbers
average = calcAvg(num1, num2);
// Output the results
outputResults(num1, num2, average);
System.exit(0);
} // End main
// **************************************************************
// Methods section
// Explain the program to the user
public static void explain() {
System.out.println(
"This program averages two real numbers " +
"entered by the user. " +
"The output is the two numbers and their average. " +
"Note: methods are used.");
} // End explain
// ***************************************************************
// Return the number input by the user
public static double getOneNumber(String phrase) {
double num; // Number input by the user
System.out.print("Enter your " + phrase + ": ");
num = console.nextDouble();
return num;
} // end getOneNumber
// ***************************************************************
// Return the average of two numbers, a and b
public static double calcAvg(double a, double b) {
return (a + b) / 2.0;
} // End calcAvg
// ***************************************************************
// Output the values of numbers first and second and their average
public static void outputResults(double first,
double second,
double average) {
System.out.print(
"The average of " +
tools.leftPad(first, NUMBER_WIDTH, "0.0") +
" and " + tools.leftPad(second, NUMBER_WIDTH, "0.0") +
" is " + tools.leftPad(average, NUMBER_WIDTH, "0.00"));
} // End outputResults
} // End class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
