Quiz 12 Solution 1. Suppose you are given the class Youngster, which represents a young person with a name and an age. Complete the methods of the class Teenager, which is a subclass of Youngster, and in addition to name and age also stores a summer job. Be sure to avoid repetitive code. [4 marks] class Youngster { private String name; // this Youngster's name private int age; // this Youngster's age, in years // Constructor: initializes this Youngster's name and age // to 'n' respectively 'a' public Youngster (String n, int a) { name = n; age = a; } // Returns the information about this Youngster public String getInfo() { return "Name: " + name + "\t" + "Age: " + age; } } /** * Represents a teenager, who, in addition * to a name and an age, also has a summer job. */ class Teenager extends Youngster { private String summerJob; // Constructor: initializes this Teenager's name, age, and summer job // respectively to 'n', 'a', and 's' public Teenager (String n, int a, String s) { super (n, a); summerJob = s; } // Returns the name, age, and summer job of this Teenager // Format: similar to the Youngster (i.e. each piece of info // is labelled and separated from the one before it by a "\t") public String getInfo ( ) { return super.getInfo() + "\t" + "Summer Job: " + summerJob; } // Returns the summer job of this teenager public String getJob() { return summerJob; } } 2. a) In the main method below, there are 2 lines that cause compilation errors. Circle them. [3 marks] b) If these lines were removed from the program, what would be its output? Write it in the box below. [3 marks] class Quiz { public static void main (String[] args) { Youngster y1 = new Youngster("Alice", 10); Teenager t1 = new Teenager ("Jim", 14, "Sales Clerk"); Youngster y2 = new Teenager ("Daniel", 16, "Office Gopher"); Teenager t2 = new Youngster ("Michaela", 15); //constructor protests System.out.println (y1.getInfo()); System.out.println (y2.getInfo()); System.out.println (t1.getJob()); System.out.println (y2.getJob()); //needs a cast } } OUTPUT Name: Alice Age: 10 Name: Daniel Age: 16 Summer Job: Office Gopher Sales Clerk