Final Exam May 21, 2003

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

CSE 214 Computer Science II Heaps and Priority Queues

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

Introduction to Computing II (ITI 1121) Final Examination

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

COM S 211/ENGRD 211 May 15, 2003

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

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

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Points off Total off Net Score. CS 314 Final Exam Spring 2016

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

CS 113 MIDTERM EXAM 2 SPRING 2013

Priority Queues. 04/10/03 Lecture 22 1

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

The Nervous Shapes Example

CS100M November 30, 2000

CS 307 Midterm 2 Spring 2008

Priority Queues 1 / 15

Symbol Tables 1 / 15

CS/ENGRD 2110 SPRING 2018

CSE 142 Sp02 Final Exam Version A Page 1 of 14

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

CSE 143 Lecture 20. Circle

Selected Questions from by Nageshwara Rao

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

CMSC132 Summer 2018 Midterm 1

Object Oriented Programming 2015/16. Final Exam June 28, 2016

2. (True/False) All methods in an interface must be declared public.

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Introduction to Programming (CS112): Sample

Practice Midterm 1. Problem Points Score TOTAL 50

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

EXAMINATIONS 2016 TRIMESTER 2

Topic 7: Algebraic Data Types

TA office hours are over after this week, but Dan and Maja will still be around for the next month

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

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

CMSC132 Summer 2018 Midterm 1. Solution

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Programming II (CS300)

CSE 2123: Collections: Priority Queues. Jeremy Morris

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Before class. BSTs, Heaps, and PQs 2/26/13 1. Open TreeNodeExample.java Change main to:

Linked List. ape hen dog cat fox. tail. head. count 5

Array Based Lists. Collections

I have neither given nor received any assistance in the taking of this exam.

CS 61B, Spring 1999 MT3 Professor M. Clancy

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

COS 126 Written Exam 2 (Spring 2015)

CS165 Practice Final Exam

CS100B: Prelim 3 Solutions November 16, :30 PM 9:00 PM

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

CSC Java Programming, Fall Java Data Types and Control Constructs

Name CIS 201 Midterm II: Chapters 1-8

CS 307 Midterm 2 Fall 2008

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

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

Final Examination CS 125 Introduction to Computer Science Fall Hours

Pace University. Fundamental Concepts of CS121 1

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

CSE373 Fall 2013, Midterm Examination October 18, 2013

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

CS 307 Midterm 1 Fall 2007

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Session 04 - Object-Oriented Programming 1 Self-Assessment

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CSE 143 SAMPLE MIDTERM

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

CIS 110 Introduction to Computer Programming Summer 2014 Final. Name:

OBJECT ORİENTATİON ENCAPSULATİON

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

APCS Semester #1 Final Exam Practice Problems

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

CS 163 Practice Final Exam Winter 2012

CS 307 Midterm 1[corrected] Spring 2008

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

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

CMSC132, Practice Questions

CSC 102 Lecture Notes Week 1 Introduction to the Course Introduction to Jav a

CS 307 Midterm 2 Spring 2011

CS 151 Name Final Exam December 17, 2014

CS 113 PRACTICE FINAL

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

1 Constructors and Inheritance (4 minutes, 2 points)

Transcription:

1.00 Introduction to Computers and Engineering Problem Solving Final Exam May 21, 2003 Name: Email Address: TA: Section: You have three hours to complete this exam. For coding questions, you do not need to include comments, and you should assume that all necessary files have already been imported. Final exam is open book, open notes. No laptops, calculators, cell phones or other electronics are allowed. Sharing of notes or books is not allowed. Question Points Question 1 / 5 Question 2 / 10 Question 3 / 20 Question 4 / 10 Question 5 / 10 Question 6 / 25 Question 7 / 20 Total / 100 1.00 Spring 2003 Final Exam 1/17 5/12/2005

