Lab 10: Inheritance, Static Variables and Methods

due Thursday, February 13 at 11:59pm on Canvas

Pair Programming Required (or No Credit)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document along with your Lab code to Canvas.
  • Only ONE partner submits the Lab code on Canvas.
  • BOTH partners submit the contract.
  • Please make sure both your names are on all the files.
  • If you need help finding a partner, please contact me as soon as possible.


Battle Die Galactica

This lab uses objects to simulate a dice game played between a player and the computer. During this assignment, you will:

  • Write a class to hold the data for a die
  • Write a class that holds two instances of the die class
  • Write a class that represents a player
  • Write a subclass that represents an A.I.
  • Write a game class
  • Optionally, construct a class that uses static methods to test a class


Die Class Specifications

  • Create a class named Die to store the data about one die. You must implement the below constructors and methods.
/**
 * Die.java
 * @author
 * @author
 * CIS 36B, Lab 10
 */

import java.util.Random;

public class Die {
    private int roll;
    private int numSides;
   
    /**
     * Default constructor for a Die
     * Initializes roll to 0
     * and numSides to the standard 6
     */
    public Die() {
  
    }
   
    /**
     * Constructor for a Die
     * Initializes roll to 0
     * and numSides to specified
     * number of sides
     * @param numSides the
     * number of sides to the die
     */
    public Die(int numSides) {
       
    }
   
    /**
     * Assigns roll to a random value
     * from 1 to numSides
     */
    public void makeRoll() {
       
    }
   
    /**
     * Returns the roll number
     * @return the number generated
     * when die is rolled
     */
    public int getRoll() {
        return -1;
    }
}

  • Note 1: You are not allowed to implement any additional methods or alter the class in any other way than to write the given method bodies.
  • Note 2: The default number of sides for one die is 6. However, some dice (polyhedral dice) contain a different number of sides.
    • For this lab, we will assume that dice can have 6, 8, 10, or 12 sides only.
    • Error checking for inappropriately sided dice will happen later in main. You do not need to test for this in the dice class.
  • Note 3: to simulate a random roll, you must call methods from the Random class <-- Required or you will receive a 5 pt deduction
    • For an appropriate seed, consider the nanoTime() method from the System class

Dice Class Specifications

  • Create a class named Dice to store two Die Objects.
  • For this class, implement the below constructors and methods.
  • Note: You are not permitted to add any additional methods or otherwise alter the class except to implement the given methods.
/**
 * Dice.java
 * @author
 * @author
 * CIS 36B, Lab 10
 */

public class Dice {
    private Die die1;
    private Die die2;
   
    /**
     * Default constructor
     * sets both dice to
     * be 6 sided
     */
    public Dice() {
       
    }
   
    /**
     * Constructor to set
     * both dice to have the
     * same number of sides
     * @param sides the number
     * of sides of both dice
     */
    public Dice(int sides) {
       
    }
   
   
    /**
     * Rolls both dice
     */
    public void roll() {
       
    }
   
    /**
     * Returns the roll value
     * of Die1
     * @return the roll value
     * of Die1
     */
    public int getDie1Roll() {
        return -1;
    }
   
    /**
     * Returns the roll value
     * of Die2
     * @return the roll value
     * of Die2
     */
    public int getDie2Roll() {
        return -1;
    }
   
    /**
     * Returns the sum of the
     * roll values of Die1
     * and Die2
     * @return the sum of the rolls
     */
    public int getSumRolls() {
        return -1;
    }
   
}

Player Class Specifications

  • Write a class to represent the human player of the game
  • The player rolls his or her own pair of dice and has a current score.
  • Implement the constructor and other methods as follows.
  • Note: You are not permitted to add any additional methods or otherwise alter the class except to implement the given methods.
/**
 * Player.java
 * @author
 * @author
 * CIS 36B, Lab 10
 */

public class Player {
    private Dice dice;
    private int score;
    
    /**
     * Player constructor that
     * sets the number of sides
     * of the dice the player will
     * roll and gives the player
     * a default score of 0
     * @param sides the number of sides
     * of the dice
     */
    public Player(int sides) {
       
    }
    
    /**
     * Returns the player's current score
     * @return the current score
     */
    public int getScore() {
        return -1;
    }
    
    /**
     * Returns the value of the dice
     * @return the dice
     */
    public Dice getDice() {
        return null;
    }
    
