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.

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.

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

SampleApp.java. Page 1

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

CSIS 10A Assignment 7 SOLUTIONS

DM503 Programming B. Peter Schneider-Kamp.

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

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

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

CSCI 201L Midterm Written Summer % of course grade

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

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

Example: Building a Java GUI

COMP16121 Sample Code Lecture 1

Example: Building a Java GUI

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

User interfaces and Swing

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

Window Interfaces Using Swing Objects

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.

Dr. Hikmat A. M. AbdelJaber

Java Never Ends MULTITHREADING 958 Example: A Nonresponsive GUI 959

Window Interfaces Using Swing Objects

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

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!

RAIK 183H Examination 2 Solution. November 10, 2014

Midterm assessment - MAKEUP Fall 2010

CSCI 201L Midterm Written SOLUTION Summer % of course grade

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

Graphical User Interface

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

Introduction. Introduction

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

Graphical User Interfaces in Java - SWING

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

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

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

Agenda. Container and Component

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

Chapter 13 Lab Advanced GUI Applications

RAIK 183H Examination 2 Solution. November 11, 2013

AnimatedImage.java. Page 1

State Application Using MVC

Lecture 5: Java Graphics

Java Programming Lecture 6

Answer on question #61311, Programming & Computer Science / Java

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

CS108, Stanford Handout #22. Thread 3 GUI

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

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Chapter 13 Lab Advanced GUI Applications Lab Objectives. Introduction. Task #1 Creating a Menu with Submenus

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Section Basic graphics

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

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

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

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

INTRODUCTION TO (GUIS)

// autor igre Ivan Programerska sekcija package mine;

FirstSwingFrame.java Page 1 of 1

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

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

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

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

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

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

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

Final Examination Semester 2 / Year 2010

Graphical User Interfaces (GUIs)

Assignment 2. Application Development

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

CS Exam 1 Review Suggestions

Based on slides by Prof. Burton Ma

Programmierpraktikum

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

Part I: Learn Common Graphics Components

Transcription:

SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC124, WINTER TERM, 2009 FINAL EXAMINATION 7pm to 10pm, 18 APRIL 2009, Dunning 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 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: / 10 Problem 5: / 10 Problem 3: / 15 TOTAL: / 70

