As long as it works. Java Add copies of the files from the project that you comp
ID: 3804317 • Letter: A
Question
As long as it works.
Java
Add copies of the files from the project that you completed for Lab Exercise 5
Modify the Main driver class from the previous lab exercise as described below.
Eliminate tax rate as an input and a parameter for invoking methods.
Within the while loop, add code to prompt the user for the type of taxpayer and input the type from the user. Be sure to present the list of available types to the user, namely (1) Weekly Taxpayer, (2) Biweekly Taxpayer, and (3) Monthly Taxpayer. Tell the user what to enter to specify the taxpayer type. For example, you could show the numbers for the list items, and tell the user to enter the number of their choice. An alternative would be to have the user enter the first letter of the type name.
The existing driver program includes the following statement to create the taxpayer object (instance). t = new Taxpayer(nameStr, ssnNum, grossPay, trate);
Eliminate this statement. Replace the statement with code that creates the correct type of taxpayer object (instance) based on the taxpayer type that the user entered. The program should create instances of the WeeklyTaxpayer, BiweeklyTaxpayer, and Monthly Taxpayer classes. The program should not create instances of the Taxpayer class. Use a switch statement. In Lab 6-A you will only implement the WeeklyTaxpayer class, and in Lab 6-B you will implement the remaining two classes.
Level 1: Taxpayer Level 2: WeeklyTaxpayer BiweeklyTaxpayer MonthlyTaxpayer
Modify the for-loop that creates the final display list of all the taxpayers of all types. This for-loop is after the while(true) loop and iterates though only the actual (non-null) variables.
In order to accomplish this, do the following:
In the body of the for-loop, in the statement that concatenates to the variable outputStr2, add code to also display the taxpayer type on a separate line. Use the following code to concatenate the taxpayer type into your outputStr2 string: . . . + "Taxpayer Type: " + t.getClass().getName() + . . .
Be sure that your display includes all the data on each taxpayer.
Keep the statement that appears after the for-loop and uses a JOptionPane dialog to display the contents of outputStr2.
1
Modify your Taxpayer class. We will use the Taxpayer class as the top class of our two level class hierarchy. Make the following modifications to your Taxpayer class:
For each member variable, change the visibility (access) modifier from private to protected. This will enable the member variables to be inherited by subclasses of the Taxpayer class.
Remove tax rate as a member variable.
Change the computeTax method so that it does nothing except return 0.0. Some of you may have re-named this computeStateTax.
Modify the toString() method to include the following code to add the name of the class to the returned string: . . . + "Taxpayer Type: " + this.getClass().getName() + . . .
Note: You will answer questions about the getClass method and the getName method in a later step of Exercise B.
Create a new public class, called WeeklyTaxpayer, that is a subclass of Taxpayer. This new class must be in a separate file with the same name as the class. You can create the new class in the same package (folder) that contains the Main class (file) and Taxpayer class.
The WeeklyTaxpayer class must inherit all the member variables of the Taxpayer class. Do not declare any member variables in the WeeklyTaxpayer class.
The WeeklyTaxpayer class must have a public constructor that takes a value for each of the inherited member variables as parameter. The inherited variables are: name, SSN number, and gross pay. The constructor must invoke the constructor of the parent class to initialize the variables. The WeeklyTaxpayer constructor should not initialize the variables within its body.
The WeeklyTaxpayer class must have a public computeStateTax method that calculates tax by using the methods described previously in Lab 3B
Example for NYS tax calculation: If the gross pay is $700 and weekly, find where it fits in the table. This would be row 5 (385 to 1731). Subtract the amount 385 (row 5 col 4) from the gross pay. This is the adjusted pay (315). Multiply the adjusted pay by the corresponding rate found in row 5 of the NYS rates (0.0685). Now add the amount 18.71 to that product (21.58+18.71). This is the NYS tax, 40.29. Note that you always work from row 5 once you have determined that is where the gross pay fits in the table.
Example for federal tax calculation: if the gross pay is $700 and weekly, then subtract the weekly allowance of 65.38 from the gross pay. Take this adjusted pay, 634.62, and find where it fits into the table under wages allowance . This would be row 3 (195 to 645).. Adjust pay again by subtracting (row 3 column 5) 195 (634.62-195). The corresponding rate is then on row 3 in the rate table at 15%. Take 15% of the adjusted pay (439.62) and add $14.40 to the amount.(65.94+14.40= 80.34) This is your federal tax,. Note that you always work from row 3 once you have determined that is where the gross pay minus the exemption fits in the table.
The WeeklyTaxpayer class must have a public toString method that concatenates together and returns the following:
The string returned by invoking the parent class toString method to get a string that includes the values of all the member variables for a WeeklyTaxpayer instance. Note: The type of the taxpayer should be included in the string returned by the parent class toString method. The result of formatting the return values from invoking the computeStateTax and computeFederalTax methods that are within the WeeklyTaxpayer class.
Lab05.java
package lab05;
import javax.swing.*;
import java.text.*;
public class Lab05 {
/** Creates a new instance of Main */
public Lab05() {
}
/**
* @param args the command line arguments
*/
public static void main( String[] args ) {
final int MAX_NUMBER = 5;
Taxpayer t1 = null; // Variable to hold an instance of the Taxpayer class
Taxpayer t2 = null; // Same as above
Taxpayer t3 = null; // Same
Taxpayer t4 = null; // Same
Taxpayer t5 = null; // Same
Taxpayer t = null;
DecimalFormat prec2 = new DecimalFormat("$#.00");
DecimalFormat prec1 = new DecimalFormat("#.0%");
String nameStr = ""; // Holds taxpayer name entered by user
String ssNumStr = ""; // Holds taxpayer's SSNnumber as string, entered by user
int ssNum = 0; // Taxpayer's ssnumber as int
String tRateStr = ""; // Taxpayer's tax rate as string, entered by user
double tRate = 0.0; // Taxpayer's tax rate as double
String grossStr = ""; // Taxpayer's gross pay as string, entered by user
double grossPay = 0.0; // Taxpayer's gross pay as double
int count = 0; // The number of taxpayers created
String outputStr = ""; // String for output display to user
String outputStr2 = "";// String for output display to user
String msgStr = ""; // Holds message for display to user
int resp = 0; // User's response from showConfirmDialog method
while (true) { // Loop to input data on each taxpayer
count++;
// Read in name from user as a string
nameStr = JOptionPane.showInputDialog("Enter taxpayer name");
// Read in ID number from user as a string
ssNumStr = JOptionPane.showInputDialog("Enter SSN number");
// Convert from type String to type int
ssNum = Integer.parseInt(ssNumStr.trim());
// Read in tax rate from user as a string
tRateStr = JOptionPane.showInputDialog("Enter tax rate");
// Convert from type String to type double
tRate = Double.parseDouble(tRateStr.trim());
// Read in gross pay from user as a string
grossStr = JOptionPane.showInputDialog("Enter gross pay");
// Convert from type String to type double
grossPay = Double.parseDouble(grossStr.trim());
// Create Taxpayer instance
t = new Taxpayer(nameStr, ssNum, grossPay, tRate);
// Assign new Taxpayer object to next available Taxpayer variable
switch (count) {
case 1: t1 = t; break;
case 2: t2 = t; break;
case 3: t3 = t; break;
case 4: t4 = t; break;
case 5: t5 = t; break;
}
outputStr += count + ". " + t.toString() + " ";
if (count < MAX_NUMBER) {
msgStr = " Max number of allowed taxpayers is: " + MAX_NUMBER + " Continue?";
resp = JOptionPane.showConfirmDialog(null, outputStr + msgStr, "Confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (resp == JOptionPane.NO_OPTION)
break;
} else {
msgStr = "Program cannot handle any more additional taxpayers.";
JOptionPane.showMessageDialog(null, outputStr + msgStr, "Results",
JOptionPane.INFORMATION_MESSAGE);
break;
}
}
outputStr2 = "Taxpayer list: ";
for (int i = 1; i <= count; i++){
switch (i) {
case 1: t = t1; break;
case 2: t = t2; break;
case 3: t = t3; break;
case 4: t = t4; break;
case 5: t = t5; break;
}
outputStr2 += " " + i + ". " + t.getName() +
" SSN: " + t.getSSNumber() +
" Tax Rate: " + prec1.format(t.getTaxRate()/100) +
" Gross Pay: " + prec2.format(t.getGrossPay()) +
" Tax for Pay Period: " + prec2.format(t.computeTax()) + " ";
}
JOptionPane.showMessageDialog(null, outputStr2, "TAXPAYERS",
JOptionPane.INFORMATION_MESSAGE);
}
}
Taxpayer.java
package lab05;
import java.text.DecimalFormat;
public class Taxpayer {
// Instance variables
private String name;
private int ssNumber;
private double taxRate;
private double grossPay;
private static DecimalFormat df = new DecimalFormat("$#,###.00");
/**
* Constructor
*
* @param name - name of the tax payer
* @param ssN - ssn of the tax payer
* @param gp - gross pay
* @param trate - tax rate
*/
public Taxpayer(String name, int ssN, double gp, double trate) {
this.name = name;
this.ssNumber = ssN;
this.taxRate = trate;
this.grossPay = gp;
}
/**
* Returns the name
*/
public String getName() {
return name;
}
/**
* Returns the ssNumber
*/
public String getSSNumber() {
String ssnStr = ssNumber + "";
ssnStr = ssnStr.substring(0, 3) + "-" + ssnStr.substring(3, 6) + "-" + ssnStr.substring(6);
return ssnStr;
}
/**
* Returns the taxRate
*/
public double getTaxRate() {
return taxRate;
}
/**
* Returns the grossPay
*/
public double getGrossPay() {
return grossPay;
}
/**
* Computes the tax and returns it
* @return
*/
public double computeTax() {
return (getTaxRate() * getGrossPay())/100;
}
@Override
public String toString() {
return "Taxpayer Name: " + name + ", SSN: " + getSSNumber() + ", Tax Rate: " + taxRate + ", Gross Pay: " + df.format(grossPay)
+ ", Tax payable: " + df.format(computeTax());
}
}
Explanation / Answer
public class SavingsAccount { //Data fields private double balance; //Account Balance private double annualInterestRate; //Account annual interest rate private double monthlyInterestRate; private double totalDeposits; private double totalWithdraws; private double totalInterest; /** * Constructor * @param startBalance The account's balance. * @param annual_Interest_Rate The annual interest rate. */ public SavingsAccount(double startBalance, double annual_Interest_Rate) { balance = startBalance; annualInterestRate = annual_Interest_Rate; } //end of Constructor /** * setAnnualInterestRate method sets the annual interest * rate and calculates the monthly interest rate * @param annual_Interest_Rate The annual interest rate. */ public void setAnnualInterestRate(double annual_Interest_Rate) { monthlyInterestRate = annualInterestRate / 12; } //end of setAnnualInterestRate method /** * The deposit method adds the amount to the balance * and calculates the total deposit * @param amount */ public void setDeposit(double amount) { balance += amount; totalDeposits += amount; } //end of deposit method /** * The withdraw method subtracts the amount to the balance * and calculates the total withdraws * @param amount */ public void setWithdraw(double amount) { balance -= amount; totalWithdraws += amount; } //end of withdraw method /** * The calculateMonthlyInterest method calculates the total * interest and adds the monthly interest to the balance */ public void calculateMonthlyInterest() { totalInterest = totalInterest + balance * monthlyInterestRate; balance = balance + balance * monthlyInterestRate; } //end of calculateMonthlyInterest method /** * The getBalance method returns the account's balance. * @return The value of the balance field. */ public double getBalance() { return balance; } /** * The getAnnualInterestRate method returns the annual interest rate. * @return The value of the annual interest rate field. */ public double getAnnualInterestRate() { return annualInterestRate; } /** * The getMonthlyInterestRate method returns the monthly interest rate. * @return The value of the monthly interest rate field. */ public double getMonthlyInterestRate() { return monthlyInterestRate; } /** * The getTotalDeposits method returns the total deposit amount. * @return The value of the total deposits field. */ public double getTotalDeposits() { return totalDeposits; } /** * The getTotalWithdraws method returns the total withdraws amount. * @return The value of the total withdraws field. */ public double getTotalWithdraws() { return totalWithdraws; } /** * The getTotalInterest method returns the total interest amount. * @return The value of the total interest field. */ public double getTotalnterest() { return totalInterest; } /* displayData method displays the ending details of the savings account */ public void displayData() { balance = Math.round(balance * 100.0) / 100.0; totalInterest = Math.round(totalInterest * 100.0) / 100.0; System.out.println(); System.out.println("The ending balance is: $" + balance); System.out.println("Total amount of deposits: $" + totalDeposits); System.out.println("Total amount of withdraws: $" + totalWithdraws); System.out.println("Total interest earned: $" + totalInterest); } //end of displayData method } //end of SavingsAccount class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.