Activity 1 • Design & implement a class named Coin with the following fields & m
ID: 3674340 • Letter: A
Question
Activity 1
• Design & implement a class named Coin with the following fields & methods
o sideUp – an instance variable indicating the face value of the coin (e.g., “Heads” or “Tails”)
o coinValue – an instance variable holding the actual coin value – $0.25 (quarter,) $0.10 (dime) and $0.05 (nickel)
o Coin() –
A no-arg constructor
randomly assign the side of the coin that is facing up (“Heads” or “Tails”)
initialize it as a quarter
o Coin(amount) –
constructor to set the coin value as quarter, dime or nickel
randomly assign the side of the coin that is facing up
o Coin(amount, face) –
constructor to set the coin as quarter, dime or nickel
set its sideUp to the face value
o toss()
tossing the coin
o getSideUp() –
getter function
returns the value of the sideUp field
Activity 2 • Design & implement a Java class named Uber (taxi-like service) with the following features -
o Schedule a pickup by specifying
Name of customer
Pickup date & time (use the date found in import java.util.Date)
Number of passengers (to be picked up)
Distance (destination in term of whole miles, to make the class easier to implement)
o Compute the total cost of the trip and display cost to console $2 per mile for the 1st two passengers $5 per additional passenger (starting with the 3rd passenger)
Activity 3
• Implement a Driver class with the following functionalities –
o main() Menu-driven application allowing an user to –
• Toss a coin for a specified number of times & display its outcome o For example, an user could choose to toss the coin for 20 times o display the actual outcome: 12 “heads” & 8 “tails” (this is an example only)
• Play the “Price-is-Right” game o Start 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 have occurred (game over)
• Total of more than $0.75 (User won!)
• Schedule a ride for one of three Uber cars (4, 5 & 6 passengers)
o A car can only be scheduled if it’s available
o 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
Explanation / Answer
Activity 1:
import java.io.*;
import java.util.*;
public class Coin() {
public static void main(String args[]) throws Exception {
intcoinval; String sideup;
int ch_side,ch_cval;
switch(ch_side) {
case 1: “Heads”; break;
case 2: “Tails”; break;
}
switch(ch_cval) {
case 1: quarter = $0.25; break;
case 2 : dime = $0.10; break;
case 3: nickel = $0.05; break;
}
void Coin() {
int cval; String side;
if(sideup == “Heads”)
side = “Heads”;
else
side = “Tails”;
cval = quarter;
System.out.println(“ side is:” + side);
System.out.println(“Coin value is:” + cval); }
void Coin(int amount) {
int cval; String side;
cval = amount;
if(sideup == “Heads”)
side = “Heads”;
else
side = “Tails”;
System.out.println(“ side is:” + side);
System.out.println(“Coin value is:” + cval); }
void Coin(int amount, Stringface) {
int this.cval = amount;
String this.side = face;
}
void toss() {
int h=0,t=1;
Random random = new Random();
int side = random.nextInt(2);
if(side = = 0)
System.out.println(“Heads”);
else System.out.println(“Tails”);
}
void getsideUp() {
return this.side;
}
} }
Activity 2:
import java.io.*;
import.java.util.*;
public class Uber {
public static void main(String args[]) throws Exceptions {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter customer name :”);
String customer =br.readLine();
System.out.println(“Customer name is:” + customer);
Date date = new Date();
System.out.println(“ Date is: “ + date.toString());
System.out.println(“Enter No of passengers:”);
int n = br.readLine();
System.out.println(“ No of passengers: “ + n);
System.out.println(“Enter distance in miles:”);
int dist = br.readLine();
System.out.println(“Distance is:” + dist);
int cost = $2;
if(n>=3){
cost = cost +$5;
else cost; }
int total_cost = cost;
System.out.println(“total cost is :” + total_cost);
}
}
Activity 3:
import java.io.*;
import java.util.*;
public class User {
public static void main(String args[]) throws Exception {
void toss() {
int h=0,t=1,outcome,outcome1;
Random random = new Random();
int side = random.nextInt(2);
for(int n=1;n<=20;n++) {
if(side == 0)
outcome = h++;
else
outcome1 = t++;
}
System.out.println(“total outcomes : “ + outcome + “Heads” + outcome1 + “Tails” );
}
void priceisRight() {
int cost=$0;
int n=4;
int cval[3] = {quarter = $ 0.25; dime = $ 0.10;nickel = $ 0.05};
for(n=1;n<=4;n++) {
if(cval == quarter)
cost = cost + quarter;
else if(cval == dime)
cost = cost + dime
else cost = cost+nickel;
}
int total_cost = cost;
if(total_cost>$0.75)
System.out.println(“Won”);
else
System.out.println(“Lose”);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.