CS 61B, Spring 1999 MT3 Professor M. Clancy

Similar documents
CS 61B, Midterm #3 and solutions, Spring 1996

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

CS61B (Clancy) Spring 1996 Exam 3, solutions, and grading standards. April 12, 1996 Exam 3

CS 61B (Clancy, Yelick) Solutions and grading standards for exam 2 Spring 2001 Exam information

CS61BL Summer 2013 Midterm 2

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

CS 61B, Spring 1998 exam #1 Professor P. N. Hilfinger

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

User-defined Functions. Conditional Expressions in Scheme

CSE 143 SAMPLE MIDTERM

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS61B, Fall 1995 Midterm #2 K. Yelick

CS 2301 Exam 3 B-Term 2011

EXAM Computer Science 1 Part 1

Structure and Interpretation of Computer Programs

C12a: The Object Superclass and Selected Methods

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

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

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

INHERITANCE. Spring 2019

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

void insert( Type const & ) void push_front( Type const & )

1 Quickies String s1 = "Hello World"; String s2 = "Hello "; s2 = s2 + "World"; Point a = new Point(3, 4); Point b = new Point(3, 4);

CmpSci 187: Programming with Data Structures Spring 2015

CS 170, Spring 2000 Midterm #1 Professor M. Clancy

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

The class Object. Lecture CS1122 Summer 2008

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

TREES Lecture 10 CS2110 Spring2014

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

CSE413 Midterm. Question Max Points Total 100

CS18000: Problem Solving And Object-Oriented Programming

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

Java Fundamentals (II)

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

Largest Online Community of VU Students

CS61BL: Data Structures & Programming Methodology Summer 2014

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Inheritance (Part 5) Odds and ends

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Programming II (CS300)

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

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

CS 231 Data Structures and Algorithms, Fall 2016

AP Computer Science AB 2007 Free-Response Questions

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance. Transitivity

Computer Science Spring 98 Midterm Examination Professor L. Rowe

CS 251 Intermediate Programming Methods and More

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS 314 Principles of Programming Languages. Lecture 16

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Chapter 4 Defining Classes I

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees.

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS 101 Spring 2006 Final Exam Name: ID:

COS 126 General Computer Science Spring Written Exam 1

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Super-Classes and sub-classes

MIDTERM EXAMINATION - CS130 - Spring 2005

CS171 Final Practice Exam

CLASS DESIGN. Objectives MODULE 4

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Midterm #2 - October 5, 1998

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

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

public static void negate2(list<integer> t)

Written by John Bell for CS 342, Spring 2018

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

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

CSIS 10B Lab 2 Bags and Stacks

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CS450 - Structure of Higher Level Languages

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS 314 Exam 2 Spring 2018

Last time s big ideas

CS171 Final Practice Exam

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Practice Midterm 1. Problem Points Score TOTAL 50

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

Unit 9 Practice Test (AB27-30)

Transcription:

CS 61B, Spring 1999 MT3 Professor M. Clancy Problem #1 One approach to producing debugging output is to use inheritance to create objects that print any changes to themselves. For instance, instead of a Interval in homework assignment 4, one might have a TracedInterval that inherits from Interval, in which: (1) each constructor is overridden so that after initializing, it prints the Interval object; (2) the extendthrough method is overridden so that after updating, it prints the new value of the Interval object. Suppose for the purposes of this problem that the Interval class from homework assignment 4 is defined as follows: public class Interval { private double myleft; private double myright; // Constructor. public Interval (double left, double right)... // Return this interval's left endpoint. public double left ( )... // Return this interval's right endpoint. public double right ( )... // Return a hash value for this interval. public int hashcode ( )... // Return true when this interval represents the same interval // as the argument; return false otherwise. public boolean equals (Interval intvl)... // Return true exactly when this interval contains x; // return false otherwise. public boolean contains (double x)... // Return true when this interval overlaps the argument, // i.e. contains integers in common with it; // returns false otherwise. public boolean overlaps (Interval intvl)... // Extend this interval to include all the points in the argument. // Precondition: overlaps (intvl). public void extendthrough (Interval intvl)... CS 61B, Spring 1999 MT3 Professor M. Clancy 1

