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.

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.

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.

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.

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

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

5/24/2006. Last Time. Announcements. Today. Method Overloading. Method Overloading - Cont. Method Overloading - Cont. (Midterm Exam!

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

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

1 Short Answer (10 Points Each)

Lecture 3: Java Graphics & Events

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

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

RAIK 183H Examination 2 Solution. November 10, 2014

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

Lecture 5: Java Graphics

Come & Join Us at VUSTUDENTS.net

1. An operation in which an overall value is computed incrementally, often using a loop.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

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

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

CSC 1214: Object-Oriented Programming

Java Programming Lecture 6

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

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

Contents Chapter 1 Introduction to Programming and the Java Language

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

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

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

CS Exam 3 - Spring 2010

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

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

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

We are on the GUI fast track path

University of Cape Town ~ Department of Computer Science. Computer Science 1011H/1016S ~ January Exam

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

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

IT101. Graphical User Interface

AP CS Unit 11: Graphics and Events

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

Fall 2017 CISC124 9/16/2017

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

Midterm assessment - MAKEUP Fall 2010

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

CS1083 Week 2: Arrays, ArrayList

Final Examination Semester 2 / Year 2010

Introduction to Programming Using Java (98-388)

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

CSE wi Final Exam 3/12/18. Name UW ID#

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Final Examination Semester 2 / Year 2011

Fall 2017 CISC124 10/1/2017

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

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

Full file at

CMSC 331 Second Midterm Exam

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

COP 3330 Final Exam Review

DM503 Programming B. Peter Schneider-Kamp.

COURSE DESCRIPTION. John Lewis and William Loftus; Java: Software Solutions; Addison Wesley

CS Exam 3 - Spring 2010

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

(2½ hours) Total Marks: 75

CS506 Web Design & Development Final Term Solved MCQs with Reference

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Window Interfaces Using Swing Objects

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Programming: You will have 6 files all need to be located in the dir. named PA4:

Sample Examination Paper Programming and Software Development

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

H212 Introduction to Software Systems Honors

Final Examination Semester 2 / Year 2012

Full file at

CS 101 Fall 2006 Midterm 3 Name: ID:

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Practice Midterm 1 Answer Key

Transcription:

QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CISC124, WINTER TERM, 2011 FINAL EXAMINATION 7pm to 10pm, 26 APRIL 2011, Ross Gym 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. Student Number: Problem 1: / 10 Problem 4: / 10 Problem 2: / 35 Problem 5: / 15 Problem 3: / 10 Problem 6: / 10 TOTAL: / 90

Student Number: Page 2 of 17 Problem 1) [10 marks] Fundamental Java The following complete program runs without errors. Indicate the output of each println() statement in the box beside the statement: public class Problem1 { public static void main (String[] args) { } // end main } // end Problem1 System.out.println( 1 + 4 * 7 ); System.out.println( (1 + 4) * 7 ); System.out.println( 2-3 * 4-5 ); System.out.println( 18 % 5 ); System.out.println( 2 * Math.pow(2, 3) ); System.out.println( 60 / 3 / 2 + 3 ); System.out.println( 60 / 3 / (2 + 3) ); System.out.println( 99 / 100 ); System.out.println( 100 / 99 ); System.out.println( 99F / 100 ); System.out.println( 99 / 100.0 ); System.out.println( (float)(5 / 20) ); System.out.println( (float)5 / 20 ); System.out.println( 5.5E3 / 1000 ); System.out.println( 2 + 3 + " toes" ); System.out.println( 5 + " toes " + 2 + 3 ); System.out.println( 5 > 3 && 3!= 2 ); System.out.println(!(5 > 3)!(3!= 2) ); System.out.println( 'a' - 'c' ); System.out.println( "No more\nideas!" );

Student Number: Page 3 of 17 Problem 2) [35 marks] Fundamental Java, Text File Input, Encapsulation Text File Input Syntax: You must import java.io.* and java.util.*. Instantiate a FileReader object with a String filename. The constructor can throw a FileNotFoundException if the file is not found or cannot be opened. You will need the close() method of the FileReader object, which can throw an IOException in the unlikely event that the file cannot be closed. Supply the FileReader object to the constructor of a Scanner object. The Scanner object owns several useful methods including hasnextint() and nextint(). The former returns a boolean and the latter an int. The Scanner class and its methods can be used without having to worry about catching exceptions. Problem Summary: A soda bottling plant has been having problems with the exact volume of soda in their 2L bottles. They have installed an optical device which is supposed to measure the exact volume in each bottle. The volume should be in-between 1950 and 2050 ml, inclusive, but sometimes the device spits out numbers that are far outside this range. The device saves its measurements of volume in a text file, with one number per line in the file. Here are examples of the contents of two of these data files: Data1.txt 2003 2007 2010-100 -1080 1993 2001 2000 2015 1985 2000 2004 2011 5000 1956 1990 Data2.txt 2002 2000 2015 1985 2001 2001 2010 2100 2003 1995 2015 2020-1000 2012 2034 1998 2007 Data files can be of any length, but only contain integer numbers, one per line in the file. You must create a class called Volumes that stores and summarizes a set of data from a single file. To be a legal object an instance of your Volume class cannot contain any data values that do not lie between 1950 and 2050 inclusive. A legal object must also contain a minimum of 10 valid volume measurements, but there is no upper limit to the number of measurements. So, your Volume class must have a single public constructor that accepts a single parameter, which is the data filename as a String. The constructor must throw a NoDataException object if the file contains less than 10 valid volume measurements. Accessors in the Volume class will return: The volumes array, consisting of only legal measurements. The size, or number of legal measurements. The minimum legal volume. The maximum legal volume. The average of all legal volumes as a double.

