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.

Similar documents
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.

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

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.

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

ITI Introduction to Computing II

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.

Inheritance (Deitel chapter 9)

Making New instances of Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

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

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

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

ITI Introduction to Computing II

CS 113 PRACTICE FINAL

ITI Introduction to Computing II

Recommended Group Brainstorm (NO computers during this time)

ITI Introduction to Computing II

CS/ENGRD 2110 SPRING 2018

ITI Introduction to Computing II

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

Inheritance Motivation

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

CSE 214 Computer Science II Java Classes and Information Hiding

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

index.pdf January 21,

Chapter 9 - Object-Oriented Programming: Polymorphism

F I N A L E X A M I N A T I O N

Selected Questions from by Nageshwara Rao

Practice Midterm 1 Answer Key

Final Examination Semester 3 / Year 2012

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

CS 101 Fall 2005 Midterm 2 Name: ID:

Chapter 9 - Object-Oriented Programming: Inheritance

Use the scantron sheet to enter the answer to questions (pages 1-6)

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

COMP 250. inheritance (cont.) interfaces abstract classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Computer Programming, I. Laboratory Manual. Final Exam Solution

Operators and Expressions

Practice Midterm 1. Problem Points Score TOTAL 50

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

COP 3330 Final Exam Review

public class SomeClass OtherClass SomeInterface { }

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

Fall 2017 CISC124 9/27/2017

CS 132 Midterm Exam Fall 2004

Week 7 - More Java! this stands for the calling object:

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.

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Comparing Objects 3/14/16

PROGRAMMING FUNDAMENTALS

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

Problem Grade Total

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Do not start the test until instructed to do so!

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CSE 8B Final Exam Fall 2015

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Advanced Placement Computer Science. Inheritance and Polymorphism

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Computer Science 1 Bh

Polymorphism: Inheritance Interfaces

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

Kurt Schmidt. November 13, 2017

1st Semester Examinations CITS1001 3

CMSC 331 Second Midterm Exam

Part 1. Programming Output [Polymorphism]

CSC 1214: Object-Oriented Programming

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

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

Question 2. [5 points] Given the following symbolic constant definition

Inheritance and Polymorphism

The University of Nottingham

AP CS Unit 6: Inheritance Notes

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Transcription:

Page 1 of 16 SOLUTION HAND IN Answers Are Recorded on Question Paper QUEEN'S UNIVERSITY SCHOOL OF COMPUTING CISC212, FALL TERM, 2004 FINAL EXAMINATION 9am to 12noon, 22 DECEMBER 2004 Instructors: Alan McLeod and Nidal Nasser 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 page 16 of the exam. The back of any page can be used for rough work. This exam refers exclusively to the use of the Java language. Comments are not required in the code you write. This is a closed book exam. No computers or calculators are allowed. Student Number: Problem 1: / 12 Problem 4: / 20 Problem 2: / 10 Problem 5: / 16 Problem 3: / 16 Problem 6: / 6 TOTAL: / 80

