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.

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.

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.

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

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.

Midterm assessment - MAKEUP Fall 2010

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

Final Examination Semester 2 / Year 2012

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

Final Examination Semester 2 / Year 2010

Introduction to the JAVA UI classes Advanced HCI IAT351

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

Introduction to Programming Using Java (98-388)

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

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

Chapter 4 Defining Classes I

DM503 Programming B. Peter Schneider-Kamp.

Fall 2017 CISC124 10/1/2017

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

Window Interfaces Using Swing Objects

Final Examination Semester 2 / Year 2011

Lecture 5: Java Graphics

RAIK 183H Examination 2 Solution. November 10, 2014

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.

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!

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

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

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Dr. Hikmat A. M. AbdelJaber

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

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. Chapter 12

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

Graphics User Defined Forms, Part I

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

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Window Interfaces Using Swing Objects

Come & Join Us at VUSTUDENTS.net

University of Cape Town Department of Computer Science Computer Science CSC1017F

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

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

To gain experience using GUI components and listeners.

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

Chapter 6 Introduction to Defining Classes

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

Java Programming Lecture 6

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

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

Graphical User Interfaces 2

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

State Application Using MVC

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.

CMSC 331 Second Midterm Exam

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

Lecture 3: Java Graphics & Events

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

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

Assignment 3: Inheritance

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

EECS168 Exam 3 Review

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

Pace University. Fundamental Concepts of CS121 1

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

8. Polymorphism and Inheritance

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

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

Project #1 Computer Science 2334 Fall 2008

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Contents Chapter 1 Introduction to Programming and the Java Language

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

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

This page intentionally left blank

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Practice Midterm 1. Problem Points Score TOTAL 50

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Chapter 13 Lab Advanced GUI Applications

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Transcription:

QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CISC124, FALL TERM, 2013 FINAL EXAMINATION 7pm to 10pm, 18 DECEMBER 2013 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: / 20 Problem 4: / 15 Problem 2: / 20 Problem 5: / 10 Problem 3: / 25 TOTAL: / 90 This material is copyrighted and is for the sole use of students registered in CISC124 and writing this exam. This material shall not be distributed or disseminated. Failure to abide by these conditions is a breach of copyright and may also constitute a breach of academic integrity under the University Senate's Academic Integrity Policy Statement.

Student Number: Page 2 of 20 Problem 1) [20 marks]: Concepts Answer the following questions as briefly as possible: List all five integer primitive types in Java: Name the four types of loops available in Java: What is the difference between what a continue statement does and what a break statement does in the context of loops? What is the difference between a mutable and an immutable object? Give an example of each using types already defined in Java. How do you overload methods in Java?

Student Number: Page 3 of 20 Problem 1, Cont.) What should a method do if it cannot carry out its task with the arguments supplied to it? What is a privacy leak in the context of encapsulation? Why is it better to use a generic method instead of a method typed to use the Object class? How does a modal window behave compared to a modeless window? How many bits are required to specify a Color that contains a transparency level (or an alpha value)?

Student Number: Page 4 of 20 Problem 2) [20 marks]: Fundamental Java Here is a reminder of some Java API syntax that may be useful in this problem: The Files class has the static methods:.newbufferedreader(path_object, charset_object) which returns a BufferedReader object that should be used as a resource in a Java 7 try-with-resources block..newbufferedwriter(path_object, charset_object) which returns a BufferedWriter object to be used as a resource in a Java 7 try-with-resources block..readalllines(path_object, charset_object) which returns a List<String> object consisting of the entire file contents. A List<String> object can be cast to an ArrayList<String> object. The BufferedReader class has the method:.readline() which returns a line from a text file as a String. The next invocation of this method moves to the next line of the file. The method returns null if it reaches the end of the file. The BuffereWriter class has the void return method:.write(line_of_text) which writes the line of text supplied as a String to a file. All of the methods listed above can throw an IOException which must be caught. If an IOException is caught, just display an error message to the console and exit the method. An ArrayList<> object has the methods:.size() returns the number of elements in the collection..get(index) returns the element at location index..add(element) adds the element to the end of the collection. A Charset object is created using: Charset charset = Charset.forName("US ASCII"); A Path object is returned by the code Paths.get(filename), where filename is the name of a file as a String. One constructor of a StringTokenizer object accepts a String to be tokenized and another String representing the delimiters to be used to tokenize the supplied String. The StringTokenizer class has the methods:.counttokens() returns the number of tokens contained in an instance of a StringTokenizer object..nexttoken() - returns the next token from a StringTokenizer object as a String, removing that token from the object..hasmoretokens() returns true if there are still one or more tokens remaining in the StringTokenizer object.

