Spring 2001 CS100(J,M) Project 1 Solutions

Project 1: Part A (J & M Questions)

Note: Graders only spot-checked some answers, but you are responsible for all the answers.
1.  What is the primary difference between CS100J and CS100M?

They emphasize different programming languages.  Both teach basic
programming notions, but CS100J is heavily weighted toward Java (12
weeks of Java are followed by 2 weeks of Matlab), while CS100M
allocates equal time for both.

2.  Are the web pages for CS100 J and M identical?

No. The similarity in appearance is superficial.  You will find many
differences if you look closer.  Check out the "Announcements" page,
for example.

3.  Programming is a skill. What should one do to develop a skill?

Practice, practice, practice.  It is hard, if not impossible, to learn
programming by looking at examples developed by others.

4.  Where and when should you submit this assignment, assuming you hand
    it in on the due date?

The solutions to this assignment should be handed in on Tuesday,
January 30, in class.

5.  Where and when may you submit a project earlier than the due date?

You must submit your project personally to a consultant in Carpenter
Hall.  (Note: this applies only to early submissions.)

6.  May you submit exercises to Carpenter?

No.

7.  Why should you collect graded work, regardless of the score you
    received?

Grading provides valuable feedback on your work.  The graders'
personalized comments will help you a lot, since they show you what
you should have done to solve the problem correctly.  Just looking at
the official solutions might not help you with that, since typically
there are many ways to solve a problem, and your attempted solution
might have followed a different line of thinking.

On projects, since grading is not that strict, even if you got a
perfect score, you might have made mistakes that will cost you points
on exams.  Also, even if your project is perfect, you can still learn
about mistakes to avoid in the future from the project grading guides
stapled to corrected projects.

On exams, you should check to see that there were no arithmetic errors
in adding up your score.

8.  May you work with a partner on projects? exercises?

You can work with one partner on a project.  You can pick a different
partner for each project, though.

9.  May you work with multiple partners on the SAME project? For instance,
    you work Sam on Part 1 and Pat on Part 2.

No.

10. Give three examples of university-sanctioned excuses.

a. Conflict with religious beliefs;
b. Documented medical emergency;
c. More than 2 exams in a 24-hour period.

11. Assuming you weren't seriously ill, may you submit a late project or
    exercise, even if it is only a couple minutes late?

No.

12. In Question 11, what if the printer broke? Or your partner slept
    through an alarm? Or YOU slept through the alarm, and you're willing
    to lose the points, but you hope the partner won't? (Yes, that's
    admirable, but will it work?) The answer to all these situation
    is...?

No.  It is your duty to plan ahead and prepare contingency plans for
unexpected events.

13. In Question 12, suppose you attempt to sidestep the course instructors
    by handing the assignment to a TA, support staff, Laurie Buck, or send
    an e-mail attachment. Will any of these approaches work? Why?

No.  The homework must be submitted in lecture.  The course staff will
refuse to accept any homework handed in other circumstances.

14. Suppose you do miss a project. How might you STILL obtain a perfect
    project grade in either CS100J or CS100M?

A missed project means a loss of 10 core project points, but you do not
need all the possible core project points for a perfect project score P:

J: you need 50 core project points, so you need to get perfect scores
   on the other 5 projects.

M: you need 65 core project points, so you need to miss at most 3 core
   points on the other 7 projects to get at least 7*10-3-2=65
   core project points.  (Remember, unexcused projects count as -2, not
   as 0.)

15. Do you think starting EARLY or LATE on a project will help prevent the
    situations presented in Questions 11-13?

Starting EARLY will definitely help.

16. How do you request a regrade on any graded work in CS100?

You have to fill out a regrade request form, attach it to the exam or
assignment, and hand it to a consultant in Carpenter Hall.

17. Consult the Grading pages for CS100J and CS100M. Given the following
    averages in terms of percentages E=53, P=95, T1=71, T2=83, T3=78, F=84
    the student would receive a what numerical grade for CS100J? CS100M?

