ASSIGNMENT

90 views 8:47 am 0 Comments August 4, 2023

Java: PA2 ASSIGNMENT
TOTAL POINTS: 100
DUE:
April 6, 2021 BY 11:59 PM
READ CAREFULLY “ALL” THE INSTRUCTIONS!!!
Do not submit code that doesn’t compile, run, or generate correct output.
No assistance given via email.
If needed, seek help during open tutoring or instructor office hours.
Contents
UTSA HONOR CODE …………………………………………………………………………………………………………………………………………………………. 1
OBJECTIVE
……………………………………………………………………………………………………………………………………………………………………….. 1
PREP WORK
……………………………………………………………………………………………………………………………………………………………………… 2
GRADING
…………………………………………………………………………………………………………………………………………………………………………. 2
PROGRAM INSTRUCTIONS
……………………………………………………………………………………………………………………………………………….. 2
UML Diagram
……………………………………………………………………………………………………………………………………………………………….. 2
PROMPTS
…………………………………………………………………………………………………………………………………………………………………………. 6
Final Output Specifications
………………………………………………………………………………………………………………………………………………. 8
SUBMISSIONS REQUIREMENT………………………………………………………………………………………………………………………………………….. 8
SAMPLE OUTPUT……………………………………………………………………………………………………………………………………………………………… 9
UTSA HONOR CODE: As a UTSA student, you are bound by the honor code, so DO NOT cheat on
any of your coursework.
By submitting this assignment, you are attesting to your own
authorship
based on material from your textbook and/or your professor. Cheating can result in any
one, or combination, or all of the following: reduced or failing grade for the assignment, a signed
statement of the infraction, reduced or failing grade for the course, reporting of student name to the
Department Chair and faculty, Dean’s Office and COB faculty, and/or elevation to Student Conduct
and Community Standards.
OBJECTIVE (this is not the program purpose): Code a program that uses the concepts covered in
chapters 1-5 and lecture.
NOTE: Make sure your PA1 is running with the correct logic from the plan and correct
formatting (line advancing, spacing) and output before attempting PA2. If you followed the
PA1 plan, then the variables referenced in these instructions are the same; otherwise, you’ll
have to map them to yours. The underlying logic is basically the same.
Print out your PA1 code and the PA2 instructions. Read through the PA2 instructions
thoroughly to get a good idea of the logic, the required control structures, the methods and
any changes that have to be made. Insert hand-written changes or cut your PA1 code with
actual scissors and paste/tape the code in its proper order. The code in the main() from PA1
resides in the main() unless relocated to their respective methods, or in the case of some
variables to the class level. After you’ve done this, open DrJava, copy your PA1 code into a
.java file (named according to the file name indicated in item 4 below), then start altering it.
© 2021 Linda Shepherd 1
PREP WORK: Material from chapters 1-5 (including instructor PowerPoint slides, demo programs,
and completed exercises).
GRADING: You’ll be graded on how well you follow the program instructions and the accuracy of
your output as reflected in the prompts, the output specifications, and the sample output
.
This includes spacing and line advancing. Each line of output can be associated with multiple
points in the code!
The instructions, prompts and output are what the user wants. You are not at
liberty to change anything, but code to these requirements. You’ll also be graded on the code given
to you by your professor for this PA. Team members can receive a grade lower if their performance
rating is 3 or below out of a 5. Refer to Blackboard for details.
PROGRAM INSTRUCTIONS: Create a program that generates a payroll expense report. Don’t forget
to insert the exit statement at the end of main().
The prompts and the final output show you the logical
progression of your code and give you an idea as to where to place your code.
1. Work and submit this PA in a group.
2. UML Diagram for the class: Data members are variables. The variables can be derived from the
prompts and/or the final output. DO NOT code the methods in any way other than what is
instructed.
The – sign means private and the + means public in the UML.
Class Name
Class
Data
Members
Method
Data
Members

Methods
first, last:
+main(args:
String
String[]):
static void

TeamMembersLastNamesAlphaOrderYourSectionNoPA2
//Variables used in more than one method should
a //field or class variable.

-input: Scanner
-firstNmLength: int //NEW: Stores no of bytes in first name.
-dateTime: Calendar
-payrollExpense: String

+generateReport()
employeeName: String
//NEW: Stores the employee’s full
//name.
payRate, grossPay, retire401K, percent401K, grossPayTotal,

total401K: double
hoursWorked, trigger: int
cont: char

