Graderpractice for programmers

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

Awaiting submission
Write a class named Drink with members for name, size in ml and type of container (c=can, b=bottle, g=glass). The class needs a constructor and get methods for each member.

Test code:

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

    public static void main(String[] args) {
        Drink d = input();

        System.out.println("Drink name: " + d.getName());
        System.out.println("Size: " + d.getSizeMl() + "ml");

        System.out.print("Container: ");
        switch (d.getContainer()) {
            case 'b':
                System.out.println("bottle"); break;
            case 'c':
                System.out.println("can"); break;
            case 'g':
                System.out.println("glass"); break;
        }
    }

    private static Drink input() {
        String name = sc.nextLine();
        int volumeMl = Integer.parseInt(sc.nextLine());
        char container = sc.nextLine().charAt(0);
        return new Drink(name, volumeMl, container);
    }
}
Awaiting submission
Reuse your Drink class from question 1. Add a method named toString that returns the name and the size.

Test code:

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

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

    private static Drink input() {
        String name = sc.nextLine();
        int volumeMl = Integer.parseInt(sc.nextLine());
        char container = sc.nextLine().charAt(0);
        return new Drink(name, volumeMl, container);
    }
}
Awaiting submission
Reuse your Drink class from question 2. Add a method named getContainerDescription that returns "bottle", "can", "glass" as a String for the container name.

Test code:

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

    public static void main(String[] args) {
        Drink d = input();

        System.out.println(d.getContainerDescription());
    }

    private static Drink input() {
        String name = sc.nextLine();
        int volumeMl = Integer.parseInt(sc.nextLine());
        char container = sc.nextLine().charAt(0);
        return new Drink(name, volumeMl, container);
    }
}
Awaiting submission
Reuse your Drink class from question 3. Add a method named isSameName that takes another Drink and returns true if they have the same name.

Test code:

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

    public static void main(String[] args) {
        Drink d1 = input();
        Drink d2 = input();

        if (d1.isSameName(d2)) {
            System.out.println(d1.getName() + " has " + d1.getSizeMl() + "ml and " + d2.getSizeMl() + "ml sizes");
        }
        else {
            System.out.println("Not the same name!");
        }
    }

    private static Drink input() {
        String name = sc.nextLine();
        int volumeMl = Integer.parseInt(sc.nextLine());
        char container = sc.nextLine().charAt(0);
        return new Drink(name, volumeMl, container);
    }
}
Awaiting submission
Coffee is a subclass of Drink. Coffee has a boolean member named iced. Write the Coffee class.

Test code:

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

    public static void main(String[] args) {
        Coffee c = input();

        System.out.println(c.getName());
        if (c.isIced()) {
            System.out.println("Iced");
        }
        else {
            System.out.println("Hot");
        }
    }

    private static Coffee input() {
        String name = sc.nextLine();
        int volumeMl = Integer.parseInt(sc.nextLine());
        char container = sc.nextLine().charAt(0);
        boolean iced = sc.nextLine().charAt(0) == 'i';
        return new Coffee(name, volumeMl, container, iced);
    }
}
Awaiting submission
Reuse your Coffee class from question 5. Add a method named getFullName that returns the full name of the coffee including "Iced" or "Hot". Example: if the coffee is named "Americano" and iced = false then the full name is "Hot Americano".

Test code:

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

    public static void main(String[] args) {
        Coffee c = input();
        System.out.println(c.getFullName());
    }

    private static Coffee input() {
        String name = sc.nextLine();
        int volumeMl = Integer.parseInt(sc.nextLine());
        char container = sc.nextLine().charAt(0);
        boolean iced = sc.nextLine().charAt(0) == 'i';
        return new Coffee(name, volumeMl, container, iced);
    }
}