Graderpractice for programmers

254275 OOP - Mini Test 2 (Sec 1, 2 & 5)

Awaiting submission
Write a class named Aeroplane with fields for "model", "engines" and "seats".

Test code:

class Task1 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        System.out.print("The " + ap.getModel() + " has ");
        System.out.print(ap.getEngines() + " engines and ");
        System.out.println(ap.getSeats() + " seats");
    }

    public static Aeroplane input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        return new Aeroplane(model, engines, seats);
    }
}
Awaiting submission
Add a new method to the Aeroplane named toString.

Test code:

class Task2 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        System.out.println(ap.toString());
    }

    public static Aeroplane input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        return new Aeroplane(model, engines, seats);
    }
}
Awaiting submission
A jumbo jet has 250 or more seats. Add a method named isJumbo that returns true if the aeroplane is a jumbo jet.

Test code:

class Task3 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        if (ap.isJumbo()) {
            System.out.println(ap.getModel() + " is a jumbo jet");
        } else {
            System.out.println(ap.getModel() + " is not a jumbo jet");
        }
    }

    public static Aeroplane input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        return new Aeroplane(model, engines, seats);
    }
}
Awaiting submission
A fighter plane is an aeroplane designed for military combat. A fighter can carry up to a maximum weight of weapons. Create the Fighter class as a subclass of Aeroplane, with an extra field for "maximumWeaponsWeight".

Test code:

class Task4 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        System.out.print("The " + ap.getModel() + " has ");
        System.out.print(ap.getEngines() + " engines and ");
        System.out.println(ap.getSeats() + " seats");
    }

    public static Fighter input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        int weaponsWeight = Integer.parseInt(sc.nextLine());
        return new Fighter(model, engines, seats, weaponsWeight);
    }
}
Awaiting submission
Add a method called canCarry to the Fighter class that takes one parameter (int) and returns true if it is less than or equal to the maximum weapons weight of the fighter plane.

Test code:

class Task5 {

    public static void main(String[] args) {
        Fighter f = input();
        System.out.print("The " + f.getModel());
        if (f.canCarry(2000)) {
            System.out.println(" can carry weapons of 2,000 kg");
        } else {
            System.out.println(" cannot carry weapons of 2,000 kg");
        }
    }

    public static Fighter input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        int weaponsWeight = Integer.parseInt(sc.nextLine());
        return new Fighter(model, engines, seats, weaponsWeight);
    }
}