DOC PREVIEW
CORNELL CS 211 - Study Guide

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CS211 Prelim 1. 18 Oct 2001 NAME__________________________________ NETID____________________This prelim has 4 questions. Be sure to answer them all. Please writeclearly, and show all your work. It is difficult to give partial creditif all we see is a wrong answer. Also, be sure to place suitable comments--method specifications, variable definitions-- in your programs.Write your name and netid at the top of each page.Question 1 Recursion (20 points). Write a (static) recursive functionthat, given a nonnegative int n, yields the number of 1’s in its binaryrepresentation. For example, suppose n = 17. The binary representationof 17 is 10001, so the function would return 2.Your answer will be graded on (a) a good specification of the function(b) the correctness of your function ---a suitable base case and recursivecase, both handled correctly. No points will be given if your programuses a loop.The following facts may be useful:(1) The rightmost bit of the binary representation of n is 1 if and only if n is odd.(2) For n > 0, the binary representation of n is:(binary representation of n/2) followed by (the rightmost bit of the binary representation of n).Question 1 __________ out of 25 Question 2 __________ out of 25Question 3 __________ out of 25Question 4 __________ out of 25Total _______________ out of 1002CS211 Prelim 1. 18 Oct 2001 NAME _________________________________ NETID ____________________Question 2. Classes and subclasses (20 points). Thisquestion is designed to determine whether you know thebasics of subclasses and inheritance. To the right is a classMember. It contains more methods, but we have shownonly the ones that you will need.Below, write a subclass Student of Member. An instance ofthe class has a name, address, and status, which should beone of the static constants FROSH, SOPH, JUNIOR, SENIORof this class Student (don’t forget to define these constants).The class should have a suitable constructor, toString method,and a getter method (named getStatus) for the status.After writing the class, answer the following two questions.(a) Draw variable b and all the objects that are created by the following expression (you don’t have to fill in thecontents of the instance of class Address):Member b= new Student(“Jack”, new Address(…), Student.SOPH);(b) Below is a list of method calls, which refer to variable b above. Indicate which of these method calls is legaland, if legal, which method it refers to.(1) b.toString()(2) b.getStatus()// A member of Cornell Universitypublic class Member {private String name; // member’s nameprivate Address add; // member’s address// Constructor: a member with name n// and address bpublic Member(String n, Address b) {…}// = String representation of this Memberpublic String toString() {…}}3CS211 Prelim 1. 18 Oct 2001 NAME__________________________________ NETID____________________Question 3. Interfaces (20 points). To the right is interfaceEnumeration, in case you have forgotten it. Each instance of theclass below implements a set of at most 100 Animals, whereAnimals is some class. We have not shown all the methods ofclass SetOfAnimals because you won’t need them.Method enumerate (see below) is supposed to return anEnumeration of the Animals currently stored in the instance. Itdoes this by returning the value of the expression new Enum().However, class Enum has not been written yet. Write classEnum as an inner class of class SetOfAnimals. Class Enum mayneed a field that indicates which element of the array to enumeratenext.public class SetOfAnimals {private Animal[] b; // There are n animals, and theyprivate int n; // are stored in b[0..n-1]// Constructor: an instance that contains a set of 0 Animalspublic SetOfAnimals(){ b= new Animal[100]; n= 0; }// = an Enumeration of the Animals in this instancepublic Enumeration enumerate(){ return new Enum(); }}public interface Enumeration {// = there exists another element in the// enumerationpublic boolean hasMoreElements();// = the next element in the enumeration.// Should be called only if it is known to// existpublic Object nextElement();}4CS211 Prelim 1. 18 Oct 2001 NAME__________________________________ NETID____________________Question 4. Short questions (25 points)(a) What is meant by a base case (in a recursive procedure)?(b) What is a nested class?(c) Consider a class C with a method m. What are the two consequences of making C and m abstract?(d) Suppose function translateDay(d,m,y) translates a date given by a day d, month m, and year d into the daynumber of that date within a year and returns that day number. For example, translate(2,1,2001) equals 2,since 2 January 2001 is day 2 of the year. This function throws a DateException if the given date is not a validdate.Write a statement that stores in a variable dn the value of the function call translateDay(d1,m1,y1) ---butstores 1 in dn if translateDay throws a DateException. Note: you do NOT have to write method translateDayor class DateException.5CS211 Prelim 1 Answers. 18 Oct 2001 NAME David Gries NETID djg17There is usually more that on way to answer aquestion. These are just sample solutions.Question 1.// = number of 1’s in the binary representation of n// Precondition: n >= 0public static int numberOfOnes(int n) {if (n == 0)return 0;return numberOfOnes(n/2) + (n % 2);}Question 2.public class Student extends Member {public static final int FROSH= 1;public static final int SOPH= 2;public static final int JUNIOR= 3;public static final int SENIOR= 4;private int status;// Constructor: instance with name n, address a,// and status s (one of the class constants)public Student(String n, Address a, int s){ super(n,a); status= s; }// = a representation of this studentpublic String toString() {return super.toString() +“ status: “ + status;}// = the status of this Studentpublic int getStatus(){ return status; }}(a) the call b.toString() is legal. It refers to toStringin the Student portion of the instance.(b) the call b.getStatus is illegal.You should be able to see the results of this prelim bynoon tomorrow morning on the same website whereyou submit your assignments electronically.Question 3. Here is the inner class. Place it right aftermethod enumerate.// An enumeration of the Animals in this instancepublic class Enum() {int k= 0; // index of next element of b to enumerate // = “there is another Animal to enumerate”public boolean


View Full Document

CORNELL CS 211 - Study Guide

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?