Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

Similar documents
Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

Comp Intermediate Programming EXAM #2 April 03, 2002 Rice University - Instructors: Cox & Nguyen

Comp Intermediate Programming EXAM #1 February 12, 2003 Rice University - Instructors: Cox & Nguyen

Comp Intermediate Programming EXAM #2 November 14, 2001 Rice University - Instructors: Nguyen & Wong

Comp Intermediate Programming EXAM #2 April 02, 2003 Rice University - Instructors: Cox & Nguyen

Comp Intermediate Programming EXAM #2 Supplement April 02, 2003 Rice University - Instructors: Cox & Nguyen

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

University of Massachusetts Amherst, Electrical and Computer Engineering

APCS Semester #1 Final Exam Practice Problems

Introduction to Programming Using Java (98-388)

EXAM Computer Science 1 Part 1

7. C++ Class and Object

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

CSE 331 Midterm Exam 2/13/12

CS171 Midterm Exam. October 29, Name:

CSE wi Midterm Exam 2/8/18. Name UW ID #

CSE wi Final Exam 3/12/18. Name UW ID#

Generic BST Interface

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

CSE 331 Final Exam 12/9/13

CS 101 Fall 2005 Midterm 2 Name: ID:

CSE 142 Su 04 Computer Programming 1 - Java. Objects

JAVA MOCK TEST JAVA MOCK TEST II

Introduction to Computer Science II (CSI 1101)

COS 126 General Computer Science Spring Written Exam 1

CSE 331 Final Exam 6/5/17. Name UW ID#

COMP 401 Spring 2013 Midterm 1

Chapter 6 Introduction to Defining Classes

COS 126 Midterm 1 Written Exam Fall 2011

Final exam. CS 2110, December 15, 2016, 9:00AM

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Csci 102: Sample Exam

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

Declarations and Access Control SCJP tips

COEN244: Class & function templates

CS 100J Prelim 2 Have a good break!!! 15 March 2007

CS 1316 Exam 1 Summer 2009

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

Midterm Exam (REGULAR SECTION)

Computer Sciences 302 Exam 2 Information & Sample Exam

public static boolean isoutside(int min, int max, int value)

CS 1316 Exam 2 Summer 2009

University of Maryland College Park Dept of Computer Science

CSE 331 Spring 2018 Midterm

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CS/ENGRD 2110 SPRING 2018

Introduction to Computer Science I

CSE 331 Midterm Exam 11/9/15 Sample Solution

CMSC132 Summer 2018 Midterm 1. Solution

STUDENT LESSON A12 Iterations

Do not turn over this examination paper until instructed to do so. Answer all questions.

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

When the above code runs, which type s execute method will be run? When the above code runs, which type s execute method will be run?

CMSC132 Summer 2018 Midterm 1

Note that if both p1 and p2 are null, equals returns true.

CS 111X - Fall Test 1

CS 2604 Minor Project 1 DRAFT Fall 2000

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 10 Recursion

Procedural Abstraction

Introduction to Computing II (ITI 1121) Final Examination

CSE 131 Introduction to Computer Science Fall Exam I

CSE wi Final Exam 3/12/18 Sample Solution

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

CSE 131 Introduction to Computer Science Fall Exam II

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

CSE wi Midterm Exam 2/8/18 Sample Solution

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

Inheritance and Interfaces

Loops / Repetition Statements

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

EINDHOVEN UNIVERSITY OF TECHNOLOGY

About this exam review

Summer Session 2004 Prelim I July 12, CUID: NetID:

RECURSION AND LINKED LISTS 3

University of Massachusetts Lowell

CMPT Sample Final Exam

Final Exam. Kenneth J. Goldman December 18, Name: Student ID Number: Signature:

CS 101 Spring 2006 Final Exam Name: ID:

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

Midterm Exam 2 CS 455, Spring 2011

OO Overkill When Simple is Better than Not

CS 170 Java Programming 1. Week 12: Creating Your Own Types

