school of computing solution - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w...

21
HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2012 FINAL EXAMINATION 9am to 12pm, 26 APRIL 2012 Instructor: Alan McLeod If the instructor is unavailable in the examination room and if doubt exists as to the interpretation of any problem, the candidate is urged to submit with the answer paper a clear statement of any assumptions made. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. Please write your answers in the boxes provided. Extra space is available on the last page of the exam. The back of any page can be used for rough work. This exam is three hours long and refers exclusively to the use of the Java language. Comments are not required in the code you write. For full marks, code must be efficient as well as correct. This is a closed book exam. No computers or calculators are allowed. Problem 1: / 15 Problem 2c: / 5 Problem 2a: / 10 Problem 2d: / 30 Problem 2b: / 10 Problem 3: / 10 TOTAL: / 80 Student Number: This material is copyrighted and is for the sole use of students registered in CISC124 and writing this exam. This material shall not be distributed or disseminated. Failure to abide by these conditions is a breach of copyright and may also constitute a breach of academic integrity under the University Senate's Academic Integrity Policy Statement. SOLUTION

Upload: others

Post on 07-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

HAND IN Answers Are Recorded on

Question Paper

QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2012 FINAL EXAMINATION 9am to 12pm, 26 APRIL 2012 Instructor: Alan McLeod If the instructor is unavailable in the examination room and if doubt exists as to the interpretation of any problem, the candidate is urged to submit with the answer paper a clear statement of any assumptions made. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. Please write your answers in the boxes provided. Extra space is available on the last page of the exam. The back of any page can be used for rough work. This exam is three hours long and refers exclusively to the use of the Java language. Comments are not required in the code you write. For full marks, code must be efficient as well as correct. This is a closed book exam. No computers or calculators are allowed. Problem 1: / 15 Problem 2c: / 5 Problem 2a: / 10 Problem 2d: / 30 Problem 2b: / 10 Problem 3: / 10

TOTAL: / 80

Student Number:

This material is copyrighted and is for the sole use of students registered in CISC124 and writing this exam. This material shall not be distributed or disseminated. Failure to abide by these conditions is a breach of copyright and may

also constitute a breach of academic integrity under the University Senate's Academic Integrity Policy Statement.

SOLUTION

Page 2: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 2 of 21 Problem 1) [15 marks]: Qualitative Java Write a “T” or “F” on the line before each of the following statements to indicate if it is True or False: 1. ___ In an expression that does not contain any round brackets, binary boolean comparisons

will be carried out before binary arithmetic operations are evaluated. 2. ___ The || operator always evaluates the expressions on both sides of the operator. 3. ___ Casting operations have a higher precedence than arithmetic operations in any

expression. 4. ___ The ++ pre-increment operator carries out the increment after the variable is used in an

expression. 5. ___ If an expression contains a non-void method call that results in an exception being

thrown by that method, the returned value from the method call can still be used to complete the evaluation of the expression.

6. ___ Switch statements can be used with String type arguments in Java 7. 7. ___ Any switch statement construct can be built using a chained if construct.

8. ___ A for each loop must be coded to use the index value to obtain collection elements. 9. ___ Thrown exceptions will be propagated to the main method if they are not caught in a

try/catch block. 10. ___ Passing an array into a method involves the creation of a new copy of that array and all

of its elements in the method. 11. ___ Automatic “boxing” can simplify the use of Wrapper classes with Generic classes. 12. ___ The Math class contains only non-static methods and attributes.

13. ___ Strings are immutable objects.

14. ___ Arrays are immutable objects. 15. ___ A object that extends JFrame inherits a frame that displays with a title bar and the

normal window title bar controls.

T

T

T

T

T

T

T

F

F

F

F

F

F

F

F

Page 3: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 3 of 21 Problem 1, Cont.) 16. ___ Components laid down in a JPanel using FlowLayout will always be touching each other

when displayed. 17. ___ A component placed in the Center position of a BorderLayout will swell to touch the

edges of that position.

18. ___ A GridLayout type object can be instantiated using a constructor that accepts the number of rows and columns to be used in the grid.

19. ___ The text attribute of a JLabel in a running GUI program can only be changed in code through a mutator.

20. ___ A JTextField component can display multiple lines of text.

21. ___ There is more than one listener class that can respond to mouse initiated events.

