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