Student Number: Page 4 of 17 Problem 2, Cont.) Your class cannot have any mutators. You must also write a tostring(), an equals(), a compareto() and a clone() method for your class, using the conventions you have been taught. Equality is defined as both Volumes objects being exactly equal. Their arrays should have the same size and the exact same measurements in the same order. Comparison, for compareto(), is based only on the average volume. If compareto() is supplied with a Volumes object which has a higher average volume, it would return -1. If supplied an object with a smaller average volume, then it would return +1 and if the average volumes are exactly equal, it would return 0. You can see the output of the tostring() method in the sample testing program below. As usual, clone() should return a deep copy of the current object. If you code your Volumes class properly, it will produce the same output as that shown appended to the following testing program, which was run using the two data files shown on the previous page. import java.io.filenotfoundexception; public class TestVolumes { public static void main(string[] args) { Volumes vols1 = null; try { vols1 = new Volumes("Data1.txt"); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (NoDataException e) { System.out.println(e.getMessage()); } System.out.println("Data set 1:"); int[] cleandata = vols1.getvolumes(); for (int i = 0; i < cleandata.length; i++) System.out.print(cleanData[i] + " "); System.out.println("\nSize = " + vols1.getsize()); System.out.println("Min = " + vols1.getminvol()); System.out.println("Max = " + vols1.getmaxvol()); System.out.printf("Average = %.1f", vols1.getavgvol()); Volumes vols2 = null; try { vols2 = new Volumes("Data2.txt"); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (NoDataException e) { System.out.println(e.getMessage()); } System.out.println("\n\nData set 2:"); cleandata = vols2.getvolumes(); for (int i = 0; i < cleandata.length; i++) System.out.print(cleanData[i] + " "); System.out.println("\nSize = " + vols2.getsize());

Student Number: Page 5 of 17 Problem 2, Cont.) System.out.println("Min = " + vols2.getminvol()); System.out.println("Max = " + vols2.getmaxvol()); System.out.printf("Average = %.1f", vols2.getavgvol()); System.out.println("\n\nTesting tostring:"); System.out.println(vols1); System.out.println(vols2); System.out.println("\nTesting compareto:"); System.out.println(vols1.compareTo(vols2)); System.out.println(vols2.compareTo(vols1)); System.out.println("\nTesting equals, clone & compareto:"); System.out.println(vols1.equals(vols2)); Volumes vols1clone = vols1.clone(); System.out.println(vols1.equals(vols1Clone)); System.out.println(vols1.compareTo(vols1Clone)); } // end main } // end TestVolumes /* OUTPUT: Data set 1: 2003 2007 2010 1993 2001 2000 2015 1985 2000 2004 2011 1956 1990 Size = 13 Min = 1956 Max = 2015 Average = 1998.1 Data set 2: 2002 2000 2015 1985 2001 2001 2010 2003 1995 2015 2020 2012 2034 1998 2007 Size = 15 Min = 1985 Max = 2034 Average = 2006.5 Testing tostring: 13 volumes, average = 1998.1 15 volumes, average = 2006.5 Testing compareto: -1 1 Testing equals, clone & compareto: false true 0 */ Starting on the next page, write the NoDataException class and then write the Volumes class on the following 3 pages.

Student Number: Page 6 of 17 Problem 2, Cont.) The exception class: The Volumes class:

Student Number: Page 7 of 17 Problem 2, Cont.)

