CS 151: Object-Oriented Design October 29 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu /~mak
Jan 18, 2018
CS 151: Object-Oriented Design October 29 Class Meeting
Department of Computer ScienceSan Jose State University
Fall 2013Instructor: Ron Mak
www.cs.sjsu.edu/~mak
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
2
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
3
Midterm Results
Median 75.0Mean 76.7Standard deviation 12.1
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
4
Unofficial Field Trip Computer History Museum in Mt. View
http://www.computerhistory.org/
Saturday, December 7, 11:30 – closing time Special free admission. Do a self-guided tour of the new Revolution exhibit. See a life-size working model of Charles Babbage’s
Difference Engine in operation, a hand-cranked mechanical computer designed in the early 1800s.
Experience a fully restored IBM 1401 mainframe computer from the early 1960s in operation. General info: http://en.wikipedia.org/wiki/IBM_1401 My summer seminar: http://www.cs.sjsu.edu/~mak/1401/ Restoration:
http://ed-thelen.org/1401Project/1401RestorationPage.html
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
5
Unofficial Field Trip The new Revolution exhibit is now open!
Walk through a timeline of the First 2000 Years of Computing History.
Historic computer systems, data processing equipment, and other artifacts.
Small theater presentations.
Atanasoff-Berry Computer
HollerithCensus
Machine
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
6
Unofficial Field Trip Babbage Difference Engine,
fully operational Hand-cranked mechanical
computer. Computed polynomial
functions. Designed by Charles Babbage
in the early to mid 1800s. Arguably the world’s first
computer scientist, lived 1791-1871.
He wasn’t able to build it because he lost his funding.
Live demo at 1:30 His plans survived and this
working model was built. Includes a working printer!http://www.computerhistory.org/babbage/
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
7
Unofficial Field Trip IBM 1401 computer, fully restored and operational
A small transistor-based mainframe computer. Extremely popular with small businesses in the late 1950s
through the mid 1960s Maximum of 16K bytes of memory. 800 card/minute card reader (wire brushes). 600 line/minute line printer (impact). 6 magnetic tape drives, no disk drives.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
8
Extra Credit! There will be extra credit if you participate in the visit
to the Computer History Museum. Complete a Canvas quiz whose answers are to be found
among the museum exhibits and presentations. Up to n points added to your midterm score.
n = the number of quiz questions_
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
9
Team Exercise: UML Class Diagram A company has a manufacturing division and a research division. The company has a president and two vice presidents.
One VP runs manufacturing and the other runs research. Each division has employees other than its VP,
and it might also have an administrative assistant. All manufacturing workers are union members, but none of the
researchers. All of the researchers have college degrees, as do some of the manufacturing employees.
Each employee is paid either by the hour, biweekly, or monthly. Manufacturing workers can get overtime pay, but not researchers. All employees except the president and vice presidents can get bonuses.
The company manufactures different products, three of which are widgets, gadgets, and gizmos. A gadget can be sold separately, or several can be bundled with a widget. A gizmo can only be sold as part of a gadget.
The researchers are working on projects to improve the company’s existing products or to design new products.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
10
An Exercise Solution
Vice President
AdministrativeAssistant
0..1
Research
Manufacturing
Division
Company
CollegeDegree
President
«interface»Overtime
Pay
«interface»Union
Member
Product *
0..1
Gizmo
Gadget
Widget
«interface»Bonus
Employee
<<enumeration>>Pay
HourlyBiweeklyMonthly
*
*
Worker*
Researcher*Project
New product
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
11
Inheritance By now, inheritance should be a familiar concept.
From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
12
Manager extends Employeepublic class Employee{ public Employee(String aName) { name = aName; } public void setSalary(double aSalary) { salary = aSalary; } public String getName() { return name; } public double getSalary() { return salary; }
private String name; private double salary;}
public class Manager extends Employee{ public Manager(String aName) { ... } public void setBonus(double aBonus) { bonus = aBonus; } // new method public double getSalary() { ... } // overrides Employee method
private double bonus; // new field}
What are the differences betweenManager and Employee?
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
13
Superclasses and Subclasses Employee is the superclass
Manager is the subclass
A manager “is a” employee.
A manager is a specialized type of employee.
The set of Manager objects is a subset of the set of Employee objects._
From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
14
Class Hierarchies AKA inheritance hierarchies In the real world, hierarchies express
general/specific relationships among concepts. The most general concept is at the root of a tree. More specific concepts are children. The most specific concepts are leafs.
Object-oriented programming uses class hierarchies. The most general superclass is at the root of a tree. More specific subclasses are children. The most specific subclasses are leafs.
Class hierarchies can be complex. But they shouldn’t be.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
15
Class Hierarchies
From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
16
Class Hierarchies
From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
17
The Liskov Substitution Principle Named after Barbara Liskov
MIT computer science professor Pioneer in object-oriented programming
In your Java code, you should be able to substitute a superclass object by a subclass object:
Employee e = new Employee("John Doe");System.out.println("salary = " + e.getSalary());
Superclass
Employee e = new Manager("Mary Jane");System.out.println("salary = " + e.getSalary());
Subclass
SJSU Dept. of Computer ScienceFall 2013: October 29
CS 151: Object-Oriented Design© R. Mak
18
The Liskov Substitution Principle
The type of variable e is EmployeeThe type of the object is Manager. How does Java know which getSalary() method
to invoke at run time? The JVM uses the type of the object,
not the type of the variable. Polymorphism
How is the Liskov Substitution Principle related to the principle of Programming to the Interface? The type of variable e is the superclass Employee instead of
the more specific subclass type Manager.
Employee e = new Manager("Mary Jane");System.out.println("salary = " + e.getSalary());