Question

Include an unfair or biased dice. dice doesnt have equal opportunities (in regular dice it is...

Include an unfair or biased dice. dice doesnt have equal opportunities (in regular dice it is 1/6) DONT implement unbiased roll, reorganize the program to include BiasedDicePair class, add the new class to this project.

PairOfDiceDriver.java


public class PairOfDiceDriver {

   public static void main(String[] args) {
       PairOfDice obj = new PairOfDice();
       obj.roll();
       System.out.println(obj);
   }

}


PairOfDice.java


public class PairOfDice {
   private int face1, face2;
   public PairOfDice() {
      
   }
   public void roll() {
       face1 = (int)(Math.random()* 6 + 1);
       face2 = (int)(Math.random()* 6 + 1);
   }
   public int getSum() {
       return face1 + face2;
   }
   public String toString(){
       return face1+" : "+face2+" = "+getSum();
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class PairOfDice {
    private int face1, face2;

    public PairOfDice() {

    }

    public void roll() {
        face1 = (int) (Math.random() * 6 + 1);
        face2 = (int) (Math.random() * 6 + 1);
    }

    public int getSum() {
        return face1 + face2;
    }

    public String toString() {
        return face1 + " : " + face2 + " = " + getSum();
    }
}

class BiasedDicePair {
    private int face1, face2;

    public BiasedDicePair() {

    }

    public void roll() {
        // suppose the rolls have below possibilities
        // 1 => 20%
        // 2 => 20%
        // 3 => 10%
        // 4 => 20%
        // 5 => 20%
        // 6 => 10%

        int x1 = (int) (Math.random() * 10);
        if(x1 < 2) {
            x1 = 1;
        } else if(x1 < 4) {
            x1 = 2;
        } else if(x1 < 5) {
            x1 = 3;
        } else if(x1 < 7) {
            x1 = 4;
        } else if(x1 < 9) {
            x1 = 5;
        } else {
            x1 = 6;
        }
        int x2 = (int) (Math.random() * 10);
        if(x2 < 2) {
            x2 = 1;
        } else if(x2 < 4) {
            x2 = 2;
        } else if(x2 < 5) {
            x2 = 3;
        } else if(x2 < 7) {
            x2 = 4;
        } else if(x2 < 9) {
            x2 = 5;
        } else {
            x2 = 6;
        }

        face1 = x1;
        face2 = x2;
    }

    public int getSum() {
        return face1 + face2;
    }

    public String toString() {
        return face1 + " : " + face2 + " = " + getSum();
    }



}

public class PairOfDiceDriver {
    public static void main(String[] args) {
        BiasedDicePair obj = new BiasedDicePair();
        obj.roll();
        System.out.println(obj);
    }
}


Hi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Include an unfair or biased dice. dice doesnt have equal opportunities (in regular dice it is...
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
  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

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

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1...

    (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1,000,000 times) Given below that the class die, oddstester, and gameSimulator(partially complete and the bold missing parts need to be done), need to find the probability of winning game 1 and 2 (after the 1 million plays) hence the getWins and...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • I need the following code to keep the current output but also include somthing similar to...

    I need the following code to keep the current output but also include somthing similar to this but with the correct details from the code. Truck Details: Skoda 100 Nathan Roy 150.5 3200 Details of Vehicle 1: Honda, 5 cd, owned by Peter England Details of Vehicle 2: Skoda, 100 cd, owned by Nathan Roy, 150.5 lbs load, 3200 tow --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- class Person {     private String name;        public Person()     {        name="None";     }       ...

  • What is the output of this program? class A { public int i; private int j;...

    What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

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
Active Questions
ADVERTISEMENT