logo CodeStepByStep logo

AgeCheck2

Language/Type: Java if/else
Related Links:

Modify your solution code from the previous problem, AgeCheck, as follows. First, rename your program's class to AgeCheck2. Next, modify your message method to have the following code:

// Possibly prints some message(s) to the user based on the given age
public static void message(int age) {
    if (age >= 21) {
        System.out.println("I can legally purchase alcohol!");
    } else if (age >= 17) {
        System.out.println("I can purchase a ticket to an R-rated movie.");
    } else if (age >= 16) {
        System.out.println("I can get my driver's license!");
    }
}

For some age(s), the message(s) printed are not accurate. (In some cases, it should print no messages; in some cases, one message; and in others, many messages.) Improve the usage of if and else in this method to behave properly.

Class: Write a complete Java class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.