October Announcements

10/28 P5 NEWS:

Students, be sure to get the files from the newsgroup, run the project, and read the assignment -- all in time for section next week, as the program will be discussed.

10/28  CATCH-UP REVIEW SESSION:

There is a catch-up review session this Sunday 10/31 at 3pm in Olin 155.
show up ON TIME: we do not plan to prop the doors open.

This is targeted at people who are having trouble with the course:
+ if you got the median or higher on prelim 2, this will probably
be a waste of your time.
+ if you got around the mean on prelim 2, this might be helpful.
+ if you got substantially below the mean, you ought to attend.

10/28 EXTRA OFFICE HOURS:

Yiqun Liu has added additional office hours from 3:30 to 5:30PM on 
Friday at Upson 328. The first hour is for everyone. 
The second hour is by appointment only. 

10/26 P4 HINTS & SUGGESTIONS


We neglected to point out a very useful fact from Hints and Suggestions
in the write-up: you can convert from an int back into a char by typecasting.
for example, 's' - 'p' is 3, meaning 's' is 3 positions after 'p'.
's' can be recovered from 'p' and offset 3 as follows:

(char)('p'+3)

the above is equivalent to 's'. sorry about this oversight.

- tky

p.s. someone asked me if you could concatenate a char onto a String.
the answer is yes, e.g. "hello" + 't' is equivalent to "hellot".

10/21 ANNOUNCEMENT BY CS UNDERGRADUATE OFFICE:

> We have made some changes in CS courses
> that you should know about:

> 1. The CS 211 prerequisite of CS 100 will now
> specify a course in Java or C++. What this means
> is that students attempting to get transfer credit for
> CS 100 will need a qualified course in Java or C++.
> The AP College Board Exam in CS is given in C++, so
> it will still meet our CS 100 requirement and will serve
> as an adequate prerequisite for CS 211.

naturally, as students taking CS100, you're all set for CS211.
(and to be really obsessive, object-oriented programming
should have been mentioned as a pre-requisite for CS 211.)

> 2. We will now require all CS majors to take both
> CS 211 *and* CS 212 (they used to be functional
> equivalents and CS majors took one or the other). 
> For more detailed information on this change, visit:

> http://www.cs.cornell.edu/Ugrad/Courses/changes211-212.htm

> 3. CS 410 "Data Structures" will not be offered after
> Summer 2000. A similiar course for non-CS majors
> will continue to be offered. Currently we are offering
> this course as CS 409 and it will be taught in the
> spring.

> Let us know if you have any questions regarding these
> changes.

> Thank you,

> The Computer Science Undergraduate Office
> <ugrad@cs.cornell.edu>
> 255 0982
10/17 REVIEW SESSION FOR FINAL EXAM

Though still a ways off, the review session for the final has been scheduled:

It will be held SATURDAY (NOT Sunday!), December 11, 3 - 5pm
Olin Hall 155.

10/17 SECTION NEWS

Bela is out of town this week and will have his sections covered by Fan, Yan, and 'lan (Alan).  See who'll be running your section:

1 tue 1:25 yan 2 tue 1:25 fan
5 tue 2:30 yan 3 tue 2:30 fan 4 tue 3:35  fan
6 wed 1:25 yan
7 wed 2:30 alan
8 wed 3:35 alan

10/17 ANOTHER SOLUTION TO Q3A ON SAMPLE PRELIM

public class MM60 {
   public int tens;
   public int ones;

   public MM60 add(MM60 b) {
      MM60 c = new MM60 ();
      int t = (ones+b.ones + 10*(tens + b.tens))%60;
      c.ones = t%10;
      c.tens = t/10;
      return c;
   }

   // This one hasn't changed
   public void swap(MM60 b) {
      int tmp;
      tmp = ones; ones = b.ones; b.ones = tmp;
      tmp = tens; tens = b.tens; b.tens = tmp;
   }
}

10/17 SAMPLE SOLUTION TO P3 USING ARRAYS

Room.java using arrays

Cave.java (program with the demon; NOTE that we didn't have to change a thing!)

10/17 NEW TUTORING ROOMS
Starting this week, the following TAs will be holding their office hours in 476 RHODES HALL.  We will post a notice on the door of 455 directing students to the new room.

TAs that are affected by this change and their tutoring times:

MONDAY
2:30-3:30 CS100A Alan Renaud
3:30-4:00 CS100A Alan Renaud (Appt. only)
5:00-5:30 CS100A Alan Renaud (Appt. only)

TUESDAY
10:00-11:00 CS100A Yi Qun Liu

WEDNESDAY
3:30-4:30 CS100A Christine Chung

THURSDAY
10:00-11:00 CS100A Yi Qun Liu (Appt. only)

FRIDAY
12:15-1:15 CS100A Christine Chung (Appt. only)

10/15 PRELIM 2 REMINDER:

The exam is this Tuesday, 7:30-9PM.

Go to the appropriate room based on your last name:

A - H Ives 105

I - Z Ives 305

10/15 HW3 SOLUTIONS ERRATA:

Near the end of class Application in the while loop, the if statement for the Demon's next move should read

  if (demon.farRoom(door) != null) 
                demon = demon.farRoom(door);

and NOT

  if (player.farRoom(door) != null) 
                demon = demon.farRoom(door);

NOTE: the Web version of the solutions varies slightly from the Carpenter version (it prompts the user differently in class Application and uses variables, first, second, & demonDoor instead of r1, r2, and door (for the demon)

10/15 REVIEW SESSION:

There is a review session this Sunday from 3-5PM, OH 155.  Enter the building using the Gannett-facing door.

10/15 CORRECTIONS TO SAMPLE PRELIM 2 ANSWERS:

As of this morning, there were two errors in the Sample Prelim 2 answers [the corrected version is below]:

First Error
3 b)  The second Math.random() statement should be multiplied by 0.45 and not 0.60

Second Error
4 a) Line 2 in the add method should read

    c.tens = ( tens + b.tens + c.ones / 10)%6;

and NOT

    c.tens = tens + b.tens + c.ones/
10

10/15 SAMPLE PRELIM 2 ANSWERS:

1.  a) 
     
     b) 7 8 1 3
         7 8 3 null

2.  a)  



       
b) toString returns a value. to print a value, it would be:

   public void
toString() {
     
String s = "null";
     
if (c != null) s = "" + c.b;
     
System.out.println(a + " " + b + " " + s);
  
}

NOTE: Java requires toString to return a String.

c) A "class" is "model" or "blueprint" that defines the fields (or instance
variables) and methods of its instances (objects).

d) A "method" is a named piece of code that performs a task; it can take values
as parameters (or arguments) and can return a result value.

e) "public" means "can be seen by code anywhere".
"private" means "can be seen only by code within the class".

f) "static" means "only one copy, only accessible through/within the class".
"non-static" means one copy per instance, only accessible via (a reference
to) an object.

3. 
a)
// repeatedly do something N times
   int
counter = 0;
  
while (counter < N) {
      do-something;
      counter++;
   }


  
// process input sequence of pairs of values 
   // terminated by a pair (stop1,stop2) of stopping values

  
int a = in.readInt(); int b = in.readInt();
   while (a != stop1 || b != stop2) {
      process-a-and-b;
      a = in.readInt(); b = in.readInt();
   }

b)
int trurl = 0; // y position of trurl
    int klapaucius = 0; // y position of klapaucius
    int x = 0; // #steps; also, x position of trurl, klapaucius 
    int same = 0; // how often trurl and klapaucius meet
    while (x < 200) {
        if (Math.random()<.6) trurl++; else trurl--;
        if (Math.random()<.45) klapaucius++; else klapaucius--;
        if (trurl == klapaucius) same++;
        x++;
    }
     System.out.println("They met " + same + " times");

4. 

a) Note: The fields below are public to allow code outside the class
the modify them, but this is BAD style: They should be private, and
there should be methods for setting and getting the fields. This way,
the MM60 code can enforce the specified contraints (ranges) on the fields.

public class
MM60 {
   public int
tens;
   public int
ones;


   public
MM60 add(MM60 b) {
      MM60 c = new MM60 ();
      c.ones = ones + b.ones;
      c.tens = (tens + b.tens + c.ones / 10)%6;
      c.ones %= 10;
      return c;
   }

   public void swap(MM60 b) {
      int tmp; 
      tmp = ones; ones = b.ones; b.ones = tmp;
      tmp = tens; tens = b.tens; b.tens = tmp;
   }
}


b) a.swap(b); b.swap(c); OR a.swap(c); a.swap(b); OR b.swap(c); a.swap(c);

NOTE: x.swap(y); has the same effect as y.swap(x); thus, the code above could be

b.swap(a); c.swap(b); OR c.swap(a); b.swap(a); OR c.swap(b); c.swap(a);

c)
MM60 a = new MM60();
   int m = (-x) % 60;
   if (m<0) { m += 60; m %= 60; }
   a.ones = m % 10;
   a.tens = m / 10;


NOTE: it would have been bad style to modify x.

d) a.add(a).add(c) <-- no semicolon, since this is an expression
10/14 SAMPLE PRELIM 2 QUESTIONS:

Prelim 2 concentrates on what we have covered since Prelim 1 (up through Program 3) but will also include material  covered by Prelim 1.  Arrays are NOT covered.  However, arrays are a natural place to use loops, and you need to learn about array s for Program 4.  Thus, you might wish to rewrite Program 3 to use arrays to eliminate the search in method "findRoom" and to allow any number of doors per room.

Below are sample Prelim 2 questions.  Please use the newsgroup to communicate with us regarding errors, clarifications, etc.

  1. a)    Draw a picture of the "world" --all objects and variables and methods-- at the line indicated.
    b)    What is the output of method "main" below?


    public class Q1 {
        private static int a = 1;
        private int b;
        private Q1 c = null;

        public Q1() {
            b = a;
            a = 2*a;
        }

        public String toString() {
            String s = "null";
            if (c != null) s = "" + c.b;
            return a + " " + b + " " + s;
        }

        public void magic(int a) {
           
    Q1 now = this;
            while (now != null) {
                System.out.println(a + " " + now);
                now = now.c;
            }
        }

        public static void main (String[] args) {
            Q1 a = new Q1();
            Q1 b = new Q1();
            Q1 c = new Q1();
            c = a;
            a.c = b;
            c.c.b++;
        // draw a picture of "the world" as it exists at THIS LINE
            c.magic(7);
        }
    }
  2. a)    In the code for Q1 above, draw an arrow from each variable reference to the declaration of the local variable, parameter, field, or static variable to which it refers.  (Don't draw self-arrows for declarations.)
    b)    Does method "toString()" above return a value or print a value?  How --Java willing-- would you change the code for method "toString()" to make it do the other (print instead of return or vice versa)?
    c)    Define the term "class".
    d)    Define the term "method".
    e)    Explain the difference between public and private.
    f)    Explain the differences between static and non-static.

  3. a)    What is the pattern for repeatedly doing something N times?  The pattern for processing an input sequence of pairs of values terminated by a pair of stopping values?
    b)    Androids Trurl and Klapaucius perform a random walk on a grid.  Both begin at square (0,0).  At each step, they independently move one square to the right and one square up or down, eg., from (0,0), each android will move independently to either (1,1) or (1,-1).  Trurl's probability of moving up is 60%; Klapaucius' probability of moving is 45%.

    Write a code segment to simulate 200 steps of their random walk and count how many times they meet during their walk (how often they are standing on the same square).  Output this number to the screen.  Do not include their starting position in your count.

    HINT: The expression Math.random() <= 0.4 will be true with with probability 40%.  Function Math.random() returns a random number strictly between 0.0 and 1.0.
  4. Recall that the minute hand of a clock and the two digits for the minute son a digital clock represent minutes modulo 60: numbers go from 0..59 and then "wrap around".  Let us implement a class MM60 (short for "minutes modulo 60", for convenience) with integer fields *tens* and *ones* for the tens digit and a ones digit, respectively, where we (you) maintain the condition 

    0<= tens <=5 and 0<= digits <= 9

    because any quantity of minutes can be converted to an MM60 quantity in the range 0..59.  Examples: 
    28 minutes:  tens = 2, ones = 8
    125 minutes = 5 minutes modulo 60:         tens = 0, ones = 5
    -17 minutes = 43 minutes modulo 60: tens = 4, ones = 3

    a)     Write all the code (show us everything that would go into file MM60.java) for a class MM60 with integer fields *tens*, *ones* as described above and two methods *swap* and *add*:

    -- Method *swap* is used to swap the contents (fields) of two MM60 objects.
    -- Method *add* is used to take two MM60's and return their MM60 sum.
    -- NO LOOPS.
    -- Remember to use the (default) constructor for instances of class MM60.
    -- The two methods are independent of each other.
    -- You may do parts (b) and (c) under the assumption that part (a) is right, even if (a) is incomplete or wrong.
    -- Be aware that the remainder 4 = a%b could be negative when a < 0 (but satisfies 0 <= |r| < |b|, where |r|, |b| are the absolute values of r, b).

    b)  Use calls to your *swap* method from (a) to write a code segment (outside of class MM60) to "shift" (rearrange) the values of MM60 variables a, b, c as follows:

    a ends up with b's original value
    b with c's original value, and
    c with a's original value.
    --      You may NOT use any addiitional variables
    -- You may NOT reference the fields (instance variables) *tens*, *ones*.

    c) Use the (default) constructor for MM60 to write a short code segment (outside of class MM60) equivalent ot a = -x; where *x* is an integer variable and *a* is an MM60 variable.

    d)     Use your *add* method from (a) to write a short expression (outside of class MM60) equivalent to 2*a + c, where a and c are MM60 variables.  (Note: since you are writing an expression, you may NOT reference the fields *tens*, *ones* and you may NOT declare any more variables. )

10/14 ROOMS FOR PRELIM 2:

(go to the appropriate room based on your last name:)

A - H Ives 105

I - Z Ives 305

10/13 WORKING TOGETHER:

remember, the only allowed groupings are EITHER singletons and pairs.
please do NOT violate academic integrity; it wastes everyone's time.

10/8 RESIZING THE CONSOLE WINDOW:

the console window on macs is resizable and scrollable.

it turns out the console window on PCs can be made
larger and also made scrollable:

+ click on the colorful icon in the upper-left of
the window.

+ select Properties...
(on my computer, the menu disappears when i try to
use it, but i can type the letter 'p' to choose 
this menu item.)

+ choose the Layout tab.

+ you can change the Height of the Window Size
to be something more civilized, e.g. 40

+ you can change the Height of the Screen Buffer Size
to be friendlier, too, e.g. 1000

+ click OK.

+ you can select either "Apply ... to current window only"
or "Save ... for future windows ..." and then click OK.
note that in the public labs, this setting will have to
be reinstated each session you use the computer.

10/7 SWITCHING FROM CS100 TO CS99

i understand that, after prelim 1, some of you are considering
switching to cs99. this is probably a good idea for some of
you who had trouble with the prelim, but it is perhaps an 
overreaction for the rest of you. if you are thinking
about this, i recommend e-mailing me or dropping by to talk
about your best option.

if you do wish to switch, here is how:

you need to get permission from the instructor of the cs99 section 
you want to add:

me, Thomas Yan, tyan@cs.cornell.edu, upson 322
for Tue-Thu 8am
or Tue-Thu 9:05am

or, Matt Morgenstern, matthew@cs.cornell.edu, rhodes 603
for Mon-Wed 8am
or Mon-Wed 9:05am
or Mon-Wed 11:15am
or Tue-Thu 11:15am

you also need to get a petition to add from your college advising office.
this petition requires an instructor signature. in addition, Arts
students need to meet with their class dean before making the switch.