CIS 121 Data Structures and Algorithms with Java Spring 2018

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

CS 2604 Minor Project 1 Summer 2000

CS 101 Fall 2006 Midterm 3 Name: ID:

Transcription:

Instructions 1. This exam is conducted under the Rice Honor Code. It is a closed-notes, closed-book exam. 2. Fill in your name on every page of the exam. 3. If you forget the name of a Java class or method, make up a name for it and write a brief explanation in the margin. 4. You are expected to know the syntax of defining a class with appropriate fields, methods, and inheritance hierarchy. You will not be penalized on trivial syntax errors, such as missing curly braces, missing semicolons, etc, but do try to write Java code as syntactically correct as possible. We are more interested in your ability to show us that you understand the concepts than your memorization of syntax! 5. Write your code in the most object-oriented way possible, that is, with the fewest number of control statements and no checking of the states and class types of the objects involved. 6. In all of the questions, feel free to write additional helper methods or visitors to get the job done. 7. Make sure you use the Singleton pattern whenever appropriate. Unless specified otherwise, you do not need to write any code for it. Just write "singleton pattern" as a comment. 8. For each algorithm you are asked to write, 90% of the grade will be for correctness, and 10% will be for efficiency and code clarity. 9. You have two hours and a half to complete the exam. Please State and Sign your Pledge: 1a) 10 pts 1b) 15 pts 1c) 10 pts 2a) 15 pts 2b) 15 pts 3) 20 pts 4) 15 pts Total 100 pts 1 of 12

For your convenience, below is the UML class diagram for the scheme list framework with an abstract factory studied in class. You are free to use this list framework without explanation/implementation. 2 of 12

1. The goal is to write an IListAlgo, called ListDuplicates, that returns an IList containing all the elements that appear more than once in the IList host. The resulting IList should not contain any duplicates. For example, if the host IList is (99, 0, 17, 99, 99, 0, 5), then the result is (99, 0) or (0, 99) (the order is immaterial). You are to write this visitor in the following manner. a) Write a visitor, called Contains, to check if a given Object is in the host list, as specified in the stub code below. Use the equals method for comparison. public class Contains implements IListAlgo { the Singleton pattern. /** * Returns true if the input object is in the host, false otherwise. * @param inp inp[0] is the Object to be found in the host. * @return Boolean */ public Object emptycase(iemptylist host, Object... inp) { /** * Returns true if the input object is in the host, false otherwise. * @param inp inp[0] is the Object to be found in the host. * @return Boolean */ public Object nonemptycase(inelist host, Object... inp) { 3 of 12

b) Write a visitor, called DupHelp, which first traverses the host list to accumulate two things, 1) the list of elements seen so far in the original list and 2) the list of duplicates built so far, and then returns the complete list of duplicates, as specified in the stub code below. public class DupHelp implements IListAlgo { // Student to add appropriate field(s) and constructor(s). /** * @param inp inp[0] is the list of elements seen so far, inp[1] is the list of duplicates accumulated so far. * @return IList the complete list of duplicates. */ public Object emptycase(iemptylist host, Object... inp) { /** * @param inp inp[0] is the list of elements seen so far, inp[1] is the list of duplicates accumulated so far. * @return IList the complete list of duplicates. */ public Object nonemptycase(inelist host, Object... inp) { // Hint: // Check to see if the first of host is in inp[0], the list of elements seen so far; // If it s has been seen already then check to see if it is already in inp[1], the // accumulated list of duplicates; make appropriate updates to the accumulators // and recurse. // If it has not been seen, update the seen-so-far list and recurse. 4 of 12

c) Write ListDuplicates using Contains and DupHelp. public class ListDuplicates implements IListAlgo { // Student to add appropriate field(s) and constructor(s). /** * @param nu not used. * @return IList the complete list of duplicates. */ public Object emptycase(iemptylist host, Object... inp) { /** * @param nu not used. * @return IList the complete list of duplicates. */ public Object nonemptycase(inelist host, Object... nu) { using Contains and DupHelp 5 of 12

2. Consider the following description of a recursive immutable data structure that represents web pages. A web page has a header, which is a String, and a body, which is a web document. A web document may be empty or non-empty. When it is empty, it contains nothing. When it is not empty, it may contain a String and web document, or it may contain a web page and a web document. The following UML class diagram represents the object model for the above web page structure. Because of the limitation of our UML tool, we cannot display the variable argument list (varargs), Object inp, for the execute method and the visitor methods. Still, you should assume that all these methods have the varargs Object inp. 6 of 12

a) Write an IWebAlgo, called CountWebPages, that counts and returns the number of IWebPage that are embedded in the IWebPage host, including the host itself. 7 of 12

