Page 1 of 1

JAVA please help with demo 2 and 3 in the StructProgApplication class public class StructProgApplication { public st

Posted: Mon May 09, 2022 6:13 am
by answerhappygod
JAVA please help with demo 2 and 3 in the StructProgApplication
class
public class StructProgApplication
{
public static void main(String [] args){
Scanner keyboard = new
Scanner(System.in);
System.out.println("\f");
//************* Demo 1
***********************
// We will use the sleepIn method in
the CodingBat class
// to tell us if we can sleep in
tomorrow
System.out.println("Is tomorrow a
weekday? Please enter true or false");
boolean weekday =
keyboard.nextBoolean();
System.out.println("Is tomorrow a
holiday? Please enter true or false");
boolean holiday =
keyboard.nextBoolean();

// notice, to invoke sleepIn, we have
to precede it with the
if (CodingBat.sleepIn(weekday,
holiday))
System.out.println("You
can sleep in tomorrow!");
else

System.out.println("Better set your alarm!");

//************** End Demo 1
******************/
//**********Demo
2****************/
// We will use the sumDigits method in
the CodingBat class

System.out.println("Enter an integer
and I will tell you the sum of its digits! ");

// declare int num and read from the
keyboard

// use the sumDigits method in
CodingBat class to print the sum of the digits of num


//************** End Demo 2
******************/
/************* Demo 3
***********************
// We will use the caughtSpeeding
method in the CodingBat class
// to tell us what kind of
ticket we get

System.out.println("Greetings,
driver,");
System.out.println("Do you know how
fast you were going? (be honest!) ";
int speed = keyboard.nextInt();
System.out.println("Is today your
birthday? Please enter true or false");
boolean holiday =
keyboard.nextBoolean();

// use the caughtSpeeding method in the
CodingBat class to print how big a ticket you get




//************** End Demo 3
******************/


}
}
here is the sumDighits method from codingbat class
public static int sumDigits(int n) {
if (n < 10){
return n;
}
return (n%10) + sumDigits(n/10);
}
Here is the caughtspeeding method from the codingbat class
public static int caughtSpeeding(int speed, boolean
isBirthday) {
if (isBirthday){
if (speed <= 65)
//5mph more for birthday
return
0;
else if (speed >= 66
&& speed <= 85)
return
1;
else
return
2;
}
else {
if (speed <= 60)
//5mph more for birthday
return
0;
else if (speed >= 61
&& speed <= 80)
return
1;
else
return 2;
// fix this
}
}