Student Number: Page 5 of 20 The Double class has the static method:.parsedouble(a_num) which returns a double obtained from converting the String a_num to a double value. The method throws a NumberFormatException if the String cannot be converted, but you can invoke the method without having to catch this exception. The String class has the methods:.indexof(a_char) returns the position of the first occurrence of a_char in the String. If a_char is not found 1 is returned..indexof(a_char, pos) returns the position of a_char searching from position pos..substring(pos) returns the substring starting at position pos and going to the end of the String..substring(pos1, pos2) returns the substring starting from pos1 and going to (pos2 1)..trim() returns a string where leading and trailing whitespace has been removed. You will need to use many, but probably not all of the methods listed above in this problem. To complete the problem you need to write four methods to be used in a program that reads a text file containing comma-delimited data in rows and columns, creates a 2D array of double out of that data, rotates the data (rows become columns, columns become rows) and then writes the data to another text file. For example, if you have a file Data.csv that contains: 2.36,3.04,0.17,0.13,0.04,0.02,6.88 2.58,2.77,0.15,0.12,0.03,0.02,6.28 2.47,2.96,0.12,0.12,0.04,0.01,6.82 2.49,2.94,0.19,0.11,0.03,0.02,6.13 2.28,2.75,0.12,0.08,0.03,0.02,6.31 2.21,2.78,0.14,0.10,0.02,0.02,6.83 2.52,3.00,0.10,0.24,0.03,0.02,6.45 2.64,3.01,0.21,0.15,0.03,0.02,6.86 2.24,2.80,0.23,0.22,0.02,0.01,6.45 2.11,2.62,0.13,0.08,0.03,0.02,6.19 And, if you invoked the methods as shown in this fancy line of code: writedata("dataout.csv", rotatearray(processcontents(readfile("data.csv")))); The file DataOut.csv would contain: 2.36,2.58,2.47,2.49,2.28,2.21,2.52,2.64,2.24,2.11 3.04,2.77,2.96,2.94,2.75,2.78,3.00,3.01,2.80,2.62 0.17,0.15,0.12,0.19,0.12,0.14,0.10,0.21,0.23,0.13 0.13,0.12,0.12,0.11,0.08,0.10,0.24,0.15,0.22,0.08 0.04,0.03,0.04,0.03,0.03,0.02,0.03,0.03,0.02,0.03 0.02,0.02,0.01,0.02,0.02,0.02,0.02,0.02,0.01,0.02 6.88,6.28,6.82,6.13,6.31,6.83,6.45,6.86,6.45,6.19

Student Number: Page 6 of 20 So, you need to write four complete methods: readfile accepts a String filename as a parameter and returns the contents of that file as an ArrayList<String>. processcontents accepts an ArrayList<String> object as a parameter and returns a 2D array of double with the same row, column structure as the data in the file. rotatearray accepts a 2D array of double as a parameter and turns it 90 degrees clockwise, so that the rows are now columns and the columns are now rows, returning this array. writedata accepts a 2D array of double as well as a String filename as parameters and then saves the data in comma-delimited format to the text file. Do not write any other methods than these four, and do not assume the existence of any class attributes. Your methods must work for any number of rows and columns of data. You may assume: The file will always contain at least two rows and two columns of data. The input file will always exist and you will always have read and write privilege for the files. The filename will always contain whatever path information is required to locate the file, and will never be null or empty. The data will always be in the comma-delimited text format shown in the above example. Each line in the file will have the same format, the same number of numbers and there will not be any empty lines. Each individual number string (a token ) will convert properly to a double value. You need to retain every digit in the double value. Please write the methods in the order given above starting below and continuing onto the next two pages, as needed.

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

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