    /**
     * Updates the score
     * @param score the current score
     */
    public void setScore(int score) {
       
    }
    
    /**
     * Rolls the two player dice
     */
    public void rollDice() {
       
    }
    
    /**
     * Prints the value of the the rolls
     * of the two dice to the console, with
     * each roll separated by a blank space
     */
    public void printRoll() {
       
    }
    
    /**
     * Prints out whether a special roll was made
     * and updates the score accordingly
     * For snake eyes (two 1s) prints Snake Eyes!
     * and updates the score by 5
     * When the two die rolls match otherwise,
     * prints out Matching! and updates the
     * score by 3
     * For total of 3, prints out Total of 3!
     * and updates the score by 1
     * For total of 5, prints out Total of 5!
     * and updates the score by 2
     * For total of 7, prints out Oh no! Total of 7!
     * and resets the score back to 0
     * For total of 9, prints out Total of 9!
     * and updates the score by 3
     * For total of 11, prints out Total of 11!
     * and updates the score by 4
     * For any other odd number, prints out Odd Roll!
     * and updates the score by 2
     */
    public void updateScore() {
       
    }
}

AI Class Specifications

  • Write a class to represent the computer player of the game, which inherits from the Player class
  • The AI rolls its own pair of dice and has a current score.
  • It also has an additional variable to count how many turns it has taken
  • Based on the number of turns taken, a roll of 7 will affect the score for the AI differently (please see comment for updateScore method)
  • Note that it will override updateScore to handle a roll of 7 differently from the player class
  • Implement the constructor and other methods as follows.
  • Note: You are not permitted to add any additional methods or otherwise alter the class except to implement the given methods.


/**
 * AI.java
 * @author
 * @author
 * CIS 36B, Lab 10
 */


public class AI extends Player {
    private static int numTurns;
   
    /**
     * Constructor for AI
     * Calls Player constructor
     * Sets numTurns to 0
     * @param sides the number of
     * sides of the dice
     */
    public AI(int sides) {
       
    }
   
    /**
     * Returns the total turns taken by the AI
     * @return the number of turns the AI
     * has taken
     */
    public static int getNumTurns() {
        return -1;
    }
   
    /**
     * Updates the number of turns taken by adding 1
     */
    public static void increaseNumTurns() {
       
    }
   
    /**
     * Prints out whether a special roll was made
     * and updates the score accordingly
     * For snake eyes (two 1s) prints Snake Eyes!
     * and updates the score by 5
     * When the two die rolls match otherwise,
     * prints out Matching! and updates the
     * score by 3
     * For total of 3, prints out Total of 3!
     * and updates the score by 1
     * For total of 5, prints out Total of 5!
     * and updates the score by 2
     * For total of 7, and an even number of turns taken
     * Prints Total of 7! Double Points for Even Turn
     * Then, updates AI score by mutliplying by 2
     * For total of 7, and an odd number of turns taken
     * Prints Total of 7! Zero'd out for Odd Turn
     * Then sets AI score to 0
     * For total of 9, prints out Total of 9!
     * and updates the score by 3
     * For total of 11, prints out Total of 11!
     * and updates the score by 4
     * For any other odd number, prints out Odd Roll!
     * and updates the score by 2
     */
    @Override public void updateScore() {
       
    }

}


BattleDieGalactica Class Specifications

  • Write a class to represent one game of Battle Die Galactica
  • Implement the below methods, while leaving the constructor to be the default provided by the compiler
  • Note: You *are* permitted to add any additional methods in addition to writing the provided methods
  • Note that no methods of this class are static. Therefore you will need to make a BattleDieGalactica object to use to call your methods.
    • Try calling the methods first without this object and notice the error message.
    • Then, update your code to call the methods on this object
  • This class must work identically to what is shown in the sample output. 
  • Note that the user should be able to end the game early at any time by typing n in response to the question Roll again? (y/n):
  • As well as the displaying the game play as the game progresses, it must determine who won (player or computer) or whether the game was a tie, and print a game ending summary. 
  • Your program must display this information as shown in the sample output below.

/**
 * BattleDieGalactica.java
 * @author
 * @author
 * CIS 36B, Lab 10
 */
import java.util.Scanner;

public class BattleDieGalactica {
    private Player player;
    private AI ai;
   
