Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | estella-harmon |
View: | 215 times |
Download: | 1 times |
Object-Oriented Programming
Developing an Application
Cooperating Objects and Classes
• Object-oriented software systems consist of interacting objects
• Objects do things by sending messages to each other
• The data and behavior of an object are specified by its class
• A class contains variables for the data and methods for the behavior
Object-Oriented Analysis
• After the user requirements are established, we choose the classes that realize the desired behavior
• Classes may be built-in (String, JButton, etc.) or programmer-defined (Student, Employee, etc.)
Object-Oriented Design
• For each class, we specify the internal data structures and methods
• Within the methods, we show how objects interact to realize the desired behavior (using pseudocode)
Object-Oriented Implementation
• We code the classes, using either a bottom-up, top-down, or mixed approach
• Each class is thoroughly tested individually and then the components are integrated and tested further
• Development usually proceeds incrementally
Example: An Employee Management System
The management wants a system that allows a user to enter each employee’s name, pay rate, and hours worked for each of five days.
Users should be able to browse a list of employees for a given employee’s information.
Users should be able to obtain an employee’s total pay for the week, using time and a half for overtime.
Analysis - Proposed User Interface
Analysis – Develop Prototypes
• A prototype is a trimmed-down or skeletal version of the full system
• A prototype gives the users an idea of what to expect without full functionality
• Might not have all the commands or each one working completely
Analysis – Choose Classes
• We use the model/view pattern
• The model classes manage the data
• The view classes present the data to the user and handle user interaction with the model
The Model Classes
• Employee represents and manages an individual employee’s data
• EmployeeModel represents and manages a list of employees
The View Classes
• EmployeeManagerView displays a list of employee names and the currently selected employee’s information; provides menus for commands
• EmployeeDialog allows the user to edit an employee’s information
Class Diagram
Employee EmployeeDialog
EmployeeModel EmployeeManagerView
*
1
0..1
1
0..1
1 = contains just one0..1 = contains zero or one* = contains zero or more
Design: The Attributes of the Employee Class
• Development is incremental, so work bottom-up, starting with Employee
• What information does an employee contain?– a name– the hours worked for each of five days
Constraints on the Attributes
• There are exactly five days
• Any hours over 8 * the number of days count for overtime
• Overtime pay is 1.5 * the regular pay rate
• Hours are whole numbers
• Pay rate is a real number
Design: The Behavior of the Employee Class
• get or set the name
• get or set the pay rate
• get or set the hours of each day
• get the total pay
• get the total hours worked
Other Useful Behavior
• obtain a string representation of an employee
• compare two employees for equality
• compare two employees for less than or greater than
• clone (copy) an employee
• transfer an employee to and from a file
Constructors
• A default constructor will initialize an employee’s data to reasonable values
• A second constructor allows the client to supply the data
Design: More Details• Specify the interface (a set of public method
signatures)
• Choose the data structures to represent the attributes
• Choose the algorithms for the methods
• Write a tester program to illustrate their use
The Employee Interfacepublic Employee()
public Employee(String name, double payRate, int[] hoursWorked)
public String getName()
public void setName(String newName)
public double getPayRate()
public void setPayRate(double newRate)
public int getHours(int whichDay)
public void setHours(int whichDay, int hours)
The Employee Interfacepublic int getTotalHours()
public double computePay()
public String toString()
public boolean equals(Object other)
public int compareTo(Object other)
public Object clone()
The Employee Interfacestatic public final int MAX_DAYS = 5;
static public final int MAX_REGULAR_HOURS = MAX_DAYS * 8;
static public final double OVERTIME_RATE = 1.5;