Question

In: Computer Science

(java) Write a class called CoinFlip. The class should have two instance variables: an int named...

(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to whatever was passed in as the parameter, it should also inistantiate r to be a new instance of the Random class. In addition to the constructor there should be a mutator method called flip which takes no parameters and randomly assigns the value of coin to either 0 or 1. There should be an accessor method that returns the current value of coin as an int.

Then write a code fragment that instantiates a CoinFlip object that is initially set to heads. Using the methods from above, flip the coin twice and print out the result each time.

Solutions

Expert Solution

coin.java

import java.io.*;
import java.util.*;

class coinFlip
{
   //member varaibles
private int coin;
   private Random r;
  
   //parameterized constructor
   public coinFlip(int n)
   {
       coin = n; //set coin value
       r = new Random(); //initiate Random class object to new instance
   }
  
  
   //mutator method
   public void flip()
   {
       coin = r.nextInt(2); //set random value in between 0 and 1
      
   }
  
   //accessor method
   public int coinValue()
   {
       return coin; //return coin value
   }
  
}
public class coin
{
public static void main(String args[]) throws Exception
{
   coinFlip obj = new coinFlip(1); //create new instance of class coinFlip
  
System.out.println(obj.coinValue()); //print coin value (head = 0 , tails = 1 )
  
   obj.flip();   //flip coin with mutator method
  
   System.out.println(obj.coinValue()); //print coin value again  
  
  
}
}

OUTPUT


Related Solutions

In Java The Order class should have:  Seven instance variables: the order number (an int),...
In Java The Order class should have:  Seven instance variables: the order number (an int), the Customer who made the order, the Restaurant that receives the order, the FoodApp through which the order was placed, a list of food items ordered (stored as an array of FoodItem), the total number of items ordered (an int), and the total price of the order (a double).  A class constant to set the maximum number of items that can be ordered...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that is unique and increments by 10, and an hourly wage. There should also be some constructors and mutators, and accessor methods.
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT