EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 8, Problem 2GZ

a.

Explanation of Solution

Program:

File name: “Die.java

//Define a class named Die

public class Die

{

    //Declare the private variables and initialize the value

    private int value;

    private static final int HIGHEST_DIE_VALUE = 6;

    private static final int LOWEST_DIE_VALUE = 1;

    //Define a Die method

    public Die()

    {

        //Calculate the value

        value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +

         LOWEST_DIE_VALUE);

    }

    //Define a getValue method

    public int getValue()

    {

        //Return the value

        return value;

    }

}

File name: “FiveDice2.java

//Define a class named FiveDice2

public class FiveDice2

{

    //Define a main method

    public static void main(String[] args)

    {      

        //Declare a variable and initialize the value

        final int NUM = 5;

        //Store the five dice rolled value for computer

        Die[] comp = new Die[NUM];

        //Store the five dice rolled value for player

        Die[] player =  new Die[NUM];

        //Declare a variable

        int x;

        //For loop to be executed until x exceeds NUM

        for(x = 0; x < NUM; ++x)

        {

            //Allocating memory for the array objects

            comp[x] = new Die();

            player[x] = new Die();

        }  

        //Declare the variables

        int compMatch, playerMatch;

        //Get computer name

        String computerName = "Computer";

        //Get player name

        String playerName = "You";

        //Display the message

        display(computerName, comp, NUM);

        display(playerName, player, NUM);

        //Calls the method to find same numbers for computer

        compMatch = howManySame(comp, NUM);

        //Calls the method to find same numbers for player

        playerMatch = howManySame(player, NUM);

        //If computer has one of a kind

        if(compMatch == 1)

            //Print the result

            System.out.println("Computer has nothing");

//Else computer has a pair, three, four, or five of a //kind

        else

            //Print the result

 System.out.println("Computer has " + compMatch + " of a kind");

        //If player has one of a kind

        if(playerMatch == 1)

            //Print the result

            System.out.println("You have nothing");

        //Else player has a pair, three, four, or five of a kind

        else

            //Print the result

System.out.println("You have " + playerMatch + " of a kind");

        //If any higher combination of computer beats a lower //one of player

        if(compMatch > playerMatch)

            //Print the result

            System...

b.

Explanation of Solution

Program:

File name: “Die.java

//Define a class named Die

public class Die

{

    //Declare the private variables and initialize the value

    private int value;

    private static final int HIGHEST_DIE_VALUE = 6;

    private static final int LOWEST_DIE_VALUE = 1;

    //Define a Die method

    public Die()

    {

        //Calculate the value

        value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +

         LOWEST_DIE_VALUE);

    }

    //Define a getValue method

    public int getValue()

    {

        //Return the value

        return value;

    }

}

File name: “FiveDice3.java

//Define a class named FiveDice3

public class FiveDice3

{

    //Define a main method

    public static void main(String[] args)

    {

        //Declare a variable and initialize the value

        final int NUM = 5;

        //Store the five dice rolled value for computer

        Die[] comp = new Die[NUM];

        //Store the five dice rolled value for player

        Die[] player =  new Die[NUM];

        //Declare a variable

        int x;

        //For loop to be executed until x exceeds NUM

        for(x = 0; x < NUM; ++x)

        {

            //Allocating memory for the array objects

            comp[x] = new Die();

            player[x] = new Die();

        }

        //Declare the variables

        int compMatch, playerMatch;

        int compHigh, playerHigh;

        //Get computer name

        String computerName = "Computer";

        //Get player name

        String playerName = "You";

        //Display the message

        display(computerName, comp, NUM);

        display(playerName, player, NUM);

        //Calls the method to find same numbers for computer

        compMatch = howManySame(comp, NUM);

        //Calls the method to find same numbers for player

        playerMatch = howManySame(player, NUM);

        //Compute the value

        compHigh = compMatch / 10;

        playerHigh = playerMatch / 10;

        compMatch = compMatch % 10;

        playerMatch = playerMatch % 10;

        //If computer has one of a kind

        if(compMatch == 1)

            //Print the result

            System.out.println("Computer has nothing");

//Else computer has a pair, three, four, or five of a //kind

        else

            //Print the result

System.out.println("Computer has " + compMatch + " of a kind");

        //If player has one of a kind

        if(playerMatch == 1)

            //Print the result

            System.out.println("You have nothing");

        //Else player has a pair, three, four, or five of a kind

        else

            //Print the result

            System.out.println("You have " + playerMatch + " of a kind");

//If any higher combination of computer beats a lower one of player

        if(compMatch > playerMatch)

            //Print the result

            System.out.println("Computer wins");

        //Else

        else

        //If any higher combination of player beats

        //a lower one of computer

        if(compMatch < playerMatch)

        //Print the result

        System.out.println("You win");

        //Else

        else

        {

//If values of computer are greater than player's value

            if(compHigh > playerHigh)

                //Print the result

System...

Blurred answer
Students have asked these similar questions
Assignment Description This program will simulate part of the game of Yahtzee! This is a dice game that involves rolling five dice and scoring points based on what show up on those five dice. The players would record their scores on a score card, and then total them up, and the player with the larger total wins the game. A Yahtzee score card has two portions: The upper portion has spaces for six scores, obtained by adding up all of the 1's, 2's, 3's, etc. The lower portion has special scores for various combinations: Three of a kind -- at least 3 dice are the same number;the score is the sum of all five dice Four of a kind -- at least 4 dice are the same number;the score is the sum of all five dice Small straight -- four consecutive numbers are represented, e.g. 2345;the score is 25 points Large straight -- five consecutive numbers are represented, e.g. 23456;the score is 30 points Full House -- three of one kind, two of another; the score is 30 points Yahtzee! -- five of a kind; the…
In Chapter 4, you created a Die application that randomly “throws” five dice for the computer and five dice for the player. The application displays the values. Modify the application to decide the winner based on the following hierarchy of Die values. Any higher combination beats a lower one—for example, five of a kind beats four of a kind. Five of a kind Four of a kind Three of a kind A pair For this game, the dice values do not count; for example, if both players have three of a kind, it’s a tie, no matter what the values of the three dice are. Additionally, the game does not recognize other poker hand type combinations such as a full house (three of a kind plus two of a kind) or a straight (sequential values). Figure 8-21 shows a sample execution. Save the application as FiveDice2.java.   Improve the FiveDice2 game so that when both players have the same combination of dice, the higher value wins. For example, two 6s beats two 5s. Save the application as FiveDice3.java.
Travel Tickets Company sells tickets for airlines, tours, and other travel-related services. Because ticket agents frequently mistype long ticket numbers, Travel Tickets has asked you to write an application that indicates invalid ticket number entries. The class prompts a ticket agent to enter a six-digit ticket number. Ticket numbers are designed so that if you drop the last digit of the number, then divide the number by 7, the remainder of the division will be identical to the last dropped digit. Accept the ticket number from the agent and verify whether it is a valid number. Test the application with the following ticket numbers: . 123454; the comparison should evaluate to true . 147103; the comparison should evaluate to true . 154123; the comparison should evaluate to false Save the program as TicketNumber.java.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
CPP Function Parameters | Returning Values from Functions | C++ Video Tutorial; Author: LearningLad;https://www.youtube.com/watch?v=WqukJuBnLQU;License: Standard YouTube License, CC-BY