Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation 9 October 28, 2011.

Similar presentations


Presentation on theme: "Recitation 9 October 28, 2011."— Presentation transcript:

1 Recitation 9 October 28, 2011

2 Today’s Goals: Review abstract classes
Briefly review Java’s Scanner class Get more practice with scanning

3 Abstract classes An abstract class is a hybrid of a class and an interface Abstract classes are defined by including the word “abstract” before the word “class”:

4 Abstract classes: Like classes
Has exactly one superclass (Object if none specified) Superclass can be either abstract OR non-abstract Can implement zero or more interfaces Can contain constructors; these constructors can call super constructors Can contain variables, constants, and implement methods

5 Abstract classes: Like classes
Note the use of “this” to call constructor in same class. Call to super() is redundant, and would be included anyway. This is why the empty constructor was necessary on the test.

6 Abstract classes: Like interfaces
Can’t create an instance of an abstract class (even if it contains constructors!) Illegal: Can implement interfaces but not provide implementations for the methods in those interfaces Legal:

7 Abstract classes: Unlike classes or interfaces
Can define abstract methods (i.e. methods without implementations) and call them (!)

8 Non-abstract classes Also called “concrete” classes
If a class is non-abstract, then it provides implementations of every unimplemented abstract method from all of its superclasses, as well as all methods from all interfaces implemented by all superclasses

9 Benefits of abstract classes
Similar to benefits of inheritance Avoid repeating code Allow common behaviors Defer implementation to subclasses Indicate behavior must be provided, but is subclass-specific Prevents the creation of incomplete objects

10 Using abstraction

11 Using abstraction

12 Using abstraction In the code I provide, I also included a ResizableCartesianShape since it is difficult to resize triangles.

13 Review: java.util.Scanner
import java.util.Scanner; Create a scanner object to read input from the console: Scanner scanner = new Scanner(System.in); Note: This reads console input, not program argument input To get the next string entered by the user (up to the first whitespace): scanner.next() To get the next integer entered by the user: scanner.nextInt() Show “guess what number I’m thinking” example.

14 Recitation Specification
Download Recitation9.zip from the Recitations page Repeatedly read the next string entered by the user (scanner.next()) If the user enters “size”, print the size of the stack to the console If the user enters “pop”, pop the top element off of the stack If the user enters “triangle”, read in the following 6 integers (using scanner.nextInt()), create a CartesianTriangle object with these values, and push it onto the stack Remember, you need to manually refresh Object Editor to see these changes (View -> Refresh) Bonus (for candy): Allow the user to push as many of these as possible: lines, circles, ovals, rectangles, squares (all these classes are provided in graphics)


Download ppt "Recitation 9 October 28, 2011."

Similar presentations


Ads by Google