V22.0101-001
Intro To Computer Science (undergrad)
Spring 2011
Instructor: Kenneth Perlin

Class time/place: Tuesday / Thursday 11-12:15, RM 102, WWH

Office hours: Wednesday, 4-5pm, 715 Bwy, 12th floor

Grader: Sam Scheinhaus

Tutor: Andrew Flockhart (Mondays 12:15-3:00, Tuesdays 12:15-2:00 and 3:30 - 6:30, Thursdays 3:30 - 6:00), Rm 328

Students: who have agreed to have their work shown on-line

This will mostly be a projects course. Showing up to class is essential. Basically you will learn how to be a good programmer by programming.

It will be a lot of work, but it will be fun. :-)

Here's a good book you might want to get as a reference for Java, which you can find at the NYU Book Store:

Introduction to Java Programming, Brief Version, 7/E
(If you already own the Sixth Edition you can use it for this course)
By Y Daniel Liang
Published by Prentice Hall, 2008
ISBN-10: 0136042589 ISBN-13: 9780136042587


Homework for Thursday January 27:

Your assignment for Thursday is to set up your own Web page, customize your index.html file in some way (just so you know you can), and then email your URL to our grader Sam, whose email address I gave in class.

 
Making your NYU webpage 
 
NYU provides a free website to anyone with an NYU NetID.  The official way to do this is via 
the Files 2.0 web client which NYU provides.  Here are instructions to set yours up.
http://www.nyu.edu/its/nyuhome/web/ 
 
IMPORTANT 
Some students have reported difficulty using Google Chrome with Files 2.0 on Mac OS X 10.6+.  
If you encounter problems, try Apple Safari instead.

For Tuesday February 1:

IMPORTANT: Sign up for the class mailing list by clicking on the following link:

http://www.cs.nyu.edu/mailman/listinfo/v22_0101_001_sp11

Most of you have sent an email to grader, contining your name and the URL of your web page. But a few of you have not yet done so.

If you have not yet done so, make sure to send that email to the grader as soon as you can, whether or not you have yet succeeded in editing your homepage.

If we don't have that information for you by the start of class on Tuesday, it's going to cause real problems for you in this class going forward.