22. ___ System-generated component re-draws invoke the paint(Graphics g) method for that component.

23. ___ Code-generated component re-draws can also be carried out by invoking the paint(Graphics g) method directly.

24. ___ To invoke a component’s repaint() method you must supply the Graphics object associated with that component to the method.

25. ___ Windows created in Java 7 do not have to be rectangular in shape.

26. ___ Many listener objects can be attached to a single Timer thread.

27. ___ Once an instance of a Thread object is started, it cannot be stopped until the application owning the thread is finished.

28. ___ An exception is used to stop a Thread object.

29. ___ An exception is used to stop a Timer object.

30. ___ It is possible to speed up a Quicksort method by using the Fork/Join framework to spread the task over multiple processor cores.

T

T

T

T T

T

T

T

T

F

F

F

F

F

F

Page 4: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 4 of 21 Problem 2) [55 marks] Encapsulation, Polymorphism This is a multi-part problem that explores how a position dataset can be based on different coordinate systems. While not necessary, it is probably best to attempt each part of this problem in the order in which they are presented. A large testing program and its output is provided after the object specifications. You may not need to examine this program unless it would help to see an example of the specific behaviour of one of the objects you have to create. All of the specifications will be presented below, followed by the testing program and then space will be provided for your code. a) [10 marks] Create a properly encapsulated class called Point that must implement the Comparable<Point> interface. This interface requires that you implement a method with the signature:   public int compareTo(Point otherPoint);  This method returns -1 if the supplied other Point is larger than the current point, 0 if they are the same size (to within ±1e-6) and +1 if the supplied Point is smaller. For this compareTo() method, “size” is considered to be the square root of the sum of the squares of the attributes of the class. You can only have two double type attributes in your Point class, which will be the x and y values for the position of the point. So, the size used by compareTo() is the distance from the origin, (0, 0) to the (x, y) position of the point. The further away a point is from the origin, the “larger” it is. Note that any class that implements the Comparable<T> interface can be sorted with the static Arrays.sort() method, where the Arrays class is part of the java.util package. The Point class’ only constructor can accept two doubles of any magnitude, so it does not need to throw any exception. Write public mutators and accessors for both attributes. The Point object will be mutable. You will also need equals(), clone() and toString() methods. The equals() method must override the equals() method from the base Object class, and equality is defined as supplied and current attributes being the same to within ± 1e-6 (for simplicity). The String representation of a Point is shown in the testing program. Remember that Math.abs() returns the absolute value of its argument and Math.sqrt() returns the square root of its argument. b) [10 marks] Create another properly encapsulated class called Point3D that extends the Point class described in part a). This class can only have one double type attribute, z, which will be the third dimensional coordinate, allowing Point3D to describe a position in three-dimensional space. Write one three-parameter constructor that can accept three doubles of any magnitude. A mutator and accessor for your z parameter are also required. You will also need equals(), compareTo(), clone() and toString(), as you have for your Point class. Use the same basis for equality as described in part a), but applied to all three attributes, and compareTo() will use the square root of the sum of the squares of x, y and z. c) [5 marks] Write an exception class called DataException that extends the Exception class. Include the usual two constructors - one that takes a String message and another that has an empty parameter list which supplies a default message. This class will be used by the Dataset class described in part d). d) [30 marks] Write another properly encapsulated class called Dataset, which can only have a single attribute of type Point[ ]. This means that the underlying data structure of a Dataset object will be an array of Point objects. Note that this array cannot have any empty positions; it must always be exactly sized to store the supplied data.