+setEmployeeName()
1. Call generateReport().
2. Exit statement.
+generateReport(): static void
1. Contains code originally in main() from PA1 with a
different implementation.
2. Start by moving the code to this method, then work line-by-line to determine
which variables will be fields and which will be local and which statements
are to be moved to the other methods.
© 2021 Linda Shepherd 2
3. The while loops and the IF-ELSE for the trigger remain in this method along
with the prompt for another employee.
4. The other methods will be called in
generateReport(). In generateReport(),
these methods are called in place of the code they now hold.
5. Right after the call to
setEmployeeName(), call setHoursWorked(). Send to
setHoursWorked() this argument: employeeName.substring(0,
firstNmLength)
. This argument is a method call that extracts the
employee’s first name from employeeName and sent to
setHoursWorked()
where it is used in the prompt for the number of hours worked.
+setEmployeeName(): static String
1. Prompt 2.
2. Check the first name to see if it is not alphabetic using a while loop that
keeps prompting until the first name is a valid alphabetic name:
“Enter valid first name: “
3. Figures out the length of the first name by calling
first.length() which is a
method that returns the number of bytes in the first name and stores it in a
field called
firstNmLength.
4. Check the last name to see if it is not alphabetic using a while loop that
keeps prompting until the last name is a valid alphabetic name:
“Enter valid last name: “
5. Returns the full name of the employee.
+setHoursWorked(first: String): static int
1. Prompt 3.
2. Use the validation code for an integer from the TestValidInput program.
Refer to the
re-prompt for invalid type values in hoursWorked.
3. Receives the first name and returns the hours worked from the keyboard.
+testHoursWorked(hoursWorked: int): static int
1. Prompt 3 error messages/re-prompts.
2. Receives hours worked and returns hours worked from the keyboard after
testing whether hours worked is greater than 40 or less than 5.
3. Use the validation code for an integer from the TestValidInput program.
Refer to the
re-prompt for out-of-range values in Prompt 3.
+setPayRate(): static double
1. Prompt 4.
2. Use the validation code for a double from the TestValidInput program. Refer
to the
re-prompt for invalid type values in payRate.
3. Returns the pay rate from the keyboard.
+testPayRate(payRate: double): static double
1. Prompt 4 error message/re-prompts.
2. Receives the pay rate and returns the pay rate from the keyboard
after testing whether the pay rate is less than 7.25 or greater than 26.
3. Use the validation code for a double from the TestValidInput
program. Refer to the
re-prompt for out-of-range values in Prompt 4.
© 2021 Linda Shepherd 3
+set401K(): static double
1. Prompt 5.
2. Use the validation code for a double from the TestValidInput program. Refer
to the
re-prompt for invalid type values in percent401K.
3. Returns the 401K contribution as a percentage of salary from the keyboard.
+test401K(): static double
1. Prompt 5 error message/re-prompt.
2. Returns the 401K contribution as a percentage of salary from the keyboard
when the contribution doesn’t exceed 10%.
3. Use the validation code for a double from the TestValidInput program. Refer
to the
re-prompt for out-of-range values in Prompt 5.
+formatDollarSign(employeeName: String, grossPay: double, retire401K:
double): static String
1. Receives the employee name, the gross pay, and the 401K
contribution amount.
2. Returns the formatted String
with a dollar sign that is added to the payroll
expense report.
+formatNoDollarSign(employeeName: String, grossPay: double, retire401K:
double): static String
1. Receives the employee name, the gross pay, and the 401K
contribution amount.
2. Returns the formatted String
without a dollar sign that is added to
the payroll expense report.
+isAlpha(name: String): static boolean
/**
* Using lambda expression to check for names that are alphabetic. *
The :: tells the compiler to call the isLetter method from
* the Character class. The chars() is a Java 9 String class
* method.
*/
public static boolean isAlpha(String name)
{
return name != null &&
name.chars().allMatch(Character::isLetter);
}//END isAlpha(String): static boolean
3. Logical Control Structures (from PA1):
a. Declare a payroll expense variable and initialize it with header, date, and time lines.
String payrollExpense = String.format(“WEEKLY HOURLY EMPLOYEE PAYROLL”
+ “%n%nDate: %tD”
+ “%nTime: %tr”
+ “%n%n%56S %23S”, dateTime, dateTime, “GROSS PAY”, “401K”);
b. Use if to determine when to print the exit message (to return press Alt then left arrow).
© 2021 Linda Shepherd 4
c. Use a while-loop to process payroll data for multiple employees. Use a sentinel-control
variable.
Prompts 2-6 are within this while-loop.
i. Use nested
whiles to determine the validity of hoursWorked, payRate, and the
401k percentage.
1) Use nested
ifs to determine which error message/re-prompt to print.