For Tuesday February 8:

  • Onto your local computer, unzip the file feb-1-class.zip.

  • If you're on a Mac, you don't need to install any software -- it's already there (yay!).

  • If you're on Windows, you'll probably need to download and install the Java JDK, as follows:
    1. Download the Java JDK:

      • Go to the download page: Java JDK.
      • Click on the "Download JDK" button.
      • Select "Windows"

    2. Locate the installation directory
      • The JDK installation makes two directories, one is called jdk1.6.0_23 which contains the developer tools for java, and the other one is called jre6 and contains the java runtime and is used to run compiled java programs. To develop using the MS-DOS command line prompt, we need to get the path to the program called javac inside the bin subdirectory of the jdk1.6.0_23 directory. Go ahead and find the bin subdirectory now and write down the full path. On my computer, the path is C:\Program Files\Java\jdk1.6.0_23\bin

    3. Set the path variable.

      • From the explorer bar, open up the MS-DOS command line utility by typing cmd in the program call Run.
      • At the DOS prompt, type the following line and press enter.
        • set path=YOUR PATH FROM STEP 2 ABOVE;%PATH%
        • For example on my computer I would enter set path=C:\Program Files\Java\jdk1.6.0_23\bin;%PATH%
      • Verify that updating the path to your java jdk worked by trying the following test
        • javac -version
        • You should see something like "javac 1.6.0_23" returned on the command line.
        • If you see an error, it could be that you installed the jdk into a different location than I did. Check to make sure you wrote down the correct path to the bin directory in step 2.

      • To permanently set the Path variable in Windows 7:

        From the Start menu, click on Computer
        Then click on System Properties
        Then click on Advanced system settings
        Then click on the Advanced tab
        Then click on Environment Variables
        Then in System variables select Path
        Then click on Edit...
        Then in the pop-up text window, prepend C:\Program Files\Java\jdk1.6.0_23\bin; to the beginning of the Path variable.

      • You can also look at these instructions.

  • Notice how I put a link to the homework1 folder into the main index.html file. You should follow this structure for your own index.html file.

  • In your homework1 folder, use your favorite text editor to create your own version of myhomework1.java. Try to create something that is fun and creative and original (and looks nothing like what I showed in class).

    You can compile your java program either by running the little "compile" script that I've included, or by actually typing out: javac -source 1.2 -target 1.1 *.java

    IMPORTANT: In your command window (a Terminal window if you're on a Mac, or a Command Prompt window if you're in Windows), you must cd (change directory) to the same folder that contains your homework.java file. Otherwise, the javac program won't find your homework.java file.

    You can test your program on your own computer by typing, while in your homework1 folder: appletviewer index.html

  • When you are satisfied with your program, upload the files -- html files, java files and class files -- to your class web page.

    Documentation to help you with the assignment for Tuesday February 8:

    Here is a link to documentation about the java.awt library.

    In particular, you might want to look at documentation for the Graphics class, which contains all of the drawing methods you will be using.

    You can see the program I wrote in class today (Thursday, Feb 3) by unzipping the file feb-3-class.zip.

    Here is a useful chart by Deean Engel comparing features of Python and Java. We will go over this in class.


    Tuesday Feb 8 class and homework for Thursday Feb 10:

    In class today we talked about how an int and how a double are stored. We also talked about how the name and value of a variable are stored, and how the value of an array is just a sequence of contiguous locations in computer memory.

    If the length of an array is N, then the indices of the array go from 0 through N-1. For example, if you declare and initialize an array like this:

       int foo[] = new int[10];
    
    then valid indices are from foo[0] through foo[9].

    We also talked about the variables that I am providing, in the BufferedApplet class, that let you detect when the user has input something using the mouse or keyboard. For the mouse in particular, I am providing you with the variables:

       boolean mouseDown
       int mouseX
       int mouseY
    
    which tell you whether the mouse button is currently down, and the x,y position of the mouse within the applet window.

    I am also providing you with the variables:

       boolean wasMouseDown
       int wasMouseX
       int wasMouseY
    
    which tell you what the mouse was doing during the previous render (the one just before this one).

    So, as we discussed in class, you can respond to a mouse DOWN (eg: the start of a click) with the following code:

       if (! wasMouseDown && mouseDown) {
          ...
       }
    
    and you can respond to a mouse UP (eg: the end of a click) with the following code:
       if (wasMouseDown && ! mouseDown) {
          ...
       }
    
    When you read the above code, you should keep in mind that the symbols "!", "&&" and "||" are how you say not, and and or in Java, respectively.

    We coded up a little example in class today. The code for that example is here.

    HOMEWORK FOR THURSDAY FEB 10:

    On your public web page, describe the following:


    Thursday Feb 10 class: You can download the coding I did in class today from feb-10-class.zip.

    Mostly we covered how to use my keyDown and wasKeyDown arrays to detect keyboard events.

    We also covered how to represent the Ascii value of a character. For example:

       if (keyDown['A']) {
          ...
       }
    
    is using the Ascii value of Capital A (which happens to be 65). Which to the computer will be exactly the same as if you had actually remembered the Ascii value of this letter and had used it directly:
       if (keyDown[65]) {
          ...
       }
    

    Also, I showed you how to write to the STANDARD OUTPUT by using:

       System.out.println("this will print to standard output");
    
    and how to print error messages by using:
       System.err.println("this will print messages or messages on your console.");
    
    The second one especially is useful for debugging your programs. If you run appletviewer the following way:
       appletviewer index.html >textfile.txt
    
    then anything printed to System.out will go into the file, whereas anything printed to System.err will print in your command window.

    IMPORTANT:

    The version of BufferedApplet.java in feb-8-class.zip was the wrong one (which is why some of the input things didn't work in class today). The old one worked just great for drawing things, but not for handling mouse and keyboard input.

    The version of BufferedApplet.java in feb-10-class.zip is the right one. Make sure to use the new one. In other words, don't use the BufferedApplet.java you already had for this week's assignment. Use the newer one that is in feb-10-class.zip.

    Homework for Thursday Feb 17:

    Using your creativity and imagination, make and put up on your public site a java applet that makes interesting use of mouse and keyboard input. Now that you know how to detect clicking of both the mouse and keyboard keys, there are lots of possibilities.

    Here are just a few possibilities among many:

    The above are just a few random ideas. Of course there are at least hundreds of other ideas you might prefer to try, and I strongly encourage you to think of something that you would like to see and interact with.

    Your program can be very simple (although of course I'd love to see you make something cool and clever and fun),

    But it must make use of keyboard input.


    Tuesday, Feb 15: Guest lecture by Murphy Stein

    The code that Murphy showed you on Tuesday is in the zipped file feb-15-class.zip.


    Thursday, Feb 17: Introduction to buttons

    In class we made some simple interactive graphical buttons, which was a little difficult because we didn't make them as Objects (which we will be doing next week).

    The code from that class is in feb-17-class.zip.

    HOMEWORK due by class time Thursday Feb 24:

    Create an interactive graphical program that uses buttons and toggles. As usual, make it fun and interesting if you can, and if possible, write the program with a theme that touches on some topic you know and like.


    Tuesday Feb 22 class notes:

    In class today we covered while loops and for loops.

    Of the two, the while loop is simpler, but it does less. Here is an example:

       int sum = 0;
       int i = 0;
       while (i < 10) {
          sum = sum + i;
          i++;
       }
    

    You have to be careful when you write a while loop, because it will keep looping as long as the test at the top is true. Which means that if you're not careful, you could end up with a loop that loops forever, which means your program will appear to freeze.

    For example, the following while loop never ends (which is bad):

       int sum = 0;
       int i = 0;
       while (i < 10) {
          sum = sum + i;
          i--;  // Oops!  Variable "i" is decreasing, instead of increasing.
                // This will lead to an endless loop.
       }
    

    One of the problems with while loops is that the different parts of the control structure are scattered in too many different places in your code. For this reason, Java provides another control structure, the for loop, where all of the things that control the loop are in the same place.

    Here is what that while loop would look like if it were a for loop:

       int sum = 0;
       for (int i = 0 ; i < 10 ; i++)
          sum = sum + 1;
    
    Look at the line for ( ... ) above. The three bits of code inside the parentheses are the initializer, test and iterator. The initializer sets the iterating variable to its initial value when the loop starts (in this case, it sets i to 0). The test checks, before each iteration of the loop, whether to continue looping or not (in this case, making sure i is still less than ten). The iterator changes the looping variable (in this case, incrementing i).

    For your homework due on Thursday I'm not asking you to use loops, but it's good to get started on these concepts.

    Then in class today we showed how to make your own object classes. In particular, we made an object class called MyButton, which has some data fields and a single method. The code for that is in file feb-22-class.zip.

    Important note: When you open feb-22-class.zip you will see that the index.html file shows you how to link to the actual java source files (in this case BufferedApplet.java, MyButton.java and applet6.java). You need to put similar links in your index.html file for the assignment that you will hand in this coming Thursday (look in the index.html file in feb-22-class to see how to do that), because The grader needs to see your source code.


    Thursday Feb 24 class notes:

    Today we showed how to organize objects into an array, and also how to use for loops to iterate over all of the objects in an array of objects. Being able to use array.length turns out to be really useful, since it lets us know when to stop the for loop iteration.

    The programming I did today in class is available at feb-24-class.zip. It shows how using arrays of objects and for loops can help organize an entire array of buttons in a straightforward way.

    That example also contains several examples of making an object "smarter". In this newest version, the MyButton object now knows how to draw itself, because we've implemented a draw() method in the MyButton class.

    Also, I added a setPressed() method to the MyButton class, instead of setting button.isPressed directly. Using this style of programming will allow us to use objects in more sophisticated and useful ways in the coming weeks.

    Your Homework, due by class time on Thursday March 3:

    is to build an interactive Java Applet that makes use of arrays of objects, and uses for loops to do interesting things with them. I'm expecting to see all sorts of cool homeworks this time around, since you now have the power to do much more visually interesting things.

    Unfortunately I didn't get time today to show you how to add audio to your applets. I will be going over that next Tuesday. If you're feeling ambitious, you can get a jump on that by looking at this example AudioApplet. If you try something with audio, it's good to remember two things:

    • You need to include the line import java.applet.AudioClip; at the top of your java program, as in the example AudioApplet, and

    • You need to include a sound file (in the example I include the file thud.wav) in your folder.

      You can find lots of cool .wav files by going a Google search for "wav".

    Have fun!


    Tuesday Mar 1 class notes:

    Today I showed you a number of things, all of which are in the downloadable code examples mar-01-class.zip. that we implemented in class today.

    Among the topics we covered, all of which are in the code examples,are:


    Thursday March 3 class notes and homework

    Here is the example we did in class of an array of rows and columns that can be implemented either as a two dimensional array or a one dimensional array: mar-03-class.zip.

    ***

    In class I talked about the danger of semicolons in Java. One of the most common mistakes beginners make is to not realize that a semicolon, all by itself, is a complete statement. For example, here is a mistake I saw in one of the homeworks this past week. A student put an extra semicolon in an if statement, producing the following incorrect code:

        if (i >= 10) ; {
           x = x + 1;
           y = y - 1;
        }
    
    If you do this, the ";" will be interpreted as the body of the if statement. The result will be that everything between the "{" and "}" will always be executed, which is probably not what you meant.

    What this student actually meant to do was probably something like this:

        if (i >= 10) {
           x = x + 1;
           y = y - 1;
        }
    
    Notice that now, in the correct version, there is no semicolon between the ")" and the "{".

    You need to watch out for the same thing when writing a for loop -- make sure there's no semicolon between the ")" and the "{".

    ***

    Today we also talked about the difference between a class of objects and an instance of an object. For example, MyButton is a class:

       public class MyButton
       {
          ...
       }
    
    whereas button1 is an instance:
       MyButton button1 = new MyButton();
    
    You can think of a class as a factory that produces object instances, much as an automobile factory might produce cars. You shouldn't confuse a class with an instance, any more than you'd confuse an automobile factory with a car.

    Variables in a class definition (also called "instance variables") don't have values in the class itself, but only within individual instance objects, just as a particular steering wheel or set of tires is part of each car -- not part of the factory that makes all the cars.

    For example, when we say:

       public class MyButton
       {
           int x, y;
       }
    
    the variables "x" and "y" only exist when you create an instance of the class. Such variables are part of every button instance, so you can refer to something like, button1.x, but they are not part of the factory itself, so it makes no sense to talk about, say, MyButton.x.

    Sometimes you actually want to talk about variables and methods that refer to the object class itself -- not to particular instances of the class. In that case you need to declare those variables and methods to be static.

    For example, you might want to keep a running total of how many buttons have been instantiated. You can do that with a static variable as follows:

       public class MyButton
       {
          static int totalInstances = 0; // THIS BELONGS TO THE CLASS ITSELF
    
          int index;  // A SEPARATE COPY OF THIS IS FOUND IN EACH INSTANCE
    
          // THE CONSTRUCTOR iS GIVING EACH INSTANCE ITS OWN UNIQUE INDEX
    
          public MyButton() {
             index = totalInstances++; // INCREMENT THE TOTAL AT EACH NEW INSTANCE
          }
    
          public int myIndex() {
             return index;
          }
    
          public static int howManyInstances() {
             return totalInstances;
          }
       }
    
    In the above example, the non-static method myIndex() is something that individual buttons can call, so the application can find out that particular button's index out of all buttons created. So you could say something like button1.myIndex().

    In contrast, the static method howManyInstances() can only be called by the class itself, like this: MyButton.howManyInstances().

    ***

    In class today we also introduced two dimensional arrays. In Java you can create a two dimensional array with a statement like:

       int foo[][] = new int[10][10];
    
    The above line is just declaring and then creating an array of arrays. It's really simple once you get used to it. Every element of foo is an array. And each of those arrays itself happens to be an array of int. Once you understand that concept, things get easy.

    For example, the expression:

       foo[0]
    
    refers to something that happens to be an array of int (since every element of foo is an array of int). So it's perfectly reasonable to say something like:
       foo[0][1]
    
    and expect that the result will be an integer.

    In the code I implemented in today's class are examples of this concept. You should definitely look at that code.

    Homework due Thursday March 10

    The homework for next Thursday is to show that you can create and use a two dimensional array of objects.

    Once you have the ability to create two dimensional arrays of object -- and those objects can display as images -- then there are many interesting projects you can do. Here are a few ideas:

    About the Midterm on Tuesday March 8:

    On the midterm exam (which I don't really want to give, because it takes away from class time, but apparently departments have rules), I am mostly just going to ask you to write little bits of programming, in pen (not on a computer), so that you can demonstrate that you actually know how to create an object, declare object variables and methods, create an array of one or two dimensions, write a for loop or an if statement, show how to detect a mouse click down or a mouse click up, refer to the variables and methods within an object from another object, and the various other things we've covered in class so far.

    If you've been coming to class and have been doing the assignments, you're going to do fine on this test. I'm not going to permit notes for the test, because it's more fair if there's a level playing field. But you shouldn't need notes to do well on the midterm if you've been doing the assignments.


    Questions and answers on the midterm:

    ARE HERE

    Homework due Thursday March 31:

    After seeing the sorts of difficulties that a number of people are having with some of the concepts we've covered so far, I decided that we need to regroup a bit, and make sure everyone has a reasonable mastery of the concepts to date, rather than charging on and learning lots of new material.

    So I've created a homework assignment that is specifically designed to let you work on the concepts in Question 2 of the midterm -- the question that a good number of you had problems with.

    Here is a link to the assignment. You should get right to work on it, since it will be due next Thursday, March 31.


    Here is a link to the java programs we developed in class on Tuesday, March 29.
    Thursday, March 31

    In Murhpy Stein's guest lecture he showed you inheritance using the metaphor of an Animal Kingdom. Starting out with an Animal class, he subclassed that to a Dragon, and to a Bear. Then finally he subclassed Dragon into a MysticalDragon. He also explained that a subclass "is a" superclass.

    Then he covered:

    He also briefly touched on:

    We are going to follow up on this next Tuesday by showing how to apply these concepts of interitance within our applet framework.

    Meanwhile, here is the code from Murphy Stein's lecture.


    Here are the class notes and homework assignment for the last two lectures -- Tuesday April 5 and Thursday April 7.

    The homework assignment will be due next Thursday -- April 14.


    Course notes for the week of April 11-15.
    Here is the description of the assignment due Thursday April 28.
    In class on Thursday April 28 I showed an example of using a more mature set of packages -- a 3D modeling/rendering graphics package.

    Your homework for Tuesday May 3 is here.


    There is no class on Tuesday May 10.

    The final exam will be at 10am on Thursday May 12, in the same room where we hold class (room 102). The exam will last until 11:50am.