public interface Game Note interface in place of class pl

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
                // ...
                 
                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;
        }
----------------------------------------------------------------------------------------------------------------------
import java.util.*;

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 = new Scanner(System.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;  
        }
}
-----------------------------------------------------------------------------------------------------------------------

Create two classes that implement a Game interface:

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

There is a Game interface defined, 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 object’s 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.

Solution

Please follow the code and comments for description :

CODE :

PlayGames.java :

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) { // pop the game to get the random game
        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) { // driver method
        Game[] games = new Game[10]; // Note Game as a type
        games[gameCount] = new AdditionGame(rand, 100); // add the game of addition
        gameCount++; // increment the count
        games[gameCount] = new SubtractionGame(rand, 100); // add the game of subtraction
        gameCount++; // increment the count
        games[gameCount] = new MultiplicationGame(rand, 100); // add the game of multiplication
        gameCount++; // increment the count
      
        in = new Scanner(System.in); // get the value from the user
        int totScore = 0;
        do {
            Game g = popRandom(games); // get the random value
            totScore += g.play(); // use numerical result from the game
        } while (gameCount > 0 && agree(\"Want a game? \")); // prompt for the user
        System.out.println(\"Thanks for Playing!\");
        System.out.println(\"Your total score is \" + totScore); // print the total score
    }

    public static boolean agree(String prompt) {
        System.out.print(prompt);
        String input = in.next();
        if (input.equalsIgnoreCase(\"y\")) {
            return true;
        }
        return false;
    }
}


Game.java :

public interface Game // Note *interface* in place of *class*
{
    int play();
}


MultiplicationGame.java :

import java.util.Random; // imports for the code to run
import java.util.Scanner;

public class MultiplicationGame implements Game { // class that runs the code and implements the interface

    private Random rand; // instance variables
    private int n;

    public MultiplicationGame(Random r, int big) { // constructor
        rand = r;
        n = big;
    }

    @Override
    public int play() { // implemented classes
        final int numGames = 3;
        Scanner in = new Scanner(System.in); // scanner class
        int score = 0; // locla variables
        System.out.println(\"Welcome to the Multiplication game! We\'ll now play \" + numGames + \" rounds.\"); // message to the user
        for (int i = 0; i < numGames; i++) { // iterate over the games count
            int x = rand.nextInt(n), y = rand.nextInt(n), ans = x * y; // variable initialisation
            System.out.print(String.format(\"Enter the Multiplication Value : %d * %d = \", x, y)); // print the data
            int val = in.nextInt(); // get the value
            if (ans == val) { // check for the value
                System.out.println(\"Correct!\");
                score++; // increment the score
            } else {
                System.out.println(\"Wrong! The Right answer is \" + ans); // message
            }
        }
        System.out.println(\"Thanks for playing the Multiplication game. Your score is \" + score + \".\"); // message
        System.out.println();
        return score; // return the score
    }
}


SubtractionGame.java :

import java.util.Random; // imports for the code to run
import java.util.Scanner;

public class SubtractionGame implements Game { // class that runs the code and implements the interface

    private Random rand; // instance variables
    private int n;

    public SubtractionGame(Random r, int big) { // constructor
        rand = r;
        n = big;
    }

    @Override
    public int play() { // implemented classes
        final int numGames = 3;
        Scanner in = new Scanner(System.in);
        int score = 0;
        System.out.println(\"Welcome to the Subtraction game! We\'ll now play \" + numGames + \" rounds.\"); // message to the user
        for (int i = 0; i < numGames; i++) {
            int x = rand.nextInt(n), y = rand.nextInt(n), ans = x - y; // variable initialisation
            System.out.print(String.format(\"Enter the Difference : %d - %d = \", x, y));
            int val = in.nextInt(); // get the value
            if (ans == val) { // check for the value
                System.out.println(\"Correct!\");
                score++; // increment the score
            } else {
                System.out.println(\"Wrong! The Right answer is \" + ans);
            }
        }
        System.out.println(\"Thanks for playing the Subtraction game. Your score is \" + score + \".\"); // message
        System.out.println();
        return score;// return the score
    }

}


AdditionGame.java :

import java.util.*;

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.
    @Override
    public int play() // exactly matches heading in Game interface
    {
        final int numGames = 3;
        Scanner in = new Scanner(System.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;
    }
}


OUTPUT :

Welcome to the Subtraction game! We\'ll now play 3 rounds.
Enter the Difference : 30 - 66 = -36
Correct!
Enter the Difference : 6 - 75 = -69
Correct!
Enter the Difference : 65 - 25 = 40
Correct!
Thanks for playing the Subtraction game. Your score is 3.

Want a game? y
Welcome to the Multiplication game! We\'ll now play 3 rounds.
Enter the Multiplication Value : 66 * 69 = 548
Wrong! The Right answer is 4554
Enter the Multiplication Value : 15 * 84 = 1453
Wrong! The Right answer is 1260
Enter the Multiplication Value : 88 * 38 = 548
Wrong! The Right answer is 3344
Thanks for playing the Multiplication game. Your score is 0.

Want a game? n
Thanks for Playing!
Your total score is 3

Hope this is helpful.

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
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
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
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
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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site