Write a static method speedingTicket that decides whether a given driver should
ID: 3713376 • Letter: W
Question
Write a static method speedingTicket that decides whether a given driver should be given a speeding ticket from a police officer. The method accepts three parameters: the driver's car speed in miles per hour, as an integer; the current speed limit, as an integer; and whether or not the police officer has eaten a donut today, as a true/false value. (A police officer that has eaten a donut is happy, so he/she is less likely to give the driver a ticket.)
Your method should return true if the driver should receive a speeding ticket, and false if not. A driver should be given a speeding ticket if any of the following conditions are met:
The officer has eaten a donut (true) and the driver's speed is at least 10 mph over the speeding limit.
The officer has not eaten a donut (false) and the driver's speed is at least 5 mph over or under the limit.
The driver is going 100 mph or faster, regardless of the speed limit or donut status.
You may assume that the integers passed as parameters will be non-negative. Here are some example calls to the method and their resulting return values:
Call Value Returned
speedingTicket(58, 55, false) false speedingTicket(51, 55, false) false speedingTicket(42, 35, false) true speedingTicket(30, 35, false) true speedingTicket(45, 30, true) true speedingTicket(49, 40, true) false speedingTicket(22, 30, true) false speedingTicket(102, 15, false) true speedingTicket(101, 98, true) true
Explanation / Answer
import java.util.Scanner;
public class ticket {
public static boolean speedingTicket(int cspeed,int limit,boolean donut)
{
System.out.println("SPEEDING TICKET");
if(donut==true && cspeed>limit+10)
return true;
else if(donut==false && (cspeed>limit+5 || cspeed<=limit-5))
return true;
else if(cspeed>=100)
return true;
else
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int cspeed,limit;
boolean donut;
Scanner sc=new Scanner(System.in);
System.out.print("Enter Car speed in miles per hour=");
cspeed=sc.nextInt();
System.out.print("Enter current speed limit=");
limit=sc.nextInt();
System.out.println("Has police officer eaten a donut today?");
donut=sc.nextBoolean();
//CALLING STATIC FUNCTION SPEEDINGTICKET
System.out.println(speedingTicket(cspeed,limit,donut));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.