J: The numerical grade G is computed using the formula
       G = .05*E  + .25*P  + .1*T1 + .2*T2 + .2*T3 + .3*F  - .1*DROP,
   where DROP = min(T1,T2,T3,F).  In this case,
       G = .05*53 + .25*95 + .1*71 + .2*83 + .2*78 + .3*84 - .1*71 = 83.8

M: The numerical grade G is computed using the formula
       G = .1*E  + .2*P  + .1*T1 + .2*T2 + .2*T3 + .3*T4  - .1*DROP,
   where T4=F and DROP = min(E,P,T1,T2,T3,T4).  In this case,
       G = .1*53 + .2*95 + .1*71 + .2*83 + .2*78 + .3*84 - .1*53   = 83.5

18. Consult the Material page. What is ProgramLive? Is it required?

ProgramLive is a Multimedia environment, which combines animation,
sound, text and hypertext to provide instruction in Java and
object-oriented programming.  You are not required to purchase
ProgramLive, but you will probably find it very useful.

19. Consult the Internet page (look in Material for the link). Describe
    three things that violate the e-mail/newposting guidelines for CS100.

a. Rudeness
b. Use of anything else than plain text messages (e.g. use of HTML tags)
c. Not quoting enough of a message you reply to to establish the
   relevant context for an interested reader.

20. Consult the FAQ page. Who is Laurie Buck? What is another page that
    describes her role?

Laurie Buck is the CS100 course administrator.  Her role is described
on the page referred to by the "Staff&Help" link as well.

21. Consult the Staff pages for both CS100J and CS100M. May you see a
    teaching assistant who is not your section instructor?

Yes.

22. How do you make an appointment to see Yan? Schwartz?

(The question is ambiguous since there are 2 Yans.)

TKY: Send e-mail.
DY:  Call the CS undergraduate office.
DIS: Contact his assistant, Kathy Carpenter.

23. Consult the Staff page. What is the URL for the link that has a
    listing of a variety of services that should be the first place you
    look for additional help?

The following is the URL for "Academic Support Services For
Engineering Students":

http://www.engineering.cornell.edu/studentServices/Advising/AcademicSupportServices.htm

(Note: this URL unavoidably exceeds the specified width of text, but
since it is essentially code, it is ok.)

24. What is Maxim Zolotov's preferred e-mail address?

maxim@cs.cornell.edu

25. What is the CS100 AEW?

It is the "Academic Excellence Workshop", a collaborative, not
competitive, group learning environment.  Undergraduate students
familiar with the CS100 material present interesting and challenging
problems, and material they have prepared independently.

26. Where may you receive your graded projects and exams? If you do not
    collect them at this location, where will they go?

Graded projects and exams will usually be handed out in section and
before and after review sessions.  When close to a prelim, they might
be handed out in the Carpenter Lab.  You may retrieve your unclaimed
work from the consultants at Carpenter Lab at the end of the week in
which they were returned in sections.

27. If you go to that place in the previous question, and you cannot find
    your work, should you contact Laurie Buck to see if a grade was
    recorded?

No.  Check to see the posted scores to see if a grade was recorded.

28. What are three labs that have course software available? Which of
    these labs requires that you obtain a separate account?

The three labs are Carpenter Computer Lab (B101 Carpenter Library),
Upson Computer Lab (B07 Upson Hall), and the ACCEL lab (247
Engineering Library).  ACCEL requires a separate account.

29. Why should you NOT schedule a plane ticket for Spring Break on
    Thursday, 3/15?

This is the date of Prelim 2.

30. What page stores examples used in lecture for both CS100J and CS100M?

Examples.

31. What should you do if you miss a prelim in CS100J? In CS100M?

M: Contact the instructor and try to do as well as possible on the
   other exams to compensate for the loss of points.

J: Contact the instructor and take the comprehensive make-up exam.

32. Do you need to purchase CodeWarrior?

No.  CodeWarrior is available in several public labs.