Student Number: Page 9 of 20 Problem 3) [25 marks]: Encapsulation For this problem you need to build a few classes that are going to be used in a graphical plotting program. The longest class is called Digitized. The purpose of this class is to create and store an array of pixel coordinates for a mathematical equation which eventually will be rendered as a curve in a panel in a GUI. The single constructor for Digitized takes three parameters: An instance of a class that implements the Equation interface. The width of the panel in pixels that must lie between 80 and 800 pixels. The height of the panel in pixels that must lie between 80 and 800 pixels. This constructor must throw an instance of an exception class called IllegalPanel if either of the panel dimensions is not legal, with an appropriate message. You do not have to carry out any checks on the Equation object. The class must have a single mutator that accepts both the width and the height of the panel. The mutator must also throw an exception if either of the supplied dimensions are not legal. When a Digitized object is instantiated or modified it must calculate and store an array of Point objects, where each Point is a pixel coordinate pair that corresponds to the points generated by the supplied equation for the supplied bounds. The Digitized class must have an accessor for this array of Points that will prevent any privacy leaks. You will need to generate a Point for every pixel in the horizontal direction of the panel. For example if the panel width is 400 pixels your array of Points will be of size 400. The last method required for the Digitized class is a tostring() method that returns a listing of every twentieth point in the array for debugging purposes. An example of this output will be provided below. To be clear your Digitized class does not plot or draw anything, it just generates and stores the array of Points that will eventually be used by some GUI plotting program that you don t need to write! As you will remember from assignment 4, graphics drawing requires pixel coordinates, so you must scale real (x, y) coordinates to pixel coordinates so that they can be used without further conversion for eventual drawing to the panel. This is the purpose of the Digitized class. Here is the Equation interface: public interface Equation { double getxmin(); double getxmax(); double getymin(); double getymax(); double evaluate(double x); // end Equation interface

Student Number: Page 10 of 20 Here is a class called Parabola that implements the Equation interface: public class Parabola implements Equation { public double getxmin() { return 0; public double getxmax() { return 5; public double getymin() { return evaluate(0); public double getymax() { return evaluate(5); public double evaluate (double x) { return 2 * x * x; // end Parabola class As you can see this equation is y = 2x 2 and it is defined for 0 x 5. For example, consider an instance of the Digitized class which is created with an instance of this Parabola object and 400 for both the panel width and the panel height. If this instance of the Digitized class is printed to the console (using the tostring() method) it would display: Every 20th point: (0, 400) (20, 399) (40, 396) (60, 391) (80, 384) (100, 375) (120, 364) (140, 351) (160, 336) (180, 319) (200, 300) (220, 279) (240, 256) (260, 231) (280, 204) (300, 175) (320, 144) (340, 111) (360, 76) (380, 39) These Points consist of pixel positions as in (horizontal, vertical). If this was plotted on the screen the lower, left corner of the panel would correspond to the (0, 0) position in real coordinates and the top, right corner of the panel would correspond to the (5, 50) position in real coordinates. The Digitized class will need two other supporting classes that you also have to write. The shortest one is the Exception class called IllegalPanel. You must also write the Point class that will be used by Digitized.

Student Number: Page 11 of 20 The purpose of the Point class is to store two int type pixel coordinates. The constructor of Point takes two int values, the x value and the y value in screen coordinates. No checks on these values are needed, so the constructor of Point will not throw an exception. You will need accessors and mutators for both x and y values. Write a clone method for Point, as well as a tostring() method that shows a point as it appears as a single line from the sample output shown on the previous page ( (160, 336), for example). Finally you must write an equals method for the Point class that overrides the equals method inherited from the base Object class. You won t need this equals() method in Digitized so the purpose of this method is just to demonstrate that you know how to write a proper equals() method! Equality for a Point object is defined as both x and y values being exactly equal. You can now see that the underlying data structure used in Digitized will be typed as Point[]. This will also be the return type for the accessor in Digitized. Write the IllegalPanel class below:

Student Number: Page 12 of 20 Problem 3, Cont.) Write the Point class here:

Student Number: Page 13 of 20 Problem 3, Cont.) Write the Digitized class here and continue on the next page:

Student Number: Page 14 of 20 Problem 3, Cont.)

Student Number: Page 15 of 20 Problem 4) [15 marks]: Inheritance Here are three classes that compile without error: public abstract class ClassEh { public String methodone(string one) { return "unladen " + one; public abstract void methodtwo(string one); // end ClassEh public class ClassBee extends ClassEh { public String methodone(string one, String two) { return one + two; public void methodtwo(string one) { System.out.println(one); public int methodthree (String one) { return one.length(); // end ClassBee public class ClassSea extends ClassBee { public String methodone(string one, String two, String three) { return one + two + three; public String methodone(string one) { return "giant " + one; public void methodtwo(string one) { super.methodtwo(one); System.out.println("swallow"); // end ClassSea

Student Number: Page 16 of 20 Problem 4, Cont.) Here is a main method from another class that uses the hierarchy listed above. It has 15 method calls (not counting constructors), but four of them will not compile. Write error beside these four and provide the output of the other 11: public static void main(string[] args) { ClassSea sea = new ClassSea(); System.out.println(sea.methodOne("swallow")); System.out.println(sea.methodOne("fuzzy ", "bunny")); System.out.println(sea.methodOne("giant ", "fuzzy ", "bunny")); sea.methodtwo("laden"); System.out.println(sea.methodThree("laden swallow")); ClassBee seadoo = new ClassSea(); System.out.println(seaDoo.methodOne("giant ", "green ", "bunny")); System.out.println(seaDoo.methodOne("swallow")); System.out.println(seaDoo.methodOne("green ", "banana")); seadoo.methodtwo("laden ", "swallow"); ClassEh seabreeze = new ClassSea(); System.out.println(seaBreeze.methodOne("mouse")); seabreeze.methodtwo("feathered"); System.out.println(seaBreeze.methodThree("fuzzy")); ClassEh beejam = new ClassBee(); System.out.println(beeJam.methodOne("camel")); beejam.methodtwo("fuzzy camel"); ClassEh ehwhat = new ClassEh(); System.out.println(ehWhat.methodOne("elephant")); // end main

Student Number: Page 17 of 20 Problem 5) [10 marks]: GUI Construction The following complete GUI program sprawls over this page and the next and runs without error: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Problem5 extends JFrame { private Box box1; private MyPanel panel; private boolean panelflag = false; private JButton leftbutton; private JButton rightbutton; public Problem5() { super(); int i; setdefaultcloseoperation(jframe.exit_on_close); settitle("problem 5 GUI"); setlocation(200, 200); setsize(600, 500); Font myfont = new Font("Arial", Font.PLAIN, 18); JButton[] group1 = new JButton[4]; box1 = Box.createHorizontalBox(); for (i = 0; i < 4; i++) { group1[i] = new JButton("Button " + i); group1[i].setfont(myfont); box1.add(box.createhorizontalglue()); box1.add(group1[i]); box1.add(box.createhorizontalglue()); JLabel[] group2 = new JLabel[4]; for (i = 0; i < 4; i++) { group2[i] = new JLabel("Label " + i); group2[i].setfont(myfont); group2[i].sethorizontalalignment(swingconstants.center); panel = new MyPanel(); panel.setlayout(new GridLayout(2, 2)); for (i = 0; i < 4; i++) panel.add(group2[i]); JPanel panel2 = new JPanel(new FlowLayout()); leftbutton = new JButton("Left Button"); leftbutton.setfont(myfont); leftbutton.addactionlistener(new ActionListener() {

Student Number: Page 18 of 20 public void actionperformed(actionevent e) { panelflag = true; panel.repaint(); ); rightbutton = new JButton("Right Button"); rightbutton.setfont(myfont); panel2.add(leftbutton); panel2.add(rightbutton); add(box1, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); add(panel2, BorderLayout.SOUTH); // end constructor private class MyPanel extends JPanel { public MyPanel() { super(); public void paint(graphics g) { super.paint(g); if (!panelflag) return; leftbutton.setvisible(false); rightbutton.setvisible(false); g.setcolor(color.black); // (left, top, width, height) g.drawrect(100, 80, 400, 300); box1.setvisible(false); // end MyPanel public static void main(string[] args) { Problem5 p5 = new Problem5(); p5.setvisible(true); // end main // end Problem5 class In the empty frame shown at the top of the next page, sketch the window as it first appears. Do not worry about absolute pixel sizes, the shape of the letters or drawing straight lines, just try to get the relative positions of the components correct and show gaps where they exist. Draw a label as just text and a button as text inside a rectangle. Don t worry about colours or shading.

Student Number: Page 19 of 20 Draw the window again after the button labelled Left Button has been clicked:

Student Number: Page 20 of 20 Extra Page