b) Write an IWebAlgo, called FindPage, that takes as input a String s, finds and returns an IWebPage embedded in the host that contains s as its header. If there is no match, returns null. Use the equals method for String comparison. 8 of 12

3. Consider the set of Natural Numbers, 1, 2, 3, 4,..., i.e., the non-negative integers. For this problem, you will complete the design and implementation of a class that represents the set of Natural Numbers in the form of a list, that is, a Natural Number has a value and a successor that is a Natural Number. (The Natural Number's value and successor are analogous to the first and the rest of an IList.). A problem is that the set of Natural Numbers is infinite. Thus, our design employs a technique known as ``Lazy Evaluation''; in other words, we do not instantiate the object(s) representing a Natural Number until there is need for it. Furthermore, the object(s) representing a Natural Number should be unique, that is, created once and never recreated. For instance, the object(s) representing the Natural Number 7 should be created only when 7 is required and created one time only. Only the object(s) representing the first Natural Number, 1, is instantiated when your program begins execution. To implement lazy evaluation, we apply the state pattern to the design and implementation of the successor. Specifically, a Natural Number's successor is in one of two states, evaluated or unevaluated. If it is evaluated, then the object(s) representing the successor to the current Natural Number is already instantiated. On the other hand, if it is unevaluated, no object(s) yet exist(s). Asking for the unevaluated successor of a Natural Number should instantiate that Natural Number, changing the state of the current Natural Number. Please replace the comments below with your code to complete the design and implementation of the class NaturalNumbers. class NaturalNumbers { private int _value; private interface IState { public NaturalNumbers getrest(naturalnumbers owner); private static class EvaluatedState implements IState { private static class UnevaluatedState implements IState { private IState _successor; 9 of 12

public final static NaturalNumbers One = new NaturalNumbers(1); private NaturalNumbers(int value) { public final int getvalue() { public final NaturalNumbers getsuccessor() { private final void setsuccessor(istate successor) { 10 of 12

4. There is an abstract notion of a processor. Processors are manufactured by fabrication plants (fab-plants). A fab-plant takes as input a "version" and creates a processor of that type. A fab-plant needs to remember all processors it creates. A processor has a unique serial number, and remembers its version and its fab-plant of manufacture. A version is the design specification of a given type of processor. It remembers the list of fabplants that manufacture processors of that version. When a fab-plant starts creating processors of a given version, it "registers" itself with that version. The version then adds this fab-plant to its list of fab-plants. Occasionally, "upgrades" are issued for a version. An upgrade consists of a string representing code and a unique identifier. A version takes as input this code to create a new upgrade. An upgrade remembers its version. A version can have multiple upgrades and remembers them all. When a processor of a given version is created by any fab-plant, all upgrades for the corresponding version must be applied to the processor. Similarly, when a new upgrade is issued, all processors of that version produced by all fab-plants need to apply that upgrade. Your task is to produce an object-oriented design simulating processors, fab-plants, versions and upgrades. Express your design as Java classes/interfaces with appropriate data fields and methods. Pseudo code in the form of comments is acceptable for the body of the methods. Use inner classes whenever appropriate. 11 of 12

A blank page for writing code 12 of 12