Graderpractice for programmers

254275 OOP - Labs 6-10 Review

Awaiting submission
Write a class named Album with fields for "title", "artist" and "songs".

Test code:

class Task1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        String artist = sc.nextLine();
        int songs = Integer.parseInt(sc.nextLine());

        Album a1 = new Album(title, artist, songs);
        System.out.println(a1.getTitle() + " by " + a1.getArtist() + " is " + a1.getSongs() + " songs");
    }
}
Awaiting submission
Add a new method to the Album named toString that returns title - artist.

Test code:

class Task2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        String artist = sc.nextLine();
        int songs = Integer.parseInt(sc.nextLine());

        Album album1 = new Album(title, artist, songs);
        System.out.println(album1.toString());
    }
}
Awaiting submission
An EP is a short album with less than 8 songs. Add a method named isEP that returns true if the album is an EP.

Test code:

class Task3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String title = sc.nextLine();
        String artist = sc.nextLine();
        int songs = Integer.parseInt(sc.nextLine());

        Album album1 = new Album(title, artist, songs);
        if (album1.isEP()) {
            System.out.println(album1.getTitle() + " is short");
        } else {
            System.out.println(album1.getTitle() + " is normal");
        }
    }
}
Awaiting submission
A soundtrack is an album with music from a movie. Write a class named Soundtrack that is a subclass of Album with an extra field for "movie".

Test code:

class Task4 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String movie = sc.nextLine();
        int songs = Integer.parseInt(sc.nextLine());
        
        Album st1 = new Soundtrack(movie + " Soundtrack", "Various Artists", songs, movie);
        System.out.println(st1.toString());
    }
}
Awaiting submission
Add a method called getMovie() to the Soundtrack class.

Test code:

class Task5 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String movie = sc.nextLine();
        int songs = Integer.parseInt(sc.nextLine());

        Soundtrack st1 = new Soundtrack(movie + " Soundtrack", "Various Artists", songs, movie);
        System.out.print("There are " + st1.getSongs());
        System.out.println(" songs in the movie " + st1.getMovie());
    }
}
Awaiting submission
Write a class named Aeroplane with fields for "model", "engines" and "seats".

Test code:

class Task1 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        System.out.print("The " + ap.getModel() + " has ");
        System.out.print(ap.getEngines() + " engines and ");
        System.out.println(ap.getSeats() + " seats");
    }

    public static Aeroplane input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        return new Aeroplane(model, engines, seats);
    }
}
Awaiting submission
Add a new method to the Aeroplane named toString.

Test code:

class Task2 {

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

    public static Aeroplane input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        return new Aeroplane(model, engines, seats);
    }
}
Awaiting submission
A jumbo jet has 250 or more seats. Add a method named isJumbo that returns true if the aeroplane is a jumbo jet.

Test code:

class Task3 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        if (ap.isJumbo()) {
            System.out.println(ap.getModel() + " is a jumbo jet");
        } else {
            System.out.println(ap.getModel() + " is not a jumbo jet");
        }
    }

    public static Aeroplane input() {
        Scanner sc = new Scanner(System.in);
        String model = sc.nextLine();
        int engines = Integer.parseInt(sc.nextLine());
        int seats = Integer.parseInt(sc.nextLine());
        return new Aeroplane(model, engines, seats);
    }
}
Awaiting submission
A fighter plane is an aeroplane designed for military combat. A fighter can carry up to a maximum weight of weapons. Create the Fighter class as a subclass of Aeroplane, with an extra field for "maximumWeaponsWeight".

Test code:

class Task4 {

    public static void main(String[] args) {
        Aeroplane ap = input();
        System.out.print("The " + ap.getModel() + " has ");
        System.out.print(ap.getEngines() + " engines and ");
        System.out.println(ap.getSeats() + " seats");
    }

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