ii. Use nested if-else to determine the printing of the $ sign and adding an
employee’s payroll data to
payrollExpense.
Use
nested if to determine when to print the final output that is in payrollExpense.
iii.

d. Use printf() with format specifiers where needed.
e.
CODE FOR PROPER FINAL OUTPUT ALIGNMENT & JUSTIFICATION: where
payrollExpense is the String variable you’ve already declared and populated with the header
information. Each employee’s payroll data will be added to this variable. Examine the actual
Java code below and note the use of fields with different sizes to right and left justify so the
output can be aligned according to the
final output specifications. Note the use of += to add
to what is already in the
payrollExpense variable.
/*This if-else prints a $ sign for the first employee; otherwise, there’s no $ sign printed. Look at
*the output specs above or the sample output. The trigger variable determines the first
*employee.*/
if(trigger == 1)
{
payrollExpense += String.format(“%n%-30s %8s $%,15.2f %8s $%,13.2f”,
first + ” ” + last, ” “, grossPay, ” “, retire401K);
trigger = 0;
}
else
{
Resets the trigger variable so the code in the else, that
doesn’t print the $ sign, is executed for the next employee.

payrollExpense += String.format(“%n%-30s %9s %,15.2f %9s %,13.2f”,
first + ” ” + last, ” “, grossPay, ” “, retire401K);
}//END if trigger is 1 else trigger is not 1
/*The next 2 Java statements add the TOTALS line to payroll expense and then prints
*payroll expense which now contains all the output information.*/
payrollExpense += String.format(“%n%n%25s TOTALS %6s $%,15.2f %8s $%,13.2f”, ” “, ” “,
grossPayTotal, ” “, total401K);
Arguments for the TOTALS line. There
is a space between the double quotes.
System.out.printf(“%n%n%s”, payrollExpense); //PRINTS THE FINAL OUTPUT
//STORED IN payrollExpense
f. Don’t forget to insert the exit statement at the end of main().
g.
The Prompts, the final output specs, and the sample output show you in what order to
place your code.
To return from these links press Alt then left arrow.
© 2021 Linda Shepherd 5
h. You are to generate output from your program according the instructions in the sample output
section.
4.
Commenting Your Program:
a. In your program, YOU MUST insert a program purpose in the first comment box. The
content of that first comment box was shown to you in the
Anatomy of a Java Program lecture
for chapter 1.
b. Use Javadoc comment boxes beginning with /** and ending with */ for your comment boxes.
c. Insert a Javadoc comment box above your methods explaining what is going on in the method
that goes for the main() which is a method.
d. Line comment the import statements and the variables declared at the class level and/or in any
method [including main()].
5.
Formatting Rules: Refer to the Java Style Guide posted in Blackboard. Always test to validate
your program is functioning properly with the correct output and spacing (line advances and
spacing after punctuation). The %n can function differently when using separate printf statements
versus one printf.
PROMPTS: Code the bold from the prompts below in printf statements for capturing data. Once
again, the prompts tell you your input variables. Except for the first prompt, all the others reside
within the while-loop.
1st Prompt: Prompt to enter the weekly payroll system. Entry is determined by the value entered for
the loop-control variable.
WEEKLY HOURLY PAYROLL SYSTEM
Continue? Enter ‘Y’ or ‘N’:
When the answer to continue is ‘Y’ enter while-loop, otherwise, display this exit message:
Exiting Weekly Hourly Payroll System.
2nd Prompt: Capture the names into 2 separate variables.
Enter the employee’s first name press enter then the last name followed by the enter key:
3rd Prompt: Where the Xs represent the first name of the employee.
Enter the number of hours worked for Xxxxxxxxxx:
Keep displaying this error message/re-prompt as long as the hours entered are not the correct data
type.
Invalid type! Re-enter the number of hours worked for Xxxxxxxxxx:
Once the user enters a valid number, keep displaying this error message/re-prompt as long as the
number of hours worked exceeds 40.
Hours worked cannot EXCEED 40. Please re-enter:
© 2021 Linda Shepherd 6
OR keep displaying this error message/re-prompt as long as the number of hours worked is less
than 5.
Hours worked cannot be LESS than 5. Please re-enter:
If the hours re -entered for the re-prompt is an invalid type, keep displaying this error
message/re-prompt until a valid data type value is entered.
Invalid type! Re-enter the number of hours worked not less than 5 or greater than 40:
4th Prompt:
Enter the employee’s hourly pay rate:
Keep displaying this error message/re-prompt as long as the hours entered are not the correct data
type.
Invalid type! Re-enter the hourly pay rate:
Once the user enters a valid number, keep displaying this error message/re-prompt as long as the
pay rate is under $7.25.
Hourly pay cannot be LESS than $7.25. Please re-enter:
OR keep displaying this error message/re-prompt as long as the pay rate exceeds $26.00.
Hourly pay cannot EXCEED $26.00. Please re-enter:
If the pay rate re-entered for the re-prompt is an invalid type, keep displaying this error message/reprompt until a valid data type value is entered.
Invalid type! Re-enter an hourly pay rate that is not less than $7.25 or greater than $15.00:
5th Prompt:
Enter the employee’s 401K contribution as a percentage of salary (not to exceed 10%):
Keep displaying this error message/re-prompt as long as the 401K contribution entered is not the
correct data type.
Invalid type! Re-enter the 401K contribution not to exceed 10% of salary:
Once the user enters a valid number, keep displaying this error message/re-prompt as long as the
contribution percentage exceeds 10%.
Contribution cannot EXCEED 10%. Please re-enter:
If the 401K contribution re-entered for the re-prompt is an invalid type, keep displaying this error
message/re-prompt until a valid data type value is entered.
Invalid type! Re-enter a contribution NOT exceeding 10% of salary:
6th Prompt: This prompt is displayed at the end of the while-loop and alters the loop-control variable
that determines whether to re-enter the while-loop to process another employee or exit the payroll
system.
© 2021 Linda Shepherd 7
Enter ‘Y’ to add another employee or ‘N’ to exit:
FINAL OUTPUT SPECIFICATIONS: The Zs and 9s represent the output as formatted numbers,
where leading zeroes (Zs) are suppressed. The XX for Time indicates AM or PM. The Xs represent
the name of the employee. Use System.out.printf() and the appropriate format specifiers (look in
Appendix
I) to properly format the output. The date is NOT to be hard coded into the header, instead,
you will capture the system’s date (Appendix
I) and convert it to a String literal using String.format().
This is so the date corresponds to when the program is run. Right justify the monetary values. The
String variable
payrollExpense is used to store the header information (title through time); then,
somewhere else in the code, the employees, their weekly gross salary, and 401K contribution are
added. The $ sign doesn’t have to float (move) with the leading digit.
WEEKLY HOURLY EMPLOYEE PAYROLL