33. Do you need CodeWarrior to write programs?

No.  Code Warrior is just one of a relatively large number of
integrated development environments (IDEs) that one can use to
simplify programming and debugging.  A plain text editor and a Java
compiler is all one needs to write programs.

34. How can you obtain a free copy of Java?

Please check the Java page on either the J or M page.

35. Precisely what version of CodeWarrior is installed in Carpenter lab?
    Precisely what versions are sold in the campus store? What do you do
    if you want to frequently switch between different versions of
    CodeWarrior?

The Carpenter Lab has version 5.3 of CodeWarrior installed, while the
Campus Store sells version 6.  The different CodeWarrior versions have
incompatible project files, so to switch between them:

a. Create 2 separate Projects, one for each version of CodeWarrior.
b. Add the same .java files to both projects.

36. Precisely what version of MATLAB is installed in Carpenter lab?
    Precisely what versions are sold in the campus store? What do you do
    if you want to frequently switch between different versions of
    MATLAB?

The Carpenter Lab has version 5.3 (coincidence!) on the PCs (the Macs
probably have version 5.2 or so), and the Campus Store sells version
6.  The file formats should be compatible between various Matlab
versions, so you should not have to do anything to switch between the
different versions.

37. Consult CS100J's Material page. Somewhere on this page is a link for
    table with keyboard character names. What are two names for the
    character "&"?

"Ampersand", "And".

38. Below, write the following sequence of characters: bar, tilde, pound
    sign, backquote, forward quote, dollar sign, virgule.

|~#`'$/

39. Guess how many students take CS100 each year (including the summer
    semester).

About 1200 students.

40. Which professor, Yan or Schwartz, has a beard? (Just checking :-)

Schwartz.

Project 1: Part B (Matlab)

%-----------------------------------------------------------------------------%
% p1PartB.m
% DIS
%-----------------------------------------------------------------------------%

% Cleanup:
  close all % close all figure windows
  clear     % clear variable assignments
  clc       % command window

% Assign data for plotting:

  weights =  0:10:100;

  deflections = [ 0 0.7 2.2 2.9 4.1 4.8 5.9 7.0 8.3 9.1 9.9 ];

% Generate plot:

  plot( weights, deflections, 'r-' )

% Label plot:
  title('Weights vs Deflections')
  ylabel('Deflections')
  xlabel('Weights')

% Answer question on plot:

  text(50,2,'Deflection for weight of 75N = 7.65')

Note: For this assignment, it suffices to get the value for the deflection by "eyeballing" the graph. A better solution might be to linearly interpolate the values for 70N and 80N. An even better solution might be to perform a least-square fit of a line through all the points and then compute the value for 75N.

Project 1, Part C (Java)

/*****************************************************************************
 * p1PartC.java                                                              *
 *****************************************************************************/

import java.awt.*;

public class p1PartC {
    public static void main(String[] args) {
        Drawing d = new Drawing();    // create object for drawing graphics
        d.setTitle("Drawing Window"); // set title of Drawing window
        d.setSize(400,200);           // set size of Drawing window
        d.show();                     // display/show window (starts off hidden)
    }
}

class Drawing extends Frame {
    public void paint(Graphics g) {
        // Drop below window title bar:
           g.translate(getInsets().left, getInsets().top);
        // Draw leftmost snowperson:
           g.setColor(Color.blue); // was: g.setColor(Color.red);
           g.fillOval(40-10, 10, 20, 20);
           g.fillOval(40-20, 30, 40, 40);
           g.fillOval(40-30, 70, 60, 60);
           g.drawLine(20, 50, 20-20, 50-20);
           g.drawLine(60, 50, 60+20, 50-20);
        // Draw middle snowperson:
           g.setColor(Color.red); // was: g.setColor(Color.green);
           g.fillOval(80+40-10, 10, 20, 20);
           g.fillOval(80+40-20, 30, 40, 40);
           g.fillOval(80+40-30, 70, 60, 60);
        // Draw arms:
           g.drawline(80+20, 50, 80+20-20, 50-20);
           g.drawline(80+60, 50, 80+60+20, 50+20);
        // Draw rightmost snowperson:
           g.setColor(Color.green); // was: g.setColor(Color.blue);
           g.fillOval(2*80+40-10, 10, 20, 20);
           g.fillOval(2*80+40-20, 30, 40, 40);
           g.fillOval(2*80+40-30, 70, 60, 60);
           g.drawLine(2*80+20, 50, 2*80+20-20, 50+20);
           g.drawLine(2*80+60, 50, 2*80+60+20, 50+20);
    }
}