Student Number: Page 8 of 17 Problem 2, Cont.)

Student Number: Page 9 of 17 Problem 2, Cont.)

Student Number: Page 10 of 17 Problem 3) [10 marks] Fundamental Java, Methods and Parameters The following complete program runs without errors. Indicate the output of each println() statement in the box beside the statement: public class Problem3 { public static int method1(int[] array) { array[0] += 10; return array[0]; } // Note that the parameter list for method2 is on two lines: public static int method2(int anum, String astr, int[] array1, float[] array2, int[] array3) { anum = 5000; astr = "Goodbye!"; array1[0] = 100; float[] fnums = {1.5F, 2.5F}; array2 = fnums; array3[0] += 10; return 10 + method1(array3); } public static void main(string[] args) { int num = 1000; String astr = "Hello!"; int[] arr1 = {1, 2, 3}; float[] arr2 = {0.5F, 1.5F}; int[] arr3 = {5, 6, 7}; int retnum = method2(num, astr, arr1, arr2, arr3); System.out.println(num); System.out.println(aStr); System.out.println(arr1[0]); System.out.println(arr2[0]); System.out.println(arr3[0]); System.out.println(retNum); } // end main } // end Problem3

Student Number: Page 11 of 17 Problem 4) [10 marks] Generic Methods Here is a version of binary search written to accept int values: public static int binsearch (int[] a, int key) { int lo = 0; int hi = a.length - 1; int mid = (lo + hi) / 2; while (lo <= hi) { if (key < a[mid]) hi = mid - 1; else if (a[mid] < key) lo = mid + 1; else return mid; mid = (lo + hi) / 2; } // end while return -1; } // end binsearch Write a generic version of the same method designed to operate on arrays of objects of type T. Objects of type T will implement the Comparable<T> interface, which means that objects of type T will have a compareto(t) method that returns a negative number if the supplied object is greater than the object used to invoke compareto().

Student Number: Page 12 of 17 Problem 5) [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. All Boolean comparisons must be carried out before arithmetic operators are evaluated in a Java expression. 2. The && operator always evaluates the expressions on both sides of the operator. 3. Casting operations have the highest precedence in any expression. 4. The ++ post increment operator carries out the increment after the variable is used in an expression. 5. If an expression contains a method call that results in an exception, the assignment operation will still be carried out as the last operation in the expression. 6. Unfortunately the current Java version does not contain the syntax for a for each loop. 7. Switch statements can be used with floating point values to see if they lie in certain, predefined value ranges. 8. Any switch statement construct can be built using a chained if construct. 9. Thrown exceptions will be propagated to the main method if they are not caught in a try/catch block. 10. Random file I/O can only be used to read data from very large files. 11. Storing numeric data in ASCII text format will usually produce a larger file than if the data is stored in Binary format. 12. Random file I/O can be slower than any other file I/O technique. 13. Passing an array into a method involves the creation of a new copy of that array in the activation frame for that method. 14. Automatic un-boxing can simplify the use of Wrapper classes with Generic classes. 15. Wrapper classes only contain static methods and attributes.

Student Number: Page 13 of 17 Problem 5, Cont.) 16. The process of functional decomposition helps to reduce the number of methods contained in a program. 17. Proper JUnit tests can be carried out in any order. 18. The rules of TDD describe how you should write your unit tests to ensure test coverage of methods you have already written. 19. A object that extends JFrame inherits a frame with a title bar and the normal window title bar controls. 20. Components laid down in a JPanel using FlowLayout will always be touching each other. 21. The text attribute of a JLabel in a running GUI program can only be changed in code. 22. There is more than one listener class that can respond to mouse initiated events. 23. System-generated component re-draws invoke the paint(graphics g) method for that component. 24. Code-generated component re-draws can also be carried out by invoking the paint(graphics g) method directly. 25. To invoke a component s repaint() method you must supply the Graphics object associated with that component to the repaint() method. 26. Java is not capable of associating a transparency level with a Color object. 27. You should not attach a window s animation code to a Timer thread since it is a daemon thread. 28. Many listener objects can be attached to a Timer thread. 29. Once an instance of a Thread object is started, it cannot be stopped until the application is finished. 30. It is impossible to convert a Java application into an applet. Applets must be written from scratch.