Page 5: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 5 of 21 Problem 2d, Cont.) The Dataset object will have two public constructors, one which takes two one-dimensional arrays of double, and another which takes three one-dimensional arrays of double. These arrays will hold coordinate positions. One array for x values, a second array for y values and the third array for z values. You can see that a Dataset can store coordinates as an array of Point objects (two-dimensional) or an array of Point3D objects (three-dimensional). Fortunately, the Point[ ] type attribute of your class can point to either array structure. You must throw a DataException object with an appropriate message (see the testing class’ output), if any of the supplied arrays are null, of a length less than 2, or of different lengths. A Dataset object will be immutable, and you must make sure there are no data “leaks”. This means that you must not return pointers aliased to your attributes. You will need an accessor for your Point[ ] type attribute, but no public mutator is required. You must also supply a clone() method that returns a “deep copy” of the current Dataset object. Write a sort() method for Dataset that employs the compareTo() methods written for the Point hierarchy to sort the Point[ ] array in situ. Remember that there is an easy way to do this! Include an equals() method that overrides the equals() method of the base Object class. Two Dataset objects are equal if their Points are all equal to within ±1e-6, and in the same array positions. (Do not sort the arrays before comparing.) Include a compareTo() method that compares Dataset objects on the basis of the length of their arrays only. So, a Dataset object with more points will be “larger” than a Dataset object with fewer points. Finally, you need to include two, overloaded append() methods in your Dataset class. The first append() method takes two doubles, an x value and a y value. The second append() method takes three doubles, an x value, a y value and a z value. Both append() methods must create the appropriate Point object and add this new Point object to the end of the Point[ ] array, without deleting any of the existing Point objects in the array. To make the append() methods more interesting, they should throw a DataException when a Dataset holding two-dimensional data is asked to append a three-dimensional Point, or when a Dataset holding three-dimensional data is asked to append a two-dimensional point. Remember that there are two ways to check object types – using the instanceof keyword and using the getClass() method inherited from the base Object class. For example, invoking getClass().toString() on an instance of a Point object would yield the String “class Point”. The Testing program carries out a set of tests on the Point, Point3D and Dataset objects. Your classes should provide the output shown as block comments in the body of the program:  public class Testing {  // Console output is shown in a block comment under each section:   public static void main(String[] args) {      // Testing 2D Point object:     Point point1 = new Point(3.0, 4.0);     Point point2 = new Point(6.0, 7.0);     Point point3 = new Point(6.0 + 1e‐8, 7.0 ‐ 1e‐10);     System.out.println("point1 is: " + point1);     System.out.println("supplied is larger: " + point1.compareTo(point2));     System.out.println("supplied is smaller: " + point2.compareTo(point1)); 

Page 6: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 6 of 21     System.out.println("supplied is the same: " + point2.compareTo(point3)); /* point1 is: x = 3.0, y = 4.0 supplied is larger: ‐1 supplied is smaller: 1 supplied is the same: 0 */         System.out.println("not equals: " + point1.equals(point2));     System.out.println("equals: " + point2.equals(point3)); /* not equals: false equals: true */         Point point4 = point1.clone();     System.out.println("point4 is: " + point4);     point4.setX(10.0);     point4.setY(20.0);     System.out.println("point4 is: " + point4);     System.out.println("point1 is: " + point1); /* point4 is: x = 3.0, y = 4.0 point4 is: x = 10.0, y = 20.0 point1 is: x = 3.0, y = 4.0 */         // Testing 3D Point object:     Point3D point5 = new Point3D(3.0, 4.0, 5.0);     Point3D point6 = new Point3D(6.0, 7.0, 8.0);     Point3D point7 = new Point3D(6.0 + 1e‐8, 7.0, 8.0);     System.out.println("point5 is: " + point5); /* point5 is: x = 3.0, y = 4.0, z = 5.0 */     System.out.println("supplied is larger: " + point5.compareTo(point6));     System.out.println("supplied is smaller: " + point6.compareTo(point5));     System.out.println("supplied is the same: " + point6.compareTo(point7)); /* supplied is larger: ‐1 supplied is smaller: 1 supplied is the same: 0    */     // Testing Dataset object ‐ Constructors:     double[] nullArray = null;     double[] tooSmallArray = {1.0};     double[] size3Array = {1.0, 2.0, 3.0};     double[] array1 = {3.0, 1.0, 4.0, 6.0};     double[] array2 = {1.0, 2.0, 5.0, 8.0};     double[] array3 = {0.0, 1.0, 3.0, 9.0};     Dataset test = null;     try {       test = new Dataset(nullArray, size3Array);     } catch (DataException e) {       System.out.println(e.getMessage()); 

Page 7: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 7 of 21     }     try {       test = new Dataset(tooSmallArray, tooSmallArray);     } catch (DataException e) {       System.out.println(e.getMessage());     }     try {       test = new Dataset(array1, size3Array);     } catch (DataException e) {       System.out.println(e.getMessage());     }     try {       test = new Dataset(array1, array2);     } catch (DataException e) {       System.out.println(e.getMessage());     }     System.out.println(test); /* One or the other array is null. Not enough points. Array sizes do not match. Dataset contents: x = 3.0, y = 1.0 x = 1.0, y = 2.0 x = 4.0, y = 5.0 x = 6.0, y = 8.0 */     Dataset test3D = null;     try {       test3D = new Dataset(array1, array2, size3Array);     } catch (DataException e) {       System.out.println(e.getMessage());     }     try {       test3D = new Dataset(array1, array2, array3);     } catch (DataException e) {       System.out.println(e.getMessage());     }     System.out.println(test3D); /* Array sizes do not match. Dataset contents: x = 3.0, y = 1.0, z = 0.0 x = 1.0, y = 2.0, z = 1.0 x = 4.0, y = 5.0, z = 3.0 x = 6.0, y = 8.0, z = 9.0 */     // Testing sort method:     test3D.sort();     System.out.println(test3D); /* Dataset contents: 

