CIS5200 - Advanced Programming (Java)

www.cs.fit.edu/~mmahoney/cis5200

Schedule

Jan 11-Apr. 26, 2006, Wednesdays, 5:15-7:45 PM, Crawford Science Tower room 523. Final exam is Wed. May 3, 6:00-8:00 PM.

Topic

This course is a continuation of Data Structures and Programming (Java). Topics include graphical user interfaces (GUI), network programming, applets, and threads. You should already be familiar with Java programming including object-oriented techniques, exceptions, and basic data structures such as arrays, stacks, linked lists, and trees.

Instructor

Matt Mahoney, mmahoney@cs.fit.edu or matmahoney@yahoo.com

Textbook

Java, How to Program, Deitel & Deitel, 6th Ed. We will cover the remainder of the book not covered in CIS5100 (chapter 11 to the end). The book comes with a CD that you won't need.

Introduction
Quick reference
Java Tutorials
Code examples from book

Homework

All programming assignments must run under Java 5.0 or any older version. Note that Java 5.0 requires Windows XP or Windows 2000 and 512 MB of memory. If you have an older computer I recommend Java 1.4.2 Note that some programs in the book are written for Java 5.0 and will not run in 1.4.2 (e.g. using printf() or Scanner). You may use Windows (98 or later), Linux, Solaris, or Mac.

Grading Policy

50% homework and 50% exams. Programming assignments will be submitted by email only. Submit only source code (.java) preferably as ONE file. There is a late penalty of 20% per day including non-class days, weekends and holidays.

There will be 4 exams including the final. Your lowest grade is dropped. The other 3 exams are weighted equally. There are no makeup exams for any reason. Exams are open book and open notes. Exams will usually be given in the first hour of class before break so that we can go over them afterwards.

Attendance is not mandatory. However you are responsible for (and will be tested on) all material covered in class.

Students are expected to do their own work (no group projects) and to cite all outside sources, including help received from other students. You are responsible for knowing the policy on academic honesty, covered in the student handbook, p. 33.

Exams

Spring 2006 Exam 1 solution
Spring 2006 Final Exam solution

Conflict Resolution

I normally answer email within 24 hours. In case of a conflict that cannot be resolved with me, resolution policy is covered in the student handbook, p. 55. The CIS program chair is Dr. Rhoda Baggs Koss, 321-223-4821, rkoss (at) fit.edu. Her office is on the 5th floor of the Crawford Science Tower.

Mailing List

Within 24 hours after the first class you should receive an email notifying you that you have been added to the fit-cis5100 mailing list. If you do not, then email me to request an invitation. You are responsible for all material posted to the list, such as homework assignments, test announcements, and additional course material. Once you are a member, you are encouraged to post questions (or answers) to the class at fit-cis5100@googlegroups.com. If you have a Google account you may also access the list at the members-only website groups.google.com/group/fit-cis5100.

If you took CIS5100 from me last semester and still have the same email address then you are already subscribed and don't need to do anything.

Homework

Homework 1

