Question: can anybody please help? Java problem Write implementing classes for a Game interface and create and use an abstract class, including demonstrating polymorphism. For extra

can anybody please help? Java problem

Write implementing classes for a Game interface and create and use an abstract class, including demonstrating polymorphism. For extra credit, create a Coin class that implements Comparable.

Create two classes that implement a Game interface:

There is a Game interface defined in the interface, 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.

So the codes are as belows.. Please please help!!

------------------------------------------------------------------------------------------------------------------

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 } 

------------------------------------------------------------------------------------------------------------------

import java.util.*; /// Starting point for Interface Lab. public class PlayGames { private static Scanner in; private static Random rand = new Random(); 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 example: // games[gameCount] = new MyGame1(rand, 50); // MyGame1 must implement Game // gameCount++; // games[gameCount] = new MyGame2(rand, 25); // MyGame2 must implement Game // gameCount++; in = new Scanner(System.in); 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; } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!