Page 8: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 8 of 21 x = 1.0, y = 2.0, z = 1.0 x = 3.0, y = 1.0, z = 0.0 x = 4.0, y = 5.0, z = 3.0 x = 6.0, y = 8.0, z = 9.0     */     // Testing clone method and getData() accessor:     Dataset test3DCopy = test3D.clone();     Point3D firstPoint = (Point3D)test3DCopy.getData()[0];     firstPoint.setX(100.0);     firstPoint.setY(200.0);     firstPoint.setZ(300.0);     System.out.println("firstPoint is: " + firstPoint);     test3DCopy.getData()[1] = new Point3D(700.0, 800.0, 900.0);     System.out.println(test3DCopy);     System.out.println(test3D); /* firstPoint is: x = 100.0, y = 200.0, z = 300.0 Dataset contents: x = 1.0, y = 2.0, z = 1.0 x = 3.0, y = 1.0, z = 0.0 x = 4.0, y = 5.0, z = 3.0 x = 6.0, y = 8.0, z = 9.0  Dataset contents: x = 1.0, y = 2.0, z = 1.0 x = 3.0, y = 1.0, z = 0.0 x = 4.0, y = 5.0, z = 3.0 x = 6.0, y = 8.0, z = 9.0     */     // Testing equals and clone on 2D dataset:     Dataset testCopy = test.clone();     System.out.println(test.equals(testCopy));     testCopy.sort();     System.out.println(testCopy);     System.out.println(test.equals(testCopy));     System.out.println(test.equals(test3D)); /* true Dataset contents: x = 1.0, y = 2.0 x = 3.0, y = 1.0 x = 4.0, y = 5.0 x = 6.0, y = 8.0 false false    */     // Testing compareTo() with 2D dataset:     double[] largerArray1 = {2.0, 3.0, 4.0, 5.0, 6.0, 7.0};     double[] largerArray2 = {1.0, 0.0, 3.0, 5.0, 8.0, 9.0};     Dataset testLarger = null;     try {       testLarger = new Dataset(largerArray1, largerArray2); 

Page 9: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 9 of 21     } catch (DataException e) {       System.out.println(e.getMessage());     }     System.out.println("supplied is larger: " + test.compareTo(testLarger));     System.out.println("supplied is smaller: " + testLarger.compareTo(test));     System.out.println("supplied is the same: " + test.compareTo(test)); /* supplied is larger: ‐2 supplied is smaller: 2 supplied is the same: 0    */     // Testing append() methods:     try {       test.append(10.0, 12.0, 14.0);     } catch (DataException e) {       System.out.println(e.getMessage());     }     try {       test.append(10.0, 12.0);     } catch (DataException e) {       System.out.println(e.getMessage());     }     System.out.println(test);     try {       test3D.append(10.0, 12.0, 14.0);     } catch (DataException e) {       System.out.println(e.getMessage());     }     try {       test3D.append(10.0, 12.0);     } catch (DataException e) {       System.out.println(e.getMessage());     }     System.out.println(test3D); /* Cannot append 3D point to 2D array. Dataset contents: x = 3.0, y = 1.0 x = 1.0, y = 2.0 x = 4.0, y = 5.0 x = 6.0, y = 8.0 x = 10.0, y = 12.0 Cannot append 2D point to 3D array. Dataset contents: x = 1.0, y = 2.0, z = 1.0 x = 3.0, y = 1.0, z = 0.0 x = 4.0, y = 5.0, z = 3.0 x = 6.0, y = 8.0, z = 9.0 x = 10.0, y = 12.0, z = 14.0 */   } // end main } // end Testing class 

