Internet Computing: CSM02 Lab. session 3: Java with Swing and Exceptions Question 1 The following code is an application that creates two PlaneRectangles (see last assessment) and then checks if one is inside the other. Don’t worry about the details of the associated Java classes at this stage. Modify the application so that: 1. The parameters are entered through dialog boxes; 2. The result is also output via a dialog box Remember to ensure that the application terminates properly. public class MakeRectangle { public static void main(String[] args) { double length1 = Double.parseDouble(args[0]); double width1 = Double.parseDouble(args[1]); double xLocation1 = Double.parseDouble(args[2]); double yLocation1 = Double.parseDouble(args[3]); PlaneRectangle rectangle1 = new PlaneRectangle(length1, width1, xLocation1, yLocation1); double length2 = Double.parseDouble(args[4]); double width2 = Double.parseDouble(args[5]); double xLocation2 = Double.parseDouble(args[6]); double yLocation2 = Double.parseDouble(args[7]); PlaneRectangle rectangle2 = new PlaneRectangle(length2, width2, xLocation2, yLocation2); if ( rectangle1.isInside(rectangle2) ) { System.out.println(“The second rectangle lies inside the first”); } else { System.out.println(“The second rectangle is not within the first”); } } } Solution: import javax.swing.*; [1 mark] public class MakeRectangle { public static void main(String[ ] args) { String input = JOptionPane.showInputDialog("Enter length"); [1 mark] double length1 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter width"); [1 mark] double width1 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter x co-ordinate"); [1 mark] double xLocation1 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter y co-ordinate"); [1 mark] double yLocation1 = Double.parseDouble(input); PlaneRectangle rectangle1 = new PlaneRectangle(length1, width1, xLocation1, yLocation1); input = JOptionPane.showInputDialog("Rectangle2: Enter length"); [1 mark] double length2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Rectangle2: Enter width"); [1 mark] double width2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Rectangle2: Enter x co-ordinate"); [1 mark] double xLocation2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Rectangle2: Enter y co-ordinate"); [1 mark] double yLocation2 = Double.parseDouble(input); PlaneRectangle rectangle2 = new PlaneRectangle(length2, width2, xLocation2, yLocation2); if ( rectangle1.isInside(rectangle2) ) { JOptionPane.showMessageDialog(null, "The second rectangle lies inside the first"); [1 mark] System.exit(0); [1 mark] } else { JOptionPane.showMessageDialog(null, "The second rectangle is not within the first"); [1 mark] System.exit(0); [1 mark] } } } Additional [2 marks] if all correct: Total [15 marks] Question 2. Modify the Rectangle class below to make it into a “robust” class definition with checks to ensure that an instance cannot be set up with negative values for length and width. Do not explicitly handle the errors when a negative value is identified. Instead, throw an exception, as described at the end of the lecture. You will cover the exception handling in the next question. public class Rectangle { public double length; public double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double area() {return length* width; } public double circumference() {return 2*length* width; } } Solution: Note: “private” is a fully acceptable alternative to “protected” in the following public class Rectangle { protected double length; protected double width; [1 mark] [1 mark] protected void checkLength(double length) { if (length < 0.0) { throw new IllegalArgumentException("length must not be negative"); } } [2 marks] protected void checkWidth(double width) { if (width < 0.0) { throw new IllegalArgumentException("width must not be negative"); } } [2 marks] public Rectangle(double length, double width) { checkLength(length); checkWidth(width); this.length = length; this.width = width; } [1 mark] [1 mark] public double getWidth() {return width;} public double getLength() {return length;} [1 mark] [1 mark] public void setWidth(double width) { checkWidth(width); this.width = width; } [2 marks] public void setLength(double length) { checkLength(length); this.length = length; } [2 marks] public double area() {return length* width; } public double circumference() {return 2*(length+width); } } Plus [1 mark] if all correct. Total: [15 marks] Question 3 Now modify the application of Question 1 to explicitly handle any exceptions thrown by the Rectangle class of Question 2. Use the try – catch – finally construct. Solution: import javax.swing.*; public class MakeRectangle { public static void main(String[] args) { try { String input; input = JOptionPane.showInputDialog("Please enter length for first rectangle."); double length1 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Please enter width for first rectangle."); double width1 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Please enter first x-coordinate."); double xLocation1 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Please enter first y-coordinate."); double yLocation1 = Double.parseDouble(input); PlaneRectangle rectangle1 = new PlaneRectangle(length1, width1, xLocation1, yLocation1); input = JOptionPane.showInputDialog("Please enter length for second rectangle."); double length2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Please enter width for second rectangle."); double width2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Please enter second x-coordinate."); double xLocation2 = Double.parseDouble(input); input = JOptionPane.showInputDialog("Please enter length y-coordinate."); double yLocation2 = Double.parseDouble(input); PlaneRectangle rectangle2 = new PlaneRectangle(length2, width2, xLocation2, yLocation2); if ( rectangle1.isInside(rectangle2) ) { JOptionPane.showMessageDialog(null, "The second rectangle lies inside the first"); } else { JOptionPane.showMessageDialog(null, "The second rectangle is not within the first");} } For code correctly inserted in “try” block [2 marks] catch(IllegalArgumentException e1) { JOptionPane.showMessageDialog(null, e1.toString(), "Illegal argument", JOptionPane.ERROR_MESSAGE); } [6 marks] finally { System.exit(0); } Use of “finally” block [2 marks] } } Total: [10 marks]