Problem 1. Exceptions (5 points) Which of the following statements is true given the program ExceptionTest? (Circle only one answer.) a. The program will not compile. b. The program will compile. If we run it, it will throw an exception before outputting any results. c. The program will compile. If we run it, it will output some results, but then throw an exception. d. The program will compile and run without throwing any exceptions. public class ExceptionTest { public static void main(string args[]) { String[] greek = {"Alpha", "Beta", "Gamma"; System.out.println(greek[1]); System.out.println(greek[3]); 1.00 Spring 2003 Final Exam 2/17 5/12/2005

Problem 2. Abstract Classes & Inheritance (10 points) Enraged by the high price of software, you decide to martyr yourself by writing a free Java version of Photoshop for the world, which you call jimp. You d like for the user to be able to scale, and rotate shapes such as rectangles, triangles, circles, and squares. You decide to create an abstract Shape class to represent the commonalities these shapes will share in your program. // Shape.java 1 public abstract class Shape 2 { 3 // x,y coordinates of upper left corner of Shape's bounding box 4 private int xcoord; 5 private int ycoord; 6 private final String type; // rectangle, circle, triangle, etc 7 8 public Shape(int x, int y, String t) 9 { 10 xcoord=x; 11 ycoord=y; 12 type=t; 13 14 15 // get methods omitted 16 public abstract void scale(double factor); 17 public final void move(int newx, int newy) 18 { 19 xcoord=newx; 20 ycoord=newy; 21 22 23 public String tostring(){ 24 return "SHAPE:"+type+",("+xCoord+","+yCoord+")"; 25 26 You then define a Rotatable interface and a Rectangle class as follows. The Rectangle class contains a main() method for unit testing. 1 // Rotatable.java 2 public interface Rotatable{ 3 public void rotate(double theta); 4 1 // Rectangle.java 2 public class Rectangle extends Shape implements Rotatable 3 { 4 private int width; 5 private int height; 6 1.00 Spring 2003 Final Exam 3/17 5/12/2005

7 // The type of a Rectangle is "rectangle". 8 public Rectangle(int x, int y, int w, int h) 9 { 10 // Part A. 11 12 13 public String tostring(){ 14 return super.tostring() + "," + width + "," + height; 15 16 public void move(int newx, int newy){super.move(newx, newy); 17 public void scale(double factor){... // implementation omitted 18 public void rotate(double theta){... // implementation omitted 19 20 public static void main(string[] args) 21 { 22 Rotatable r1=new Rectangle(25,25,5,5); 23 Shape r2=new Rectangle(50,50,12,12); 24 Rectangle r3=new Shape(75,75,"rectangle"); 25 Shape r4=new Shape(100,100,"rectangle"); 26 27 r2.rotate(45); 28 29 System.out.println(r1); 30 System.out.println(r2); 31 32 System.exit(0); 33 34 A. Complete the constructor for the Rectangle class. (Type should be rectangle.) B. Answer the following by circling T for true, or F fo false. (i) Line 16 of Rectangle.java will cause a compilation error. T F (ii) Line 22 of Rectangle.java will cause a compilation error. T F (iii) Line 23 of Rectangle.java will cause a compilation error. T F (iv) Line 24 of Rectangle.java will cause a compilation error. T F (v) Line 25 of Rectangle.java will cause a compilation error. T F (vi) Line 27 of Rectangle.java will cause a compilation error. T F (vii) Line 29 of Rectangle.java will cause a compilation error. T F (viii) Line 30 of Rectangle.java will cause a compilation error. T F 1.00 Spring 2003 Final Exam 4/17 5/12/2005

Problem 3. Binary Search Tree Insertion (5 points) In a binary search tree (with no duplicate values), the data values of all descendants to the left of any node are less than the data value stored in that node, and all descendants to the right have greater data values. Here is a binary search tree that contains a single integer at each tree node. 10 7 12 2 Starting with an empty tree (don t use the one above), diagram the resulting binary search tree if we added nodes with the following keys in this order: 51, 11, 77, 20, -4, 30, 12, 19 Your diagram must follow the simple convention of the tree above. 1.00 Spring 2003 Final Exam 5/17 5/12/2005

Problem 4. Binary Search Tree Fun (25 points) The class defined below, DoubleTree, is a binary search tree that can only contain doubles. public class DoubleTree { int size = 0; Node root = null; public DoubleTree() { public void add(double d) { if ( root == null ) { root = new Node(d); size++; else { if ( root.add(d) ) { size++; public double treetotal() { if (root!= null) return root.total(); else return 0; private static class Node { double value; Node left = null; Node right = null; Node(double d) { value = d; public boolean add(double d) { if ( d > value ) { if ( right == null ) { right = new Node(d); return true; else { return right.add(d); else if ( d < value ) { 1.00 Spring 2003 Final Exam 6/17 5/12/2005

if ( left == null ) { left = new Node(d); return true; else { return left.add(d); return false; public double total() { /* part A */ // End Node // End DoubleTree You must implement several methods below of the Node and DoubleTree classes. a) Implement an instance method in the Node class that will return the sum of all the double values contained in the sub-tree rooted at this node. public double total() { /* this is a Node method */ b) Implement an instance method in the DoubleTree class that will return the average of all the doubles in the tree. This method must throw a NoSuchElementException if it is invoked on an empty DoubleTree. public double average() { // this is a DoubleTree method 1.00 Spring 2003 Final Exam 7/17 5/12/2005

c) Implement an instance method in the DoubleTree class that returns the smallest double in the tree. This method must throw a NoSuchElementException if it is invoked on an empty DoubleTree. (Do not create any additional methods or data members in your solution.) public double min() { /* this is a DoubleTree method */ 1.00 Spring 2003 Final Exam 8/17 5/12/2005

Problem 5. More Exceptions (10 points) Consider the following code: public class TryException { public static void main(string[] args) { int value; int [] anarray = {5, 6, 7; try { int index = Integer.parseInt( JOptionPane.showInputDialog("Index: ")); value = anarray[index]; System.out.println("Value is "+value); catch (IndexOutOfBoundsException e) { System.out.println("Wrong index! Input 0, 1, or 2"); System.out.println("DONE."); System.exit(0); Answer the following questions by circling YES or NO and filling in the blank. (a) If the user inputs 2, is an exception thrown? YES NO If the program outputs any result, what will it be? Write your answer in the box. (b) If the user inputs 4, is an exception thrown? YES NO If the program outputs any result, what will it be? Write your answer in the box. (c) If the user inputs 2.5, is an exception thrown? YES NO If the program outputs any result, what will it be? Write your answer in the box. 1.00 Spring 2003 Final Exam 9/17 5/12/2005

Problem 6. Inner Classes and Linked Lists (25 points) nwcorner In this problem we are working with a grid of lights, like those used as electronic signs. Each element of the light is a Bulb ; its value is true if the element is on, and false if it is off. The picture above shows an example of the grid. At the right is a picture of one Bulb whose value is true. It has references to the surrounding Bulb objects to the North, South, East and West. If element is at the edge of the grid, then one or more of these references may be null. You can assume that Bulbs are connected together correctly in this problem, and that the grid is rectangular. An example of a 3x3 grid is shown below. W N true S E nwcorner null null null null F F T null null T F F null null F F T null null null null The LightGrid and Bulb classes are given below. public class LightGrid { class Bulb { boolean value; Bulb n, s, e, w; /* all null by default */ Bulb(boolean v) { value = v; Bulb nwcorner; /* reference to the upper left Bulb */ public LightGrid(int rowcount, int colcount) /* constructor body omitted */... 1.00 Spring 2003 Final Exam 10/17 5/12/2005

To reduce clutter, we have omitted the body to the LightGrid constructor, but you should assume that this constructor exists and that it creates a LightGrid object with the number of rows and columns passed in. The LightGrid class has only one member variable, nwcorner, which is a reference to the northwesternmost Bulb in the grid, as shown in the diagrams. You may not add any additional member variables to either the LightGrid or the Bulb classes. (a) Complete the getvalue method for the LightGrid class. getvalue should return the value of the Bulb at row row and column col. The northwest Bulb is at row 0, column 0. You may not add, assume, or wish into existence any new member variables or methods. Your method does not need to do error checking; assume that the Bulb at row, col exists. /* (a LightGrid method) */ public boolean getvalue(int row, int col) { 1.00 Spring 2003 Final Exam 11/17 5/12/2005

(b) Complete the percenton method, which returns the percentage of Bulb objects whose value is true. If all Bulbs are on, it should return 1.0. If all Bulbs are off, it should return 0.0. You may not add, assume, or wish into existence any new member variables or methods. public double getpercenton() { /*(a LightGrid method)*/ 1.00 Spring 2003 Final Exam 12/17 5/12/2005

Problem 7. Priority Queues (20 points) In this problem, you will use a sorted Vector to implement a priority queue. This priority queue will only store objects of the Integer class in ascending order. In a priority queue, elements with the highest priority (lower integer values) are stored at the head of the queue. The priority queue always maintains the order of the elements according to their priority levels. Just like a regular queue, all the elements are initially added at the tail, and removed from the head. The IntegerPQ class assigns higher priorities to smaller numbers, which means that it will always store Integer objects in ascending order from the head to the tail. The diagram below shows how an Integer is added to the priority queue. 2 Head (index 0) 2 Head 2 Head 5 5 5 7 7 6 10 Tail (index size-1) 10 Tail 7 1. Priority queue before add You may find the following methods of Vector useful in your solution. 6 2. add(6) 10 Tail 3. Priority Queue after add Selected java.util.vector methods public void addelement(object obj) Adds the specified element to the end of the Vector. public void add(int index, Object obj) Adds the specified element at the specified position in the Vector. public void removeelementat(int index) Removes the element at the specified index from the Vector. public Object remove(int index) Removes and returns the element at the specified position. public Object get(int index) Returns the element at the specified position in the Vector. public void clear() Removes all of the elements from this Vector. 1.00 Spring 2003 Final Exam 13/17 5/12/2005

(a) Complete the add() method below. (b) Complete the remove() method below. (c) Complete the clear() method below. (d) Complete the main() method as directed below to output the contents of the priority queue. (e) Write the output of the main()method in the space provided below. public class IntegerPQ { // Vector used to store elements private Vector pq; public IntegerPQ() { pq = new Vector(); // Part a. Add an Integer object to the priority queue. // You should assume that pq is sorted with highest // priority items (lowest values) at the beginning of // the pq Vector. pq must also be sorted after add is // called. Important: The head of the queue is at pq[0]. public void add( Integer i ) { 1.00 Spring 2003 Final Exam 14/17 5/12/2005

// This method removes an element from Vector and // returns it public Integer remove() throws NoSuchElementException { // Part b // If queue is empty, throw a NoSuchElementException // Else, remove the element at the head and return it public int size() { /* omitted */ public boolean isempty() { /* omitted */ public void clear() { // Part c // Complete the clear() method 1.00 Spring 2003 Final Exam 15/17 5/12/2005

Now that IntegerPQ is complete, you will complete IntegerPQTest class, which has a main() method, to test your implementation. public class IntegerPQTest { public static void main( String[] args ) { IntegerPQ priorityq = new IntegerPQ(); // Add integers priorityq.add( new Integer( 23 ) ); priorityq.add( new Integer( 71 ) ); priorityq.add( new Integer( 10 ) ); priorityq.add( new Integer( 89 ) ); priorityq.add( new Integer( 63 ) ); priorityq.add( new Integer( 41 ) ); priorityq.add( new Integer( 55 ) ); // remove elements priorityq.remove(); priorityq.remove(); // Add more integers priorityq.add( new Integer( 99 ) ); priorityq.add( new Integer( 22 ) ); priorityq.add( new Integer( 15 ) ); // Part d // Remove and print out all the Integer objects in // priorityq. (Do not add any new methods.) System.exit( 0 ) ; 1.00 Spring 2003 Final Exam 16/17 5/12/2005

// Part e // What is the output after executing the main method? // End of test // Thank you and enjoy the summer 1.00 Spring 2003 Final Exam 17/17 5/12/2005