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 Lab BlueJ project and copy your ActivityPayStub class into a new class. Call this class ActivityPayStub. To make it you can create a new class with this name, then remove all of its default contents, then copypaste all the code from ActivityPayStub, and then make sure the line of code reading
public class ActivityPayStub
is modified to
public class ActivityPayStub
The class ActivityPayStub will have four methods in it Create the three new methods below the main method ie after the brace that closes main like so:
public static void mainString args
Code from Activity is in here now
Don't modify main yet...
public void getInputScanner keyboard
public void calculate
public void printPayStub
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 ie 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.
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.
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.
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 mainString args
Scanner keyboard new ScannerSystemin;
THE FOLLOWING METHOD CALLS DO NOT WORK
getInputkeyboard; 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 ActivityPayStub class from main, you must make an ActivityPayStub object and use the object to access the methods. Here is how:
public static void mainString args
Scanner keyboard new ScannerSystemin;
Create an ActivityPaystub object
aps is an ActivityPayStub object
ActivityPayStub aps new ActivityPayStub;
call the methods inside of an ActivityPayStub object
apsgetInputkeyboard;
apscalculate;
apsprintPayStub;
Note that this main method could be in the ActivityPayStub 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
