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

Similar documents
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.

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.

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.

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.

CSCI 201L Midterm Written Summer % of course grade

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

Example: Building a Java GUI

Example: Building a Java GUI

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

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

SampleApp.java. Page 1

CSCI 201L Midterm Written SOLUTION Summer % of course grade

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

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.

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

Dr. Hikmat A. M. AbdelJaber

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

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

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

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

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

Final Exam CS 251, Intermediate Programming December 13, 2017

CS Exam 3 - Spring 2010

CS Exam 3 - Spring 2010

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

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

CS 1316 Exam 3 Fall 2009

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

CMP 326 Midterm Fall 2015

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

Points Missed on Page page 1 of 8

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

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

State Application Using MVC

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

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

RAIK 183H Examination 2 Solution. November 10, 2014

Midterm assessment - MAKEUP Fall 2010

Agenda. Container and Component

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

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

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

CSCI 201L Written Exam #1 Fall % of course grade

Graphical User Interfaces in Java - SWING

Building Java Programs Bonus Slides

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Final Examination Semester 2 / Year 2010

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

Based on slides by Prof. Burton Ma

CSIS 10A Assignment 7 SOLUTIONS

DM503 Programming B. Peter Schneider-Kamp.

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

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

CSC Java Programming, Fall Java Data Types and Control Constructs

PIC 20A GUI with swing

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

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

More about GUIs GEEN163

CSCI 201L Written Exam #1 - SOLUTION Fall % of course grade

RAIK 183H Examination 2 Solution. November 11, 2013

Part I: Learn Common Graphics Components

Midterm Test II Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours. Answer all questions

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

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

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

// autor igre Ivan Programerska sekcija package mine;

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

CS 101 Spring 2007 Midterm 2 Name: ID:

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

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

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

Lecture 5: Java Graphics

MIT AITI Swing Event Model Lecture 17

8. Polymorphism and Inheritance

Computer Science II (20082) Week 1: Review and Inheritance

CS 134 Programming Exercise 7:

Window Interfaces Using Swing. Chapter 12

CS/ENGRD2110: Prelim 2 SOLUTION

Transcription:

Solution HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2007 FINAL EXAMINATION 7pm to 10pm, 10 DECEMBER 2007, Jeffery Hall 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 14 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. For full marks, code must be efficient as well as correct. This is a closed book exam. No computers or calculators are allowed. Student Number: Problem 1: / 10 Problem 4: / 10 Problem 2: / 10 Problem 5: / 5 Problem 3: / 30 Problem 6: / 5 TOTAL: / 70

