QuestionQuestion

1. (Die.java and DiceTester.java) Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to store the number of sides on your die and another to store which side (value) is currently facing up. Your class should have two constructors – one that takes zero arguments and creates a standard 6-sided die and a second constructor that takes a single argument. The argument should allow the user to create a die that has 4, 6, 8, 10, 12, 20 or 100 sides; if the user enters any other value then the die should default to 6 sides. Both constructors should leave the side-up data field with a random (but valid) value. Your class should have a single accessor (getValue()) that returns the current value facing up. Your class should have a single void mutator method (roll()) that randomly rolls the die – make use of your number of sides data field here. Finally, add a toString() method to make it easy to print out the state of your die objects.

Write a complete tester program to show that your die class is fully functional. In particular you should create at least two dice, one with each constructor (get user input for the constructor that takes an argument, don’t do any input checking). Then roll them independently singly and using a loop. Also, since many games use more than one die show the sum of all your dice objects.

Creating two dice - one with 6 sides, enter number of sides for the second one: 20 Die[6 sides, value=3]
Die[20 sides, value=11]
Rolling each separately and printing the values:
Die 1 showing:6
Die 2 showing:3
Sum of the dice:9
for loop to roll the two dice 10 times and show the values and the sum:
Die 1, Die 2, Sum
4, 13, 17
1, 17, 18
5, 10, 15
3, 19, 22
4, 12, 16
1, 2, 3
1, 4, 5
3, 8, 11
2, 1, 3
2, 6, 8
3, 1, 4

(Extra) Add the following to your tester class. Create five more six-sided dice and use nested loops (for loop and while loop?) to determine the average number of rolls it take to get a Yahtzee (all 5 dice with the same value). Keep rolling the dice until you achieve a Yahtzee – keep track of the number of rolls it took to get it. Once you have done this, wrap it in a loop that repeats this process 1000 times. Display the maximum, minimum and average number of rolls it takes to get a Yahtzee. The output below should be in addition to the output shown above.

Creating five dice.
Die[6 sides, value=5]
Die[6 sides, value=4]
Die[6 sides, value=4]
Die[6 sides, value=2]
Die[6 sides, value=3]
Rolling until i find 1000 Yahtzees. Counting the number of rolls it takes to get each one. Found 1000 Yahtzees. Max rolls to find one: 12057 min rolls: 1 avg rolls: 1297.22

2. Write a hero class (Hero.java) that could be used in a role playing game. A hero has a name, an amount of health, an amount of mana (magic power) and can carry a limited number of items to help him or her on their quest. Your class should have data fields for these attributes:

name – self-explanatory? health – an integer number between 100 (full health) and 0 (expired/deceased) max_items – the maximum number of items that the hero can carry at one time items – a list (ArrayList of Strings?) of items currently in the hero’s possession. Think of this as the hero’s backpack of holding. Once this value is set, it cannot be changed.

The class should define the following methods:

-Constructor (argumented): This constructor should allow for two arguments – a name and a number of items. The initial health value for a hero should be 100. A new hero should not be carrying any items but should have an empty list ready to accept items later on. If the maximum items is greater than 10 or less than 0 throw an IllegalArgumentException (look back in the book if you need a reminder).

-Constructor (default): This constructor should allow for zero arguments. The initial health value 100. The hero’s name should be “Anonymous” and they should have a limit of 2 items in their backpack.

-inventory() : This method takes no arguments and returns (not simply print) a String representation of the items that the hero is currently carrying. If the hero is carrying nothing then a message should be displayed.   

-take() : This method should take one argument - the name of an item that the hero is adding to their inventory. The hero can only “take” an item if they are carrying less than their maximum number of items. If the hero is at their maximum carrying capacity, print an appropriate message.   

-drop() : This method should take a single argument – the name of an object the hero would like to remove from their inventory. If the user is not currently carrying the requested item then print an appropriate message, otherwise remove the item from inventory.

-takeDamage() : This method takes a single integer argument and subtracts that amount from the hero’s health. It the resulting hero health would be less than zero, set the health to zero.

-heal() : This method takes a single integer argument and adds that amount from the hero’s health. It the resulting hero health would be greater than 100, set the health to 100.

-toString() : write a proper toString method that will display the current state of a hero.


