 |
Defining classes, Part 1
|
Reading: Chapter 5.1-5.4.7
Numeric Processing
-
Numbers are an important part of computing
-
Need to calculate pay for employees
-
Calculate student grades
-
Calculate the trajectory of an incoming missile
-
Java offers a built-in primitive data type that represents whole numbers
(integers)
-
the name of the data type is called int
- -2,147,483,648 to 2,147,483,647
-
various operations can be performed
-
+ for addition (+ is an operator)
-
- for subtraction (- is an operator)
-
* for multiplication (* is an operator)
-
/ for integer division (/ is an operator)
-
% for remainder (% is an operator)
If we have variables that contain integer values, we can construct arithmetic
expressions:
x+y
x+(y/z)
mph/distance
rate*hours
We can also print out ints, using
the print and println
methods of PrintStream
System.out.println (rate*hours);
Primitive data types are not classes
-
we cannot send them messages
-
they have no instance variables (although, they can be instance variables...)
To declare a variable that can contain an int value, we declare it using
the same template as we do for other variables:
int hoursInDay = 24;
-
notice, no new operator
-
hoursInDay is an identifier
-
now we know the meaning general template for declaring
type identifier;
-
type can be either a class or a primitive data type
Why use integers?
-
Unlike, for example, Strings, ints we can perform numerical computations
-
a facility seriously lacking in our vocabulary up until now
Using ints in a class/program.
Statement of problem
-
Suppose we want to write a program to calculate the pay for each employee
in a small shop, and keep track of how much money being paid to all employees
in total. Each employee has an hourly rate (represented in whole dollars)
and a number of hours that they have worked in a week (to the nearest hour).
Sample scenario
-
The manager of the store, at then end of the week, sets the number of hours
each employee has worked, and gets the following report printed out (on
the screen):
Lesley Bloggs - Hours this week: 7, Pay rate: $12/hr, Pay due: $84
At the end of the process of entering in for each employee, a summary of
the money paid out is given:
Total amount paid this week: $705 to 3 employees
Finding the primary objects
-
What are the nouns and phrases in the problem statement?
-
pay, employee, shop, hourly rate, hours worked
-
The most important of these are employees (they have an hourly rate and
the number of hours worked) and shop (which needs to keep track of how
much money is being spent on employees
-
pay can be (and is required to be) calculated from hourly rate and number
of hours worked.
-
We have two classes: Employee and Shop
-
We can design the two classes
-
do Employee first, then Shop.
Determining the behavior of the class Employee
-
pay depends on the number of hours worked and the hourly rate
-
Employee should have methods to return this information, and the employee's
name
-
Also need a constructor
-
Employee (constructor)
-
getPayRate
-
getHoursWorked
-
getName
-
Notice we are only considering Employee in the context of what the program
needs.
Defining the interface for class Employee
-
Remember, the interface consists of the prototypes that specify how objects
of that class are to be used.
-
How would we construct an Employee?
Employee LBloggs = new Employee ("Lesley Bloggs", 12, 37); // $12/hr at 37hrs
Querying the Employee for the name, pay rate and hours worked is straightforward
LBloggs.getPayRate () LBloggs.getHoursWorked() LBloggs.getName ()
(Note that all of these return a value)
Because calculating the pay rate requires arithmetic on the hours worked
and pay rate, the associated methods should return int - the name should
be stored and returned as a String.
Now, we can write the class skeleton
class Employee {
// Methods
public Employee (String name, int payRate, int hoursWorked) {...}
public int getPayRate () {...}
public int getHoursWorked () {...}
public String getName () {...}
// Instance variables
...
}
The information about an Employee must be retained, so that the shop can
access it. Thus, we need instance variables
private int payRate;
private int hoursWorked;
private String name;
Implementing methods
-
For this simple class, implementing methods is quite straightforward (like
the House class, it is mainly used to store data)
class Employee {
// Methods
public Employee (String name, int payRate, int hoursWorked) {
this.name = name;
this.payRate = payRate;
this.hoursWorked = hoursWorked;
}
public int getPayRate () {
return payRate;
}
public int getHoursWorked () {return hoursWorked;}
public String getName () {return name;}
// Instance variables
private int payRate;
private int hoursWorked;
private String name;
}
Last modified: 2/15/99