Please bring a camera to the first class to take the class photo.
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 didnt 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.
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). Dont 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 wont 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. Dont 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. (Lets 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
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.
Look at the two parallel arrays, a and name, in Array2.java. Beethoven2.java currently has an array of ints named pitch. Give it a parallel array of longs named duration, giving the duration of each note in milliseconds. (One thousand milliseconds equals one second.) You can even have an array of ints 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.
For consistency, I changed all the array examples to use this syntax:
int[] a = etc.instead of this syntax:
int a[] = etc.Following a students 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 chars containing
XOX X O Othe program should put an X in the lower left corner.
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.
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 10s to sizes, and all the 11s to size + 1s. You will need parentheses to make the + 1 happen before the multiplication.
Let class
shell.addMouseListener(this);