Question

Can anyone help me with this java program? We are still very early with lessons so...

Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you

Create 2 Java files for this assignment: Die.java and TestDie.java

Part 1 - The Die Class

            The program

Design a Die class that contains the following attributes:

  • sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have more. This will be provided by the user
  • value: represents the current value that was rolled in the dice.

The die Class should have the following methods:

  • Constructor: takes one parameter that represents the number of sides in the dice. It then rolls the dice to give it an initial value
  • Accessor methods: Should return the fields of the Die class.
  • roll: Randomly generates a value within the number of sides of the dice. For example, if the dice has 6 sides you need to set value to be a random number between 1 and 6. If the dice has 12 sides, you need to set the value to a random number between 1 and 12 and so on… (Hint, this function is of type VOID)

Part 2 – The TestDie Class

            The Program

The program should create two instances of the Die class (each a six-sided die). One Die object is the computer’s die, the other Die object is the user’s die.

The program should have a loop that iterates 20 times. Each time the loop iterates, it should roll both dice. The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice).

As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins. After the loop performs all of its iterations, the program should display who was the grand winner, the computer or the user.

Finally, if the user wins, the program must ask the user to enter a nickname (The nickname must be at least 1 character and at most 5 characters), keep asking to user to enter a name while it is not valid. Once the user enters their nickname, append their nickname and number of wins to a file called “scores.txt”. Opening this file will contain the score of every player that has won the game.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here are the two classes, i have commented the code as much as possible. Hope it helps you

let me know for any quesitons

======================================================================================

import java.util.Random;

public class Die {

    //sides: represents how many sides a dice has.
    private int sides;
    //value: represents the current value that was rolled in the dice.
    private int value;

    //Constructor: takes one parameter that represents the number of sides in the dice.
    public Die(int sides) {
        this.sides = sides;
        //It then rolls the dice to give it an initial value
        roll();

    }

    //roll: Randomly generates a value within the number of sides of the dice.
    public void roll() {
        value = new Random().nextInt(sides) + 1;
    }

    //getter method to get the value
    public int getValue() {
        return value;
    }

    // getter method to get the number of sides
    public int getSides() {
        return sides;
    }
}

======================================================================================

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class TestDie {

    public static void main(String[] args) {

        //The program should create two instances of the Die class (each a six-sided die).
        // One Die object is the computer’s die, the other Die object is the user’s die.
        Die computerDie = new Die(6);
        Die userDie = new Die(6);

        int computerWins = 0;
        int userWins = 0;
        int draw = 0;

        // run a for loop 20 times for each game
        for (int game = 1; game <= 20; game++) {
            computerDie.roll();
            userDie.roll();
            System.out.println("Game - " + game);
            System.out.println("Computer rolled: " + computerDie.getValue());
            System.out.println("User rolled:     " + userDie.getValue());
            if (userDie.getValue() > computerDie.getValue()) {
                System.out.println("User won!");
                userWins += 1;
            } else if (userDie.getValue() < computerDie.getValue()) {
                System.out.println("Computer wins!");
                computerWins += 1;
            } else {
                System.out.println("Its a draw");
            }
            System.out.println("");
        }

        if (computerWins > userWins) {
            System.out.println("User has won this game");
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter your nick name: "); // ask user to enter nick name
            String nickName = scanner.nextLine();
            writeToFile(nickName, userWins);  // calls this mehtod to write it to file
        } else if (computerWins > userWins) {
            System.out.println("Computer wins the game");
        } else {
            System.out.println("It's was a tie!");
        }
    }

    // if user wins, user nickname and win count are written to a file
    private static void writeToFile(String nickName, int userWins) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("scores.txt"));
            writer.write(nickName + " Wins: " + userWins);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

======================================================================================

Add a comment
Know the answer?
Add Answer to:
Can anyone help me with this java program? We are still very early with lessons so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WITHOUT FUNCTIONS. Could anyone help me solve this problem not using the function ? Thanks a...

    WITHOUT FUNCTIONS. Could anyone help me solve this problem not using the function ? Thanks a lot. Pig Dice Game Pig is a simple two player dice game, played with one die. The first player to reach or surpass 50 is the winner. Each player takes a turn rolling the dice. They add to the pot with each roll, having to decide to roll again and increase the pot, or cash out. The risk being they could lose the amount...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game....

    Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game. It is played through dice. Rule for scoring The player who rolls higher number gets one point. If both players roll the same number, it is considered a draw and no one gets a point. Dice Specification There are two kinds of dice: normal die, represented by Die class. loaded die, represented by the loadedDie class. Classes Die class Die class has a member...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • Java How to Program, Early Objects ISBN-13: 9780133813432 Project Name: C2800_Proj1_RaceGame Source File Name: (Submit zip...

    Java How to Program, Early Objects ISBN-13: 9780133813432 Project Name: C2800_Proj1_RaceGame Source File Name: (Submit zip of these) RaceGame.java (contains main) RaceCar.java                                                                                                                                                              RaceGame is a text based car racing game. The user is car #1 and the computer is car #2. A car is drawn using 2 lines. The number on the first line is 1 for the user and 2 for the computer. __/1\__ -O---O- When the car has moved down the track, print underscore’s on the second line...

  • In Python, design a program that simulates the roll of a pair of dice (two dice)...

    In Python, design a program that simulates the roll of a pair of dice (two dice) and calculates the probability that various number combinations will be rolled. Let the user enter a number. This is the number of times they will roll the dice. You will roll the dice in a loop and keep track of the number of times that various rolls occur. When the loop has completed, your program should produce a table that shows the value of...

  • JAVA We will write a psychic game program in which a player guesses a number randomly...

    JAVA We will write a psychic game program in which a player guesses a number randomly generated by a computer. For this project, we need 3 classes, Player, PsychicGame, and Driver. Description for each class is as following: Project Folder Name: LB05 1. Player class a.Input - nickName : nick name of the player - guessedNumber : a number picked for the player will be assigned randomly by a computer later. b. Process - constructor: asks a user player’s nickName...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • ( Java Programming ) Write an application to simulate the rolling of two dice. The application...

    ( Java Programming ) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT