Graderpractice for programmers

Back to 254275 OOP - Labs 6-10 Review

5. Add a method for canCarry

Problem

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);
    }
}

Examples

Input

Tornado GR4
2
2
5000

Output

The Tornado GR4 can carry weapons of 2,000 kg

Input

Lockheed Martin F-35 Lightning II
1
1
1100

Output

The Lockheed Martin F-35 Lightning II cannot carry weapons of 2,000 kg

Submit your solution

java