Java Basics for New Programmers
X52.9238 / Y12.1007

  1. Catalog descriptions.
  2. Syllabus and grading policy for X52.9238.
  3. How to …
  4. SOURCE CODE (.java files) for all the examples in the course
  5. Documentation.
  6. Eclipse and its page in Wikipedia
  7. AWT (Abstract Window Toolkit) and its page in Wikipedia
  8. SWT (Standard Widget Toolkit) and its page in Wikipedia
  9. Swing and its page in Wikipedia
  10. Databases:
  11. Your instructor at his other job.

Spring, 2007 Homework

Please bring a camera to the first class to take the class photo.

  1. February 26, 2007: If you don’t already jave a Java book, get one. I recommend The Java Programming Language, Fourth Edition, by Ken Arnold, James Gosling, and David Holmes. Admire the class photo. Read the above grading policy.

    Download the latest version of Java and Eclipse to your PC or Mac. Follow the hints for the HelloWorld.java program (not the Hello, World SWT application).

    Look at the source code of the programs we ran in class, e.g., HellowWorld.java. What happens if you try to increment a variable that already contains the maximum possible value? I rearranged the Input.java program to isolate the two chunks of code which I didn’t explain. Just paste in the two chunks that start with the comments “The following three lines” and “The following 16 lines”.

    Write and run a Java program that asks you to type in numbers and then gives you one or more answers. Here is an example that makes heavy use of % (the remainder operator).

    How many pounds does the turkey weigh? 7
    How many minutes per pound do you have to cook it? 20
    Then you have to cook it 2 hour(s) and 20 minute(s).
    What time is it now?
    Hour: 1
    Minute: 15
    The turkey will come out at 3:35.
    
    Hand in a printout of the program and the output produced by the program. Can anyone discover a way to print the output besides copying the text in the Console and pasting it into Notepad?

    Extra credit. If you are interested in making a GUI, run the “Hello, World” SWT application. See the instructions.

  2. March 5, 2007: Look at the source code of the programs we ran in class. Read about loops and if statements in whatever Java book you have. For example, in the fourth edition of the Gosling book, pp. 3–6 for while loops, 9–12 for for loops and if-else statements, and 229–246 for the in-depth treatment. In the Horton book, see pp. 77–118.

    The program JobTake.java illustrates the remainder operator %.

    Study the Pierogies.java program. Note that it has only one loop and only one variable, h (for “Hungarians”). You can change the h = h + 1 to h += 1 or, better yet, to ++h. Then write a program named Thruway.java that will print those signs you see at 10-mile intervals as you drive north on the New York State Thruway. The output will be the following 28 lines (including an empty line after each paragraph). Imitate the design of the Pierogies.java program. Use only one loop and one variable. Name the variable a, for “Albany”. Hand in the program and its output.

    Albany    98
    Montreal 308
    Buffalo  368
    
    Albany    88
    Montreal 298
    Buffalo  358
    
    Albany    78
    Montreal 288
    Buffalo  348
    
    Albany    68
    Montreal 278
    Buffalo  338
    
    Albany    58
    Montreal 268
    Buffalo  328
    
    Albany    48
    Montreal 258
    Buffalo  318
    
    Albany    38
    Montreal 248
    Buffalo  308
    
    

    You have already run the HelloWorld tutorial in Eclipse. Now run the HelloWorldSWT tutorial. Instead of writing in the Console, it will create an empty window. In addition to what you had to do for HelloWorld, you will have to download a new file, the “Standard Widget Toolkit” (step 3). You will also have to import the new file into Eclipse (step 4), and configure your HelloWorldSWT project (step 5). Don’t hand this program in.

    Then run FlagSWT.java. Like HelloWorldSWT.java, it will create a window. This time, however, the window will contain a picture in color. You won’t have to download the SWT or import it into Eclipse again—you have already done these things when you ran HelloWordSWT.java. But you will have to configure your FlagSWT project as you did in the step 5 of HellowWorldSWT. Don’t hand this program in. But read the part between “Point A” and “Point B” carefully. It has nested for loops, and chained-together if-else statements.

    Play around with it. Can you make the flag bigger? Can you make the stripes an attractive shade of bluish-green? Then modify FlagSWT.java to dispay a different flag. (Let’s not all do France, please.) Hand in the modified program and a screen snapshot of the flag.

    No class March 12. Please email me if you have questions: mark.meretzky@nyu.edu

  3. March 19, 2007: Here are links to databases of colors and flags. I put a random number generator into the TooLow.java guessing game.

    Pythagorean theorem: what is the distance from the origin to the point whose coördinates are (3, 4)? See the Math.hypot in DominicanSWT.java.

    Study Switch2.java, with and without the break statements. Then write a Java program that produces the following 114 lines of output. (There is one empty line at the end of each verse, including the last.)

    On the 1st day of Christmas
    My true love gave to me
    A partridge in a pair tree.
    
    On the 2nd day of Christmas
    My true love gave to me
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 3rd day of Christmas
    My true love gave to me
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 4th day of Christmas
    My true love gave to me
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 5th day of Christmas
    My true love gave to me
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 6th day of Christmas
    My true love gave to me
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 7th day of Christmas
    My true love gave to me
    Seven swans a-swimming
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 8th day of Christmas
    My true love gave to me
    Eight maids a-milking
    Seven swans a-swimming
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 9th day of Christmas
    My true love gave to me
    Nine ladies dancing
    Eight maids a-milking
    Seven swans a-swimming
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 10th day of Christmas
    My true love gave to me
    Ten lords a-leaping
    Nine ladies dancing
    Eight maids a-milking
    Seven swans a-swimming
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 11th day of Christmas
    My true love gave to me
    Eleven pipers piping
    Ten lords a-leaping
    Nine ladies dancing
    Eight maids a-milking
    Seven swans a-swimming
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    On the 12th day of Christmas
    My true love gave to me
    Twelve drummers drumming
    Eleven pipers piping
    Ten lords a-leaping
    Nine ladies dancing
    Eight maids a-milking
    Seven swans a-swimming
    Six geese a-laying
    Five Golden Rings
    Four colly birds
    Three French hens
    Two turtledoves
    And a partridge in a pair tree.
    
    
  4. March 26, 2007: Run a Java program without using Eclipse. Write YearOfThe.java without any array.

    Look at the two parallel arrays, a and name, in Array2.java. Beethoven2.java currently has an array of int’s named pitch. Give it a parallel array of long’s named duration, giving the duration of each note in milliseconds. (One thousand milliseconds equals one second.) You can even have an array of int’s named volume, giving the volume for each note. See MidiChannel.

    If you want the class to hear your music, email me the arrays or post them on the web.

  5. April 2, 2007: See how we created a method without arguments in Method1.java and Method2.java, and a method with an argument in MyPrint.java. And of course, the main method has always taken arguments. In fact, its argument has always been an array of String’s.

    For consistency, I changed all the array examples to use this syntax:

    int[] a = etc.
    
    instead of this syntax:
    int a[] = etc.
    
    Following a student’s suggestion, DetectWin.java now uses a counter. Someone has won if we count three X's or O's in a row (or column or diagonal). For consistency, I changed all the methods in MyPrint.java (except for the main method) to private.

    Create a method with no arguments, a method whose argument is not an array, and a method whose argument is an array. Call each method.

    For space cadets only. The program DetectWin.java detected if there was already a winning position on the tic-tac-toe board. Write a program that takes a position in which no one has yet won, and makes an X in a place that wins the game if there is such a place. For example, given a two-dimensional array of char’s containing

    XOX
    X O
      O
    
    the program should put an X in the lower left corner.

  6. April 9, 2007: The comment at the start of Manhattan.java now contains directions for taking an address on a street and finding the two Avenues that surround it. Change the program so it says
    Please type 1 if your address is on a Street, 2 if it's on an Avenue: 1
    Type 1 for east side, 2 for west side:
    Building number:
    Street number:
    The address is between 2 Avenue and 3 Avenue.
    

    Write a method named prev for the class Date in DateDemo.java that will move the date 1 day backwards. It should be very similar to the next method. Make sure that the year does not go before the Integer.MIN_VALUE that we saw in Integers.java. Also write another next method that takes one argument, an int, telling it how many days you want to move the date forward. The new next method should call the existing next method over and over to move the date forward.

  7. April 16, 2007: Write the julian and compareTo methods for class MyDate. I’m working on class Car.
  8. April 23, 2007: Write a superclass with two or more subclasses. Use a textbook example: a superclass employee with subclasses programmer and manager; or a superclass shape with subclasses circle, square, etc. The constructor for the subclass always begins by calling the constructor for its superclass. If the subclass has fields that were not in its superclass, the subclass will need its own toString method. The toString method of the subclass will call the toString method of the superclass.

    Also modify Bug.java. Instead of mentioning the numbers 10 and 11 many times in the paint method, write it once and for all as the value of the following field of class Bug:

    	private static final int size = 10;
    
    Then change all the 10’s to size’s, and all the 11’s to size + 1’s. You will need parentheses to make the + 1 happen before the multiplication.

    Let class Bug be a MouseListener as well as a PaintListener and KeyListener. The Bug can change color or position in response to a click. You will have to give class Bug the three methods mouseDoubleClick, mouseDown, and mouseUp. mouseDoubleClick and mouseUp can be empty, just like the keyReleased method of class Bug. You will have to import org.eclipse.swt.events.MouseListener and org.eclipse.swt.events.MouseEvent. Remember to say

    shell.addMouseListener(this);
    

  9. April 30, 2007: Seatch for Input.java in the source code page and you’ll see links to the methods that are called, and the classes of the exceptions that might be thrown by these methods. Nothing to hand in.
  10. May 7, 2007:

Images