Write a tester class (HeroTester.java) that tests your hero class. Create a hero object using the argumented constructor and create another one the default constructor and print their initial state. Test your take(), drop(), inventory(), takeDamage() and heal() methods on one of your heroes to show that all of the methods work as advertised above. Hard-code names and objects from your own imagination (be creative but not insulting or derogatory). Show your heroes’ final state before exiting the program. Use my output as a rough guide.

There are a couple of things to note about this output. 1) There are no loops in my runner script and all of the arguments were hard-coded so there is no user input. 2) All of the bold text is generated by the hero class, not by the tester meaning all non-bold text is generated by the tester (simply System.out statements), not by the hero class.

-------Creating heroes--------
Creating a hero with no name.
Displaying hero: Hero: Anonymous, health: 100, can carry: 2 items, current inventory: Your hero is unburdened by worldly possessions.] Creating a hero with a name.
Displaying hero: Hero: Brave Sir Robin, health: 100, can carry: 3 items, current inventory: Your hero is unburdened by worldly possessions.]

-------Testing methods--------
Handing Sir Robin a sword.
Displaying inventory: [sword, ]
Handing Sir Robin a spoon.
Displaying inventory: [sword, spoon, ]
Handing Sir Robin a cape of good fortune.
Displaying inventory: [sword, spoon, cape of good fortune, ]
Handing Sir Robin a tomato:
Your hero is overburdened and cannot carry any more...
Displaying inventory: [sword, spoon, cape of good fortune, ]
Brave Sir Robin drops his spoon:
Displaying inventory: [sword, cape of good fortune, ]
Brave Sir Robin drops his watch:
Your hero is not carrying that item - watch
Displaying inventory: [sword, cape of good fortune, ]
Brave Sir Robin drops his sword:
Displaying inventory: [cape of good fortune, ]
Brave Sir Robin drops his cape of good fortune:
Displaying inventory: Your hero is unburdened by worldly possessions.]
Brave Sir Robin drops his bravery.
Your hero is not carrying that item – bravery
Displaying inventory: Your hero is unburdened by worldly possessions.]
Handing Sir Robin a pointy hat of COSC success:
Displaying inventory: [pointy hat of COSC success, ]
Brave Sir Robin is so busy worrying about his inventory he doesn't notice the danger and takes 50 damage from a shrubery!
Displaying hero: Hero: Brave Sir Robin, health: 50, can carry: 3 items, current inventory: [pointy hat of COSC success, ]
Brave Sir Robin takes a deap breath and recovers 5 health.
Displaying hero: Hero: Brave Sir Robin, health: 55, can carry: 3 items, current inventory: [pointy hat of COSC success, ]
The shrubery regroups and deals 119 damage to poor Brave Sir Robin.
Displaying hero: Hero: Brave Sir Robin, health: 0, can carry: 3 items, current inventory: [pointy hat of COSC success, ]
Brave Sir Robinn calls on the power of all the past Avatars, enters the Avatar state and heals for 128 points. Displaying hero: Hero: Brave Sir Robin, health: 100, can carry: 3 items, current inventory: [pointy hat of COSC success, ]
Remember, a hero can't have -ve health nor health > 100.

-------Final State--------
Displaying hero: Hero: Anonymous, health: 100, can carry: 2 items, current inventory: Your hero is unburdened by worldly possessions.]
Displaying hero: Hero: Brave Sir Robin, health: 100, can carry: 3 items, current inventory: [pointy hat of COSC success, ]

(Extra) This would be so much better if a user could interactively create and maintain a character of their own. Create a tester program that allows a user to create a new hero object. Create a menu that allows them to manipulate the character as needed. The menu options should correspond to the methods that can be called. Be sure to display your hero after each change that is made. Loop the menu until the user decides they are done.

P.S. It should be pretty easy to load from and save to file instead of having to re-create the character each time. You don’t have to do this but for those of you that are interested, give it a try.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Die {
   
    private int number_of_sides;
    private int value;
    Random random;
   
    /**
    * default constructor
    */
    public Die() {
       number_of_sides = 6;
       random = new Random();
       value = random.nextInt(this.number_of_sides) + 1;   
    }

    /**
    * one parameter constructor
    * @param number_of_sides
    */
    public Die(int number_of_sides) {
       if (number_of_sides == 4 ||
                number_of_sides == 6 ||
                number_of_sides == 10 ||
                number_of_sides == 20 ||
                number_of_sides == 100) {
            this.number_of_sides = number_of_sides;
       }else {
            this.number_of_sides = 6;
       }
      
       random = new Random();
       value = random.nextInt(this.number_of_sides) + 1;...
$33.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats