// ============================ // Q1 // ============================ class Wallet { private int value; public Wallet(int v) { value = v; } public void change(int v) { value += v; } public String tell() { return value + " "; } } class CheatWallet extends Wallet { private Wallet secretWallet = new Wallet(0); public CheatWallet() { super(0); } public void take10(Wallet w1) { w1.change(-15); secretWallet.change(5); this.change(10); } public String tell() { return super.tell() + " " + secretWallet.tell(); } } public class Main { public static void main(String[] args) { Wallet w = new Wallet(100); CheatWallet cw = new CheatWallet(); cw.take10(w); System.out.println(cw.tell()); // Statement A System.out.println(w.tell()); // Statement B } } // ============================ // Q2 // ============================ // ============================ // Main // ============================ public class Main { public static void main(String[] args) { /* Part A */ Logbook lb = Logbook.getInstance(); Student a = new Student("Ada"); Student b = new Student("Bob"); Student c = new Student("Candy"); Student d = new Student("Doris"); Student e = new Student("Emily"); Student f = new Student("Frank"); Student g = new Student("Gabriel"); c.infect(d); // Candy infects Doris d.infect(g); // Doris infects Gabriel lb.show_count(); // Output: 2 records /* Part B */ lb.showPathTo(g); // Output: Candy => Doris => Gabriel lb.showSources(); // Output: Candy a.infect(b); b.infect(f); a.infect(e); lb.showPathTo(f); // Output: Ada => Bob => Frank lb.showPathTo(e); // Output: Ada => Emily lb.showSources(); // Output: Candy Ada lb.show_count(); // Output: 5 records } } // ============================ // Student.java // ============================ public class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } // Add your own code below } // ============================ // Logbook.java // ============================ import java.util.*; public class Logbook { private ArrayList log; // Add your own code below } // ============================ // InfectionRecord.java // ============================ import java.util.*; public class InfectionRecord { // Add your own code below } // ============================ // Q3 // ============================ // ============================ // Main // ============================ import java.util.ArrayList; public class Main { public static void main(String[] args) { Student a = new Student("Amy"); Student b = new Student("Bob"); Student c = new Student("Cathy"); Student d = new Student("Daisy"); StudentGroup x = new StudentGroup(); StudentGroup y = new StudentGroup(); b.join(x); b.join(y); c.join(x); c.join(y); d.join(y); ArrayList projects = new ArrayList<>(); projects.add(new Project(x, "Honesty")); projects.add(new Project(y, "Sports")); projects.add(new Project(y, "Crime")); projects.add(new Project(a, "Tourism")); projects.add(new Project(b, "Earthquakes")); projects.add(new Project(a, "Cookery")); for (Project p : projects) { p.showdetails(); } } } // ============================ // Output // ============================ /* [Honesty] by Bob Cathy [Sports] by Bob Cathy Daisy [Crime] by Bob Cathy Daisy [Tourism] by Amy [Earthquakes] by Bob [Cookery] by Amy */ // ============================ // Project // ============================ public class Project { private String title; private Writer writer; public Project(Writer writer, String title) { this.title = title; this.writer = writer; } public void showdetails() { System.out.printf("[%s] by %s\n", title, writer.say_I_am()); } } // ============================ // Writer // ============================ interface Writer { String say_I_am(); } // ============================ // Q4 // ============================ // ============================ // MissingValueEx.java // ============================ public class MissingValueEx extends Exception { public MissingValueEx() { super("Missing value!"); } public MissingValueEx(String msg) { super(msg); } } // ============================ // OutOfRangeEx.java // ============================ public class OutOfRangeEx extends Exception { private int x, v1, v2; public OutOfRangeEx(int x, int v1, int v2) { this.x = x; this.v1 = v1; this.v2 = v2; } @Override public String getMessage() { return x + " is out of range (" + v1 + ", " + v2 + ")"; } } // ============================ // Main.java // ============================ import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("d m y: "); String input = sc.nextLine(); // Accept a string until \n ('\n' is not stored) try { Day d = new Day(input); System.out.println("Day created: " + d); } sc.close(); } } // ============================ // Day.java // ============================ import java.util.*; public class Day { private int y, m, d; @Override public String toString() { // given return d + "-" + m + "-" + y; } }