Project 1: Part D (Algorithms)

Task 1: Tying Shoelaces

 1. Put left shoe on left foot.
 2. Grab left end of shoelace with right hand.
 3. Grab right end of shoelace with left hand.
 4. Cross shoelace ends; pull tightly.
 5. Form a loop in the right shoelace end with left thumb and forefinger,
    holding the loop tightly against the foot.
 6. With right hand, pull the left shoelace end around both the left thumb
    and first loop.
 7. With left thumb, pull left shoelace through the second loop formed by the
    left shoelace and thumb.
 8. Tighten. 
 9. Put right shoe on right foot.
10. Repeat steps 1-8 with right shoe.

Task 2: Most Common Birthday

In the following we will assume that all students are seated and that they don't change positions until the algorithm ends. The following is a semi-informal description of the algorithm:

The instructor marks "date = " and "nos = 0" (number of students with
birthday on the given day) onto the board.

While there are students who did not indicate their birthday in the class
  Find one such student.
  Ask his/her birthdate.
  Ask all those whose birthday is on the same date to raise their hand.
  Count the total number of students (including the first one) who answer.
  Compare this number to the one written near "nos" on the board.
  If it is bigger
  then
    Erase the old date, if any from the blackboard.
    Write the new date near "date" onto the board.
    Write the number of students who were counted in this step near "nos."

Some observations about the algorithm:

  1. The instructor needs a way to determine whether there are students who did not give their birthdates already.
    He could do this by just asking whether there is anybody in this situation. If he decides to do this, the algorithm would stop when nobody replies to his question. If several people reply, he would have to choose one individual to talk to. In programming, as in real life, the lack of an answer is not equivalent to a 'no' answer. What if somebody did not hear the question, or there was noise in the class and the instructor did not hear the answer? These issues are analogous to some problems that occur in real computer systems, especially when one sends messages over networks. Good solutions to these are hard to find, and certainly much beyond the scope of the course.
    A solution more in the spirit of our class is for the instructor to systematically talking to each student by questioning them in order, going from left to right on each row, starting with the first one, and proceeding toward the back of the room. The question the instructor would ask each student in turn would be "Did you indicate your birthdate already?" (or something equivalent).
  2. Note the careful wording. "Indicate" means that a student either explicitly provided his/her birthdate, or implicitly indicated it when raising his/her hand and being counted by the instructor.
  3. Each student indicates his/her birthday only once. After that, s/he will be asked at most whether s/he gave his/her birthdate already, and this question will be asked at most once from each student (assuming the instructor questions the students in order, row by row). Can you tell why this happens? A good way to start thinking about this question is to understand who might not be asked whether s/he already gave his/her birthdate.

Project 1: Part E (Bonus Problem, CS100M ONLY)

Suppose Alan, the TA, performed the following algorithm at one of the joint CS100J&CS100M software demos:

    While there are 2 or more CS100 students
        Pick 2 students at random.
        If they are from the same class (both J or both M) then
            They both leave.
            A J student enters.
        If they are from different classes (one J and one M) then
            The J student stays.
            The M student leaves.

Observe that at each step, the number of CS100 students goes down by exactly one. What, if anything, can be said about the last CS100 student left in the room?

Solution: If we start with 1 student, then he/she is the last student; if we start with 2 or more students, then the last student is always a J student.

Justification: by case analysis on the initial number N of students.