Activity 1 Create a class named Coin with the following fields & methods - sideU
ID: 3673384 • 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
Outputs: http://postimg.org/image/5ypxkimlx/
http://postimg.org/image/e2y1p9911/
http://postimg.org/image/61eel3fn9/
http://postimg.org/image/72a1wh791/
http://postimg.org/image/vrpsgvks5/
Explanation / Answer
// Because this question contains multiple questions, I am writing the code for only 1st activity
class Coin{
private String sideUp;
private String coinValue;
public Coin(){
toss();
coinValue = "quarter";
}
public Coin (String amount) {
toss();
coinValue = amount;
}
public Coin(String amount, String face){
sideUp = face;
coinValue = amount;
}
public void toss () {
Random rand = new Random();
int side = rand.nextInt(2);
if (side == 0) {
sideUp = "heads";
} else {
sideUp = "tails";
}
}
public String getSideUp(){
return sideUp;
}
public String getCoinValue{
return coinValue;
}
////////// Driver Class //////////
import java.util.Scanner;
class Driver {
static int numberOfToss;
public static void main (String args[]) {
Scanner in = new Scanner (System.in);
System.out.println("Enter no of tosses");
numberOfToss = in.nextInt();
Coin myCoin = new Coin();
System.out. println("Tosding the coin for "+numberOfTosses+" times");
int no_of_heads = 0;
for( int I = 0; I < numberOfToss; I ++) {
myCoin.toss();
System.out.println(" Toss: "+myCoin.getSideUp());
if ( "heads".equals(myCoin.getSideUp()))
no_of_heads ++;
}
System.out.println(no_of_heads+" Heads & "+(numberOfToss - no_of_heads)+" Tails);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.