Object-Oriented Programming with Java, part I – Exercise 15

This program asks a user to input their age and then returns whether they have reached the age of majority (18) yet or not.

My Code

import java.util.Scanner;

public class AgeOfMajority {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        // Type your program here 
        System.out.println("How old are you? ");
        int age = Integer.parseInt(reader.nextLine());
        
        if (age < 18){
            System.out.println("You have not reached the age of majority yet!");
        } else {
            System.out.println("You have reached the age of majority!");
        }
    }
}

Model Code

import java.util.Scanner;

 

public class AgeOfMajority {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

 

        // Type your program here 

        System.out.print("How old are you?");

        int age = Integer.parseInt(reader.nextLine());

        System.out.println(""); // tyhjä rivi

        if (age < 18) {

            System.out.println("You have not reached the age of majority yet!");

        } else {

            System.out.println("You have reached the age of majority!");

        }

    }

}

The major difference between my code and the model code is the use of a System.out.println(“”); to create some whitespace.

Leave a comment