Graderpractice for programmers

Back to 254275 OOP - Labs 6-10 Review

4. Add comparison methods

Problem

Reuse your Printer class from question 3. Add 2 new methods: a) isColourLaser() returns true if the printer is colour and is type laser; b) isCheaperThan() method takes another Printer and returns true if the first printer has a price lower than the passed parameter.

Test code:

class MT2cTask4 {
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        Printer p1 = input();
        Printer p2 = input();

        if (p1.isColourLaser()) {
            System.out.println(p1.getFullName() + " is a color laser");
        }
        else {
            System.out.println(p1.getFullName() + " is NOT a color laser");
        }

        if (p1.isCheaperThan(p2)) {
            System.out.println(p1.getFullName() + " is cheaper than " + p2.getFullName());
        }
        else {
            System.out.println(p1.getFullName() + " is more expensive than " + p2.getFullName());
        }
    }

    private static Printer input() {
        String brand = sc.nextLine();
        String model = sc.nextLine();
        char type = sc.nextLine().charAt(0);
        boolean colour = sc.nextLine().equalsIgnoreCase("C");
        int price = Integer.parseInt(sc.nextLine());
        return new Printer(brand, model, type, colour, price);
    }
}

Examples

Input

Canon
G3000
t
c
8500
HP
Officejet 1000
i
c
6100

Output

Canon G3000 is NOT a color laser
Canon G3000 is more expensive than HP Officejet 1000

Submit your solution

java