In the space below, define the TraceInterval class described above. The main program: public static void main (String [ ] args) { TracedInterval intvl = new TracedInterval(1.0, 3.0); intvl.extendthrough(2.5, 6.0); should produce something like the following as output: new interval is [1.0, 3.0] extended interval is [1.0, 6.0] CS 61B, MT3, Spring 1999 Your TracedInterval class should not have any private data of its own; it should rely as much as possible on information stored and methods provided by the Interval class. Problem #2 Recall from lab assignment 9 that the Amoeba class was defined as follows. public class Amoeba {... // various methods private String myname; // this amoeba's name private Amoeba myparent; // this amoebas parent private Amoeba myoldersibling; // this's most recently added sibling private Amoeba myyoungestchild; // this's most recently added child On the nect page, write a public Amoeba method named amoebasnamed that, given a String as argument, returns a list of all Amoeba objects among this and its descendants whose names are the same as the argument. The sequence of the Amoeba objects within the list is not important. Assumbe that a List class has been defined that supplies methods given below. public class List { // Initialize and empty list. pubic List ( )... //Initialize a list containing only obj. public List (Object obj)... // Return the result of inserting obj at the front of this list. // In Scheme, this would be (cons obj this). public List cons (Object obj)... // Return the result of appending the argument to this list. // In Scheme, this would be (append this list2). public List append (List list2)... // Return true if this list is empty, false otherwise. // In Scheme, this would be (null? this). public boolean isempty ( )... Problem #1 2

// Return the first element in this list. // Precondition:!isEmpty ( ) public Object car ( )... // Return a list containing all but the first element in this list. // Precondition:!isEmpty ( ) public List cdr ( )... // Various private data fields.... You may supply auxilary methods, but do not use any of the existing Amoeba methods. In particular, do not use or reimplement the enumeration methods; your method or one of the methods it calls must be recursive. Your solution to problem 2: public List amoebasnamed (String name) { Problem #3 The "8 puzzle" uses eight square blocks numbered 1 to 8 in a three-by-three tray. The goal is to slide the blocks aroudn in the tray until the blocks are in numerical order when read row by row. For example, the four moves below solve the problem for the given initial configuration. initial after moving after moving after moving after moving configuration 7 down 5 down 2 left 3 up 1 5 2 1 5 2 1 2 1 2 1 2 3 4 7 3 4 3 4 5 3 4 5 3 4 5 6 8 6 7 8 6 7 8 6 7 8 6 7 8 the lst page of this exam contains the framework of a program to solve this puzzle. Its primary class, named Tray, represents the tray and its blocks with an array of the integers 0 through 8; o represents the space in the tray. Rows and columns are numbered from the top left corner of the tray: Problem #2 3

column 0 1 2 row 0 1 5 2 1 4 7 3 2 6 8 CS 61B, MT3, Spring 1999 Part a: Supply a pseudocode body for the processupmove method, in detail sufficient for another CS 61B student to translate it immediately into Java code (essentially one pseudocode statement for each Java statement in processupmove). // Process the tray (if any) that results from moving // a block up in the arguement tray by including it // in the enumeration if it hasn't been seen before. public void processupmove (Tray current) { Part b: Given below and on the next page is most of the Collection class used by the TrayEnumeration methods. Fill in the missing code so that trays are enumerated in depth first order, that is, so that the trays reachable by moving a block in one direction from the initial tray are returned before any of the trays reachable only by moving a block in any of the other directions. Assume that a ListNode has a public Object item field and a public ListNode next field, plus a one-argument constructor that initializes item to the argument and next to null. public class Collection { private ListNode myfirst; private ListNode mylast; // Initialize an empty collection. public Collection ( ) { myfirst = mylast = null; // Initialize a collection with one element (the argument). public Collection (Object obj) { myfirst = mylast = new ListNode (obj); // Add the given element to the collection. Problem #3 4

public void put (Object obj) { if (myleft == null) { myleft = myright = new ListNode (obj); else { //you fill this in so that trays are enumerated depth first // Remove an element from the collection, // and return the element removed. // Precondition:!isEmpty ( ). public Object get ( ) { Object obj; if (myfirst == mylast) { obj = myfirst.item; myfirst = mylast = null; else { //you fill this in so taht trays are enumerated depth first return obj; // Return true if teh collection is empty; return false otherwise. public boolean isempty ( ) { return myfirst == null; Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If you have any questions about these online exams please contact examfile@hkn.eecs.berkeley.edu. Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley 5 If y