use code from the previous question. Write a separate Java class for calculation
ID: 3819214 • Letter: U
Question
use code from the previous question. Write a separate Java class for calculations named Calc that will have three private instance variables of type double for the gross pay, savings rate and IRA investment rate. Note that any calculated fields are NOT private variables in this class. The Java class should contain these methods:
A constructor that initializes the instance variables to default values of 0. This Constructor has no parameters
A constructor with three parameters that initializes the instance variables to the parameter values. Note this constructor is not used in the assignment.
A method for the user to input the gross pay, savings rate and IRA investment rate. Include prompts. Allow values only greater than 0
A method that outputs the input values along with appropriate messages
A method that returns the savings amount
A method that returns the IRA investment amount
This class should be placed in a file that is separate from the class containing the main program. Note: Only method #2 above has parameters. As is typical in a class, the rest of the class methods do not have parameters.
Write a Java application main program in Java file Main in the order specified below. This code will be in a .java file that is separate from the one for the Calc class above. Note that the main method should have savings amount and IRA investment amount as local variables. It should NOT have declarations for the gross pay, savings rate and IRA investment rate.
You can use global scope only for the Toolkit or variables for formatting. All other declarations, including creating an instance of the calculation class, should be local to the main program. (If necessary, of course, you can declare local variables in the main program’s methods as needed.)
Call a void method in the main program that outputs an explanation of what the program does. Include in the explanation that this program uses a class for calculations and include your name.
Get the gross pay, savings rate and IRA investment rate from the user. Use the method from the calculation class.
Calculate the savings amount. Use the method from calculation class.
Calculate the IRA investment amount. Use the method from the calculation class.
Use a void method to output, with appropriate messages: the gross pay, savings rate and IRA investment rate (use the method from the calculation class), the savings amount, the IRA investment amount and the sum of the latter two.
Include an explanation for all variables, parameters and methods.
(original code to transform)
import java.io.*;
import java.util.Scanner;
import java.text.DecimalFormat;
public class savingsCalc {
public static void explanation()
{
System.out.println("explanation");
}
public static double entry()
{
Scanner console = new Scanner(System.in);
double value;
value = console.nextDouble();
if(value<=0){System.out.println("number must be greater then 0");
System.exit(0);}
else;
return value;
}
public static double calcAccount (double a, double b)
{
return (a * b) / 100;
}
public static void displayResults(double grossPay, double iraRate, double savingsRate,
double savingsAccount, double iraAccount, double bothAccounts)
{
DecimalFormat format1 = new DecimalFormat("0,000.00");
DecimalFormat format2 = new DecimalFormat("0.0");
System.out.println("Your gross pay is " + format1.format(grossPay));
System.out.println("The savings percentage rate is " +
format2.format(savingsRate));
System.out.println("The IRA investment rate is " + format2.format(iraRate));
System.out.println("The amount going into savings is " +
format1.format(savingsAccount));
System.out.println("The amount going into IRA account is " +
format1.format(iraAccount));
System.out.println("The total going into both accounts is " +
format1.format(bothAccounts));
System.out.println("name");
}
public static void main(String[] args)
{
explanation();
double grossPay, savingsRate, iraRate;
System.out.print("Enter your grosspay: ");
grossPay = entry();
System.out.print("Enter the percentage of savings: ");
savingsRate = entry();
System.out.print("Enter the percentage of your IRA investment: ");
iraRate = entry();
double iraAccount = calcAccount(grossPay, iraRate);
double savingsAccount = calcAccount(grossPay, savingsRate);
double bothAccounts = iraAccount + savingsAccount;
displayResults(grossPay, iraRate, savingsRate, savingsAccount,
iraAccount, bothAccounts);
}
}
Explanation / Answer
mport java.io.*;
import java.util.Scanner;
import java.text.DecimalFormat;
public class savingsCalc {
public static void explanation()
{
System.out.println("explanation");
}
public static double entry()
{
Scanner console = new Scanner(System.in);
double value;
value = console.nextDouble();
if(value<=0){System.out.println("number must be greater then 0");
System.exit(0);}
else;
return value;
}
public static double calcAccount (double a, double b)
{
return (a * b) / 100;
}
public static void displayResults(double grossPay, double iraRate, double savingsRate,
double savingsAccount, double iraAccount, double bothAccounts)
{
DecimalFormat format1 = new DecimalFormat("0,000.00");
DecimalFormat format2 = new DecimalFormat("0.0");
System.out.println("Your gross pay is " + format1.format(grossPay));
System.out.println("The savings percentage rate is " +
format2.format(savingsRate));
System.out.println("The IRA investment rate is " + format2.format(iraRate));
System.out.println("The amount going into savings is " +
format1.format(savingsAccount));
System.out.println("The amount going into IRA account is " +
format1.format(iraAccount));
System.out.println("The total going into both accounts is " +
format1.format(bothAccounts));
System.out.println("name");
}
public static void main(String[] args)
{
explanation();
double grossPay, savingsRate, iraRate;
System.out.print("Enter your grosspay: ");
grossPay = entry();
System.out.print("Enter the percentage of savings: ");
savingsRate = entry();
System.out.print("Enter the percentage of your IRA investment: ");
iraRate = entry();
double iraAccount = calcAccount(grossPay, iraRate);
double savingsAccount = calcAccount(grossPay, savingsRate);
double bothAccounts = iraAccount + savingsAccount;
displayResults(grossPay, iraRate, savingsRate, savingsAccount,
iraAccount, bothAccounts);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.