Designing
Programs
Out-Lab
4, Chapter 4
An overview of Object Oriented programming
• OOP has two types of classes
– Encapsulate classes: Those that encapsulate data
and the methods that act on it together
• The
function is to create a new type
– Application classes: Those with a main method.
•
The function is to execute a program
• For the first three chapters, we worked mostly with
creating a new type with a class
• For the next few chapters we will be working on tools
to help you write methods in either type of class
Types in Java
• Primitive types
–
int, double, char, boolean
• Types created by classes
–
The Java library
has hundreds of prewritten classes
–
Programmers can
create their own types of the library does not contain exactly what they want
• The Java API lists all the predefined classes
and their methods
• Examples of prewritten classes
–
String object,
Scanner objects
–
Objects of
classes can call methods to work on objects of their type
Application classes
• Contain a main method to begin execution of the
program
• May contain other methods.
• These methods should be marked static
– This is because they are not encapsulated in a class
to manipulate data.
Out-Lab 4
Figuring Gross Pay
• This lab is to write a Java program that will
determine the gross pay for employees.
• We’ll use pseudocode with top-down design to figure
out how to write this program.
• Top-down design says we formulate the bigger problems
first, then worry about the smaller problems later
Top-down design
• First, of course, we have to understand our problem
• Look at the instructions: (it is often good to start
with I/O)
–
The program
should prompt the user for the number of hours worked and the pay rate for an
indefinite number of employees
–
After you have
the data for one employee, you should call the method that calculates
the gross pay to get the gross pay for that employee.
–
The gross pay for
each employee should be output before prompting for the data for the next
employee
–
The company you
are writing this program for pays straight time for the first 40 hours worked
by each employee and time and a half for all hours worked in excess of 40 hours
Compound assignment operators
• One of the most common operations the computer does is
to add a value to a variable, then store it back in the variable
var = var + num;
• This is so common there is a shortcut for typing it
var += num;
• There are other compound assignment operators
var - = num;
var *= num;
var /=num;
var %=num
Increment & decrement Operators
• Increment operators add one to a variable
– They can be either prefix increment or postfix
increment
– Prefix adds one, then completes the statement
– Postfix completes the statement, then adds one
– Example num
= 8; var = 4;
num = ++var +
10;
what is the value of num?
num = var++ + 10; what is the value of num?
• Pre and post fix decrement operators act the same way
The Conditional Operator
• There is a shortcut for if/else statements
• It is optional to use, but you need to know how to
read it in code
•
Standard if/else
statement
if (x > size)
y = 10;
else y = 15;
•
The shortcut that
means exactly the same
(x>size) ?: y=10 : y=15;
• (condition) ?: do_if_true: do_if_false
• We want to program a calculator (ours will do only
addition and subtraction)
• The user should be able to enter an expression like 576 + 943, or 456 -345, and our
program should give the answer.
• The user should be prompted to ask if they want to do
another problem.
• Do first in pseudocode
• Then implement in the simplest way possible, then
compile that much