Page 10: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 10 of 21 Problem 2a) Code – the Point class:  

public class Point implements Comparable<Point> {    private double x;   private double y;      public Point(double x, double y) {     setX(x);     setY(y);   }      public double getX() { return x; }   public double getY() { return y; }      public void setX(double x) { this.x = x; }   public void setY(double y) { this.y = y; }      public boolean equals (Object obj) {     if (obj instanceof Point) {       Point otherPoint = (Point)obj;       boolean xCompare = Math.abs(otherPoint.x ‐ x) < 1e‐6;       boolean yCompare = Math.abs(otherPoint.y ‐ y) < 1e‐6;       return xCompare && yCompare;     }     return false;   }      public int compareTo(Point oP) {     double mag = Math.sqrt(x * x + y * y);     double otherMag = Math.sqrt(oP.x *oP.x + oP.y * oP.y);     if (Math.abs(mag ‐ otherMag) < 1e‐6)       return 0;     else if (mag < otherMag)       return ‐1;     else       return 1;   }      public Point clone() {     return new Point(x, y);   }      public String toString() {     return "x = " + x + ", y = " + y;   }  } // end Point 

Page 11: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 11 of 21 Problem 2a) Code – the Point class, Cont.:  

Page 12: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 12 of 21 Problem 2b) Code – the Point3D class:  

public class Point3D extends Point {    private double z;      public Point3D(double x, double y, double z) {     super(x, y);     this.z = z;   }    public double getZ() { return z; }   public void setZ(double z) { this.z = z; }      public boolean equals (Object obj) {     if (obj instanceof Point3D) {       Point3D otherPoint = (Point3D)obj;       boolean xCompare = Math.abs(otherPoint.getX() ‐ getX()) < 1e‐6;       boolean yCompare = Math.abs(otherPoint.getY() ‐ getY()) < 1e‐6;       boolean zCompare = Math.abs(otherPoint.z ‐ z) < 1e‐6;       return xCompare && yCompare && zCompare;     }     return false;   }    public int compareTo(Point3D oP) {     double x = getX();     double y = getY();     double oX = oP.getX();     double oY = oP.getY();     double mag = Math.sqrt(x * x + y * y + z * z);     double otherMag = Math.sqrt(oX * oX + oY * oY + oP.z * oP.z);     if (Math.abs(mag ‐ otherMag) < 1e‐6)       return 0;     else if (mag < otherMag)       return ‐1;     else       return 1;   }      public Point3D clone() {     return new Point3D(getX(), getY(), z);   }      public String toString() {     return super.toString() + ", z = " + z;   } } 

Page 13: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 13 of 21 Problem 2b) Code – the Point3D class, Cont.:  

Page 14: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 14 of 21 Problem 2c) Code – the DataException class:  

Problem 2d) Code – the Dataset class:

 public class DataException extends Exception {    public DataException(String message) {     super(message);   }   public DataException() {     super("Attempt to create illegal dataset.");   } } 

