Obtain file Main.txt Create a new project and add the file from instructor to th
ID: 3828171 • Letter: O
Question
Obtain file Main.txt
Create a new project and add the file from instructor to the project. Paste the contents of the instructor-provided file into the default Main class (file) created by NetBeans for your project.
Do not change the Main class (file) provided to you except for the package name at the top of the file. Use your package name instead of the one in the instructor-provided file.
Create the Taxpayer Java file. The Taxpayer file must be in your new project mentioned above. It must be in a separate file from the Main class. To create the new file, select menu File>New File. In the New File dialog window, select the top choices (Categories: Java Classes; File Types: Java Class), and then click the Next button. In the New Java Class window, fill in the Class Name with “Taxpayer” (without the quotes) and make sure that the location and package is the same as for the Main file. Then click the Finish button.
As a result, under Source Packages, you should now have the two files listed within your package folder.
Write the code for the Taxpayer class (file). You should follow the instructions listed below carefully, since your Taxpayer class must work with the Main class (file) provided to you. You must use the Main class (file) to demonstrate that your Taxpayer class works correctly. The Taxpayer class must include the following items: Private member variable named name of type String. Private member variable named ssNumber of type int. Private member variable named taxRate of type double. Private member variable named grossPay of type double. Private DecimalFormat object to format monetary values, as in previous exercises. Public constructor that takes four parameters to initialize the member variables. The order of the parameters must be: String name, int ssN, double gp, double trate. A public get method for each member variable. Naming convention is to uppercase the firstletter of the member variable and then concatenate the word ?get? on the left and of the variable (e.g., getName, getSSNumber, getTaxRate, getGrossPay). Each get method takes no parameters and must return the value of the corresponding variable. For the social security number, return a string formatted version.
1
A public computeTax method that takes no parameters and returns a value of type double. The returned value is the taxpayer's tax, computed by simply multiplying taxRate times grossPay. Do not add a member variable for tax. A public toString method that takes no parameters and returns a string. The string includes the values of all the member variables as well as the taxpayer?s computed tax, with an explanatory label identifying each. All monetary values must be formatted with a dollar sign and two digits to the right of the decimal point. The returned string must not include any line breaks.
Format the source code properly: Reformat the Java code so that it has proper indentation and vertical and horizontal alignment. Be sure to have a blank line right above each method definition.
Compile and debug your modified program. Test your program using a sufficient variety of data to make sure that it works on all cases. Test using a mix of positive, negative, and zero integers.
package lab05;
import javax.swing.*;
import java.text.*;
public class Lab05 {
/**
* Creates a new instance of Main
*/
public Main() {
}
/**
* @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);
}
}
Explanation / Answer
package tax;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Taxpayer {
private String name;
private int ssNumber;
private double grossPay;
private double taxRate;
private DecimalFormat d;
public Taxpayer(String nameStr, int ssNum, double gp, double tRate) {
// TODO Auto-generated constructor stub
this.name=nameStr;
this.ssNumber=ssNum;
this.grossPay=gp;
this.taxRate=tRate;
}
public String getName() {
// TODO Auto-generated method stub
return name;
}
public String getSSNumber() {
// TODO Auto-generated method stub
return d.format(ssNumber);
}
public double getTaxRate() {
// TODO Auto-generated method stub
return taxRate;
}
public double getGrossPay() {
// TODO Auto-generated method stub
return grossPay;
}
public double computeTax() {
// TODO Auto-generated method stub
return taxRate*grossPay;
}
public String toString(){
NumberFormat formatter = d.getCurrencyInstance();
return String.format("Name: "+ name +" SecurityNum: "+ ssNumber + " Grosspay: " + formatter.format(grossPay)+" Taxrate: "+ formatter.format(taxRate));}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.