    /**
     * Prompts the user to input a number of die sides
     * Prints out the user choice and
     * Constructs a new player and ai based on the number
     * of sides of the dice
     * Note that the user input will be rounded up to 6, 8, 10
     * or 12 and also rounded down to 12 if a higher number is
     * input.
     * @param input the Scanner
     */
    private void gameSetUp(Scanner input) {
        System.out.print("Enter the number of sides (6, 8, 10 or 12): ");
       
    }
   
    /**
     * Prints out the current score for each opponent
     * to the console in the form:
     * Player: <score>
     * Computer: <score>
     * Note: no <> should be displayed
     */
    public void printScore() {
       
    }
   
    /**
     * Determines whether any player has reached
     * 21 points or more, thus ending the game
     * @return whether the game is over
     */
    private boolean gameOver() {
        return false;
    }
   
   
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
       
        System.out.println("***Welcome to Battle Die Galactica!***\n");
       
       
        input.close();
       
    }
}


Optional Test Class

  • There are a lot of components involved in this assignment.
  • To make sure that each piece is working properly, I recommend that you create an optional GameTest.java class where you call the different methods you write as you go along to see that they are working properly.
  • For example:
/**
 * GameTest.java
 * @author
 * @author
 * CIS 36B Lab 10
 */
public class GameTest {
    public static void main(String[] args) {
        
        System.out.println("***Testing Die Class***");
        Die d = new Die(6);
        d.makeRoll();
        System.out.println("Die roll: " + d.getRoll());
        
        System.out.println("\n***Testing Dice Class***");
        Dice dice = new Dice(8);
        dice.roll();
        System.out.println("Die 1 roll: " + dice.getDie1Roll());
        System.out.println("Die 2 roll: " + dice.getDie2Roll());
        
        System.out.println("\n***Testing Player Class***");
        Player player = new Player(6);
        player.rollDice();
        player.printRoll();
        System.out.println("Score: " + player.getScore());
        
        //add more method calls where needed
    }
}


Sample Output:

***Welcome to Battle Die Galactica!***

Enter the number of sides (6, 8, 10 or 12): 6
6-sided dice!

Roll the dice? (y/n): n

Game summary:
Player: 0
Computer: 0

It's a tie!



Sample Output:

***Welcome to Battle Die Galactica!***

Enter the number of sides (6, 8, 10 or 12): 7
8-sided dice!

Roll the dice? (y/n): y

Your Roll: 2 5
Oh no! Total of 7!

Current scores:
Player: 0
Computer: 0

Counter Roll: 1 2
Total of 3!

Current scores:
Player: 0
Computer: 1

Roll again? (y/n): y

Your Roll: 3 3
Matching!

Current scores:
Player: 3
Computer: 1

Counter Roll: 1 2
Total of 3!

Current scores:
Player: 3
Computer: 2

Roll again? (y/n): y

Your Roll: 5 7

Current scores:
Player: 3
Computer: 2

Counter Roll: 7 2
Total of 9!

Current scores:
Player: 3
Computer: 5

Roll again? (y/n): y

Your Roll: 5 6
Total of 11!

Current scores:
Player: 7
Computer: 5

Counter Roll: 6 2

Current scores:
Player: 7
Computer: 5

Roll again? (y/n): y

Your Roll: 5 4
Total of 9!

Current scores:
Player: 10
Computer: 5

Counter Roll: 6 6
Matching!

Current scores:
Player: 10
Computer: 8

Roll again? (y/n): n

Game summary:
Player: 10
Computer: 8

Congratulations! You win!


Sample Output:

***Welcome to Battle Die Galactica!***

Enter the number of sides (6, 8, 10 or 12): 7
8-sided dice!

Roll the dice? (y/n): y

Your Roll: 1 5

Current scores:
Player: 0
Computer: 0

Counter Roll: 4 6

Current scores:
Player: 0
Computer: 0

Roll again? (y/n): y

Your Roll: 2 6

Current scores:
Player: 0
Computer: 0

Counter Roll: 1 4
Total of 5!

Current scores:
Player: 0
Computer: 2

Roll again? (y/n): y

Your Roll: 4 6

Current scores:
Player: 0
Computer: 2

Counter Roll: 2 3
Total of 5!

Current scores:
Player: 0
Computer: 4

Roll again? (y/n): y

Your Roll: 7 1

