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.

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.

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

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

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.

SampleApp.java. Page 1

COMP16121 Sample Code Lecture 1

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

Example: Building a Java GUI

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

Example: Building a Java GUI

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

Window Interfaces Using Swing Objects

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

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

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

Selected Questions from by Nageshwara Rao

Final Examination Semester 2 / Year 2010

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

Window Interfaces Using Swing Objects

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

RAIK 183H Examination 2 Solution. November 10, 2014

CSCI 201L Midterm Written Summer % of course grade

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

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

Final Exam CS 251, Intermediate Programming December 13, 2017

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

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

Midterm assessment - MAKEUP Fall 2010

CMP 326 Midterm Fall 2015

EXAMINATIONS 2010 END-OF-YEAR COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN

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

CS Exam 1 Review Suggestions

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

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

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

CSCI 201L Midterm Written SOLUTION Summer % of course grade

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

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS Exam 3 - Spring 2010

Based on slides by Prof. Burton Ma

Graphical User Interface

Class 16: The Swing Event Model

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Agenda. Container and Component

COMP16121 Notes on Mock Exam Questions

Graphical User Interfaces in Java - SWING

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

Dr. Hikmat A. M. AbdelJaber

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

1 Looping Constructs (4 minutes, 2 points)

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

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

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Final Exam CS 251, Intermediate Programming December 10, 2014

RAIK 183H Examination 2 Solution. November 11, 2013

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

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

CMSC 150 Lab 8, Part II: Little PhotoShop of Horrors, Part Deux 10 Nov 2015

CSIS 10A Assignment 7 SOLUTIONS

Abstract Classes and Interfaces

PIC 20A GUI with swing

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

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

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

OLLSCOIL NA héireann THE NATIONAL UNIVERSITY OF IRELAND COLÁISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK. Summer Examination 2012

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

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

Final Examination Semester 2 / Year 2011

State Application Using MVC

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

// autor igre Ivan Programerska sekcija package mine;

DAFTAR LAMPIRAN. Source Code Java Aplikasi Keyword to Image Renamer Split

CS Exam 3 - Spring 2010

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

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

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

More about GUIs GEEN163

Transcription:

QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2010 FINAL EXAMINATION 11 DECEMBER 2010, 9am SOLUTION HAND IN Answers Are Recorded on Question Paper 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 and write your student number on each page. 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. Student Number: Problem 1: / 20 Problem 6: / 10 Problem 2: / 10 Problem 7: / 20 Problem 3: / 10 Problem 8: / 20 Problem 4: / 10 Problem 9: / 10 Problem 5: / 10 TOTAL: / 120

