Question: Open your Lab 3 BlueJ project and copy your Activity 1 PayStub class into a new class. Call this class Activity 2 PayStub. To make

Open your Lab3 BlueJ project and copy your Activity1PayStub class into a new class. Call this class Activity2PayStub. To make it you can create a new class with this name, then remove all of its default contents, then copy/paste all the code from Activity1PayStub, and then make sure the line of code reading
public class Activity1PayStub
is modified to
public class Activity2PayStub
2. The class Activity2PayStub will have four methods in it. Create the three new methods below the main method - i.e., after the brace that closes main - like so:
public static void main(String[] args)
{
... Code from Activity 1 is in here now ...
... Don't modify main yet...
}
public void getInput(Scanner keyboard)
{
}
public void calculate()
{
}
public void printPayStub()
{
}
3. Move all your code to get input from your main method into the new getInput method. Leave the Scanner declaration in main. Most likely you will move some System.out.print method calls that prompted the user for input, as well as some Scanner object method calls to get that input.
Notice that the variables you scan the input into are not in your new method but are still in main. Should we move those to the new method, too?
We could, but then when we need to use them in the calculate method (next activity step) then we'd find they were not available. (Remember that local variables are only visible to the statements in that method that appear after their declarations.)
So, instead, we will move them out and up into the class. This makes them fields of the class rather than variables local to a method. According to our style document, fields should be placed below constants and before any methods.
One of your new fields should look like this:
private String employeeName;
For reasons to be discussed later, we need to add the keyword private when we turn variables into fields. Of course, it's OK if your variable (now field) name is different from employeeName. Make all of the variables in your getInput method into fields in the way you did this for first one (i.e., for employeeName, or whatever you have called yours).
Now, you will still need to assign a value to employeeName in the getInput method, as follows:
employeeName = keyboard.nextLine();
You need to do the same for all of the variables you moved from main into the getInput method. (Why?) Note, however, that the Scanner object will still be declared in main and passed to the getInput method as a parameter.
4. Now move all your code for performing calculations from your main method into the calculate method. These are the statements computing the gross pay, etc. Again, some local variables likely will need to become fields.
5. Now move all your code for displaying output to the printPayStub method. These will most likely be some formatting statement and some printf method calls.
6. Your main method should now be empty except for constructing the Scanner object. We don't actually want our main method to do much work; instead, it acts as a kind of traffic cop and directs other methods to do their part at the appropriate time.
So, after constructing the Scanner object, have your main make a sequence of method calls to coordinate the other methods. You might think the following would work, but there is a problem:
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
//THE FOLLOWING METHOD CALLS DO NOT WORK
getInput(keyboard); //We don't have access to getInput
calculate(); //We don't have access to calculate
printPayStub(); //We don't have have access to printPayStub
}
Why don't we have access to getInput, calculate, and printPayStub? Since main is static, it no longer has access to things that are not static. You might be tempted to make the fields and methods static to fix this, but you must not do this. You must learn the correct way to access members from main.
You will learn about static methods later in the semester, but for now, do not make any fields or methods static. The only items that should be static are main and constants until you learn the very specific times to use static methods.
To access the methods of the Activity2PayStub class from main, you must make an Activity2PayStub object and use the object to access the methods. Here is how:
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
//Create an Activity2Paystub object
//a2ps is an Activity2PayStub object
Activity2PayStub a2ps = new Activity2PayStub();
//call the methods inside of an Activity2PayStub object
a2ps.getInput(keyboard);
a2ps.calculate();
a2ps.printPayStub();
}
Note that this main method could be in the Activity2PayStub class or in a completely different class. It doesn't matter what class main is in, but it must interact using objects and methods and has no dire

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!