Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A physiological experiment was concluded to study the eect of various factors on a pulse rate.Life is strange when it comes to this game. -

A physiological experiment was concluded to study the eect of various factors on a pulse rate.Life is strange when it comes to this game.

- Class diagram including three classes, Beetle, Die, and BeetleGame.

- Implementation of the Die class.

- Implementation of Beetle class, with each addXXX() method 2 points and variable declaration 2 points, constructor 2 points, and isComplete() method 2 points.

- Test your Die class and Beetle class, with each class evaluation 3 points.

- Implementation of BeetleGame class, with the Play() method and takeTurn() method, each 3 points.

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

/** The game of Beetle for two players. */ public class BeetleGame {

/** For reading from the console. */ public static final java.util.Scanner INPUT = new java.util.Scanner(System.in);

/** Player 1's Beetle. */ private Beetle bug1;

/** Player 2's Beetle. */ private Beetle bug2;

/** A die. */ private Die die;

/** Create the Die and Beetles. */ public BeetleGame() { //TODO initilize data members bug1 = new Beetle(); bug2 = new Beetle(); die = new Die(); }

/** Play until someone wins. */ public void play() { int player = 1; Beetle bug = bug1;

//TODO take turns to roll die and draw component while (!(bug.isComplete())) { if (!(takeTurn(player, bug))) { if (player == 1) { player = 2; bug = bug2; } else { player = 1; bug = bug1; } } } System.out.println(" Player " + player + " wins!"); System.out.println(bug); }

/** * Take a turn for the current player. Return true if the player * earned a bonus turn. */ public boolean takeTurn(int player, Beetle bug) { System.out.println(" Player " + player + ", your beetle:"); System.out.println(bug); System.out.print("Hit return to roll: "); INPUT.nextLine(); die.roll(); System.out.print("You rolled a " + die.getTopFace());

//TODO draw component according to the top value on the die switch (die.getTopFace()) { case 1: System.out.println(" (body)"); return bug.addBody(); case 2: System.out.println(" (head)"); return bug.addHead(); case 3: System.out.println(" (leg)"); return bug.addLeg(); case 4: System.out.println(" (eye)"); return bug.addEye(); case 5: System.out.println(" (feeler)"); return bug.addFeeler(); default: System.out.println(" (tail)"); return bug.addTail(); } }

/** Create and play the game. */ public static void main(String[] args) { System.out.println("Welcome to Beetle."); BeetleGame game = new BeetleGame(); game.play(); } } -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/** A six-sided die for use in games. */ public class Die implements Comparable {

/** The face of this Die that is showing. */ private int topFace;

/** Initialize the top face to 1. */ public Die() { //TODO initialize the data member this.topFace = 1; }

// Added in Chapter 8 public int compareTo(Die that) { return topFace - that.topFace; } /** Return true if that Die has the same top face as this one. */ public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } Die thatDie = (Die)that; return topFace == thatDie.topFace; }

/** Return the top face of this Die. */ public int getTopFace() { return this.topFace; }

/** * Set the top face to a random integer between 1 and 6, inclusive. */ public void roll() { //TODO roll the die this.topFace = ((int)(Math.random() * 6)) + 1; }

/** Set the top face to the specified value. */ public void setTopFace(int topFace) { this.topFace = topFace; }

public String toString() { return "" + this.topFace; } /** Create a Die, print it, roll it, and print it again. */ public static void main(String[] args) { Die d = new Die(); System.out.println("Before rolling: " + d); d.roll(); System.out.println("After rolling: " + d); }

}

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

/** Beetle with parts for the Beetle game. */ public class Beetle {

/** True if this Beetle has a body. */ protected boolean body;

/** Number of eyes this Beetle has, from 0-2. */ protected int eyes;

/** Number of feelers this Beetle has, from 0-2. */ protected int feelers;

/** True if this Beetle has a head. */ protected boolean head;

/** Number of legs this Beetle has, from 0-6. */ protected int legs;

/** True if this Beetle has a tail. */ protected boolean tail;

/** A new Beetle has no parts. */ public Beetle() { //TODO Initialize data members body = false; eyes = 0; feelers = 0; head = false; legs = 0; tail = false; } /** Try to add a body and return whether this succeeded. */ public boolean addBody() { //TODO add body if (body) { return false; } else { body = true; return true; } }

/** Try to add an eye and return whether this succeeded. */ public boolean addEye() { //TODO add an eye if (head && (eyes < 2)) { eyes++; return true; } else { return false; } }

/** Try to add a head and return whether this succeeded. */ public boolean addHead() { //TODO add a head if (body && !head) { head = true; return true; } else { return false; } } /** Try to add a feeler and return whether this succeeded. */ public boolean addFeeler() { //TODO add a feeler if (head && (feelers < 2)) { feelers++; return true; } else { return false; } }

/** Try to add a leg and return whether this succeeded. */ public boolean addLeg() { //TODO add a leg if (body && (legs < 6)) { legs++; return true; } else { return false; } } /** Try to add a tail and return whether this succeeded. */ public boolean addTail() { //TODO add a tail if (body && !tail) { tail = true; return true; } else { return false; } }

/** Return true if that Beetle has the same parts as this one. */ public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } Beetle thatBeetle = (Beetle)that; return body == thatBeetle.body && eyes == thatBeetle.eyes && feelers == thatBeetle.feelers && head == thatBeetle.head && legs == thatBeetle.legs && tail == thatBeetle.tail; }

/** Return true if this Beetle has all of its parts. */ public boolean isComplete() { //TODO check whether all components of a beetle exist return body && (eyes == 2) && (feelers == 2) && head && (legs == 6) && tail; } public String toString() { if (body) { String result = ""; if (feelers > 0) { result += "\\"; if (feelers == 2) { result += " /"; } result += " "; } if (head) { if (eyes > 0) { result += "o"; } else { result += " "; } result += "O"; if (eyes == 2) { result += "o"; } result += " "; } if (legs > 0) { result += "-"; } else { result += " "; } result += "#"; if (legs > 1) { result += "-"; } result += " "; if (legs > 2) { result += "-"; } else { result += " "; } result += "#"; if (legs > 3) { result += "-"; } result += " "; if (legs > 4) { result += "-"; } else { result += " "; } result += "#"; if (legs > 5) { result += "-"; } if (tail) { result += " v"; } return result; } else { return "(no parts yet)"; } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions