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 Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...
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...
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 a class named Animal
In java: -Create a class named Animal
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...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Create a driver class that tests this class and provide the UML. 2.) In a separate program, define DiscountPolicy as an interface instead of the abstract class. Create a driver class that tests...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
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