Java Paycheck

Java Paycheck


Welcome to the wonderful world
of programming using Java. This presentation introduces your first
programming project in the class, “The PaycheckProgram”. Watch this video carefully. It not only covers the first project,
but many of the small details in the Java programming language. You may want to watch this video
more than one time, especially if this is your first class in programming. You not only need to complete the project
but you also need to understand the information presented because
it forms the basis for all of the rest of the class. As the course progresses, you will be
required to start a project from scratch and create the entire project yourself. I will cover the paycheck lab assignment
by discussing the project definition, how to develop an algorithm,
entering and running the program and completing and
submitting the lab report. With Java, it really does not matter whether
you are using a Mac, a PC or Linux. Before you even start, I suggest that you
create several folders on your computer or storage device
* Main folder for the Java Programming Class * One subfolder to keep
copies of all your lab reports * Separate subfolder for each lab project
* You may want another folder to keep copies of the class presentations. The college removes the class Canvas
web pages after the end of the class. Here is the project definition. Create a program using Java
that does the following: Read the number of hours worked and pay rate
Compute the pay including overtime at time-and-a-half Change the withholding tax rate
from 17% to 21%. Compute the taxes. Display gross pay, taxes and net pay
Even if you are really smart, REALLY really smart and know how
to pay no income tax at all, the tax withheld for this project must be 21%. The first thing to do when given
a programming task is to figure out what the output should look like. Draw it out on paper. As the project progresses, don’t worry if
the actual program output doesn’t look exactly like your original plan. Plans can change. Pseudocode is fake code.
It’s code that’s not really in any language. It’s more like English. A lot of times, pseudocode is given to the programmer to say,
“Here is how I want the program to work,” and it’s up to the programmer to write the code in Java, C, C++, or
what ever language is being used. In this hierarchy, input, process, output
chart, also called a HIPO chart we want to define the inputs
(in this case it’s the hours and pay rate) and the outputs (gross pay, taxes and net pay)
First identify the inputs and outputs. Once we know these, we then need
to decide how to get from input all the way to the output. We’re going to read the hours and pay rate
from the keyboard, determine the number of regular hours, determine the
number of overtime hours, compute regular pay, compute the
overtime pay, and then the compute gross pay. Compute the taxes, and finally the net pay
(that is what is left over after everything has been taken out. Finally, display the gross pay,
taxes and net pay. I am going to use flowcharts to continuously breakdown the program into smaller manageable parts. Flowcharts are used to represent
different steps of a program. The most commonly used symbols are
those in the left column. A circle or rounded rectangle is used to
indicate the beginning or end. The parallelogram is used
for input or output. The rectangle represents
some type of process and the diamond is used to represent a decision. Other symbols can be used to represent
subroutines, functions, disks, databases, etc. Although flowcharts are not used as often
as they once were, they still help give a visual representation of
how a program is organized. The concept of structured programming states
that all programs can be created with only three constructs:
Sequence, Selection and Repetition or a combination of any of them. Sequence is the easiest
to understand. Program steps are executed sequentially,
one after another. Many times, the order in which
the steps are executed is very important. Other times it does not matter. For example, if I were
baking a cake, it may not matter very much whether I put the egg in the bowl
and then the flour or I reversed the two before mixing them with water. However, it would make a
lot of difference if I put the cake batter in a 9 x 13 baking dish, placed
it in an oven for 1 hour and 30 minutes, took it out and then turned the oven on to 350 degrees. Yuck. This simplified flowchart shows the sequence
of steps that are required for the program. There are no loops,
so the repetition structure is not needed. In the another slide, I am going to expand the
“Compute Paycheck” box to show a lot more detail. Here are two examples showing hours worked
with an example of $20.00 per hour. The first example
shows 37 hours which is less than 40 hours, therefore there is no overtime. All 37 hours should be paid at $20 per hour. The second example shows 45 hours. Therefore, the first 40 hours should be
paid at the standard rate of $20 per hour but the overtime of 5 hours should
be paid at time and half. That makes the overtime
hours paid at $30 per hour. The easiest way for the program to compute
the pay is to separate the hours into regular hours and overtime hours
and then compute the pay separately for each. I have expanded the flowchart to show more
detail in computing the paycheck. The center column uses
a diamond to represent a decision. The program needs to look at all the
hours worked to see if they are less or equal to 40. In this case, the regularHours are set to
the number of hours worked and the overtimeHours are set to zero. However, if the test condition of hours are
less or equal to 40 is NOT true, there is some overtime. Therefore, we set the regular hours to 40
and set the overtime hours to anything over 40. If hours is 43, then the person worked
40 hours at regular pay and had 3 hours of overtime. overtimeHours=hours=40
which is 3. We can then proceed to compute the
grossPay, payroll taxes and netPay and then display them. We need to remember that a payroll tax
is collected on the money you make and sales tax is collected
on the money you spend. In this project, we are
working with the payroll tax. PROGRAM ORGANIZATION: Most of the programs
you write will be organized like this. Use comments to place
a title block at the top of each file. The title block identifies what this part
of the program does as well as the date, version and
programmer’s name. It can be very frustrating
later on when there are many files that make up one program and you
look at a file that contains code and have no idea what the code does. You then need to re-analyze or ‘reverse-engineer’
the code instead of just looking at a short description in the title block. Many companies want the programmers
to place their name in the file in case anybody has
questions about the file. The date and version number
also help identify the latest version of the program. The first part of the real code contains the
import statement and any global variables and constants. Next is the main body of the program with its four
sub-sections: List of variables -Input -Process -Output. Next, convert the algorithm into code. The real work gets done during the INPUT,
PROCESSING and OUTPUT sections. Most of what is in the first and last
sections is really just house-keeping to get the program started and shut down. If you are using a Mac or PC,
you should have already installed Eclipse or NetBeans on your computer. If you don’t have Eclipse or NetBeans yet, you can use Jdoodle for this project
with just a web browser. You will need to install Java and preferably a
full-featured IDE on a computer for future projects. Using Eclipse
You need to specify a workspace (folder) the first time you start Eclipse to identify
where your projects will be stored. The Eclipse window is divided into several
sections which contains several ‘views’. One can be the text
editor, another one, a list of your projects, the console output etc. These are all combined into the
‘Java Perspective’. As you become more experienced, you may want
to use the ‘Debug Perspective.” To use the Eclipse IDE Create a Java project New ->File ->Java
Project Your code should be in a directory named src
(source) Create a new Java class. Right-click the Java project name in Project
Explorer ->New ->Class. Don’t add .java to the end of the class name. Syntax errors will be underlined with a jagged red line The light bulb gives suggested fixes to errors Control-space will help complete your typing Click Run to start your program Be sure to click the mouse in the Console
Window before typing entries for things like hours or pay rate. Using NetBeans is very similar to using Eclipse. Even the layout of its window is very similar
to the Eclipse window. To use the NetBeans IDE Create a Java project
New Project ->Java Application Enter a name for your project
instead of the default name Create a new Java class. Right-click the Java project name in Project
Explorer ->New ->Class. Don’t add .java to the end of the class name. Syntax errors will be underlined with a jagged red line The light bulb gives suggested fixes to errors Control-space will help complete your typing Click Run to start your program Be sure to click the mouse in the Console
Window before typing entries for things like hours or pay rate. All you need to use JDoodle is access to the
Internet and a web browser. It is a free online compiler that
works for many languages, including Java. You can use JDoodle for the first few lab
assignments, but you need to install Java on a computer system such as a Mac, PC
or Linux for many of the projects in the class. You will not be able to use JDoodle for
the Graphic User Interface (GUI) projects. JDoodle may also not work for multi-file projects or programs that access files on the disk. To use JDoodle Start Jdoodle at
https://www.jdoodle.com/online-java-compiler Enter your program. NOTE: you do not need the line at the top
of the program that identifies a Java package. Remove it !
package paycheck; Be sure to save your work on a regular basis. To copy your program into the clipboard
on a PC, use Ctrl+A, Ctrl+C . On a Mac, use command+A,
command+C. Set the Interactive mode : to ON,
and click Execute Using Java in a console mode is really cool
if your are a real computer geek or a computer nerd. If you are not sure of how to locate the JDK
on your computer, or even what it is, or how to modify the operating system
PATH environment variable, I strongly suggest that you do not
use the command mode for now. To use the command line interface
Make sure that the JDK is installed and the PATH environment variable
includes the folder of the JDK. Use a text editor such as Notepad or TextEdit
to enter your program and save it in a file whose name is the same as the
‘class’ name of the Java program. Example: Paycheck.java Use javac to compile the program. If there are any errors, fix them with
the editor and recompile the program. javac Paycheck.java Use the Java JRE to run the program:
java Paycheck
(note do not include the .java extension) And here is a sample run of the program
starting with the Java compiler,
javac Paycheck.java Start the compiler with the javac command,
followed by a list of files to be compiled, complete with their .java file extension. Once the compiler shows no errors, use the
java command to actually run the program. NOTE: don’t include the .java file extension here. The characters in blue represent what the user
needs to type in response to the prompt messages. Congratulate yourself when you have a
successful run of the program. ENTER THE PROGRAM Get a copy of the code
from the class website and print it. The code is provided as an image file. You need to type the program yourself
to complete the lab project. You will learn far more by entering in the
program yourself than if you have someone else enter it in or use a scanner
to convert the image of the program code into actual Java code. Be very careful when typing Java programs. Except for comments, Java is case sensitive,
which means that upper-case letters (A-Z) are treated completely differently
from lower-case letters (a-z). Do not put spaces in the middle
of variable names. Example: overtimePay has no spaces. Watch the placement of the semicolon ; character. It does not appear at the end of every line. Use n to move the cursor to the next line
when outputting text. The n uses a backslash not a
forward slash /. Pay close attention to the parentheses ( ), the
curly braces { } and the angle bracketsThe title block at the top of the program
file uses the BLOCK style comments, starting with a /* and ending with a */ The comment block identifies what is in the
file as well as the inputs and outputs. Some companies want
the programmer’s name in the title block, others don’t. I want your name in the title block and I will look for it when I grade your projects. Although not shown here, another
couple of things that are actually placed in the title block are the version
number and the date. This can help determine the latest version
of the code when there are multiple files that are not the same. The line package paycheck; is not needed when
using either the JDoodle online compiler or the command line compiler. The import java.util.Scanner; brings in the
code for the program to read from the keyboard if using the Scanner object. Java is an object oriented programming language. Objects are defined with the class statement. Each Java class must be it in its own file and the name of the file needs to be the same
as the class, with the .java extension. The Paycheck program is defined with
public class Paycheck
code and must be in a file named Paycheck.java The curly-braces { } are used
to identify the start and end of a block of code or data. Many other programming languages use the words
Begin and End instead of curly-braces. Each open curly-brace {
needs to have a matching close curly-brace. The close curly-brace for the
public class Paycheck
is at the end of the file. This means that everything else in the file
belongs to public class Paycheck Java does not have the exact same thing as
a constant definition like C++ or Visual Basic, but the same thing can be accomplished using public static final
to define the equivalent of a constant. For normal identifiers like variables,
subroutines, methods and objects it is traditional to use the ‘camel case’ method for the names with the first word in each name capitalized. Class definitions typically have the first
letter capitalized but variables, subroutines and methods leave the
first letter in lower case. To make constants easier to
identify, they are usually done with all CAPITAL_LETTERS and use the underscore character _ to separate
words instead of spaces. Constants are used later in the program so
that the values will have a name instead of just a number. For example, when TAX_RATE is used in the expression
taxes=grossPay * TAX_RATE; it is much better than
taxes=grossPay * 0.17; because someone looking
at the program may wonder where the 0.17 came from and why it is there. If 0.17 were used, it would be referred
to as a ‘Magic Number’ Who knows how it got there? Another reason to define TAX_RATE
at the top of the program is that it may end up being used
more than once in a larger program. Changing it where it is defined makes
sure that it gets changed everywhere. A comment is provided for both OVERTIME_RATE
and TAX_RATE so that the next person reading the program will know what the numbers actually mean. The program itself is enclosed within the
curly braces { } started by the line public static void main
The character that follows the closing parenthesis ( is an open curly-brace. { There needs to be a close
curly-brace near the end of file. Constants and variables must be declared
before they can be used. I placed the constants and variables
at the top of the program and even declared several variables on the same line
with a comma between each variable name. Depending on where you work, some companies
permit closely related variables to be defined on the same line,
while others require that each variable be defined on its own line. A comment should follow
any variable definition that is not perfectly clear from its name. When writing a console application, it is
necessary to output a prompt to the screen to request data from the program’s user. If you don’t have a prompt message, all the
user will see is a flashing cursor and probably think that the program crashed. You need to think of yourself as two different
persons when writing a program that has a user interface. As the program developer, you need to have
the program display a ‘prompt’ message asking the user to input a number for hours. As the user, when you see the ‘prompt’
message, you will know it is your turn to type the number of hours. Don’t have the program display a 40 and then
just sit there waiting for something to be typed. When writing a prompt message, I like to end
the prompt with a colon : and a space to let the users know they need to type something in. The space after the colon is to
make the screen look nice so that the user’s typing won’t be right next to the prompt. It is necessary to create a Scanner object
in order to use the Scanner to input data from the keyboard. You can give the Scanner object any name you
wish, but make it descriptive. In the past, I have used
Scanner input and Scanner keyboard. What ever name you use as the definition,
this is the name that you need to use later in the program when
reading from the keyboard. David Eck, the author of the textbook, has
provided the TextIO user interface that makes the program much more user friendly. When including TextIO in your program, it
checks to see if the characters typed as user input are appropriate for the
data type in the program. It has the program retry the user’s input
if the information typed does not look good. On the other hand, programs using Scanner
for keyboard input will crash and die if the characters typed are not appropriate
for the expected data type. For the Paycheck program, I want you to use
Scanner, especially for the next lab. You will be identifying different types of errors
that can occur with a computer program. The if…else statements are used to identify
the number of regular hours worked and the number of overtime hours. The if statement has a logical expression
enclosed within the parentheses ( ) that is testing to see if the hours
are less than or equal to 40. The logical expression must evaluate to either a TRUE or a FALSE. When the expression evaluates TRUE, that means
there is no overtime and the block of code attached to the if statement is executed. When the expression evaluates FALSE,
the block of code attached to the if statement is skipped over
and the code attached to the else statement is executed. The curly-braces { } identify the begin and
end of each block of code. If there is no overtime, just set the regular
hours to the number of hours worked, and the overtime to zero. If there is overtime, the first 40 hours get
paid at the regular rate, but anything over 40 gets paid at time and a half. Set the regular hours to 40 and the overtime
hours to anything over 40. The computations for the regPay and overtimePay
are fairly straight forward. regPay=regHours * payRate
overtimePay=overtimeHours * payRate * OVERTIME_RATE; The overtime rate is from the constant
1.5 at the top of the program. The grossPay (pay before taxes or any other
deductions) is the sum of regPay and overtimePay. Taxes are based on the TAX_RATE defined at
the top of the program. netPay is anything that is left over from
the grossPay after the taxes have been deducted. The first part of the format string in printf
is just text that will be displayed “Your pay is $” followed by
the format specifier “%.2f” and then the “n” which moves the cursor down to the next line. The %.2f tells printf to replace the %.2f
with the data that is in the next printf parameter. The f identifies that the data is expected to be of type double, and the .2 says
display two digits past the decimal. If pay were computed at 803 dollars,
then the output on the screen would show Your pay is $803.00
and the n moves the cursor down one line. Remember how printf displays
two digits past the decimal. You will need this in future lab assignments. We opened the Scanner object near the top
of the program as one of our resources and we should close
it when we are done with it. We will need to do something similar in later
programs when we open a disk file. The first close curly-brace matches the open
curly-brace for main and the second curly-brace matches the open curly-brace
for the Paycheck class definition. Many of the IDE systems will let you
double-click on an open or close parenthesis or curly-brace. It will turn yellow and so will its mate. This helps make it
easier to find the matching pairs. What is this program missing? This may seem like a big task for you first
program, but a lot of code is missing to make a robust program that is more
user friendly and able to detect and handle bad inputs. Here are some of the things that are
missing that we will need to learn how to process in later projects:
Check for negative hours, pay rate or taxes Check for hours>168.
That is the maximum number of hours in one week Check for excessive pay rate or tax rate
Check for illegal inputs Since this program can process a paycheck
that does not have overtime (40 hours or less), and also a paycheck
that has overtime (more than 40 hours), it is important to test both conditions. It also important to test the program at 40
hours to make sure that the program did not check for only greater than and less than and skip testing for 40. Project Documentation and Lab Report. A multi-page
text document for the lab report is provided. Most of the lab report for this
first project is already filled in. You need to complete the rest of the lab report. You need to complete the
TEST VALUES & RESULTS table. I have already provided some sample test values. If you use any values other than 39, 40 and
41 hours or a pay rate other than 21/hour, you need to update this table. Use a calculator to compute the EXPECTED RESULTS
and then put in what was computed by your program
in the ACTUAL RESULTS column. Don’t just copy the program output into the
EXPECTED RESULTS column. Otherwise, how are you going to know if the
program is actually producing the correct results? When you submit your lab assignment, I may
be comparing the data in this table to the three screenshots that are also part of the lab report. In the DISCUSSION section of the lab report,
you need to write what you did to develop the program, any problems you had
and how you overcame those problems. I don’t expect a lot of writing, but I want
you to describe what you did for the project. I realize that not everyone taking this class
may have perfect English. Try your best. I may mark up your report
for English composition problems to help you in your writing, but I will not
take off any points for your English. This is a programming class, not English 1A. The lab has instructions on how to copy just
a small portion of the screen. If you place a screenshot of
the entire screen, I may not be able to read the output of your program. If this happens, I may ask that
you resubmit your report. Congratulations on completing
your first Java assignment. Refer back to this lab assignment when working
on future labs for instructions on creating new Java programs. And a special thanks to David Eck
for sharing his textbook Introduction to Programming Using Java Bye, bye – and enjoy this lab. Have fun! I hope you learn a lot.

1 thought on “Java Paycheck”

Leave a Reply

Your email address will not be published. Required fields are marked *