Page 15: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 15 of 21 Problem 2d) Code – the Dataset class, Cont.: import java.util.Arrays;  public class Dataset {    private Point[] data;      public Dataset(double[] xArray, double[] yArray) throws DataException {     if (xArray == null || yArray == null)       throw new DataException("One or the other array is null.");     int size = xArray.length;     if (yArray.length != size)       throw new DataException("Array sizes do not match.");     if (size < 2)       throw new DataException("Not enough points.");     data = new Point[size];     for (int i = 0; i < size; i++)       data[i] = new Point(xArray[i], yArray[i]);   }      public Dataset(double[] xArray, double[] yArray, double[] zArray) throws DataException {     if (xArray == null || yArray == null || zArray == null)       throw new DataException("One of the arrays is null.");     int size = xArray.length;     if (yArray.length != size || zArray.length != size)       throw new DataException("Array sizes do not match.");     if (size < 2)       throw new DataException("Not enough points.");     data = new Point3D[size];     for (int i = 0; i < size; i++)       data[i] = new Point3D(xArray[i], yArray[i], zArray[i]);   }    private Dataset(Point[] data) {     this.data = data;   } 

Page 16: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 16 of 21 Problem 2d) Code – the Dataset class, Cont.:   private Point[] copyData() {     int size = data.length;     Point[] copy;     if (data[0] instanceof Point3D)       copy = new Point3D[size];     else       copy = new Point[size];     for (int i = 0; i < size; i++)       copy[i] = data[i].clone();     return copy;       }      public Point[] getData() {     return copyData();   }      public Dataset clone() {     return new Dataset(copyData());   }      public boolean equals(Object obj) {     if (obj instanceof Dataset) {       Dataset otherDs = (Dataset)obj;       Point[] otherData = otherDs.getData();       if (otherData.length == data.length) {         for (int i = 0; i < data.length; i++)           if (!otherData[i].equals(data[i]))             return false;         return true;       }     }     return false;   }    public int compareTo(Dataset otherDs) {     int otherSize = otherDs.getData().length;     int size = data.length;     return size ‐ otherSize;   }    

Page 17: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 17 of 21 Problem 2d) Code – the Dataset class, Cont.:   public String toString() {     String out = "Dataset contents:\n";     for (int i = 0; i < data.length; i++)       out = out + data[i] + "\n";     return out;   }      public void sort() {     Arrays.sort(data);   }      public void append(double x, double y) throws DataException {     if (data[0].getClass().toString().equals("class Point")) {       Point[] temp = copyData();       data = new Point[temp.length + 1];       for (int i = 0; i < temp.length; i++)         data[i] = new Point(temp[i].getX(), temp[i].getY());       data[temp.length] = new Point(x, y);     }     else       throw new DataException("Cannot append 2D point to 3D array.");   }      public void append(double x, double y, double z) throws DataException {     if (data[0] instanceof Point3D) {       Point[] temp = copyData();       data = new Point3D[temp.length + 1];       for (int i = 0; i < temp.length; i++) {         Point3D temp3D = (Point3D)temp[i];         data[i] = new Point3D(temp3D.getX(), temp3D.getY(), temp3D.getZ());       }       data[temp.length] = new Point3D(x, y, z);     }     else       throw new DataException("Cannot append 3D point to 2D array.");   }    } // end Dataset 

Page 18: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 18 of 21 Problem 2d) Code – the Dataset class, Cont.:

Page 19: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 19 of 21 Problem 3) [10 marks] GUI Coding

Here is some of the class that was used to generate the window shown above: import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Font;  public class ExamWindow extends JFrame {      private final int WIDTH = 500;     private final int HEIGHT = 400;     private final int LEFT = 200;     private final int TOP = 100;     Font f = new Font("Arial", Font.PLAIN, 18);     JButton[] buttonArray;      public ExamWindow() {          super();         setSize(WIDTH, HEIGHT);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setTitle("Layout Problem");         setLocation(LEFT, TOP);         setLayout();      } // end constructor } // end ExamWindow class 

Page 20: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 20 of 21 Problem 3, Cont.) In the box below, write the method setLayout(), to be included in the ExamWindow class, that creates the buttons and the window layout shown on the previous page. You may use the array of JButton objects, and you cannot use any layout managers other than the three imported in the code shown on the previous page.     private void setLayout() {          buttonArray = new JButton[8];         for (int i = 0; i < 8; i++) {             buttonArray[i] = new JButton("Button " + (i + 1));             buttonArray[i].setFont(f);         }                  add(buttonArray[0], BorderLayout.NORTH);         eastPanel = new JPanel(new GridLayout(3, 2));         southPanel = new JPanel(new FlowLayout());          for (int i = 1; i < 6; i++)             eastPanel.add(buttonArray[i]);         add(eastPanel, BorderLayout.EAST);          southPanel.add(buttonArray[6]);         southPanel.add(buttonArray[7]);          add(southPanel, BorderLayout.SOUTH);              } // end setLayout  

Page 21: SCHOOL OF COMPUTING SOLUTION - research.cs.queensu.caresearch.cs.queensu.ca › home › cisc124w › Winter2020 › ... · spread the task over multiple processor cores. T T T T

Student Number: ____________________ Page 21 of 21

(Extra page)