Problem 1) [20 marks] The following complete program runs without error. Write the output for each println() statement beside that statement in the box provided: public class Problem1 { public static void main(string[] args) { System.out.println(5 * 2-3); System.out.println(7 >= 6 && 5!= 3); System.out.println(5.0 / 10); System.out.println(5 / 10); System.out.println(1 / 2 * 10); System.out.println(1 / 2 * 10.0); System.out.println((double)(1 / 2) * 10); System.out.println(10 * 1 / 2); System.out.println(10.0 / 0.0); System.out.println(5!= 7 3 <= 10 7 > 20); System.out.println(Math.sqrt(-1.0F)); System.out.println("100" + 10 + 1); 7 true 0.5 0 0 0.0 0.0 5 Infinity NaN 100101 System.out.println(Double.MAX_VALUE + 1 == Double.MAX_VALUE); true true System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE); true System.out.println(0x10); System.out.println(0xA); System.out.println(5 * (2-3)); System.out.println(100 + 10 + "1"); System.out.println(010); System.out.println(0x10 + 010); // end main // end Problem1 16 10-5 1101 8 24

Problem 2) [10 marks] In the box provided below write the output of the second call to showarray() in the following complete program which runs without error. The output of the first call to showarray() is provided. public class Problem2 { public static void showarray(int[][] data) { int row, col; for (row = 0; row < data.length; row++) { for (col = 0; col < data[row].length; col++) System.out.printf("%4d", data[row][col]); System.out.println(); // end showarray public static void fiddle(int[][] data) { int row, col; for (row = 0; row < data.length; row++) for (col = 0; col < data[row].length; col++) if (data[row][col] > 0) data[row][col] = 10; else data[row][col] = row; // end fiddle public static void main(string[] args) { int[][] test = {{1, 0, 0, 0, 1, {0, 0, 0, 0, 0, {0, 0, 1, 0, 0, {0, 0, 0, 0, 0, {1, 0, 0, 0, 1; showarray(test); fiddle(test); showarray(test); // end main // end Problem2 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 10 0 0 0 10 1 1 1 1 1 2 2 10 2 2 3 3 3 3 3 10 4 4 4 10

Problem 3) [10 marks] The following complete program runs without error. Write the output of the program in the box provided. public class Problem3 { public static void showarray(int[] data) { for (int i = 0; i < data.length; i++) System.out.printf("%4d", data[i]); System.out.println(); // end showarray public static void showarray(int[][] data) { for (int i = 0; i < data.length; i++) showarray(data[i]); // end showarray public static int fiddle(int one, String two, int[] three, int[] four, int[][] five) { int sum = 0; int i; int[] inside = {5, 4, 3; for (i = 0; i < three.length; i++) { sum = sum + three[i]; three[i] = three[i] * 2; one = one + sum; two = two.tolowercase(); four = inside; for (i = 0; i < five.length / 2; i++) { inside = five[i]; five[i] = five[five.length - i - 1]; five[five.length - i - 1] = inside; return one; // end fiddle public static void main(string[] args) { int num = 10; String str = "HELLO"; int[] test = {2, 3, 4; int[] nums = {20, 30, 40; int[][] test2d = {{1, 2, 3, {4, 5, 6, {7, 8, 9; int result = fiddle(num, str, test, nums, test2d); System.out.println(num); System.out.println(str); showarray(test); showarray(nums); showarray(test2d); System.out.println(result); // end main // end Problem3 10 HELLO 4 6 8 20 30 40 7 8 9 4 5 6 1 2 3 19

Problem 4) [10 marks] The following complete program runs without error. Write the output of the program in the box provided. Write the name of the sorting algorithm used here: public class Problem4 { public static void showarray(int[] data) { for (int i = 0; i < data.length; i++) System.out.printf("%4d", data[i]); System.out.println(); // end showarray public static void swap(int p1, int p2, int[] data) { int temp = data[p1]; data[p1] = data[p2]; data[p2] = temp; // end swap public static int findmax(int start, int[] data) { int max = start; for (int i = start; i < data.length; i++) if (data[i] > data[max]) max = i; return max; // end findmax public static void sort(int[] data) { int max; for (int i = 0; i < data.length - 1; i++) { max = findmax(i, data); if (max!= i) swap(i, max, data); showarray(data); // end sort public static void main(string[] args) { int[] values = {4, 12, 1, 3, 11, 8, 7, 6; showarray(values); sort(values); // end main // end Problem4 Selection Sort 4 12 1 3 11 8 7 6 12 4 1 3 11 8 7 6 12 11 1 3 4 8 7 6 12 11 8 3 4 1 7 6 12 11 8 7 4 1 3 6 12 11 8 7 6 1 3 4 12 11 8 7 6 4 3 1 12 11 8 7 6 4 3 1

Problem 6) [10 marks] In lecture we saw a summation example: 1 n i S where: S n n i n( n 1) 2 i 1 n i 1 Unfortunately, for large values of n, the sum can be far away from one because some terms of the summation are simply too small to make a difference to the sum. The Kahan Summation Algorithm, also called Compensated Summation can solve this problem by changing the way the sum is calculated. 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 summed back into the next term. In the box provided, write a method that returns the value of this summation in the most accurate way possible using the Kahan Summation Algorithm. Write just the method, no other code such as main is required. Use a double for your sum, and note that the method accepts n as a parameter. public static double summationkahan (int numterms) { double sum = 0; double calcsum; int i; double tempsum; double term; double termportion; double remainder = 0; calcsum = numterms * (numterms + 1.0) / 2.0; for (i = 1; i <= numterms; i++) { term = i / calcsum + remainder; tempsum = sum + term; termportion = tempsum - sum; remainder = term - termportion; sum = tempsum; return sum;

Problem 7) [20 marks] Write a fully encapsulated class called Toothpaste and an exception class called ITException. Here are the attributes that describe a tube of Toothpaste: Brand : Crest or Colgate. Size : between 50 and 150 ml inclusive. Whitener : true or false, the default is false (no whitener). No other attributes are required. A Toothpaste object can be created with values for all three attributes or with just the brand and size, in which case the whitener attribute is assumed to be false. If an attempt is made to create a Toothpaste object, or modify an attribute with illegal values, then the constructors and mutators must throw an ITException with a relevant message. Write public mutators and accessors for all attributes. Here are a couple of examples of how you should represent your Toothpaste object as a String: Crest toothpaste, 100 ml size, with whitener. Colgate toothpaste, 125 ml size, without whitener. The Toothpaste class also needs the other standard methods: tostring(), compareto(), equals() and clone(). Toothpaste objects will be compared (for sorting, for example) on the basis of their size only. Equality is defined as all attributes being exactly equal. (Use the String.equals() method to compare the String attribute). The clone() method should return a deep copy of the current object. Your equals() method must override the equals() method from the base Object class. Use good encapsulation practices and prevent privacy leaks. Write the exception class on this page and your Toothpaste class on the following three pages. Do not write any other classes. public class ITException extends Exception { public ITException() { super("illegal Toothpaste Object."); public ITException(String message) { super(message); // end ITException

Problem 7, Cont.) public class Toothpaste { private String brand; private int size; private boolean whitener; public Toothpaste(String brand, int size, boolean whitener) throws ITException { setbrand(brand); setsize(size); setwhitener(whitener); public Toothpaste(String brand, int size) throws ITException { this(brand, size, false); public void setbrand(string brand) throws ITException { if (brand.equals("crest") brand.equals("colgate")) this.brand = brand; else throw new ITException("Illegal brand name: " + brand); public void setsize(int size) throws ITException { if (size >= 50 && size <= 150) this.size = size; else throw new ITException("Illegal size: " + size); public void setwhitener(boolean whitener) { this.whitener = whitener; public String getbrand() { return brand; public int getsize() { return size; public boolean getwhitener() { return whitener;

Problem 7, Cont.) public String tostring() { String output = brand + " toothpaste, "; output += size + " ml size, with"; if (whitener) output += " whitener."; else output += "out whitener."; return output; public int compareto(toothpaste othert) { return size - othert.size; public boolean equals(object o) { if (o instanceof Toothpaste) { Toothpaste othert = (Toothpaste)o; return othert.brand.equals(brand) && othert.size == size && othert.whitener == whitener; return false; public Toothpaste clone() { Toothpaste othert = null; try { othert = new Toothpaste(brand, size, whitener); catch (ITException ite) { return othert; // end Toothpaste

Problem 8) [20 marks] Here is a table describing a few fasteners: Fastener: Bolt Nail Staple Screw Bolt Head n/a n/a n/a Phillips n/a Finish no galvanized galvanized bright bright Length (inch) 3 3.5 0.5 2.5 4 Price per lb n/a $10 n/a n/a n/a Price each $0.50 n/a n/a n/a $0.75 Price per 100 $40.00 n/a $2 $4.50 $50.00 n/a means that this attribute is not applicable to this item. You will note that bolts have two prices, a bulk price per 100 and an individual price. As a service to the customer, bolt orders can be sized up in order to get a better price. For example, for the last Bolt in the table, it would be cheaper to price 480 bolts as 5 boxes * $50 (total $250), rather than 4 boxes * $50 + 80 * $0.75 (total $260). Write the minimum amount of code necessary to build a hierarchy to store these fastener descriptions and calculate costs for given order sizes. You are supplied with the base Fastener class, which you cannot change. If you are careful, there will not be any redundancy in the declaration of your attributes. No attribute should be null, empty or zero after instantiation. Do not carry out any error checking on the arguments. Write the minimum amount of code to satisfy the operation of the testing program shown below. The output of this program is shown on the next page. public class Problem8 { public static void showitemcost(fastener fast, int ordersize) { String output = ordersize + fast.getdescription(); output += String.format(" Cost = $%.2f", fast.getcost(ordersize)); System.out.println(output); // end showitemcost public static void main(string[] args) { // end main // end Problem8 // First three parameters are length, finish and unit price: Fastener nails = new Nail(3.5, "galvanized", 10); Fastener bolts1 = new Bolt(3, "no", 0.50, 40); Fastener bolts2 = new Bolt(4, "bright", 0.75, 50); Fastener staples = new Staple(0.5, "galvanized", 2); Fastener screws = new Screw(2.5, "bright", 4.50, "Phillips"); showitemcost(nails, 3); // 3 lbs showitemcost(bolts1, 210); // 210 bolts showitemcost(bolts2, 480); // 480 bolts showitemcost(staples, 4); // 4 boxes of 100 each showitemcost(screws, 3); // 3 boxes of 100 each

Problem 8, Cont.) /*OUTPUT: 3 lb. of 3.5 inch nails, galvanized finish. Cost = $30.00 210 of 3.0 inch bolts, no finish. Cost = $85.00 480 of 4.0 inch bolts, bright finish. Cost = $250.00 4 boxes of 100 each 0.5 inch staples, galvanized finish. Cost = $8.00 3 boxes of 100 each Phillips head 2.5 inch screws, bright finish. Cost = $13.50 */ public abstract class Fastener { private double length; private String finish; private double unitprice; public Fastener(double len, String fin, double price) { length = len; finish = fin; unitprice = price; public double getlength() { return length; public String getfinish() { return finish; public double getcost(int numunits) { return unitprice * numunits; public abstract String getdescription(); // end Fastener public class Nail extends Fastener { public Nail(double len, String fin, double price) { super(len, fin, price); public String getdescription() { return " lb. of " + getlength() + " inch nails, " + getfinish() + " finish.";

Problem 8, Cont.) public class Bolt extends Fastener { private double bulkprice; public Bolt(double len, String fin, double price, double bulk) { super(len, fin, price); bulkprice = bulk; public String getdescription() { return " of " + getlength() + " inch bolts, " + getfinish() + " finish."; public double getcost(int numunits) { int bulknum = numunits / 100; int part = numunits % 100; double cost1 = bulknum * bulkprice + super.getcost(part); double cost2 = (bulknum + 1) * bulkprice; if (cost1 < cost2) return cost1; else return cost2; public class Staple extends Fastener { public Staple(double len, String fin, double price) { super(len, fin, price); public String getdescription() { return " boxes of 100 each " + getlength() + " inch staples, " + getfinish() + " finish."; public class Screw extends Fastener { private String head; public Screw(double len, String fin, double price, String hd) { super(len, fin, price); head = hd; public String getdescription() { return " boxes of 100 each " + head + " head " + getlength() + " inch screws, " + getfinish() + " finish.";

Problem 9) [10 marks] Here is a complete GUI class (which should be familiar to you!) on this and the next page. Indentation has been messed with a bit to allow the code to better fit on the page. import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jlabel; import javax.swing.jtextfield; import java.awt.flowlayout; import java.awt.font; import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class GUIProblem extends JFrame { private final int WIDTH = 800; private final int HEIGHT = 160; private String message1 = "Almost Over"; private String message2 = "VACATION TIME!"; private JLabel messagelabel1 = new JLabel(message1); private JLabel messagelabel2 = new JLabel(message2); public GUIProblem () { super(); setdefaultcloseoperation(jframe.exit_on_close); settitle("gui Problem"); setsize(width, HEIGHT); messagelabel1.setfont(new Font("Lucida Handwriting Italic", Font.PLAIN, 26)); messagelabel2.setfont(new Font("Lucida Handwriting Italic", Font.PLAIN, 26)); JButton switchmessagebutton = new JButton("Switch"); switchmessagebutton.addactionlistener(new MessageButtonListener()); JTextField textbox = new JTextField(8); setlayout(new BorderLayout()); JPanel bottompanel = new JPanel(new FlowLayout()); JPanel midpanel = new JPanel(new FlowLayout()); midpanel.add(messagelabel1); midpanel.add(textbox); midpanel.add(messagelabel2); bottompanel.add(switchmessagebutton); add(midpanel, BorderLayout.CENTER); add(bottompanel, BorderLayout.SOUTH); // end constructor

private class MessageButtonListener implements ActionListener { public void actionperformed (ActionEvent e) { if (messagelabel1.gettext().equals(message1)) { messagelabel1.settext(message2); messagelabel2.settext(message1); else { messagelabel1.settext(message1); messagelabel2.settext(message2); // end actionperfomed // end MessageButtonListener // end GUIProblem class You can assume that there is another class with a main method which instantiates this one and then shows the window. Sketch the appearance of this window after the Switch button has been clicked once: Don t worry about what the font looks like. Use the following key to draw your components, changing the text where necessary: A Label A Text Box A Button