# spring05Prelim1Answers - CS 100J Prelim I Solutions 1(a...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 100J Prelim I - Solutions 1. (a) Class - A file drawer containing manilla folder of objects Object - a manilla folder in the file drawer (b) Static - in the class file drawer Non-static - In each manilla folder in the class's file drawer. (a) Parameter - A variable declared within the parentheses of a method header Argument - An expression that appears within the parentheses of a method call (b) Constructor - Initializes fields of a newlycreated object Procedure - Performs some action without returning a value Function - Performs some action and returns a value (c) true || (5/0 < 1) (5/0 < 1) || true 3/2 2. true BAD 1 (b) canCross (c) true 24 February 2005 4. (a)public class Airplane extends Vehicle { /** number of passengers */ private int numPassengers; /** Constructor: instance with weight w and p passengers */ public Airplane(int w, int p) { setWeight(w); numPassengers= p; } /** = total weight of plane */ public int flyWeight() { return getWeight() + numPassengers*150; } /** = "it can cross a bridge with max weight w" */ public boolean canCross(int w) { return true; } OR /** = "plane can cross a bridge with max weight w while taxiing" */ public boolean canCross(int w) { return flyWeight() <= w; } } c1 weight 0 Vehicle getWeight() setWeight(int) c2 weight 0 Vehicle 5. /** spec given on prelim 1, question 5 */ public String makeValidMethodName(String s) { s= s.replaceAll(" ",""); if (s.length() == 0) s= "_"; if (!Character.isLetter(s.charAt(0)) && s.charAt(0) != '_') s= "_" + s; if (s.length() > 65535) s= s.substring(0,65535); return s; } canCross(int) 4. (b) 1. Declare n (or use a constant instead) 4. (b) 2. Change flyweight to flyWeight getWeight() setWeight(int) capacity 0 canCross(int) Truck getCapacity() setCapacity(int) canCross(int) 3. (a) weight, isEqual, getWeight, setWeight, canCross ...
View Full Document

## This note was uploaded on 06/01/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell.

Ask a homework question - tutors are online