lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/csc111/fall2005/lectures/lecture21.pdf ·...

20
Lecture 21 CSC 111 - Fall 2005

Upload: others

Post on 22-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Lecture 21

CSC 111 - Fall 2005

Page 2: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Cohesion

Presented in 1:00 class by request:Cohesion: A measure of how well a class represents a single concept

Previously, discussed how a CashRegisterclass should have no concept of nickels, pennies, dimes, and quarters

Meaning of nickels and other coins are distinct from attributes and behaviors of CashRegisterCashRegister doesn’t require any particular value of coin to work, better separated into Coin classCashRegister interacts with abstract idea of Coin

Page 3: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Cohesion

Another example - here’s a description of a vending machine system:

“Users place coins in a vending machine and select a product by pushing a button. If the inserted coins are sufficient to cover the purchase price of the product, the product is dispensed and change is given. Otherwise, the inserted coins are returned to the user.”

What would be appropriate, well-separated (internally cohesive) objects for modeling this system?

Page 4: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Cohesion

“Users place coins in a vending machine and select a product by pushing a button. If the inserted coins are sufficient to cover the purchase price of the product, the product is dispensed and change is given. Otherwise, the inserted coins are returned to the user.”

VendingMachine, Coin, Product, Price(?)

VendingMachines should be customizable to take any type of money VendingMachines should be customizable to sell any type of productProduct should have customizable prices (in customizable currency)

Page 5: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

InheritanceThink about this problem:

Where would we define getArea()?

And what if we want to do this?public void printShapeProperties(Shape shape){

… some work here …System.out.println(shape.getArea());

}

Page 6: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

InheritanceEvery class, except for Shape, we can come upwith a formula to compute an area. So in every class except Shape we could put a getArea() method.

To run this function, public void printShapeProperties(Shape shape){

… some work here …System.out.println(shape.getArea());

}Shape also needs a getArea(), or it won’t compile!However, there’s no meaningful getArea() definition for a Shape

Page 7: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Inheritance

We could:Implement getArea(), returning a zero for a shape objects area, but that doesn’t really signify the same as “undefined”

ORIndicate that a getArea should exist, but not actually implement it – give it an “empty”implementation.

Page 8: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Inheritance

What is an empty implementation? public double getArea();

Semicolon (;) is used in place of{instructions

}Effectively means we are not providing instructions to implement

Page 9: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

InheritanceIf you want to have empty implementations, have to add in the abstract keyword in two places:

Top of java filepublic abstract ClassNameIn front of non-implemented methodspublic abstract returnType

methodName(parameters) ;

Page 10: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Inheritance

A behavior that has an empty implementation is called an abstract behavior (abstract method).

A class that has an abstract behavior is called an abstract class.

Page 11: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

InheritanceEffects of Using Abstract Classes/Methods

First Effect: Can’t create instances of an abstract class

Abstract class is essentially an unfinished piece of code –can’t construct things whose behaviors aren’t fully specified.

Remember construct means to say “new Type(parameters)”such as “new Shape(0,0);”

Page 12: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Compile error if don’t overwrite

Compiles fine once overwritewith real code in place

InheritanceSecond effect:

All subclasses of abstract class must:

EITHER:

Overwrite the abstract method with a useful implementation

OR

Declare themselvesabstract

Page 13: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Inheritance

Shape has abstract getArea()

Triangle, Rectangle overwritewith a real implementation

Square inherits Rectangle’simplementation

All work in a method that only cares about “Shape”

Page 14: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Inheritance

Another (closer-to-home?) example:Attributes

Student: String name, String id, …

CollegeStudent: String collegeAttending, …

WFUStudent: int wakeForestAdvisingGroup, …

Behaviors:Where would computeGPA() go?

Relevant to all students,but implemented at lowest levels

(different schools do different things)

Page 15: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Inheritance

Assume GPAs come back on same scale

computeGPA should beabstract behavior in Student,CollegeStudent, HighSchoolStudent[Really undefined for these]

Should be implemented forWFUStudent, DukeStudent,etc where formula is known

Page 16: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Input/Output

Think back to last week’s lab:TestEmployee – had to enter in by typing an employee’s name, id number, base salaryTestCommissionedEmployee – had to enter in by typing a name, id, base salary, commission rate, weekly sales, …

What if we had 1000 employees and were using our current programs for generating paychecks?

Too tedious to type in by hand

Page 17: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Input/Output

Useful to have a means of writing toand reading from files

Keyboard InputMonitor Output

Files for Inputand Output

Page 18: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Input/Output

Keyboards/Monitors are input/output devices we already know how to use:

To Monitor: System.out.println(“Hello World”);

From Keyboard: Scanner keyboardReader = new Scanner(System.in);String whatTheyTyped = keyboardReader.nextLine();

Would be nice if other input/output devices (such as files) worked similarly.

Page 19: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Input/Output

Two main types of files:Text

Human readable – everything stored as characters12345 (integer) => ‘1’’2’’3’’4’’5’ (5 characters)

Easy to work withBinary

Stored as bytes (groups of 8 bits)12345 (integer) => 0 0 48 57 (4 groups of 8 bits)

12345 = 48 * 256 + 57More compact and efficient than text encoding

We’ll initially focus on text filesMost like screen/keyboard input/output

Page 20: Lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/CSC111/Fall2005/lectures/Lecture21.pdf · Cohesion zPresented in 1:00 class by request: zCohesion: A measure of how well a

Input/Output

Three main types of instructions for reading from files:

Instructions for opening fileInstructions for reading dataInstructions for closing file

Miscellaneous:Determining that there’s no more data to read