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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply