CS100, Spring 2000, Solutions to Prelim 2 1. // read non-ascending sequence of even #s; // for each n, print a centered row of n stars int biggest = in.readInt(); // max # stars to print in any row for (int n = biggest; n > 0; n = in.readInt()) { // print (biggest-n)/2 spaces for (int i = 0; i < (biggest-n)/2; i++) System.out.print(" "); // print n stars for (int i = 0; i < n; i++) System.out.print("*"); // advance to next line System.out.println(); } Thought question: Suppose we wanted to somewhat relax the conditions "non-ascending" and "all even". What changes to the code are required and/or what weaker conditions suffice? 2. "Crossed-out objects" are simply omitted below. "nu*l" stands for "crossed out $null$". "0/1/2" stands for "crossed out 0, crossed out 1, 2". We ignored $args$ of method main both below and in your solutions. Do not confuse $null$ with "undetermined/nonsense/garbage value". +--------------------------------+ | | class String | class Glorph | +-------------+-+ +----------------------+-----+ | | | | | | | "sputty"<---+ | | +----------+ +------+---+ | | | | | +----+ | | +--+-+ | | +---------------+ | | s |null| | | s |nu*l| | | | | +----+ | | +----+ | | class Problem2 | | | | | | +---------------+ | | +----+ | | +----+ | | | | | | g |null| | | g |null| | | | main (args) | | | +----+ | | +----+ | | | +-----------+ | | | | | | | | | +----+ | | | | +----+ | | +----+ | | | | G1 | *--+-+-+-------+>| w |nu*l| | | w |nu*l| |<+--------+ | | +----+ | | | | +--+-+ | | +--+-+ | | | | | | | | +------+---+ +------+---+ | | | | +----+ | | | | | | | | | W1 | *--+-+-+----+ +--------+-------------+-----+ | | | +----+ | | | | | | | | | | | +---+ +------+ | | | +----+ | | | | | | | | W2 | ? | | | | class Whappy | | | | | +----+ | | | +------------|--+----------------+ | | +-----------+ | | | V | | | | | | | +----------+ | +----------+ | | +---------------+ | | | +-----+| | | +----+ | | | +--+>| x |0/1/2||<-+ | x | 0 | | | | | | +-----+| | +----+ | | | | | | | | | | | | +----+ | | +----+ | | | | | g |nu*l| | | g |null| | | | | | +--+-+ | | +----+ | | | | +------+---+ +----------+ | | | | | | +--------+-----------------------+ | | | +----------------------------+ 3. class Worker { int id; Worker boss; // return company owner Worker owner() { Worker topboss = this; while (topboss.boss != null) topboss = topboss.boss; return topboss; } // return description: ID, boss's ID (if any), owner's ID String describe() { String s = "ID: " + id + ", ID for boss: "; if (boss == null) s += "NO BOSS"; else s+= boss.id; s += s + ", ID for owner: " + owner().id; return s; } } another solution to describe: String s = "NO BOSS"; if (boss != null) s = "" + boss.id; return "ID: " + id + ", ID for boss: " + s + ", ID for owner: " + owner().id; 4. Worker thirty = new Worker (); thirty.id = 30; thirty.boss = sixty; five.owner().boss = thirty; System.out.println(x.owner().describe()); if (x.owner() != y.owner()) System.out.println(y.owner().describe()); B1. 70 total possible core project pts; 55 required for perfect project score. B2. "loop storyline" or "loop invariant": tank contains 2*time liters of water. B3. Worker owner() { return boss == null ? this : boss.owner(); } B4. void dtd() { if (13 <= age && age <=19) describe(); if (youngestChild != null) youngestChild.dtd(); if (olderSibling != null) olderSibling.dtd(); } B5. 4 or 4 / \ / \ 2 3 3 2 | | 1 1