Student Number: Page 2 of 16 Problem 1) [12 marks] Write the output of the following program in the box beside (or under) each System.out.println() statement. The program compiles and runs without any errors. public class WhatIsOutput { public static void main (String[] args) { //Integer.MAX_VALUE = 2147483647 //Integer.MIN_VALUE = -2147483648 //Double.MAX_VALUE = 1.7976931348623157E308 //Double.MIN_VALUE = 4.9E-324 System.out.println((Integer.MAX_VALUE + 1) == Integer.MIN_VALUE); true System.out.println((Integer.MAX_VALUE + 1) == Integer.MAX_VALUE); false System.out.println((Double.MAX_VALUE + 1) == Double.MAX_VALUE); true System.out.println(Double.MIN_VALUE / 2); System.out.println(1.0 / 0); 0.0 Infinity System.out.println(Math.sqrt(-1.0)); System.out.println((1E10 + 1E-5) == 1E10); System.out.println((1E10 + 1E-8) == 1E10); NaN false true System.out.println((1E-10 + 1E-30-1E-10) == 1E-30); false System.out.println((1E-10-1E-10 + 1E-30) == 1E-30); true

Student Number: Page 3 of 16 Problem 1 Cont.) double sum = 0; for (int i = 1; i <= 100; i++) sum += 0.1; System.out.println(sum == 10.0); false System.out.println(Math.abs(sum - 10) < 1e-6); // end main true // end WhatIsOutput Problem 2) [10 marks] For this problem you must examine the following four classes, ClassA, ClassB, ClassC and TestClass. You must provide the output of the main method in TestClass. These classes compile and run without any errors. public abstract class ClassA { private int num1; private int num2; public ClassA (int n1, int n2) { num1 = n1; num2 = n2; public abstract int sumnums(); public abstract int subnums(); public abstract int subnums(char c); public int timesnums() { return num1 * num2; public int getnum1 () { return num1; public int getnum2 () { return num2; // end ClassA

Student Number: Page 4 of 16 Problem 2 Cont.) public class ClassB extends ClassA { private int num1; public ClassB (int n1, int n2, int n3) { super(n1, n2); num1 = n3; public ClassB (int n) { super(n, n + 1); num1 = n; public int sumnums () { return num1 + getnum1() + getnum2(); public int timesnums() { return super.timesnums() * num1; public int subnums () { return num1 - super.getnum2() - super.getnum1(); public int subnums (char dir) { if (dir == 'f') return super.getnum1() - super.getnum2() - num1; else return subnums(); public int getnum1 () { return num1; // end ClassB public class ClassC extends ClassB { private int num1; public ClassC (int n1, int n2, int n3, int n4) { super(n1, n2, n3); num1 = n4;

Student Number: Page 5 of 16 Problem 2 Cont.) public ClassC (int n) { super(n); num1 = n; public int timesnums() { return super.timesnums() * num1; // end ClassC public class TestClass { public static void main (String[] args) { ClassA b = new ClassB(2, 3, 4); System.out.println(b.getNum1()); System.out.println(b.sumNums()); System.out.println(b.timesNums()); System.out.println(b.subNums('f')); System.out.println(b.subNums()); ClassA c = new ClassC(3); System.out.println(c.timesNums()); System.out.println(c.sumNums()); System.out.println(c.getNum1()); System.out.println(c.getNum2()); ClassA cc = new ClassC(5, 6, 7, 8); System.out.println(cc.sumNums()); 4 11 24-5 -1 108 10 3 4 20 // end main // end TestClass

Student Number: Page 6 of 16 Problem 3) [16 marks] The SolidSphere class must extend the Sphere class and implement the Shape interface, given below: public interface Shape { double PI = Math.PI; // The circumference is pi * diameter double circumference (); // The surface area of a sphere is 4*pi*r*r double area (); // The volume of a sphere is 4*pi*r*r*r/3 double volume (); // Returns true if a rectangle of size length by width fits within // the sphere boolean fits (double length, double width); // end Shape interface public class Sphere { private double r; public Sphere (double radius) { setradius(radius); public double getradius () { return r; private void setradius (double radius) { r = radius; // end Sphere These two classes compile without problems and cannot be changed. The following page contains the code for a SolidSphere class that does not compile. Below the incorrect code, list all the compilation errors that you can find. On the next page, write your own version of SolidSphere that will compile.

Student Number: Page 7 of 16 Problem 3 Cont.) // Does not compile! public class SolidSphere extends Sphere implements Shape { private String m; public SolidSphere (double radius, String material) { m = material; PI = Math.PI; public SolidSphere (double radius) { m = "wood"; PI = Math.PI; private double volume () { return 4.0 * PI * Math.pow(r, 3) / 3.0; private boolean fits (double l, double w, double h) { double diagonal = Math.sqrt(Math.pow(l, 2) + Math.pow(w, 2)); return diagonal <= 2 * r; // end SolidSphere List errors here: - No call to super in constructors. - Can t change PI - volume and fits cannot be private - Must have circumference and area methods

Student Number: Page 8 of 16 Problem 3 Cont.) Write a correct version of SolidSphere here. You can continue onto the next page, if necessary. public class SolidSphere extends Sphere implements Shape { private String m; public SolidSphere (double radius, String material) { super(radius); m = material; public SolidSphere (double radius) { super(radius); m = "wood"; public double circumference () { return 2.0 * PI * getradius(); public double area () { return 4.0 * PI * Math.pow(getRadius(), 2); public double volume () { return 4.0 * PI * Math.pow(getRadius(), 3) / 3.0; public boolean fits (double l, double w) { double diagonal = Math.sqrt(Math.pow(l, 2) + Math.pow(w, 2)); return diagonal <= 2 * getradius(); // end SolidSphere // end SolidSphere

Student Number: Page 9 of 16 Problem 4) [20 marks] The Java API for the Arrays.sort(Object[] a) method in the java.util package states: All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareto(e2) must not throw a ClassCastException for any elements e1 and e2 in the array). Here is the code for the Comparable interface: package java.lang; public interface Comparable { public int compareto(object o); You must write the code for the Point class on the next page. You do not need to write code for any other classes. An array of Point objects must be sortable by the Arrays.sort() method. You must also override the equals() method of the Object class, so that search techniques can be used. Here are some more design details: - The Point class holds two private double attributes, x and y, that represent the Cartesian coordinates of a point on a plane. - You do not need to carry out any error checking on these two values any values are acceptable. - Equality is defined as the x and y values agreeing with the values of another Point object to within 1 part per million (1x10-6 ). - The signature for the equals method is public boolean equals (Object o). - Point objects will be ordered by the distance of a Point from the origin, where this distance is defined as Math.sqrt(x*x + y*y). Points closer to the origin must come before Points that are further away, when an array of Points is sorted. So, compareto will return 1 if the current Point is closer to the origin than the Point supplied to the method. - If the Object supplied to the equals() method is not a Point, return false. - If the Object supplied to the compareto method is not a Point, return 999. - Write a single, full parameter constructor only. - Do not write any mutator methods. - None of the other standard ADT methods (such as tostring()) are required. - Do not write any other classes or a main method.

Student Number: Page 10 of 16 Problem 4 Cont.) Write your Point class here and continue onto the next page, if necessary: public class Point implements Comparable { private double x, y; public Point (double xvalue, double yvalue) { x = xvalue; y = yvalue; public double getx () { return x; public double gety () { return y; public boolean equals (Object o) { if (o instanceof Point) { Point p = (Point)o; if ((Math.abs(p.getX() - x) < 1e-6) && (Math.abs(p.getY() - y) < 1e-6)) return true; else return false; return false; // end equals public int compareto (Object o) { if (o instanceof Point) { Point p = (Point)o; double r = Math.sqrt(x * x + y * y); double ro = Math.sqrt(Math.pow(p.getX(), 2) + Math.pow(p.getY(), 2)); if (r < ro) return -1; if (Math.abs(r - ro) < 1e-6) return 0; else return 1; return -999; // end compareto //endpoint