Code for Activity2PayStub: // Activity2PayStub.java import java.util.Scanner; public class Activity2PayStub{ // constants public static final double OVERTIME_RATE = 1.5; public static final double SS_WITHHOLDING = .1; public static final double FEDERAL_TAX = .2; // fields private String name, ssn; private int regHours, overHours; private double hourlyRate, regPay, overRate, overPay, grossPay, ssWith, fedTax, netPay; public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); // create an object of Activity2PayStub class Activity2PayStub a2ps = new Activity2PayStub(); // get inputs a2ps.getInput(keyboard); // compute payments a2ps.calculate(); // display information a2ps.printPayStub(); } /** * method that takes inputs of name, ssn, regular hours, overtime hours * and hourly rate and set the fields to the values input by the user */ public void getInput(Scanner keyboard) { System.out.print("Enter your name: "); name = keyboard.nextLine(); System.out.print("Enter your SSN: "); ssn = keyboard.nextLine(); System.out.print("Enter your regular hours: "); regHours = keyboard.nextInt(); System.out.print("Enter your overtime hours: "); overHours = keyboard.nextInt(); System.out.print("Hourly pay rate: $"); hourlyRate = keyboard.nextDouble(); } /** * method to calculate the regular pay, overtime rate and pay, gross pay * ssWitholding, federal tax and net pay */ public void calculate() { regPay = regHours * hourlyRate; overRate = hourlyRate * OVERTIME_RATE; overPay = overHours * OVERTIME_RATE * hourlyRate; grossPay = regPay + overPay; ssWith = grossPay * SS_WITHHOLDING; fedTax = (grossPay - ssWith) * FEDERAL_TAX; netPay = grossPay - ssWith - fedTax; } /** * method to display the details input and calculated */ public void printPayStub() { System.out.println("___________________________________________________" + "_________________"); String format = "Name: %-37s SSN: %-11s\n"; System.out.printf(format, name, ssn); format = "Regular Hours: %-8d Reg Rate: $%-8.2f Reg Pay: $%-8.2f\n"; System.out.printf(format, regHours, hourlyRate, regPay); format = "Overtime Hours: %-7d OT Rate: $%-8.2f OT Pay: $%-8.2f\n"; System.out.printf(format, overHours, overRate, overPay); format = "Gross Pay: $%-8.2f\n"; System.out.printf(format, grossPay); format = "SS Withholding: $%-8.2f\n"; System.out.printf(format, ssWith); format = "Federal Tax: $%-8.2f\n"; System.out.printf(format, fedTax); format = "Net Pay: $%-8.2f\n"; System.out.printf(format, netPay); System.out.println("___________________________________________________" + "_________________"); } } // end of Activity2PayStub.java Create a new class named PayStub. Copy the code in your Activity2PayStub class into the new class. (Remember to update your comments and the class name.) Add this empty constructor: public PayStub(Scanner keyboard) { } Inside the constructor make calls to your getInput and calculate methods. Since you are within the PayStub class, you have direct access to getInput and calculate. You don't need to create an object or use . Do not create a PayStub object here. Simply call the methods directly. Notice that the constructor does not have a return type. Notice also that we are not putting a call to printPayStub in the constructor. Normally we want to create objects and manipulate them, but we want to have control over when we print them. We do not want to print every PayStub object as soon as it is created We want the code that uses PayStub objects to create the objects and to display the objects, but not to get input or calculate directly. Those two methods are now called from within the constructor. So the constructor and the display method need to be public methods, i.e., usable by other classes, objects, and methods. The constructor is already public, but you may need to change your display method. Similarly, the getInput and calculate methods should not be public, so make them private.
Code for Activity2PayStub:
// Activity2PayStub.java
import java.util.Scanner;
public class Activity2PayStub{
// constants
public static final double OVERTIME_RATE = 1.5;
public static final double SS_WITHHOLDING = .1;
public static final double FEDERAL_TAX = .2;
// fields
private String name, ssn;
private int regHours, overHours;
private double hourlyRate, regPay, overRate, overPay, grossPay, ssWith, fedTax, netPay;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// create an object of Activity2PayStub class
Activity2PayStub a2ps = new Activity2PayStub();
// get inputs
a2ps.getInput(keyboard);
// compute payments
a2ps.calculate();
// display information
a2ps.printPayStub();
}
/**
* method that takes inputs of name, ssn, regular hours, overtime hours
* and hourly rate and set the fields to the values input by the user
*/
public void getInput(Scanner keyboard)
{
System.out.print("Enter your name: ");
name = keyboard.nextLine();
System.out.print("Enter your SSN: ");
ssn = keyboard.nextLine();
System.out.print("Enter your regular hours: ");
regHours = keyboard.nextInt();
System.out.print("Enter your overtime hours: ");
overHours = keyboard.nextInt();
System.out.print("Hourly pay rate: $");
hourlyRate = keyboard.nextDouble();
}
/**
* method to calculate the regular pay, overtime rate and pay, gross pay
* ssWitholding, federal tax and net pay
*/
public void calculate()
{
regPay = regHours * hourlyRate;
overRate = hourlyRate * OVERTIME_RATE;
overPay = overHours * OVERTIME_RATE * hourlyRate;
grossPay = regPay + overPay;
ssWith = grossPay * SS_WITHHOLDING;
fedTax = (grossPay - ssWith) * FEDERAL_TAX;
netPay = grossPay - ssWith - fedTax;
}
/**
* method to display the details input and calculated
*/
public void printPayStub()
{
System.out.println("___________________________________________________"
+ "_________________");
String format = "Name: %-37s SSN: %-11s\n";
System.out.printf(format, name, ssn);
format = "Regular Hours: %-8d Reg Rate: $%-8.2f Reg Pay: $%-8.2f\n";
System.out.printf(format, regHours, hourlyRate, regPay);
format = "Overtime Hours: %-7d OT Rate: $%-8.2f OT Pay: $%-8.2f\n";
System.out.printf(format, overHours, overRate, overPay);
format = "Gross Pay: $%-8.2f\n";
System.out.printf(format, grossPay);
format = "SS Withholding: $%-8.2f\n";
System.out.printf(format, ssWith);
format = "Federal Tax: $%-8.2f\n";
System.out.printf(format, fedTax);
format = "Net Pay: $%-8.2f\n";
System.out.printf(format, netPay);
System.out.println("___________________________________________________"
+ "_________________");
}
}
// end of Activity2PayStub.java
Create a new class named PayStub. Copy the code in your Activity2PayStub class into the new class. (Remember to update your comments and the class name.)
Add this empty constructor:
public PayStub(Scanner keyboard)
{
}
Inside the constructor make calls to your getInput and calculate methods. Since you are within the PayStub class, you have direct access to getInput and calculate. You don't need to create an object or use . Do not create a PayStub object here. Simply call the methods directly. Notice that the constructor does not have a return type. Notice also that we are not putting a call to printPayStub in the constructor. Normally we want to create objects and manipulate them, but we want to have control over when we print them. We do not want to print every PayStub object as soon as it is created
We want the code that uses PayStub objects to create the objects and to display the objects, but not to get input or calculate directly. Those two methods are now called from within the constructor. So the constructor and the display method need to be public methods, i.e., usable by other classes, objects, and methods. The constructor is already public, but you may need to change your display method. Similarly, the getInput and calculate methods should not be public, so make them private.
Add an accessor method for the overtime hours worked. A Java best practice is to name these methods getXXXX for some eld named XXXX. You may need to adjust the name below to match your elds' actual names.
![input of Marge's pay stub? The last prompt from Homer's pay stub was to enter the hourly pay
rate. Suppose "23.45\n" is entered as the hourly pay rate. Then the call to nextDouble that reads
this input stops at the newline and leaves it in the input. The next call to a Scanner method
happens to come from the constructor for Marge's pay stub. And this rst request is for the name,
which is ready by nextLine. The nextLine method nds the newline character and interprets that
as a line (an empty line, but still a line), and so immediately returns this empty line that then
causes getInput to ask for Marge's social security number. It's a bit tricky, but if you run the
program a couple times and reread this section then you should be able to follow it. So, that's
the problem, but how can we x it? We just need to make sure to get rid of that last newline
character when we are nished getting the hourly rate. To do this, modify your constructor to call
nextLine after the call to getInput.
(d) After constructing the three pay stub objects (which causes you to enter all the input data),
print the second one (the one for Marge is the second one) using your display method.
(e) Modify the code in main to give the second worker (Marge, above) a raise by mutating her
hourly pay rate. Make her new hourly pay rate $20.00 per hour.
(f) Changing Marge's wage should cause a recalculation of her pay stub, so let's print out her
new gross pay using the accessor method. Include a newline before displaying Marge's new
gross pay, and then simply copy and paste the following line so that it is the last line in your
main method:
System.out.printf("New gross pay: $%-8.2f\n", marge.getGrossPay());
You may have to change marge.getGrossPay() to use whatever object and method name you
used.
When you run your main method you should see the following if you type the red text exactly.
You should get the pay stub shown and the line with the new gross pay. Notice the blank line
between the last line of underscores and the new gross pay line.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6ee690c1-7550-48bb-b0b1-146590e5cb48%2F9369b561-7aa2-47e1-bc9d-ef47d785549e%2F8hpzajj_processed.jpeg&w=3840&q=75)
![public int getOvertime HoursWorked()
{
return overtime HoursWorked;
}
Add an accessor for the gross pay field.
Add a mutator eld for the overtime hours worked. A Java best practice is to name these
methods setXXXX for some eld named XXXX. You may need to adjust the name below to
match your elds' actual names. In addition to changing a eld's value, a mutator method can also
verify that the new value makes appropriate sense and prevent erroneous modications. (You'll
learn about if statements very soon, so just copy this for now.) Since changing a raw data value
might impact other elds we use the calculate method to recalculate based on the new value of
the overtime hours eld. This gives:
public void setOvertime HoursWorked (int newValue)
{
if (newValue >= 0)
{ overtimeHoursWorked = newValue; calculate();
}
}
Add a mutator eld for changing the pay rate. Don't forget to recalculate the pay after that raise.
Note that a pay rate is a double value.
Now let's tackle our new main method.
(a) Delete everything in main except for the Scanner object initialization.
(b) Declare three PayStub object variables like this:
PayStub homer = new PayStub(keyboard);
PayStub marge = new PayStub(keyboard);
PayStub lisa = new PayStub(keyboard);
(c) You should be able to compile and run your main method. You likely will discover that the
input for the rst one works ne, but the second one doesn't get the name properly. What is
happening? The issue highlights a tricky aspect of using Scanner. When you try to get numeric
input with a Scanner method (e.g, nextInt or nextDouble) it will skip over any leading whitespace
including any newlines (i.e., strokes of the enter key). Once any leading whitespace is gone, it
begins to read digits. It continues to read digits until some non-digit appears. Note that a
newline is not a digit! When this non-digit input is encountered, the Scanner method will stop
and not read any more input. So, if the input is a newline character followed by a 7, a 5, and
then a newline character (i.e., is "\n75\n"), then nextInt will skip the leading newline character,
read the 7 and the 5 as the integer value seventy ve, and stop reading input. The next call to a
Scanner method will begin with the newline character after the 5. However, the Scanner method
nextLine works a little dierently. It reads a line which is terminated by a newline character;
moreover, it reads this terminating newline out of the input. So, if the input is "hello\ngoodbye\n"
then Scanner's nextLine method will read the characters in "hello" and the newline character.
The next call to a Scanner method begins with the character `g'. So, what is happening to the](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6ee690c1-7550-48bb-b0b1-146590e5cb48%2F9369b561-7aa2-47e1-bc9d-ef47d785549e%2Ftl27vr_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)