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

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

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

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

  • ( 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...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

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
ADVERTISEMENT