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

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

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

MIT AITI Swing Event Model Lecture 17

RAIK 183H Examination 2 Solution. November 11, 2013

RAIK 183H Examination 2 Solution. November 10, 2014

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

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

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

We are on the GUI fast track path

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

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CMP 326 Midterm Fall 2015

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

Swing from A to Z Some Simple Components. Preface

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

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

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

Attempt FOUR questions Marking Scheme Time: 120 mins

H212 Introduction to Software Systems Honors

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

SINGLE EVENT HANDLING

CSE 143 Lecture 20. Circle

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

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

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

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

Final Examination Semester 2 / Year 2011

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

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

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

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

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

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

Graphics User Defined Forms, Part I

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

Midterm assessment - MAKEUP Fall 2010

JAVA NOTES GRAPHICAL USER INTERFACES

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSIS 10A Assignment 7 SOLUTIONS

Java - Applets. public class Buttons extends Applet implements ActionListener

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Final Examination Semester 2 / Year 2010

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

GUI Forms and Events, Part II

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Project 1. LibraryTest.java. Yuji Shimojo CMSC 335

AP CS Unit 11: Graphics and Events

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

Graphical User Interface

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Fall 2011 Final Test

Window Interfaces Using Swing Objects

Practice Midterm 1. Problem Points Score TOTAL 50

INTRODUCTION TO (GUIS)

University of Cape Town Department of Computer Science Computer Science CSC1017F

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

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

Lab Assignment 13 (week 13)

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

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

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

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Window Interfaces Using Swing Objects

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

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

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

Lecture 3: Java Graphics & Events

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

Points Missed on Page page 1 of 8

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Chapter 13 Lab Advanced GUI Applications

UTM CSC207: Midterm Examination October 28, 2011

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

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)

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.

Packages: Putting Classes Together

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