Current scores:
Player: 0
Computer: 4

Counter Roll: 8 5
Odd Roll!

Current scores:
Player: 0
Computer: 6

Roll again? (y/n): y

Your Roll: 5 6
Total of 11!

Current scores:
Player: 4
Computer: 6

Counter Roll: 3 3
Matching!

Current scores:
Player: 4
Computer: 9

Roll again? (y/n): y

Your Roll: 2 1
Total of 3!

Current scores:
Player: 5
Computer: 9

Counter Roll: 4 2

Current scores:
Player: 5
Computer: 9

Roll again? (y/n): y

Your Roll: 1 5

Current scores:
Player: 5
Computer: 9

Counter Roll: 6 1
Total of 7! Zero'd Out for Odd Turn!

Current scores:
Player: 5
Computer: 0

Roll again? (y/n): y

Your Roll: 7 6
Odd Roll!

Current scores:
Player: 7
Computer: 0

Counter Roll: 8 2

Current scores:
Player: 7
Computer: 0

Roll again? (y/n): y

Your Roll: 7 4
Total of 11!

Current scores:
Player: 11
Computer: 0

Counter Roll: 2 7
Total of 9!

Current scores:
Player: 11
Computer: 3

Roll again? (y/n): y

Your Roll: 3 1

Current scores:
Player: 11
Computer: 3

Counter Roll: 8 8
Matching!

Current scores:
Player: 11
Computer: 6

Roll again? (y/n): y

Your Roll: 8 3
Total of 11!

Current scores:
Player: 15
Computer: 6

Counter Roll: 4 1
Total of 5!

Current scores:
Player: 15
Computer: 8

Roll again? (y/n): y

Your Roll: 6 1
Oh no! Total of 7!

Current scores:
Player: 0
Computer: 8

Counter Roll: 6 7
Odd Roll!

Current scores:
Player: 0
Computer: 10

Roll again? (y/n): y

Your Roll: 4 6

Current scores:
Player: 0
Computer: 10

Counter Roll: 2 4

Current scores:
Player: 0
Computer: 10

Roll again? (y/n): y

Your Roll: 1 5

Current scores:
Player: 0
Computer: 10

Counter Roll: 2 1
Total of 3!

Current scores:
Player: 0
Computer: 13

Roll again? (y/n): y

Your Roll: 5 2
Oh no! Total of 7!

Current scores:
Player: 0
Computer: 13

Counter Roll: 1 1
Snake Eyes!

Current scores:
Player: 0
Computer: 18

Roll again? (y/n): y

Your Roll: 3 6
Total of 9!

Current scores:
Player: 3
Computer: 18

Counter Roll: 6 8

Current scores:
Player: 3
Computer: 18

Roll again? (y/n): y

Your Roll: 2 7
Total of 9!

Current scores:
Player: 6
Computer: 18

Counter Roll: 8 2

Current scores:
Player: 6
Computer: 18

Roll again? (y/n): y

Your Roll: 1 2
Total of 3!

Current scores:
Player: 7
Computer: 18

Counter Roll: 4 3
Total of 7! Double Points for Even Turn!

Game summary:
Player: 7
Computer: 36

Computer wins! Better luck next time.


What to Submit:

  • Submit your completed Die.java, Dice.java, Player.java, AI.java, BattleDieGalactica.java file (make sure both your names are on all files) to Canvas.
  • One partner submits all of the source code files. Both partners submit the pair programming contract.
    • 5 point deduction for missing contract
  • 5 point deduction for any additional package statements (alteration to starter code) - please remove added package statements before submitting your assignment
  • 5 point deduction for not using the Random class to implement randomization for this assignment
  • 5 point deduction if your code is not properly formatted.
    • Go to Source ->Format in Eclipse to autoformat your code
  • 5 point deduction if your names are not on all of the files.

How You Will Be Graded:

  • 100 points: The assignment works as shown in the sample output. All required features are implemented and implemented correctly.
  • 80-100 points: The assignment works mostly as shown in the sample output. Most required features are implemented and implemented correctly.
  • 40-80 points: Some of the assignment works as shown in the sample output. Some required features are implemented and implemented correctly.
  • 10-40 points: Serious problems in implementation and little correspondence between sample output and what is shown. Few of the features implemented or implemented correctly.
  • 0 points: Code does not compile or outputs an error at runtime.