Java Programming, Loose-leaf Version
Java Programming, Loose-leaf Version
8th Edition
ISBN: 9781337685917
Author: Joyce Farrell
Publisher: Cengage Learning
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
1.[30 pts] Computers generate color pictures on a video screen or liquid crystal display by mixing three different colors of light: red, green, and blue. Imagine a simple scheme, with three different lights, each of which can be turned on or off, projecting onto a glass screen: We can create eight different colors based on the absence (0) or presence (1) of light sources R,G and B: R G B Color 0 0 0 Black 0 0 1 Blue 0 1 0 Green 0 1 1 Cyan 1 0 0 Red 1 0 1 Magenta 1 1 1 0 Yellow 1 White 1 Each of these colors can be represented as a bit vector of length 3, and we can apply Boolean operations to them. a. The complement of a color is formed by turning off the lights that are on and turning on the lights that are off. What would be the complement of each of the eight colors listed above? b. Describe the effect of applying Boolean operations on the following colors: Λ 1. Red(100) ^ Magenta(101)= Blue(001) 2. Bue(001) | Green(010)= 3. Yellow(100) & Cyan(011)= 2.[30 pts] Perform the following…
D. S. Malik, Data Structures Using C++, 2nd Edition, 2010
Methods (Ch6) - Review 1. (The MyRoot method) Below is a manual implementation of the Math.sqrt() method in Java. There are two methods, method #1 which calculates the square root for positive integers, and method #2, which calculates the square root of positive doubles (also works for integers). public class SquareRoot { public static void main(String[] args) { } // implement a loop of your choice here // Method that calculates the square root of integer variables public static double myRoot(int number) { double root; root=number/2; double root old; do { root old root; root (root_old+number/root_old)/2; } while (Math.abs(root_old-root)>1.8E-6); return root; } // Method that calculates the square root of double variables public static double myRoot(double number) { double root; root number/2; double root_old; do { root old root; root (root_old+number/root_old)/2; while (Math.abs (root_old-root)>1.0E-6); return root; } } Program-it-Yourself: In the main method, create a program that…
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:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
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