Question: Java Help 2 Questions: Lab 2.1 Game, Lab 2.2 Animal 2.1). Create two classes that implement a Game interface: There is a Game interface defined
Java Help 2 Questions: Lab 2.1 Game, Lab 2.2 Animal
2.1). Create two classes that implement a Game interface:
There is a Game interface defined in the Week 11 Source Code folder, and also a PlayGames class that uses objects from classes that implement that interface. The Game interface declares a single abstract method play that returns an int, the score from playing the game, where bigger is better.
PlayGames creates and stores Game objects in a Game[] and uses a popRandom method to randomly choose one of the games to play next by running that objects play method.
There is an example of one such game, AdditionGame, that implements the Game interface. PlayGames creates an AdditionGame object and stores it in the Game[] array.
You must write at least 2 more classes that implement Game, store objects from those classes in the Game[] array in PlayGames, and run PlayGames to test that they work. You will test them manually by interacting with PlayGames, not by writing specific tests in a main method.
Template:
/// Starting point for Game Interface Lab.
import java.util.*;
public class PlayGames
{
public static final Scanner170 in = new Scanner170(System.in);
private static Random rand = new Random(1); // so Random always generates the same numbers
private static int gameCount = 0;
public static Game popRandom(Game[] g)
{
int n = gameCount;
int i = rand.nextInt(n);
Game ret = g[i];
g[i] = g[n-1];
gameCount--;
return ret;
}
public static void main(String[] args)
{
Game[] games = new Game[10]; // Note Game as a type
games[gameCount] = new AdditionGame(rand, 100);
gameCount++; // next index to put a Game at
// write at least 2 more different types of Game classes
// and add a new one of each type to games
// ...
/* for this exercise, write a SubtractionGame and a
MultiplicationGame by copying and modifying the
AdditionGame below - the SubtractionGame should be
constructed with 100 as the maximum number, like
AdditionGame, and the MultiplicationGame should be
constructed with 20 as the maximum number */
/* add those two games to the games array here, in that order */
int totScore = 0;
do {
Game g = popRandom(games);
totScore += g.play(); // use numerical result from the game
} while (gameCount > 0 && agree("Want a game? "));
System.out.println("Thanks for Playing!");
System.out.println("Your total score is " + totScore);
}
public static boolean agree(String prompt)
{
System.out.print(prompt);
String input = in.next();
if (input.equalsIgnoreCase("y")) return true;
return false;
}
}
/* DO NOT MARK THIS INTERFACE PUBLIC */
interface Game // Note *interface* in place of *class*
{
/// play the game and return the final score
/// where a higher score should be better,
/// and a negative score is allowed.
int play(); // Note semicolon in place of a body
// You can have multiple method headings declared
}
/* DO NOT MARK THIS CLASS PUBLIC */
class AdditionGame implements Game // note implements!!
{
private Random rand;
private int n;
// Constructor for objects of class AdditionGame
public AdditionGame(Random r, int big)
{
rand = r;
n = big;
}
// play all games and keep score.
public int play() // exactly matches heading in Game interface
{
final int numGames = 3;
Scanner in = PlayGames.in;
int score = 0;
System.out.println("Welcome to the addition game! We'll now play " + numGames + " rounds.");
for (int i = 0; i < numGames; i++) {
int x = rand.nextInt(n), y = rand.nextInt(n), ans = x+y;
System.out.print(String.format("Enter the sum: %d + %d = ", x, y));
int val = in.nextInt();
if (ans == val) {
System.out.println("Correct!");
score++;
}
else
System.out.println("Wrong! Right answer is " + ans);
}
System.out.println("Thanks for playing the addition game. Your score is " + score + ".");
System.out.println();
return score;
}
}
/* DO NOT MARK THIS CLASS PUBLIC */
class SubtractionGame implements Game // note implements!!
{
/* copy the contents of AdditionGame here and modify it so that
it becomes the SubtractionGame - it should also play 3 rounds */
}
/* DO NOT MARK THIS CLASS PUBLIC */
class MultiplicationGame implements Game // note implements!!
{
/* copy the contents of AdditionGame here and modify it so that
it becomes the MultiplicationGame - it should also play 3 rounds */
}
2.2). Create and use an abstract class:
Modify the Animal class to be abstract and change its greet method to be abstract:
Changing Animal on in-class slide 5 to be abstract should only take two changes. The greet method should no longer have a method body.
Also, write the Friendly interface shown in the slides and change Animal to implement it.
Create a simple Dog class that looks like the one shown in the inheritance slides and have it extend Animal. Its greet method must @Override the abstract greet method in Animal and fully define it. Note that Dog cannot reuse Animals greet, because there is no longer a greet definition in Animal. The greet method in Dog should produce the following two lines, using Animal's getName method to get the name:
Hello, my name is
In addition Dog should define a toString method that returns this:
, type = Dog
Write a main method either in Dog or in a separate class that creates a Dog object, assigns it to a Dog reference variable, and runs that objects greet method: Dog d = new Dog("fido"); d.greet();
Also in main assign that Dog object or another new one to an Animal reference variable and run its greet method: Animal a = d; a.greet();
Finally in main assign a Dog object to a Friendly reference variable and run its greet method: : Friendly f = d; f.greet();
Template:
/* DO NOT MARK THIS CLASS PUBLIC */
class Animal /* convert this class to be abstract and have it implement Friendly */
{
private String name;
public Animal(String name) // constructor sets instance variable
{
this.name = name;
}
public String getName()
{
return name;
}
public void greet() /* change this method to be abstract, with no method body */
{
System.out.println("Hello, my name is " + name);
}
@Override
public String toString()
{
return "Animal: " + name;
}
}
/* DO NOT MARK THIS INTERFACE PUBLIC! */
interface Friendly
{
/* write this interface as shown in the slides in Week 11 */
}
public class Dog extends Animal // a class that extends Animal
{ // Dog now implicitly implements Friendly, since Animal does
/* write the Dog constructor that takes a String name and calls
the Animal constructor using super(), passing it the name */
@Override
public void greet()
{
/* fill in the greet method as required above */
}
@Override
public String toString()
{
return super.toString() + ", type = Dog";
}
public static void main(String[] args)
{
Dog fido = new Dog("fido");
fido.greet();
System.out.println(fido);
System.out.println();
Animal animal = new Dog("rover");
animal.greet();
System.out.println(animal);
System.out.println();
Friendly friendly = new Dog("wolfy");
friendly.greet();
System.out.println(friendly);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
