Question

In: Computer Science

JAVA Homework 1) Create a die class. This is similar to the coin class , but...

JAVA Homework

1)

Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die.

Altogether you will have one attribute (face), and the following methods: constructor (calls roll), roll, getFace.

Test it by writing a main method in which you roll 2 dice. If you get exactly 7 you win, 11 you lose, anything else roll both dice again until you either win or lose. in order to verify your program put a println in the loop so that every time you roll the dice you print out the total value.

Solutions

Expert Solution

Die.java:


public class Die {

   private int face;

//constructor  
   public Die(){
       roll();
   }
  
   public void roll(){
       face=(int) Math.ceil((Math.random()*6));

//assigns a value between 1 and 6 to variable face
   }
  
   public int getFace(){
       return this.face;
   }
  
   public static void main(String args[]){
       Die d1;
       Die d2;
       int dieValue;
       for(int i=0;;i++){
           d1=new Die();
           d2=new Die();
           dieValue=d1.getFace()+d2.getFace();
          
           if(dieValue==7){
               System.out.println("You got "+dieValue+ "\n You won!! \n");
               break;
           }
          
           else if(dieValue==11){
               System.out.println("You got "+dieValue+ "\n You lost!! \n");
               break;
           }
          
           else
          
               System.out.println("You got "+ dieValue + " \n Rolling again! \n");
       }
   }
}


Sample run 1:
You got 4
Rolling again!

You got 10
Rolling again!

You got 7
You won!!


Sample run 2:
You got 5
Rolling again!

You got 11
You lost!!



Related Solutions

java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Create a coin and bill class Coin and Bill constructors generate the coinDenomination and billDenomination randomly...
Create a coin and bill class Coin and Bill constructors generate the coinDenomination and billDenomination randomly based on the number of denominations defined in the respective enums. The method getValue returns the value of a coin (for example 0.25 for a quarter) or the value of paper bill respectively. The method toString returns the String representation of the object; for example: “WASHINGTON landed TAILS” or “NICKEL landed TAILS”. I already have the coinDenomination and billDenomination just need help with the...
In java: -Create a class named Animal
In java: -Create a class named Animal
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Create a derived class that allows you to reset the number of sides on a die....
Create a derived class that allows you to reset the number of sides on a die. (Hint: You will need to make one change in diceType.) -- Completed in Part 1 Overload the << and >> operators. -- Completed in Part 1 Overload the +, -, and ==, <, > operators for your derived class. Overload the = operator. I need help with 3 and 4 of the question. I've already completed step 1 and 2 Note: You should test...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT