Graderpractice for programmers

254275 OOP - Mini Test 2 (Sec 3 & 4)

Awaiting submission
Write a class named Watch with members name, colour and price. The class needs a constructor and get methods for each member.

Test code:

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

    public static void main(String[] args) {
        Watch w = input();

        System.out.println("Watch name: " + w.getName());
        System.out.println("Colour: " + w.getColour());
        System.out.println("Price: " + w.getPrice() + " baht");
    }

    private static Watch input() {
        String name = sc.nextLine();
        String colour = sc.nextLine();
        int price = Integer.parseInt(sc.nextLine());
        return new Watch(name, colour, price);
    }
}
Awaiting submission
Reuse your Watch class from question 1. Add a method named toString that returns the name and the colour.

Test code:

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

    public static void main(String[] args) {
        Watch w = input();

        System.out.println(w.toString());
    }

    private static Watch input() {
        String name = sc.nextLine();
        String colour = sc.nextLine();
        int price = Integer.parseInt(sc.nextLine());
        return new Watch(name, colour, price);
    }
}
Awaiting submission
Reuse your Watch class from question 1. Add a method named getPriceRange that returns a String. If the price is less than 1000 then it is "Budget", if it is greater than 10000 then it is "Executive", else it is "Regular".

Test code:

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

    public static void main(String[] args) {
        Watch w = input();

        System.out.print(w.getName() + " is ");
        System.out.println(w.getPriceRange());
    }

    private static Watch input() {
        String name = sc.nextLine();
        String colour = sc.nextLine();
        int price = Integer.parseInt(sc.nextLine());
        return new Watch(name, colour, price);
    }
}
Awaiting submission
Reuse your Watch class from question 3. Add a method named isBudget that returns true if the watch is less than 1000 baht (0-999).

Test code:

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

    public static void main(String[] args) {
        Watch w = input();

        if (w.isBudget()) {
            System.out.println(w.getName() + " is under 1000 baht");
        }
        else {
            System.out.println(w.getName() + " is too expensive");
        }
    }

    private static Watch input() {
        String name = sc.nextLine();
        String colour = sc.nextLine();
        int price = Integer.parseInt(sc.nextLine());
        return new Watch(name, colour, price);
    }
}
Awaiting submission
Smartwatch is a subclass of Watch. Smartwatch has a String member named operatingSystem (e.g. "WatchOS" or "Android Wear"). Write the Smartwatch class.

Test code:

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

    public static void main(String[] args) {
        Smartwatch w = input();

        System.out.println("Watch name: " + w.getName());
        System.out.println("Operating System: " + w.getOperatingSystem());
    }

    private static Smartwatch input() {
        String name = sc.nextLine();
        String colour = sc.nextLine();
        int price = Integer.parseInt(sc.nextLine());
        String operatingSystem = sc.nextLine();
        return new Smartwatch(name, colour, price, operatingSystem);
    }
}
Awaiting submission
Reuse your Smartwatch class from question 5. Add a method named isAppleWatch that returns true if the operating system is "WatchOS".

Test code:

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

    public static void main(String[] args) {
        Smartwatch w = input();

        if (w.isAppleWatch()) {
            System.out.println(w.getName() + " is an Apple Watch");
        }
        else {
            System.out.println("Not an Apple Watch");
        }
    }

    private static Smartwatch input() {
        String name = sc.nextLine();
        String colour = sc.nextLine();
        int price = Integer.parseInt(sc.nextLine());
        String operatingSystem = sc.nextLine();
        return new Smartwatch(name, colour, price, operatingSystem);
    }
}