Student Number: Page 14 of 17 Problem 6) [10 marks] GUI Programming Starting on this page and continuing on the next is a complete GUI class that displays a window without error, when run: import javax.swing.*; import java.awt.*; public class Problem6 extends JFrame { public Problem6() { super(); setsize(540, 600); setdefaultcloseoperation(jframe.exit_on_close); settitle("problem 6"); setlocation(20, 20); int rows = 2; int cols = 4; JPanel jpanel1 = new JPanel(new GridLayout(rows, cols)); JButton[] buttons = new JButton[rows * cols]; for (int i = 0; i < rows * cols; i++) { buttons[i] = new JButton("Button " + i); buttons[i].setfont(new Font("Arial", Font.BOLD, 18)); jpanel1.add(buttons[i]); } JPanel jpanel2 = new JPanel(new FlowLayout(FlowLayout.CENTER)); JLabel[] labels = new JLabel[rows * cols]; for (int i = 0; i < rows * cols; i++) { labels[i] = new JLabel("Label " + i); labels[i].setfont(new Font("Arial", Font.BOLD, 18)); if (i % 2 == 0) labels[i].setvisible(false); jpanel2.add(labels[i]); } add(jpanel1, BorderLayout.NORTH); APanel jpanel3 = new APanel(new BorderLayout()); jpanel3.add(jpanel2, BorderLayout.SOUTH); add(jpanel3, BorderLayout.CENTER); } // end constructor

Student Number: Page 15 of 17 Problem 6, Cont.) private class APanel extends JPanel { public APanel(LayoutManager lm) { super(lm); } public void paint(graphics g) { super.paint(g); int strokesize = 5; int width = getwidth(); int height = getheight(); int gap = 50; BasicStroke stroke = new BasicStroke(strokeSize); Graphics2D g2d = (Graphics2D)g; g2d.setstroke(stroke); g2d.setcolor(color.black); g2d.drawoval(0 + gap, 0 + gap, width - 2 * gap, height - 2 * gap); } // end paint(g) } // end APanel class public static void main(string[] args) { Problem6 prob6 = new Problem6(); prob6.setvisible(true); } // end main } // end Problem6 On the next page, draw the appearance of this window when it is first displayed. Other than for the font size, all numeric dimensions are in pixels. 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 Button

Student Number: Page 16 of 17 Problem 6, Cont.)

Student Number: Page 17 of 17 (Extra page)