Activity 1 Create a class named Coin with the following fields & methods - sideU
ID: 3672377 • Letter: A
Question
Activity 1
Create a class named Coin with the following fields & methods -
sideUp – an instance variable indicating the face value of the coin (e.g., “Heads” or “Tails”)
coinValue – an instance variable holding the actual coin value – $0.25 (quarter,) $0.10 (dime) and $0.05 (nickel)
Coin() –
A no-arg constructor
randomly assign the side of the coin that is facing up (“Heads” or “Tails”)
initialize it as a quarter
Coin(amount) –
constructor to set the coin value as quarter, dime or nickel
randomly assign the side of the coin that is facing up
Coin(amount, face) –
constructor to set the coin as quarter, dime or nickel
set its sideUp to the face value
toss()
tossing the coin
getSideUp() –
getter function
returns the value of the sideUp field
getCoinValue()
Activity 2
Design & implement a Java class named Uber (taxi-like service) with the following features -Schedule a pickup service by specifying
Name of customer
Pickup date & time
Number of passengers (to be picked up)
Distance (destination in term of miles, to make the class easier to implement)
Compute the total cost of a trip and display cost to console
$2 per mile for the 1st two passengers
$5 per additional passenger (starting with the 3rd passenger)
Add one additional requirement (feature) of your choice (for the class)
See your course instructor if you need help on this
Include 1 paragraph in your write-up to discuss it. What’s it?
This activity allows each student to design and implement a class from the ground up.
Activity 3
Implement a Driver class with the following functionalities –main()Menu-driven application allowing an user to –Toss a coin for a specified number of times & display its outcome
For example, an user could choose to toss a coin for 20 times
display the actual outcome: 12 “heads” & 8 “tails” (this is an example only)
Play the “Price-is-Right” gameStart a game with $0 in a jackpot as the game begins
Simulate a randomly tossing one of three coins (quarter, dime & nickel)
Add the value of the selected coin to the jackpot (e.g., $0.05, $0.10 or $0.25)
Repeat this process until
A maximum of 4 tosses has occurred (game over)
Total of more than $0.75 (User won!)
Schedule a ride for one of three Uber cars (4, 5 & 6 passengers)
A car can only be scheduled if it’s available
Display scheduled info to console once a car has been scheduled
Allow user to continue using this program for as long as they choose to
In general, students should also consider including & utilizing -
additional fields, methods, constructors, getters & setters to a class (if needed)
output formats, such as currency for any dollar amounts (values)
Scanner or JOptoinPane class for inputs
methods to make overall coding easy to read. main() should be calling others to perform tasksinput validations.
Explanation / Answer
import java.util.Random;
public class CoinTossSimulator
{
class Coin
{
private String sideUp;
public Coin()
{
toss();
}
public void toss()
{
Random rand = new Random();
int value = rand.nextInt(2);
if (value == 0)
{
this.sideUp = "heads";
}
else
{
this.sideUp = "tails";
}
}
public String getSideUp()
{
return sideUp;
}
}
static final int NUMBER_OF_TOSSES = 20;
public static void main(String args[])
{
CoinTossSimulator coinTossSimulator = new CoinTossSimulator();
Coin myCoin = coinTossSimulator.new Coin();
System.out.println("The side initially facing up: "+ myCoin.getSideUp());
System.out.println("Now I will toss the coin " + NUMBER_OF_TOSSES+ " times.");
int headCount = 0;
for (int i = 0; i < NUMBER_OF_TOSSES; i++)
{
myCoin.toss();
System.out.println("Toss: " + myCoin.getSideUp());
if ("heads".equals(myCoin.getSideUp()))
{
headCount++;
}
}
System.out.println("Heads facing up: " + headCount);
System.out.println("Tails facing up: " + (NUMBER_OF_TOSSES - headCount));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.