JApplet. toy example extends. class Point { // public int x; public int y; } p Point 5.2. Point. Point p; p = new Point(); instance,

Final Examination Semester 3 / Year 2008

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

To gain experience using GUI components and listeners.

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Practice Midterm 1 Answer Key

Lecture 5: Java Graphics

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

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

Original GUIs. IntroGUI 1

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

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Introduction to the JAVA UI classes Advanced HCI IAT351

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

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

Transcription:

Midterm Test II 60-212 Object Oriented Programming in Java Computer Science, University of Windsor Fall 2014 Time 2 hours Answer all questions Name : Student Id # : Only an unmarked copy of a textbook on Java, your printed class notes (unmarked) will be allowed. No computers or any other electronic devices allowed. Answer all questions. Note that Question 3 has 2 alternatives. You are advised to answer Alternative I of Question 3, which carries 25 marks. If you fail to answer Alternative I of Question 3, you should try answering Alternative II of that question, which carries only 15 marks. The total number of pages in this test is 9. The test will involve regular expressions. A cheat sheet with selected information from the lecture slides on regular expressions is included. When developing a class definition to address the problem in question 3, you must document your class definitions and follow all programming conventions mentioned in the lectures. (Your documentation will be useful if your code is not correct. If your code is incorrect, you will get part marks, if your documentation shows that your approach has some merit). You must not remove any of the staples before you are authorized to remove it and start writing the test. All your responses must be given in this exam booklet. You may request additional sheets of paper as needed. You may make assumptions which do not violate anything I have specified. It is recommended that you ask me to approve any assumptions you make. 1

Question 1) Consider the class Q1Mid2 given below. If this application is executed, it displays one or more objects of the JFrame class. Show, using a diagram, all the frames displayed and their contents, AFTER ALL THE STEPS given below are carried out: Step 1: Type 12.34 in the frame with the title Q1Mid2FrameII and then press the button Press Me. Step 2: Type abcd in the frame with the title Q1Mid2FrameII and then press the button Press Me. Step 3: Type -.45 in the frame with the title Q1Mid2FrameII and then press the button Press Me. (Note that this input starts with a or dash) import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Q1Mid2 extends JFrame implements ActionListener{ private int currentresult = 0; private int numberenteredbyuser = 0; private JLabel alabel; private JTextField inputarea; private JButton abutton; private JTextArea outputarea; private Q1Mid2 ptr; public Q1Mid2(){ super("q1mid2framei"); setlayout(new FlowLayout()); outputarea = new JTextArea(10, 10); add(outputarea); setsize(220, 220); setvisible(true); public Q1Mid2(Q1Mid2 ptr){ super("q1mid2frameii"); this.ptr = ptr; setlayout(new FlowLayout()); alabel = new JLabel("type input followed by CR"); inputarea = new JTextField(10); add(inputarea); outputarea = new JTextArea(10, 10); add(outputarea); abutton = new JButton("Press Me"); abutton.addactionlistener(this); add(abutton); setsize(160, 280); setvisible(true); 2

public void actionperformed(actionevent e){ String s; double value; s = inputarea.gettext(); inputarea.settext(""); if (valid(s)){ value = Double.parseDouble(s); if (value >= 0){ outputarea.append(value + " is good\n"); else{ outputarea.append(value + " is not good\n"); else { ptr.outputarea.append(s + " is invalid\n"); private boolean valid(string s){ String pattern = "[+-]?\\d*\\.\\d+"; if (s.matches(pattern)){ return true; else { return false; public static void main(string args[]){ Q1Mid2 aframe = new Q1Mid2(); Q1Mid2 bframe = new Q1Mid2(aFrame); Your answer: (Full Marks 20) 3

Marking scheme: Two frames with all GUI in the proper positions following the flowlayout scheme: 6 marks. (Note that the JLabel object was not added. If some one includes that, penalty 1 mark. TextField is blank 2 marks. Each line of output -4 marks X 3 = 12 marks Question 2) Consider the following Java application: import java.util.*; import javax.swing.*; // Bonus marks for missing import 1 mark public class Q2Faulty extends JFrame{ private JTextField inputarea; private JLabel citynamelabel; private JLabel outputlabel; private JButton mybutton; private JTextField outputarea; public Q2Faulty(){ super("ques 2"); setlayout(new FlowLayout()); citynamelabel = new JLabel("Type City name"); add(citynamelabel); inputarea = new JTextField(10); add(inputarea); inputarea.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ // actionperformed has an // empty body 5 marks ); outputlabel = new JLabel("Message is"); add(outputlabel); outputarea = new JTextField(10); add(outputarea); mybutton = new JButton("Clear all"); // No actionlistener added to // mybutton 5 marks add(mybutton); setsize(230, 140); setvisible(true); 4

private boolean validcityname(string cityname){ String cityregularexpression; // must specify an appropriate regular // expression 5 marks if (cityname.matches(cityregularexpression)){ // line 36 return true; else { return false; public static void main(string args[]){ Q2Faulty aframe = new Q2Faulty(); The objective of this application is to initially display an object of class Q2Faulty as shown below. The user is expected to type in the name of a city in inputarea and press Carriage Return. The city name is valid if it contains one, two or three components, where each component starts with an upper case alphabet and contains one or more lower case alphabets. In addition, for city names with two or three components, the first and/or the second component may have a period (i.e., the symbol. ) at the end. If the city name is valid, a message Name is valid should be displayed in output Area as shown below. If the name is invalid, a message Name is not valid should be displayed in output Area as shown below. 5

(Note: This name is invalid since it has only one component that ends with a period). When the user presses the button, both inputarea and outputarea should be cleared. Some valid city names in Ontario are as follows: Windsor, St. Catherines, Sault Ste. Marie, Niagara Falls, Prince Edward County Eclipse indicated there is a error/warning in line 36 (a comment indicates which is line 36) and the message was cityregularexpression may not have been initialized. There may be other problems with the code. Your task is to fix all errors in the application run time or compile time. You must not delete any line or part of a line of the above application. You are only allowed to add one or more lines of code or part of lines of code. Hint: Do not stop after correcting the first mistake. One solution: import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class Q2 extends JFrame{ private JTextField inputarea; private JLabel citynamelabel; private JLabel outputlabel; private JButton mybutton; private JTextField outputarea; public Q2(){ super("ques 2"); setlayout(new FlowLayout()); citynamelabel = new JLabel("Type City name"); add(citynamelabel); inputarea = new JTextField(10); add(inputarea); inputarea.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ String cityname; cityname = inputarea.gettext(); 6 (Full Marks 15)

if (validcityname(cityname)){ outputarea.settext( "Name is valid"); else { outputarea.settext("name is not valid"); ); outputlabel = new JLabel("Message is"); add(outputlabel); outputarea = new JTextField(10); add(outputarea); mybutton = new JButton("Clear all"); mybutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ outputarea.settext(""); inputarea.settext(""); ); add(mybutton); setsize(230, 140); setvisible(true); private boolean validcityname(string cityname){ String cityregularexpression = "([A-Z][a-z]+\\.? ){0,2[A-Z][a-z]+"; // This will point to a // regular expression for valid city names /* St. Catherines Sault Ste. Marie Niagara Falls Prince Edward County Windsor */ if (cityname.matches(cityregularexpression)){ return true; else { return false; public static void main(string args[]){ Q2 aframe = new Q2(); Question 3 alternative a) Consider classes Shape, Rectangle and Interface Sortable given below. You are not allowed to modify these classes in any way. These class definitions have no errors. 7

public interface Sortable { public boolean lessthan(sortable x); public abstract class Shape { private String color; public Shape(String color){ this.color = color; public abstract double getarea(); public String getcolor(){ return color; public abstract class Rectangle extends Shape implements Sortable{ private double width; private double height; public Rectangle(double width, double height, String color){ super(color); this.width = width; this.height = height; public String tostring(){ return "Rectangle with width " + width + " and height " + height + " having color " + getcolor(); public double getarea(){ return width * height; public void changewidth(double newwidth){ width = newwidth; public void changeheight(double newheight){ height = newheight; Your task is to define Square - a concrete subclass of Rectangle as follows. An object of class Square represents a special type of rectangle where, by definition, the width is always the same as the height. Class Square should include the following methods: A constructor which has two parameters i) the colour of the square ( a string such as Red or Black ) and ii) the size of the square, specifying both the width and the height of the square. 8

A tostring method which describes the size of the square, and the colour. See the testing program for the expected output. A lessthan method satisfying the requirements of the interface Sortable specified in Rectangle. An object O1 of class Square is less than an object O2 of class Square, if the area of O1 is less than that of O2. To compute the area of a square, you must use method getarea() of Rectangle. Feel free to include other methods in Square as needed. You are not allowed to have any instance variable or static variable in Square. A tester program to illustrate features of class Square is given below. public class Q3Tester { public static void main(string args[]){ Square s1, s2; s1 = new Square(10.0, "Black"); s2 = new Square(20.0, "Red"); s1.changewidth(12.0); // Since s1 is pointing at an object of class Square, // both width and height of object s1 are now changed to 12.0 s2.changeheight(15.0); // Similarly both width and height of object s2 are now changed to 15.0 if (s1.lessthan(s2)){ // s1.getarea() returns 144.0 and s2.getarea() returns 225.0, // so the condition is true System.out.println(s1 + " is less than " + s2); else { System.out.println(s1 + " is not less than " + s2); Output produced: Square with size 12.0 having color Black is less than Square with size 15.0 having color Red One solution: public class Square extends Rectangle{ // 1 mark // Penalty for adding variables 3 marks public Square(double size, String color){// 1 mark super(size, size, color); // 3 marks public String tostring(){ // 1 mark String s; String tokens[]; double width; s = super.tostring(); tokens = s.split(" "); //extracting the value of width may be done in many ways width = Double.parseDouble(tokens[3]); // 4 marks 9 (Full Marks 25)

return "Square with size " + width + " having color " + getcolor(); // 2 marks public void changewidth(double newwidth){ // 1 mark super.changewidth(newwidth); // 1 mark super.changeheight(newwidth);// 1 mark public void changeheight(double newheight){ // 1 mark super.changewidth(newheight); // 1 mark super.changeheight(newheight); // 1 mark public boolean lessthan(sortable x){ // 1 mark Rectangle temp; if (x instanceof Rectangle){ // may check for Rectangle or Square and down cast 2 marks temp = (Rectangle) x; // to rectangle or Square 1 mark if (this.getarea() < temp.getarea()){ // check if condition satisfied 1 mark return true; // // if so return true 1 mark return false; // Otherwise return false. 1 mark // I have returned false even if x is not instance of Rectangle. This may be handled // differently since I did not specify that 10

Question 3 alternative b) Consider class Person given below. We wish to modify this class to define class SortablePerson, so that an array of objects of class SortablePerson can be sorted, using the year of birth as the primary key and the name as the secondary key, by invoking the method sortanything, that was discussed in class. Interface Sortable is given above and class Sort, which includes static method sortanything is given below. Class PersonTester, given below, may be used to test SortablePerson and obtain a sorted list of objects of class SortablePerson. public class Person { private String name; private int yearofbirth; public Person(String name, int yearofbirth){ this.name = name; this.yearofbirth = yearofbirth; public String tostring(){ return name + " was born in " + yearofbirth; public class Sort { public static void sortanything(sortable objects[], int numobjects){ Sortable temp; for(int i = 0; i < numobjects-1; ++i) for(int j = i+1; j < numobjects; ++j) if(!objects[i].lessthan(objects[j])){ temp = objects[i]; objects[i] = objects[j]; objects[j] = temp; public class PersonTester { public static void main(string args[]){ SortablePerson table[] = new SortablePerson[6]; table[0] = new SortablePerson("John", 1985); table[1] = new SortablePerson("Mary", 1989); table[2] = new SortablePerson("Tom", 1985); table[3] = new SortablePerson("Liz", 1985); table[4] = new SortablePerson("Tony", 1989); table[5] = new SortablePerson("Cathy", 1989); Sort.sortAnything(table, 6); for (SortablePerson p : table){ System.out.println(p); 11

The expected output is given below: John was born in 1985 Liz was born in 1985 Tom was born in 1985 Cathy was born in 1989 Mary was born in 1989 Tony was born in 1989 One solution: (Full Marks 15) public class SortablePerson implements Sortable{ // 2 marks private String name; private int yearofbirth; public SortablePerson(String name, int yearofbirth){// 1 mark this.name = name; this.yearofbirth = yearofbirth; public String tostring(){ return name + " was born in " + yearofbirth; public boolean lessthan(sortable x){ // 1 mark SortablePerson temp; temp = (SortablePerson)x; // 3 marks. Students may choose to test first if this casting is valid if (this.yearofbirth < temp.yearofbirth){ // compare on the basis of primary key 4 marks return true; else if (this.yearofbirth > temp.yearofbirth){ return false; else if (this.name.compareto(temp.name) < 0){ // compare on the basis of secondary // key 4 marks return true; else { return false; 12