Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Similar documents
Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

COMP16121 Sample Code Lecture 1

Final Exam CS 251, Intermediate Programming December 13, 2017

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

SampleApp.java. Page 1

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

AppBisect > PrBisect > class Functie. AppBisect > PrBisect > class Punct. public class Functie { double x(double t) { return t;

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

Window Interfaces Using Swing Objects

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Window Interfaces Using Swing Objects

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

CSCI 201L Midterm Written Summer % of course grade

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Example: Building a Java GUI

First Name: AITI 2004: Exam 2 July 19, 2004

Example: Building a Java GUI

ITI Introduction to Computing II

ITI Introduction to Computing II

CS Exam 3 - Spring 2010

CS 180 Final Exam Review 12/(11, 12)/08

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Java Interfaces Part 1 - Events Version 1.1

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

// autor igre Ivan Programerska sekcija package mine;

JFrame In Swing, a JFrame is similar to a window in your operating system

Graphical User Interface

Graphical User Interfaces in Java - SWING

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

Midterm assessment - MAKEUP Fall 2010

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

CS Exam 3 - Spring 2010

Dr. Hikmat A. M. AbdelJaber

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CSCI 201L Midterm Written SOLUTION Summer % of course grade

Do not turn to the next page until the start of the exam.

Final Examination Semester 2 / Year 2010

Final Exam CS 251, Intermediate Programming December 10, 2014

Object-Oriented Software Engineering (Re-exam for Object-Oriented Analysis, Design and Programming)

Java Never Ends CHAPTER MULTITHREADING 1100 Example: A Nonresponsive GUI 1101

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Agenda. Container and Component

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Points Missed on Page page 1 of 8

PIC 20A GUI with swing

8. Polymorphism and Inheritance

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

CMP-326 Exam 2 Spring 2018 Solutions Question 1. Version 1. Version 2

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Give one example where you might wish to use a three dimensional array

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

CS 209 Programming in Java #10 Exception Handling

package As7BattleShip;

CSE 214 Computer Science II Java Classes and Information Hiding

Final Examination Semester 2 / Year 2011

First Name: AITI 2004: Exam 2 July 19, 2004

CS Exam 1 Review Suggestions

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

User interfaces and Swing

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

encompass a group of features for building Graphical User Interfaces (GUI).

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

This exam is open book. Each question is worth 3 points.

CSIS 10A Assignment 7 SOLUTIONS

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Window Interfaces Using Swing. Chapter 12

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

Object- Oriented Analysis, Design and Programming

Building Java Programs Bonus Slides

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

!"# $ %&# %####' #&() % # # # #&* # ## +, # -

Transcription:

Page 1 of 16 SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2005 FINAL EXAMINATION 9am to 12noon, 19 DECEMBER 2005 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 page 16 of the exam. The back of any page can be used for rough work. This exam refers exclusively to the use of the Java language. Comments are not required in the code you write. This is a closed book exam. No computers or calculators are allowed. Student Number: Problem 1: / Problem 2: / Problem 3: / TOTAL: /

Student Number: Page 2 of 16 Problem 1) [ marks] Three classes form an object hierarchy. Here are two of the three classes, ClassR and ClassA: public abstract class ClassR { final int ONE = 1; abstract int method1 (int a, int b); public int method2 (int a) { return a + a; } // end method2 } // end ClassR public class ClassA extends ClassM { public ClassA (int a, int b) { super(a, b); } // end ClassA constructor public int method3 (int a) { return ONE + super.method3(a); } // end method3 public int method4 () { return getx() + gety(); } // end method4 } // end ClassA On the next page is a separate testing class with a copy of its console output, which illustrates the operation of the methods in the hierarchy:

Student Number: Page 3 of 16 public class MainClass { public static void main(string[] args) { ClassA ca = new ClassA(100, 1000); System.out.println(ca.method1(10, 20)); System.out.println(ca.method2(30)); System.out.println(ca.method3(40)); System.out.println(ca.method4()); } // end main } // end MainClass /* OUTPUT: 30 60 1141 1100 */ On the next page, write the complete code for the missing class. Write only the code required to complete the hierarchy in the most efficient way. Note that the only arithmetic operation used in any of the methods is addition.

Student Number: Page 4 of 16 public class ClassM extends ClassR { private int x; private int y; public ClassM (int a, int b) { x = a; y = b; } // end constructor public int getx () { return x; } public int gety () { return y; } public int method1 (int a, int b) { return a + b; } // end method1 public int method3 (int a) { return a + x + y; } // end method3 } // end ClassM

Student Number: Page 5 of 16 Problem 2) [ marks] For this problem you will need to write a complete class called SurfaceShape, with all the methods that should be present when you follow good encapsulation practices. You will need to use some other classes that are provided below. You will also be given a syntax summary for the ArrayList<T> class. SurfaceShape is designed to hold a collection of points in space that all lie on a plane. Use an ArrayList<T> for the collection. The constructor of the class will need to calculate the equation of the plane for the points, and it will also store this equation as an object. Here is an exception class to be used by SurfaceShape: public class SurfaceShapeException extends Exception { public SurfaceShapeException (String s) { super(s); } public SurfaceShapeException () { super("illegal surface shape!"); } } // end SurfaceShapeException class Points will be stored as objects, as defined by the Point class: public class Point { private double x, y, z; public Point (double x, double y, double z) { this.x = x; this.y = y; this.z = z; } // end Point constructor public double getx () { return x; } public double gety () { return y; } public double getz () { return z; } public boolean equals (Object p) { boolean check = false; Point pp; double tolerance = 1e-6; if (p instanceof Point) {

Student Number: Page 6 of 16 pp = (Point)p; check = Math.abs(pp.x - x) < tolerance; check = check && Math.abs(pp.y - y) < tolerance; check = check && Math.abs(pp.z - z) < tolerance; } // end if return check; } // end equals public Object clone () { return new Point (x, y, z); } // end close } // end Point class The equation of the surface will be stored in a SurfaceEquation object: public class SurfaceEquation { double a, b, c; public SurfaceEquation (double a, double b, double c) { this.a = a; this.b = b; this.c = c; } // end SurfaceEquation constructor public double calcz (double x, double y) { return a * x + b * y + c; } // end calcz public String tostring () { return String.format("z = %6.3f * x + %6.3f * y + %6.3f", a, b, c); } // end tostring public Object clone () { return new SurfaceEquation(a, b, c); } // end clone } // end SurfaceEquation

Student Number: Page 7 of 16 Assume the existence of a class called MatrixSolver that contains a static method called gaussjordan that provides the solution to a system of simultaneous equations. Remember assignment 2? The system of equations is described by: A x = b where A, x and b are matrices. A is n by n, and x and b are of size n. The gaussjordan method accepts the matrices A and b as arrays of double s, and returns the matrix r as an array of double s, where r is defined as: I x = r The matrix I is the identity matrix. Consider describing a plane, in three dimensions (x, y and z) using the general equation: z = ax + by + c If you had three points: (x 1, y 1, z 1 ), (x 2, y 2, z 2 ) and (x 3, y 3, z 3 ), then you could create the following system of equations: Or, in matrix form: ax 1 + by 1 + c = z 1 ax 2 + by 2 + c = z 2 ax 3 + by 3 + c = z 3 x x x 1 2 3 y y y 1 2 3 1.0 a z 1.0 = b z 1.0 c z 1 2 2 A solution to this matrix equation would yield the equation coefficients, a, b and c. The SurfaceShape class constructor accepts an ArrayList<T> which has been typed to contain Point objects. The constructor will throw an exception if the ArrayList is either null or contains fewer than three Point s. Next, the constructor will have to determine the equation of the plane using the first three Point s in the ArrayList. After creating a SurfaceEquation object, you will throw an exception if any of the remaining Point s in the ArrayList do not lie on the plane. For a Point to be said to lie on the plane, calculated and given z values must agree to less than 1x10-6.

Student Number: Page 8 of 16 You will find the following ArrayList<T> methods useful: size() // returns the number of occupied positions in the // ArrayList collection add(object) // adds the provided object to the end of the ArrayList // collection get(int) // returns the object at the given position in the // ArrayList collection as an Object contains(object) // Returns true if the collection contains the Object // provided as a parameter Do not write any mutator methods for your SurfaceShape class. You will need to write the two accessors, one for the ArrayList and the other for the SurfaceEquation object. Your equals method should override the equals method inherited from Object. Equality is defined as both ArrayList attributes having the same size and containing all the same Point s in any order. Compare SurfaceShape objects based only on the size of their ArrayList collections. Return a positive integer if the supplied object has more Point s. Your tostring() method should return a String containing just the number of Point s and the equation describing the plane. For example, it could return: 10 points, on plane: z = 4.000 * x + 5.000 * y + 7.000 Finally, include a clone() method that returns an Object. No other methods are required. That was a very long description! You solution should be shorter. Write your class in the space provided on the next three pages.

Student Number: Page 9 of 16 import java.util.arraylist; public class SurfaceShape { private ArrayList<Point> shape; private SurfaceEquation eqn; public SurfaceShape (ArrayList<Point> s) throws SurfaceShapeException { if (s == null s.size() < 3) throw new SurfaceShapeException("Not enough points!"); Point[] p = new Point[3]; double[][] a = new double[3][3]; double[] b = new double[3]; double[] factors = new double[3]; double z; Point pcheck; for (int i = 0; i < 3; i++) { p[i] = s.get(i); a[i][0] = p[i].getx(); a[i][1] = p[i].gety(); a[i][2] = 1; b[i] = p[i].getz(); } // end for factors = MatrixSolver.gaussJordan(a, b); eqn = new SurfaceEquation(factors[0], factors[1], factors[2]); plane!"); for (int i = 3; i < s.size(); i++) { pcheck = s.get(i); z = eqn.calcz(pcheck.getx(), pcheck.gety()); if (Math.abs(z - pcheck.getz()) > 1e-6) throw new SurfaceShapeException("Not all points lie on } shape = new ArrayList<Point>(); for (int i = 0; i < s.size(); i++) shape.add(s.get(i)); } // end SurfaceShape constructor

Student Number: Page 10 of 16 public ArrayList<Point> getshape () { ArrayList<Point> list = new ArrayList<Point>(); for (Point p : shape) list.add((point)p.clone()); return list; } // end getshape public SurfaceEquation getequation () { return (SurfaceEquation)eqn.clone(); } // end getequation public boolean equals (Object s) { if (s instanceof SurfaceShape) { SurfaceShape ss = (SurfaceShape)s; if (ss.shape.size()!= shape.size()) return false; for (Point p : ss.shape) if (!shape.contains(p)) return false; } // end if else return false; return true; } // end equals public int compareto (SurfaceShape s) { return s.shape.size() - shape.size(); } // end compareto

Student Number: Page 11 of 16 public String tostring () { return shape.size() + " points, on plane: " + eqn; } // end tostring public Object clone () { SurfaceShape se; try { se = new SurfaceShape (getshape()); } catch (SurfaceShapeException e) { return null; } return se; } // end clone } // end SurfaceShape class

Student Number: Page 12 of 16 Problem 3) [ marks] For this problem you will complete the code in the following GUI window definition. When your window first opens, it looks like: When you click on the Make a Wish button, you will see: The window is displayed by the code on the next page:

Student Number: Page 13 of 16 public class GUIExamProblem { public static void main(string[] args) { int nummessages = 40; GUIExamProblemWindow gep = new GUIExamProblemWindow(numMessages); gep.setvisible(true); } // end main } // end GUIExamProblem You will find the following methods useful: sethorizontalalignment(jlabel.center) add(component) add(component, BorderLayout.position) addactionlistener(class_implementing_actionlistener) settext(string) Inside your ActionListener class, you will need to define the following method: public void actionperformed (ActionEvent e) Here is the start of the GUIExamProblemWindow class definition: import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jlabel; import java.awt.flowlayout; import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class GUIExamProblemWindow extends JFrame { private final int WIDTH = 440; private final int HEIGHT = 300;

Student Number: Page 14 of 16 // Add more class attributes here, if necessary int nummessages; JLabel toplabel; JLabel[] label; JPanel midpanel; public GUIExamProblemWindow (int nummessages) { super(); this.nummessages = nummessages; setdefaultcloseoperation(jframe.exit_on_close); settitle("gui Layout Problem"); setsize(width, HEIGHT); // Complete the class below and on the next page:

Student Number: Page 15 of 16 toplabel = new JLabel(); toplabel.sethorizontalalignment(jlabel.center); add(toplabel, BorderLayout.NORTH); midpanel = new JPanel(new FlowLayout()); label = new JLabel[numMessages]; for (int i = 0; i < nummessages; i++) { label[i] = new JLabel(); midpanel.add(label[i]); } // end for add(midpanel, BorderLayout.CENTER); JButton changebutton = new JButton("Make a Wish"); changebutton.addactionlistener(new ChangeLabel()); JPanel flowpanel = new JPanel (new FlowLayout()); JPanel borderpanel = new JPanel (new BorderLayout()); flowpanel.add(changebutton); borderpanel.add(flowpanel, BorderLayout.CENTER); add(borderpanel, BorderLayout.SOUTH ); } // end constructor private class ChangeLabel implements ActionListener { public void actionperformed (ActionEvent e) { toplabel.settext("exams are over!"); for (int i = 0; i < nummessages; i++) label[i].settext(" Happy Holidays! "); } // end actionperfomed } // end ChangeLabel } // end GUIExamProblemWindow class

Student Number: Page 16 of 16 (Extra page)