Date:
Time:
99/99/99
99:99:99 XX
GROSS PAY
$ZZZ,ZZ9.99
ZZZ,ZZ9.99
401K
$Z,ZZ9.99
Z,ZZ9.99
Xxxxxxxxxx Xxxxxxxxxxx
Xxxxxxxxxx Xxxxxxxxxxx

Date Label & Date
Time Label & Time
TOTALS $Z,ZZZ,ZZ9.99 $ZZ,ZZ9.99
***END FINAL OUTPUT SPECIFICATIONS***
SUBMISSIONS REQUIREMENT:
Employee name and
weekly gross pay and
401K contribution.
1. The group will only submit one folder with all the files. This means only one of the group
members should submit the PA
. ALL submissions will be ignored if both group members post
submissions. The
folder and the main() file will reflect the last names of both group members
in alphabetical order
. E.g., BunnyDuck001PA2. Through line and method comments, group
members will claim authorship and the work must be evenly distributed. Example:
int age = 0; //By Bugs Bunny: STORES AN AGE
/**
* By Daffy Duck
* main() captures a first and last name, an age, and a zip code.
* It multiplies the age and zip code to get a product.
* Output will be made to the console screen and to a
* GUI window.
*/
2.
WARNING: One group member CANNOT and MUST NOT carry the load for these
assignments. All group members have to be actively engaged in developing and writing code.
3.
Word Document: Copy your .java code into a Word document and save it with the same name
as your program. Upload the document to Blackboard under this PA.
4.
Zipping Folders: Your Java files “must be” in a folder.
© 2021 Linda Shepherd 8
a. Create a folder named for the program (excluding the file extension).
b. Put your
.java, .class, .java~ files in the folder.
c. To zip the folder, point to it then right click and
i.
Filzip if you have it OR
ii. Click Send To then click Compressed (zipped) Folder
e. Upload your zipped folder to Blackboard under this PA.
5.
Uploading to Blackboard: Make sure your browser is properly configured for Blackboard
(see syllabus).
a. Your submissions are to be uploaded to Blackboard through Assignments only.
b.
Upload your files no later than the due date by 11:55 pm; otherwise, you don’t have time to
recover from any problems and your assignment may not be accepted by Blackboard.
c. Check to make sure your submission is uploaded. Please
do not ask your instructor to
check whether your assignment has been uploaded. You can do this yourself. Or upload
during a tutoring session when someone can help you.
d. If you submit your assignment before the due date, want to make changes or upload additional
files, you can
re-upload your files.
6.
NO ASSIGNMENTS WILL BE ACCEPTED LATE OR VIA E-MAIL. DO NOT UPLOAD
PROGRAMS THAT DON’T COMPILE OR DON’T PRODUCE CORRECT OUTPUT.
SAMPLE OUTPUT: It is always good to test your code using sample data to see if your program
meets the output specifications.
Run your program using the data in the following sample
output. Copy and paste the output into a comment box at the end of your PA2.java file.
The
comment box needs to be outside of the close brace for the class.
Change your font in DrJava to
Monospaced or Courier New if your output is out of alignment. Worth 5 points! Your output
will not print in bold.
****OUTPUT WHEN CONTINUE IS ‘N’**** NOT PART OF OUTPUT
WEEKLY HOURLY PAYROLL SYSTEM
Continue? Enter ‘Y’ or ‘N’: n
Exiting Weekly Hourly Payroll System.
****OUTPUT WHEN CONTINUE IS ‘Y’ WITH VALID AND INVALID RANGE ENTRIES****
***NOTE: Several lines are Word wrapped. They’re coded as single lines.***
WEEKLY HOURLY PAYROLL SYSTEM
Continue? Enter ‘Y’ or ‘N’: y
Enter the employee’s first name press enter then the last name press enter:
Davey
Jones
Enter the number of hours worked for Davey: 4
© 2021 Linda Shepherd 9
Hours worked cannot be LESS than 5. Please re-enter: 5
Enter the employee’s hourly pay rate: 7
Hourly pay cannot be LESS than $7.25. Please re-enter: 30
Hourly pay cannot EXCEED $26.00. Please re-enter: 17
Enter the employee’s 401K contribution as a percentage of salary (not to exceed
10%): 12
Contribution cannot EXCEED 10%. Please re-enter:
3
Enter ‘Y’ to add another employee or ‘N’ to exit: y
Enter the employee’s first name press enter then the last name press enter: Fred
Flintstone
Enter the number of hours worked for Fred: 40
Enter the employee’s hourly pay rate: 20
Enter the employee’s 401K contribution as a percentage of salary (not to exceed
10%): 0
Enter ‘Y’ to add another employee or ‘N’ to exit:
n
WEEKLY HOURLY EMPLOYEE PAYROLL