Student Number: Page 2 of 17 Problem 1) [20 marks] Fundamental Java Starting below and continuing in the box on the next page write a static void method called prepare() that accepts a 2D array of int values of any size. The method will then sort this 2D array, in situ, based on the sum of the values in each row, so that the row with the smallest sum ends up at the top of the 2D array and the row with the largest sum is at the bottom of the array. The values within a row will not be moved around only the entire row itself. You may use any sorting algorithm you please. For example, selection sort would be appropriate, where the smallest remaining value in the unsorted portion of a collection is located and then swapped with the value one location above the sorted portion of the collection. For example, if the following 2D collection of numbers is supplied to prepare(): 7 10 11 7 14 20 2 0 1 3 4 1 5 6 7 3 4 8 20 18 19 11 30 12 8 10 7 9 6 5 it will be altered to be: 2 0 1 3 4 1 5 6 7 3 4 8 8 10 7 9 6 5 7 10 11 7 14 20 20 18 19 11 30 12 You may write any other supporting methods you need, but you do not need a main method. If the supplied 2D array has only one row, is empty or is null it is sufficient to just exit the method. public static void swap(int pos1, int pos2, int[] oned, int[][] twod) { int temp = oned[pos1]; oned[pos1] = oned[pos2]; oned[pos2] = temp; int[] temp2 = twod[pos1]; twod[pos1] = twod[pos2]; twod[pos2] = temp2;

Student Number: Page 3 of 17 Problem 1, Cont.) public static int sumrow(int[] oned) { int sum = 0; for (int i = 0; i < oned.length; i++) sum += oned[i]; return sum; public static void prepare(int[][] twod) { if (twod == null twod.length < 2) return; int numrows = twod.length; int i, j, smallestloc; int[] sums = new int[numrows]; for (i = 0; i < numrows; i++) sums[i] = sumrow(twod[i]); for (i = 0; i < numrows - 1; i++) { smallestloc = i; for (j = i + 1; j < numrows; j++) if (sums[j] < sums[smallestloc]) smallestloc = j; if (smallestloc!= i) swap(i, smallestloc, sums, twod);

Student Number: Page 4 of 17 Problem 2) [10 marks] Fundamental Java The main method for this program is on the next page. In the box provided on the next page, complete the console output of this program. The output of the first call to printtwod() is given so you can see how this method works. public class Problem2 { public static int attr = 0; public static int method1(int[][] twod, int[] oned, int nod) { int max = 0; for(int i = 0; i < twod.length; i++) if (sumrow(twod[i]) > max) { max = sumrow(twod[i]); nod = i; // end if for(int i = 0; i < twod[nod].length; i++) { oned[i] = twod[nod][i]; twod[nod][i] = 0; // end for return max; // end method1 public static int sumrow(int[] oned) { int sum = 0; for(int i = 0; i < oned.length; i++) sum = sum + oned[i]; attr = attr + sum; return sum; // end sumrow public static void printoned(int[] oned) { for(int i = 0; i < oned.length; i++) System.out.print(oneD[i] + " "); // end printoned public static void printtwod(int[][] twod) { for(int i = 0; i < twod.length; i++) { printoned(twod[i]); System.out.print("\n"); // end for // end printtwod

Student Number: Page 5 of 17 Problem 2, Cont.) public static void main(string[] args) { int[][] test2d = {{0, 0, 0, 1, {0, 0, 1, 1, {1, 1, 1, 1, {1, 0, 1, 1; int[] test1d = {2, 1, 2, 1; int anum = 0; printtwod(test2d); // This output is supplied System.out.println(method1(test2D, test1d, anum)); printtwod(test2d); printoned(test1d); System.out.println("\naNum = " + anum); System.out.println("attr = " + attr); // end main // end Problem2 0 0 0 1 0 0 1 1 1 1 1 1 1 0 1 1 Complete the output from here down: 4 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 1 1 1 1 1 anum = 0 attr = 17

Student Number: Page 6 of 17 Problem 3) [15 marks] Java Hierarchy Here is the Machine class: public abstract class Machine { private String model; private String manuf; private double cost; public Machine(String manuf, String model, double cost) { this.manuf = manuf; this.model = model; this.cost = cost; @Override public String tostring() { String output = manuf + " " + model + ", $"; output += String.format("%.2f", cost); return output; public abstract String getuse(); // end Machine And here is a class demonstrating the use of the hierarchy: public class Problem3 { public static void main(string[] args) { Machine[] machines = new Machine[5]; machines[0] = new Serger("Janome", "MAGNOLIA7034D", 400, 4); machines[1] = new SewingMachine("Janome", "HT2008", 500); machines[2] = new Embroidery("Janome", "MC11000", 5700, true); machines[3] = new SewingMachine("Singer", "7463", 200); machines[4] = new Embroidery("Kenmore", "Elite", 1200, false); for (int i = 0; i < 5; i++) { System.out.print(machines[i].getUse()); System.out.println(machines[i]); // end main // end Problem3 /* OUTPUT: Serger machine: Janome MAGNOLIA7034D, $400.00, 4 threads. Sewing machine: Janome HT2008, $500.00 Embroidering machine: Janome MC11000, $5700.00, has a touch screen. Sewing machine: Singer 7463, $200.00 Embroidering machine: Kenmore Elite, $1200.00, does not have a touch screen. */

Student Number: Page 7 of 17 Problem 3, Cont.) On this page and the next, write the minimum amount of code for the classes required to complete the hierarchy so that the Problem3 class performs as indicated by the listed output. Assume all parameters will be legal, so no error checking is required. Note that a sewing machine is a machine, a serger is a sewing machine and an embroidering machine is a sewing machine. public class SewingMachine extends Machine { public SewingMachine(String manuf, String model, double cost) { super(manuf, model, cost); public String getuse() { return "Sewing machine: "; // end SewingMachine public class Serger extends SewingMachine { private int numthreads; public Serger(String manuf, String model, double cost, int numthreads) { super(manuf, model, cost); this.numthreads = numthreads; @Override public String tostring() { String output = super.tostring(); output += ", " + numthreads + " threads."; return output; @Override public String getuse() { return "Serger machine: "; // end Serger

Student Number: Page 8 of 17 Problem 3, Cont.) public class Embroidery extends SewingMachine { private boolean hastouchscreen; public Embroidery(String manuf, String model, double cost, boolean hastouchscreen) { super(manuf, model, cost); this.hastouchscreen = hastouchscreen; @Override public String tostring() { String output = super.tostring(); output += ", "; if (hastouchscreen) output += "has a touch screen."; else output += "does not have a touch screen."; return output; @Override public String getuse() { return "Embroidering machine: "; // end Embroidery

Student Number: Page 9 of 17 Problem 4) [15 marks] Threading In assignment 5 the code used to calculate the colours for the Mandelbrot figure was quite time consuming. When it was running, nothing else on the window was responsive. For this problem, you will need to alter the assignment 5 solution code so that the calculation and display operations will run on a separate thread. You create a thread by extending the Thread class. In this thread class you need to override the public void run() method. You will also find the following Thread methods useful: start() starts the thread by executing the run() method of the thread. Once the thread is started it can only be interrupted, it cannot be re-started. So, as long as the run method is executing, the thread will be active. If the run method completes without being interrupted, the thread becomes inactive and cannot be re-started. The thread would have to be re-instantiated to start it again in this case. interrupt() causes an InterruptedException to be thrown. Code inside run() must be prepared to catch this exception and stop the loop used by the thread. sleep(nmilliseconds) used inside the loop used by the thread to set the thread to sleep for a short time so other threads can execute. Will also catch and then propagate the InterruptedException. If you can remember other Thread class methods and think they will be useful feel free to use them. For example the isactive() method might be useful. It returns true if the thread has been started and not interrupted, and false otherwise. The yield() method yields control to any pending threads. On the next page is a listing of the pertinent parts of the assignment 5 sample solution. You will not need any code that is not listed here, so it is represented by a comment instead.

Student Number: Page 10 of 17 Problem 4, Cont.) // All needed imports are carried out public class FractalWindowBufferedImage extends JFrame { private DrawPanel drawingzone; // Other class attributes public FractalWindowBufferedImage() { // Constructor code private class DrawPanel extends JPanel { // DrawPanel constructor, etc. public void paintcomponent(graphics g) { int row, col; Graphics2D g2d = (Graphics2D)g; // Other declarations // Get the height and width of drawingzone BufferedImage pretty = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Iterate through the entire panel, pixel by pixel for (row = 0; row < height; row++) { for (col = 0; col < width; col++) { // Calculate the pixel colour and use setrgb on pretty g2d.drawrenderedimage(pretty, null); // end paint // end DrawPanel // Carried out by exit button private class EndingListener implements ActionListener { public void actionperformed(actionevent e) { System.exit(0); // end actionperformed method // end EndingListener class // Other listeners // end FractalWindowBufferedImage class On the next two pages, write the code you need to add to the above class in order to thread the calculation and display operations currently carried out in paintcomponent(). Identify the area of the class in which you are adding or changing code by using the names: class attribute section, class constructor section, inner classes section, or identify the existing method or inner class that you are modifying by name. Repeat whatever code you need to use to make your intentions clear.

Student Number: Page 11 of 17 Problem 4, Cont.) // Class Attribute Section private CalcThread mythread; private boolean firsttime = true; // Class Constructor Section mythread = new CalcThread(); // In paintcomponent() of JPanel: if (firsttime) { mythread.start(); firsttime = false; // Inner Classes Section private class CalcThread extends Thread { public void run() { // The code from above that was in paintcomponent() // Inside the loop: try { sleep(1); catch (InterruptedException e) { break; // end run // end CalcThread // Inside the listener for the exit button: mythread.interrupt();

Student Number: Page 12 of 17 Problem 4, Cont.)

Student Number: Page 13 of 17 Problem 5) [10 marks] Here is a complete GUI program, sprawling over this and the next two pages: import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jlabel; import javax.swing.jtextfield; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.font; public class Problem5 extends JFrame { private final int WINDOW_WIDTH = 300; private final int WINDOW_HEIGHT = 140; private JTextField txtwidth = new JTextField(8); private JTextField txtheight = new JTextField(8); private JLabel lblwidtherror = new JLabel("Illegal Width!"); private JLabel lblheighterror = new JLabel("Illegal Height!"); private JLabel lblresult = new JLabel("Area ="); public Problem5() { super(); setlayout(new BorderLayout()); settitle("problem 5"); setdefaultcloseoperation(jframe.exit_on_close); setsize(window_width, WINDOW_HEIGHT); setresizable(false); setlocation(200, 200); Font myfont = new Font("Arial", Font.BOLD, 16); JPanel toppanel = new JPanel(new FlowLayout()); JPanel centrepanel = new JPanel(new FlowLayout()); JLabel lblwidth = new JLabel("Width:"); lblwidth.setfont(myfont); JLabel lblheight = new JLabel("Height:"); lblheight.setfont(myfont); txtwidth.setfont(myfont); txtheight.setfont(myfont); lblwidtherror.setfont(myfont); lblwidtherror.setvisible(false); lblheighterror.setfont(myfont); lblheighterror.setvisible(false);

Student Number: Page 14 of 17 Problem 5, Cont.) toppanel.add(lblwidth); toppanel.add(txtwidth); toppanel.add(lblwidtherror); centrepanel.add(lblheight); centrepanel.add(txtheight); centrepanel.add(lblheighterror); add(toppanel, BorderLayout.NORTH); add(centrepanel, BorderLayout.CENTER); JPanel bottompanel = new JPanel(new BorderLayout()); lblresult.setfont(myfont); JButton calculatebutton = new JButton("Calculate"); calculatebutton.setfont(myfont); calculatebutton.addactionlistener(new CalculateListener()); JButton exitbutton = new JButton("Close"); exitbutton.setfont(myfont); exitbutton.addactionlistener(new EndingListener()); bottompanel.add(calculatebutton, BorderLayout.WEST); bottompanel.add(lblresult, BorderLayout.CENTER); bottompanel.add(exitbutton, BorderLayout.EAST); add(bottompanel, BorderLayout.SOUTH); // end Problem5 constructor private class EndingListener implements ActionListener { public void actionperformed(actionevent e) { System.exit(0); // end actionperformed method // end EndingListener class

Student Number: Page 15 of 17 Problem 5, Cont.) private class CalculateListener implements ActionListener { public void actionperformed(actionevent e) { boolean widthok, heightok; double width = 0, height = 0, area; lblwidtherror.setvisible(false); lblheighterror.setvisible(false); lblresult.settext(""); widthok = true; try { width = Double.parseDouble(txtWidth.getText()); if (width <= 0) widthok = false; catch(numberformatexception except) { widthok = false; heightok = true; try { height = Double.parseDouble(txtHeight.getText()); if (height <= 0) heightok = false; catch(numberformatexception except) { heightok = false; if (!widthok) lblwidtherror.setvisible(true); if (!heightok) lblheighterror.setvisible(true); if (widthok && heightok) { area = width * height; lblresult.settext("area = " + area); // end actionperformed method // end CalculateListener class public static void main(string[] args) { Problem5 gui = new Problem5(); gui.setvisible(true); // end main // end Problem5

Student Number: Page 16 of 17 Problem 5, Cont.) Sketch the appearance of this window for each of the three following cases. Use the following key to draw your components: A Label: A Text Box A Button a) As the window first appears, without any user entry or actions: b) If the user enters 2 for the width and 3 for the height and clicks on Calculate : c) If the user enters Summer for the width and Vacation! for the height and clicks on Calculate :

Student Number: Page 17 of 17 (Extra page)