lecture 21 - csweb.cs.wfu.educsweb.cs.wfu.edu/~turketwh/csc111/fall2005/lectures/lecture21.pdf ·...
TRANSCRIPT
Lecture 21
CSC 111 - Fall 2005
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
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?
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)
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());
}
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
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.
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
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) ;
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.
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);”
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
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”
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)
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
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
Input/Output
Useful to have a means of writing toand reading from files
Keyboard InputMonitor Output
Files for Inputand Output
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.
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
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