Date:
Time:
03/13/21
04:57:32 AM

Davey Jones $
GROSS PAY
$
401K
85.00 2.55
Fred Flintstone 800.00 0.00
TOTALS $ 885.00 $ 2.55
****OUTPUT WITH INVALID TYPES**** NOT PART OF OUTPUT
WEEKLY HOURLY PAYROLL SYSTEM
Continue? Enter ‘Y’ or ‘N’: y
Enter the employee’s first name press enter then the last name press enter:
kdlagj*((@)5___++
System.exit(0);
Enter valid first name: Davey
Enter valid last name: _!394sdkgja;l*_+#$
Enter valid last name: Jones
© 2021 Linda Shepherd 10
Enter the number of hours worked for Davey: 4O
Invalid type! Re-enter the number of hours worked for Davey: 4
Hours worked cannot be LESS than 5. Please re-enter: %
Invalid type! Re-enter the number of hours worked not less than 5 or greater
than 40: 5
Enter the employee’s hourly pay rate: &
Invalid type! Re-enter the hourly pay rate:
7
Hourly pay cannot be LESS than $7.25. Please re-enter: !0
Invalid type! Re-enter an hourly pay rate that is not less than $7.25 or greater
than $15.00: 10
Enter the employee’s 401K contribution as a percentage of salary (not to exceed
10%): 1@
Invalid type! Re-enter the 401K contribution not to exceed 10% of salary: 12
Contribution cannot EXCEED 10%. Please re-enter:
#
Invalid type! Re-enter a contribution NOT exceeding 10% of salary: 3
Enter ‘Y’ to add another employee or ‘N’ to exit:
n
WEEKLY HOURLY EMPLOYEE PAYROLL

Date:
Time:
03/13/21
05:20:49 AM

Davey Jones $
GROSS PAY
$
401K
50.00 1.50
TOTALS $ 50.00 $ 1.50
© 2021 Linda Shepherd 11

Tags: , , , , , , , , , ,