Graderpractice for programmers

254275 OOP - Mini Test 2 (re-run)

Awaiting submission
Write a class named Printer with members for brand, model, type (l=laser, i=inkject, t=thermal), colour (true or false) and price. The class needs a constructor and get methods for each member.

Test code:

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

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

        System.out.println("Brand: " + p.getBrand());
        System.out.println("Model: " + p.getModel());
        System.out.println("Type: " + p.getType());
        System.out.println("Colour: " + p.isColour());
        System.out.println("Price: " + p.getPrice() + " baht");
    }

    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);
    }
}
Awaiting submission
Reuse your Printer class from question 1. Add a method named getFullName() that returns the brand and the model (separated by a space).

Test code:

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

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

        System.out.println(p.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);
    }
}
Awaiting submission
Reuse your Printer class from question 2. Add a method named getTypeDescription that returns "laser", "inkjet", "thermal" as a String for the type description.

Test code:

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

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

        System.out.println(p.getFullName() + " is a " + p.getTypeDescription() + " printer");
    }

    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);
    }
}
Awaiting submission
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);
    }
}
Awaiting submission
NetworkPrinter is a subclass of Printer. NetworkPrinter has a string member named ipAddress. Write the NetworkPrinter class.

Test code:

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

    public static void main(String[] args) {
        NetworkPrinter np = input();

        System.out.println(np.getFullName() + " has the IP address " + np.getIPAddress());
    }

    private static NetworkPrinter 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());
        String ipAddress = sc.nextLine();
        return new NetworkPrinter(brand, model, type, colour, price, ipAddress);
    }
}
Awaiting submission
Reuse your NetworkPrinter class from question 5. Add a method named isNotSameIPAddress that takes another network printer and returns true if the 2 printers do not have the same IP address.

Test code:

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

    public static void main(String[] args) {
        NetworkPrinter np1 = input();
        NetworkPrinter np2 = input();

        System.out.print(np1.getFullName() + " and " + np2.getFullName());
        if (np1.isNotSameIPAddress(np2)) {
            System.out.println(" can be on the same network");
        }
        else {
            System.out.println(" can not be on the same network");
        }
    }

    private static NetworkPrinter 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());
        String ipAddress = sc.nextLine();
        return new NetworkPrinter(brand, model, type, colour, price, ipAddress);
    }
}