import java.io.PrintStream; import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.TreeMap; import cse1020.shape.*; import type.lib.ToolBox; public class TestB { public static void main(String[] args) { Scanner input = new Scanner(System.in); PrintStream output = System.out; // Part 1: 3 marks // 1 mark for both prompts // 1 mark for reading name // 1 mark for reading number output.print("Name of this drawing: "); String drawingName = input.nextLine().trim().toUpperCase(); output.print("How many shapes? "); int n = input.nextInt(); // Part 2: 3 marks // 1 mark for map // 1 mark for loop // 1 mark for put Map shapes = new TreeMap(); for (int i = 0; i < n; i++) { output.print("*"); shapes.put(i, ShapeFactory.getRandom()); } output.println(); // Part 3: 3 marks // 1 mark for throwing exceptions // 1 mark for catching the correct exception // 1 mark for printing "No shapes removed from map" in the catch block output.print("Which shape to remove? "); int removeKey = input.nextInt(); try { ToolBox.crash(removeKey < 0, "Key is negative: " + removeKey); ToolBox.crash(removeKey >= shapes.size(), "Key is too large: " + removeKey); } catch (RuntimeException e) { output.println(e.getMessage()); output.println("No shapes removed from map"); } // Part 4: 4 marks // 1 mark for remove // 1 mark for Circle case (should use instanceof) // 1 mark for Square case (should use instanceof) // 1 mark for Line case (should use instanceof) Shape s = shapes.remove(removeKey); if (s instanceof Circle) { Circle c = (Circle) s; output.println("removed a circle with radius " + c.getRadius()); } else if (s instanceof Square) { Square q = (Square) s; output.println("removed a square with length " + q.getLength()); } else if (s instanceof Line) { Line line = (Line) s; output.println("removed a line with length " + line.getLength()); } // Part 5: 2 marks // 1 mark for printing the drawing name // 1 mark for printing the shapes output.println(); output.println("Drawing " + drawingName + ":"); for (Integer i : shapes.keySet()) { output.println(i + "\t" + shapes.get(i)); } } }