Due Midnight, Sun. Jan. 22 (10 pts). Modify Addition.java (Fig. 11.2, p. 513) as follows:
  • When the user clicks the "cancel" button, the program immediately exits (without displaying any kind of message).
  • If the user enters an invalid number (either the first or second number), the program should prompt again to enter the number, repeating either until a number is entered correctly or until the user kills the program by pressing "cancel".

    Homework 2

    Due Midnight Sun. Jan. 29 (10 pts). Exercises 11.9 and 11.11 in the book (as two .java files).

    Homework 3

    Due Midnight Sun. Feb 5 (10 pts). Exercise 11.15.

    Homework 4

    Due Midnight Sun. Feb 19 (10 pts). Extend homework 3 to a game where the two players alternate. First the user guesses the computer's number, then the computer has to guesses the user's number. The winner is whichever player makes the fewest number of guesses. Use whatever user interface you feel is appropriate.

    Homework 5

    Due Midnight Sun. Mar 12 (20 pts). Write a program to play Tic Tac Toe. The game is played on a 3 x 3 grid. Play alternates between the user, who moves first, placing an O in one of the 9 spots by clicking with the mouse. The computer then plays an X in one of the remaining spots. The winner is the first to place 3 O's or 3 X's in a row horizontally, vertically, or diagonally. If all 9 spots are played without 3 in a row, then the game is a draw. When the game is over, the program should pop up a dialog box announcing the number of wins, losses, and draws, and ask the user to play again. If the user clicks "OK" then a new game starts. If the user clicks "Cancel" then the program exits. For every other game, the computer starts first, but the computer always plays X.

    The computer should play intelligently. If the computer can win or block you from winning on the next move then it should do so. For example, it should place an X in the left side of the middle row below.

    
       X | O |
      ---+---+---
         | O |
      ---+---+---
       X |   | O
    
    

    For this program, use graphics to draw the grid in black on a white background, the O's in blue and the X's in red. If the user resizes the window, the drawing should be scaled or stretched to fit (so that the O's might be drawn as ovals). Give the user a means to change the color of the O's and X's (e.g. a JColorChooser) at any time during the game. Add a JSlider to allow the user to change the line thickness of the grid, O's and X's at any time during the game. Set the initial line thickness to 4 pixels.

    Your game might work like the applet below, although it does not have to follow this user interface exactly. (You need Java 5 to view this applet). Note that an applet can't exit or be resized like your program.

    Solutions

    TicTacToe.java as an application
    TicTacToeApplet.java as the applet above

    Homework 6

    Due midnight Sun. Apr. 9 (20 pts). Write a text editor. You may use the built-in editing capabilities of a JTextArea to handle all normal editor functions such as insertion, cursor movement, etc. However the editor should also allow you to save and load files by clicking on "File" on the menu bar at the top. This menu should have "New", "Open", "Save", "Save As" and "Exit" options. Clicking "New" clears the screen. Clicking "Open" pops up a JFileChooser allowing the user to select a text file to open and read, replacing the current content. Clicking "Save" saves the file with the current name, or if no file was loaded, prompts the user to choose a file name as with "Save As". "Save As" uses a JFileChooser to let the user select a file name. "Exit" exits the program. The file should be saved in plain text format so that it may be edited with other programs such as Notepad. In case of I/O errors (such as file not found), the program should behave in a reasonable way without exiting, such as popping up an error message.

    Solution

    Exam 3 (take home)

    Due midnight Sunday Apr. 30, 2006, by email. Write an applet, Orbit.java, which works like the applet below.

    This applet simulates planets in space following Newton's law of gravity. To create a planet, left click in the black area. The size of the planet will depend on how long you hold down the mouse button. You can "throw" a planet by moving the mouse as you click. The initial velocity will equal that of the mouse.

    The display is centered over a "home planet", which appears fixed in the center of the screen, although (like the Earth) it is actually moving. You can change the home planet by right clicking on another planet.

    The "Zoom In" and "Zoom Out" magnify the display by 2 and 1/2 respectively. The "Clear" button removes all the planets and resets the zoom level.

    The planets obey Newton's laws of physics in two dimensions. Each planet has a position (x,y), a velocity (dx,dy), and a mass (m). Each planet exerts a gravitational force on all the other planets: f = Gm1m2/r2, where m1 and m2 are the masses of the two planets, r is the distance between them, and G is a gravitaional constant. (You can use G = 1 or whatever value makes the animation look right).

    Note that force is a vector (fx,fy) in the direction of the other planet. It can be computed: fx = Gm1m2rx/r3 where rx is the x component of the distance. fy is similar. These forces are summed over all the other planets.

    At each time step the position and velocity of each planet is updated as follows:

      dx = dx + fx/m
      dy = dy + fy/m
      x = x + dx
      y = y + dy
    
    The time step in this animation is 30 ms. This is just fast enough that the motion appears smooth.

    Planets will collide if they touch, forming a new planet whose mass is the total of both planets. The collision preserves momentum, e.g.

      dx = (m1dx1 + m2dy2) / (m1 + m2)
      dy = (m1dy1 + m2dy2) / (m1 + m2)
    

    All planets have area proportional to their mass (since there are 2 dimensions). The mass in this applet is 0.5 + the number of time steps that the mouse button is held down. I used a radius of 40*sqrt(m).

    The applet allows up to 10,000 planets, although it is not likely you could create that many and have the animation run smoothly. I tested it on a 2.2 GHz Athlon and it will run smoothly with 1000 planets before saturating the CPU. Note that the time complexity is O(n2) to compute the forces on n planets.

    For this program, you can use any color scheme you want. (It does not have to copy the above applet exactly). Also, you don't have to duplicate the feature of changing home planet with a right mouse click. However, all other features should be implemented.

    Submit one file, Orbit.java, by email, and no other files. This should be an applet, not an application. I will test it in appletviewer and/or a web browser with my own HTML file:

      <html><applet code=Orbit.class width=600 height=500></applet></html>
    
    Be sure your program is documented and readable. I am interested in commenting the program requirements and overall structure, the "big picture", not line by line commments.

    Solution

    Note: there will not be any more programming assignments. The first 6 assignments sum to 80 points, so everyone will be given 20 free points to bring the sum to 100. This is 50% of your grade. The other 50% is the average of your best 3 exams. If this exam is too hard, you may drop it and take the in-class final exam instead. Otherwise the final is optional.