Term
In Project 1 Wheel of Fortune, you used copy-paste to create WOF Version 4 with the AI player from Version 3. The design for Wheel of Fortune with Inheritance makes it so you can easily code new AI players without copy-paste. Explain how the design eliminates the copy-paste reuse. (3) |
|
Definition
1) Project 1 required copying the randomPhrase, generateHiddenPhrase, and processGuess methods. The primary difference between the user game and the AI game was the getGuess method.
2) With inheritance, the WOFUserGame and WOFAIGame classes were able to inherit from the WheelOfFortune class, which contained the methods that were common to all kinds of Wheel of Fortune games.
3) WheelOfFortune inheriting from Game allowed WOFUserGame, and WOFAIGame to inherit the methods common to all games, like play() and playNext(). |
|
|
Term
What is the purpose of the Game class? What does it provide to be reused? What responsibilities does it give to subclasses? |
|
Definition
1) Game is the parent of WheelOfFortune and the grandparent of WOFUserGame and WOFAIGame.
2) It provides data members and methods common to all games -- like score, playAll(), and reset() -- that can be used by specific kinds of games.
3) As an abstract class, Game requies that all of its methods be implemented by its descendants.
4) Not all of its descendants must implement all of its methods. They can pass some along as abstract methods, but all methods must be implemented at some point in the line of descent. |
|
|
Term
What is the purpose of GameRecord and AllGamesRecord? How are they related to each other? How are they related to Game? |
|
Definition
1) GameRecord holds a player's score for one game. AllGamesRecord holds a player's GameRecords for all of their games. It analyzes the player's overall performance by sorting their scores and calculating their average.
2) AllGamesRecord has a list of GameRecords.
3) Both are data members of Game. Game's play() method returns a GameRecord and its playAll() method returns an AllGamesRecord. |
|
|
Term
What is WOFUserGame and WOFAIGame? Where do they fit into the hierarchy? What key method does each need to provide? |
|
Definition
1) WOFUserGame and WOFAIGame are WheelOfFortune games. UserGame has a human player and AIGame has an AI player.
2) They inherit from WheelOfFortune.
3) They need to provide their own versions of getGuess(). |
|
|
Term
Difference between an interface and a class |
|
Definition
1) Interface = abstract methods, default methods, static methods. Class = concrete methods.
2) Interface = constants. Classes = all types of data members. |
|
|
Term
What is an abstract method? What is a concrete method? |
|
Definition
Abstract method only has a method signature, concrete method has a body that implements the method |
|
|
Term
How is an interface like a contract? |
|
Definition
A class that implements an interface has the responsibility to implement all of its methods |
|
|
Term
Code for Comparable interface and implementation |
|
Definition
public interface Comparable { public int compareTo(T other); } |
|
|
Term
How does implementing Comparable improve the utility of a class? (3) |
|
Definition
1) Comparable takes any class/type
2) You can compare members of a class to one another and sort them using Collections.sort.
3) You don't have to define sort's implementation. |
|
|
Term
Advantages of implementing an interface, and example (3) |
|
Definition
1) Different classes that implement the same interface have a standardized blueprint to follow for their common methods. Example: class Cat and class Dog can both implement the Animal interface with its method public void makeSound(). Their implementations will be different, but both know to take no parameters and return nothing.
2) In polymorphism, a common method signature can be applied to objects with a common compile-time type and different runtime types.
3) A class can implement multiple interfaces.
4) Some library classes take only certain interfaces, like Collections's sort() method only works with classes that implement the Comparable interface. Any class that implements Comparable (and thus compareTo) can use Collections.sort(). |
|
|
Term
When an object inherits, what does it inherit? (3) |
|
Definition
1) All data members
2) Only public and protected methods
3) The child class still cannot directly access private data members of the parent class |
|
|
Term
public class Foo public void f(Goo g) {}
What type of object can you send as a parameter? |
|
Definition
A Goo or any of Goo's descendants |
|
|
Term
public class Foo public void f(Goo g) {}
Suppose you are writing a class FooChild and want to override method f, but you want your method to do what f does, and more. Show the code. |
|
Definition
class FooChild extends Foo { @Override public void f(Goo g) { super.f(g); // more } |
|
|
Term
What does @Override do? Do you have to use it to override a method? |
|
Definition
@Override tells the compiler to check if it is a true override (method name, return type, and parameter types must be the same)
Optional, but helps avoid hard-to-find bugs |
|
|