Student Number: Page 2 of 14 Problem 1) [5 marks for a) and 5 marks for b)] a) In lecture we found that the machine epsilon for a double is 1.1102230246251565E-16, and for a float, 5.9604645E-8. Write the output of the following program beside each println() statement. Remember that a float literal has a capital F appended to the number. public class Problem1 { public static void main(string[] args) { System.out.println(1.0 + 1.0e-16 == 1.0); System.out.println(1.0 + 1.2e-16 == 1.0); System.out.println(1.0e20 + 1.0e3 == 1.0e20); System.out.println(1.0e-20 + 1.0e-35 == 1.0e-20); System.out.println(1.0F + 5.0E-8F == 1.0F); true false true false true // end main // end Problem1 class b) The two equations shown below are algebraically equivalent. Circle the equation that you would use to calculate the value of f(x) for small values of x (below about 1.0E-7 radians) in a Java program. Remember that cos(x) approaches 1, as x approaches zero. For example Math.cos(1.0E-7) evaluates to 0.999999999999995. f ( x) = 1 cos( x) f ( x) = 2 sin ( x) 1+ cos( x) Why did you choose this equation over the other? (One or two sentences only, please.) As x approaches zero the first equation approaches zero, but the second equation approaches sin 2 (x)/2 instead. The second equation is less affected by round-off error and is thus more likely to provide a more accurate value.

Student Number: Page 3 of 14 Problem 2) [10 marks] A Maclaurin series can be used to calculate e x : e x = 1+ n= 1 n x n! = 1+ 1 x 1! + 2 x 2! + 3 x 3! + 4 x 4! + 5 x 5! +... Write a method that uses the Kahan Summation Algorithm to produce a more accurate value for this sum. The principle behind the algorithm is that each time the sum is calculated the portion of the individual term that is not added to the sum is calculated and then returned back into the calculation of the next term. The method accepts a double value x, and returns the estimated value of e x. Remember that you can use the Math.pow method to calculate x n, as in Math.pow(x, n). public static double exponentialkahan (double num) { double sum = 1; double prevsum; double factorial = 1; double term; int termnum = 1; double remainder = 0; double tempsum; do { prevsum = sum; factorial = factorial * termnum; term = Math.pow(num, termnum) / factorial + remainder; tempsum = sum + term; remainder = term - (tempsum - sum); sum = tempsum; termnum++; while (sum!= prevsum); return sum; // end exponentialkahan

Student Number: Page 4 of 14 Problem 3) [30 marks] For this problem you must write three classes using proper encapsulation: BPMeasurement a class to hold blood pressure data. IllegalBP an exception class thrown by BPMeasurement. BPCollection holds a collection of BPMeasurment objects. Here is a testing class and its output to show how BPCollection works: public class TestBPCollection { public static void main(string[] args) { BPCollection bpdata = new BPCollection(); bpdata.add(140, 70); bpdata.add(135, 60); bpdata.add(172, 80); bpdata.add(130, 65); bpdata.add(143, 72); bpdata.add(160, 81); bpdata.add(170, 90); bpdata.add(240, 70); // an illegal one! bpdata.add(140, 20); // another illegal one bpdata.add(65, 120); // last illegal one bpdata.add(120, 65); bpdata.add(165, 72); System.out.println("\nCollection has " + bpdata.getsize() + " elements:"); System.out.println(bpData); System.out.println("Minimum: " + bpdata.getmin()); System.out.println("Maximum: " + bpdata.getmax()); System.out.println("Average: " + bpdata.getaverage()); // end main // end TestBPCollection /*OUTPUT: Illegal BP measurement: 240 over 70 Illegal BP measurement: 140 over 20 Illegal BP measurement: 65 over 120 Collection has 9 elements: 140 over 70 135 over 60 172 over 80 130 over 65 143 over 72 160 over 81 170 over 90 120 over 65 165 over 72 Minimum: 120 over 65 Maximum: 172 over 80 Average: 148 over 72 */

Student Number: Page 5 of 14 Problem 3, Cont.) A blood pressure measurement consists of two integer numbers the systolic and the diastolic pressures in mmhg. The systolic pressure must be higher than the diastolic and the systolic must be less than 200 and the diastolic greater than 40 mmhg. For your BPMeasurement class write one constructor, both accessors, a tostring, an equals, a clone and a compareto method. The BPMeasurement class must implement the Comparable<BPMeasurement> interface, which will force it to have a compareto method with the following signature: public int compareto(bpmeasurement otherbp) Compare BPMeasurement objects based first on just the systolic pressure. Use the diastolic pressure only if the systolic pressures are the same. Equality for the equals method is defined as both systolic and diastolic pressures being the same. For your BPCollection class, you may use either an array or an ArrayList<BPMeasurement> object as the underlying data structure. To sort the underlying data structure, you may invoke Arrays.sort(array, startindex, finishindex) on the array, where the Arrays class is available from the java.util package. If you use an ArrayList instead, you may invoke Collections.sort(ArrayList). Both ArrayList and Collections are in the java.util package. The sort() methods sort the collection in situ, meaning that the array or ArrayList is passed by reference and is sorted in place. Do not write your own sorting code that is not necessary. Write your exception class below and the other two classes on the following two pages: public class IllegalBP extends Exception { public IllegalBP() { super("illegal blood pressure measurement."); public IllegalBP(String arg0) { super(arg0);

Student Number: Page 6 of 14 Problem 3, Cont.) public class BPMeasurement implements Comparable<BPMeasurement> { private int systolic; private int diastolic; public BPMeasurement (int sys, int dias) throws IllegalBP { if (sys < dias sys > 200 dias < 40) throw new IllegalBP("Illegal BP measurement: " + sys + " over " + dias); systolic = sys; diastolic = dias; public int getsystolic () { return systolic; public int getdiastolic () { return diastolic; public String tostring () { return systolic + " over " + diastolic; public boolean equals (Object o) { if (o instanceof BPMeasurement) { BPMeasurement otherbp = (BPMeasurement)o; return (otherbp.systolic == systolic) && (otherbp.diastolic == diastolic); return false; public BPMeasurement clone () { BPMeasurement bpm = null; try { bpm = new BPMeasurement(systolic, diastolic); catch (IllegalBP e) { return bpm; public int compareto (BPMeasurement otherbp) { if (systolic!= otherbp.systolic) return systolic - otherbp.systolic; else return diastolic - otherbp.diastolic; // end BPMeasurement

Student Number: Page 7 of 14 Problem 3, Cont.) import java.util.arraylist; import java.util.collections; public class BPCollection { private ArrayList<BPMeasurement> bplist = new ArrayList<BPMeasurement>(); public void add (int systolic, int diastolic) { BPMeasurement bpm = null; try { bpm = new BPMeasurement(systolic, diastolic); bplist.add(bpm); catch (IllegalBP e) { System.out.println(e.getMessage()); // end add public BPMeasurement getmin () { Collections.sort(bpList); return bplist.get(0); // end getmin public BPMeasurement getmax () { Collections.sort(bpList); return bplist.get(bplist.size() - 1); // end getmax public int getsize () { return bplist.size(); // end getsize public BPMeasurement getaverage () { int sumsys = 0; int sumdias = 0; for (BPMeasurement bpm : bplist) { sumsys += bpm.getsystolic(); sumdias += bpm.getdiastolic(); BPMeasurement bpavg = null; try { bpavg = new BPMeasurement (sumsys / getsize(), sumdias / getsize()); catch (IllegalBP e) { return bpavg; // end getaverage public String tostring () { String output = ""; for (BPMeasurement bpm : bplist) output += bpm + "\n"; return output; // end tostring // end BPCollection

Student Number: Page 8 of 14 Problem 4) [10 marks] For this problem you are supplied with three classes and an interface. You must write the fourth class, called MissingClass. Here is the interface: public interface Specifications { int CONSTANT = 1000; int methodthree (int numone, int numtwo); // end Specifications interface Here are two hierarchy classes: public abstract class BaseClass { private int basenum; public BaseClass (int basenum) { this.basenum = basenum; public int getbasenum () { return basenum; public int methodone (int numone) { return numone + basenum; public abstract int methodtwo (int numone); // end BaseClass public abstract class NextClass extends BaseClass { private int nextnum; public NextClass (int numone, int numtwo) { super(numone); nextnum = numtwo; public int methodone () { return getbasenum() + nextnum; // end NextClass

Student Number: Page 9 of 14 Problem 4, Cont.) Here is a testing class whose output shows how MissingClass must operate: public class TestMissingClass { public static void main(string[] args) { MissingClass mc = new MissingClass (1, 20, 300); System.out.println(mc.methodOne(500)); System.out.println(mc.methodTwo(100)); System.out.println(mc.methodThree(200, 400)); // end main // end TestMissingClass /* OUTPUT: 501 1301 621 */ Write the minimal amount of code to satisfy TestMissingClass. MissingClass must extend NextClass and implement Specifications and cannot be abstract. The only mathematical operation used in MissingClass is addition. You cannot use literal values in your expressions. public class MissingClass extends NextClass implements Specifications { private int missingnum; public MissingClass (int numone, int numtwo, int numthree) { super (numone, numtwo); missingnum = numthree; public int methodtwo (int numone) { return CONSTANT + getbasenum() + missingnum; public int methodthree (int numone, int numtwo) { return methodone() + numone + numtwo; // end MissingClass

Student Number: Page 10 of 14 Problem 5) [5 marks] The following GUI code contains an anonymous class: import javax.swing.jframe; import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jlabel; import java.awt.event.actionlistener; // an interface import java.awt.event.actionevent; public class ChangeMessageWindow extends JFrame{ private JLabel message = new JLabel("First Message"); public ChangeMessageWindow() { super(); setsize(300, 300); setdefaultcloseoperation(jframe.exit_on_close); settitle("change Message Window"); add(message, BorderLayout.NORTH); JButton changemessage = new JButton("Change Message"); changemessage.addactionlistener(new ActionListener() { public void actionperformed (ActionEvent e) { message.settext("second Message"); ); add(changemessage, BorderLayout.SOUTH); // end ChangeMessageWindow constructor // end ChangeMessageWindow When the Change Message button is clicked the message label text changes from First Message to Second Message. On the next page, change this program so that the anonymous class is replaced with an inner class. Most of the old code is provided, so you don t have to re-write it.

Student Number: Page 11 of 14 Problem 5, Cont.) Add your new code to the following, where indicated: import javax.swing.jframe; import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jlabel; import java.awt.event.actionlistener; // an interface import java.awt.event.actionevent; public class ChangeMessageWindow extends JFrame{ private JLabel message = new JLabel("First Message"); public ChangeMessageWindow() { super(); setsize(300, 300); setdefaultcloseoperation(jframe.exit_on_close); settitle("change Message Window"); add(message, BorderLayout.NORTH); JButton changemessage = new JButton("Change Message"); changemessage.addactionlistener(new ChangeListener()); add(changemessage, BorderLayout.SOUTH); // end ChangeMessageWindow constructor private class ChangeListener implements ActionListener { public void actionperformed (ActionEvent e) { message.settext("second Message"); // end ChangeListener // end ChangeMessageWindow

Student Number: Page 12 of 14 Problem 6) [5 marks] Here is a complete GUI window definition: import javax.swing.jframe; import java.awt.flowlayout; import java.awt.borderlayout; import javax.swing.jpanel; import javax.swing.jbutton; import javax.swing.jtextfield; import javax.swing.jlabel; public class GUILayout extends JFrame { public GUILayout() { super(); setsize(300, 300); setdefaultcloseoperation(jframe.exit_on_close); settitle("exam Window"); setresizable(false); setlayout(new BorderLayout()); JPanel panel1 = new JPanel(new FlowLayout()); JPanel panel2 = new JPanel(new BorderLayout()); JPanel panel3 = new JPanel(new FlowLayout()); panel1.add(new JButton("Exit Program")); panel2.add(panel1, BorderLayout.EAST); panel3.add(new JLabel("A Label")); panel2.add(panel3, BorderLayout.WEST); add(panel2, BorderLayout.SOUTH); JPanel panel4 = new JPanel(new FlowLayout()); panel4.add(new JLabel("Another Label")); panel4.add(new JTextField("Text Entry")); add(panel4, BorderLayout.NORTH); JPanel panel5 = new JPanel(new FlowLayout()); panel5.add(new JButton("Another Button")); add(panel5, BorderLayout.CENTER); // end GUILayout constructor // end GUILayout In the empty frame on the next page, sketch what you think this window will look like when it is shown. Use the following key to draw your components: A label: A textfield: A button: A Label Text Entry Exit Program

Student Number: Page 13 of 14 Problem 6, Cont.)

Student Number: Page 14 of 14 (Extra page)