Rudy Rucker's CS 151 Object Oriented Programming

 

Home

Biography

Books

Paintings & Links

Classes

Email

 

 

Shortcut to the Java documenation Paintings & Links JDK 1.3 Documentation

Xiaoping Jia's Software.
You can download the software for our book from Xiaoping Jia's website.  You can download the files as needed, or get them all at once.

(Last update to Homework 3 was made on November 13, 2000)

Homework # 3, Due Monday, November 20 at the start of class. These are hard problems, so start work early! More details and hints will be given in class. Also some more information may be posted here later on, so keep checking this page.

Hand in a floppy disk with your *.java source code and all *.class files needed to run your programs. Note that your programs must run off your A: disk. I will copy calculator.html, sales.html and manyball.html files to your disk to test your work.

There are three problems called 3.1, 3.2, and 3.3. These are based, respectively, on Jia Execises 6.2, 6.4, and 6.5.

Mechanics) 5 Pts. Did you include all necessary files? Did you use the right names for your files?

2.1) 7 Pts. Write code calculator.java to implement problem 6.2 in Jia, p. 174. It will be tested with the command:
appletviewer calculator.html
and calculator.html will look for calculator.class in order to run.

The caclulator should have buttons for the digits, for +, - and =. Don't worry about * and /.

Hint. I put some code by Horstmann that is a calculator application in K:\rucker\cs151\horst_calculator. To make ours different, let's not use all of Horstmann's features, and let's limit ourselves to integers. In other words, you have to (a) change the program to an applet instead of an application, (b) only have 13 buttons: 0 - 9, =, +, -. (If you have other buttons, I will take off points, you MUST cut down to only 13 buttons), and (c) print the answer as an int (with no .0 at the end, if you have a .0 floating point format I will take points off).

The layout must look as described in class. An Answer Component on the North, a panel with + and - on the East, a panel with 0 and = on the South, and a 3x3 grid with the numbers 1-9 in the Center. If you need to see a different example of making a layout similar to this look at K:\rucker\cs151\

2.2) 7 Pts. Write code sales.java to implement Jia problem 6.4. It will be tested with
a line
appletviewer gradingsalespie.html
The html will look for sales.class and it will have a PARAM NAME that will be categories as in the book. By reading the VALUE for this String, you figure out how many more PARAM you have to read, and what their names are. Note that getParameter returns a string, so you will need to use Integer.parseInt to turn the strings into int.
You will also need to have a button to switch the pie chart and bar chart. This button can be labelled Pie/Bar.

HINT Look at K:\rucker\cs151\salespie to see an example of how I would do the code. You have a Grapher strategy which can be PieGrapher or BarGrapher.

2.3) 7 Pts. Enhance theK:\rucker\cs151\ball2 code which is about the same as Jia's BouncingBall3 example so that you can add and remove balls with two buttons labelled More and Less. Name your Applet manyball, and change the file with the code to manyball.java. Add a feature so that if two balls collide they exchange velocities and clamp so that they no longer overlap. By "clamp" I mean that when two objects are colliding, you need to move them apart so they no longer overlap otherwise they'll keep recolliding and be stuck together. I will test with with a line
appletviewer manyball.html
The html will look for a manyball.class.

DESIGN. Be sure to use a class BallArray extends ArrayList, and give it move, paint, and collide methods. A good way to do colide is to trade velocities.

 

Setting your Path

In order to run javac, java, and textpad from the DOS command line your path must include the directories where javac.exe and java.exe live and the directory where textpad.exe lives. On my machine these are c:\Program Files\jdk1.2\bin and c:\Program Files\Textpad 4.

If you have NT, you set the path by opening Start|Settings|Control Panel|System|Environment. Click on the Path line, edit it in the bottom of the dialog, and press Set to record your editing. Separate all path fields by semi-colons but not with spaces. A good path to use in the lab is this

c:\Program Files\jdk1.2\bin;c:\Program Files\Textpad 4

If you have Win98, you will have a C:\autoexec.bat file that you can edit. If you don't find autoexec.bat in the C:\ root, use Start|Find to find it. Use any text editor to add a line like this. The final %path% means to keep whatever was in the path before.

set path=c:\Program Files\jdk1.2\bin;c:\Program Files\Textpad 4;%path%

On some versions of Win98 (or Win95), a path like
c:\Program Files\Textpad 4
isn't recognized. You can use the DOS versions of the two-word names instead
c:\Program~1\Textpad~1

You know you have the path fixed if you can enter java, javac, and textpad at the command line and get responses other than "...not found."

Textpad 4.0

A good low-end Java developement environment is to install a Sun JDK and use the free TextPad 4.0 editor from www.textpad.com

Spend some time looking at the Textpad menus to get it configured properly. To begin with use View to turn off the Clip Library and Document Selector windows.

You should install Java BEFORE you install Textpad, otherwise you won't be able to add the Java Tools. If Java is already installed, then you can use Configure|Preferences|Tools|Add to add the JDK commands if they are not already present under the Tools menu.

If you don't see the Configure|Preferences|Tools option then you are going have to use the Tools|Run dialog to compile your java. In the Tools|Run dialog fill in javac as the comand, and either filename.java or *.java as the Parameters. You may need to set Initial folder as well. Check DOS Command and check Capture Output. The Run dialog will keep the same selections over a session, so it's useful.

Use Configure|Preferences|Document Classes|Java|Syntax to Enable Syntax Highlighting and select the java.syn syntax file.

Use If on your machine Java was not installed BEFORE Textpad, you will not be able to add the Tools options and you will

You can use Editor|Keystroke compatability to select Microsoft Applications to use the familiar Microsoft shortcuts. You can use Configure|Preferences|Keyboard|Tools to select shortcut keys Ctrl+1 and Ctrl+2 for JDK Compile and JDK Run Application. You can shortcut other keys as well. You can use Configure|Preferences|Document Classes|Default|Font to select bigger fonts. And so on. Textpad has a good help file.

Developing Java Programs

Java is a relatively new language, and the the situation regarding tools for Java programming is a little confusing.

The JDK

Sun gives away packages with a command-line compiler and libraries for developing Java code; these packages are called JDK (for Java Developer's Kit).  The commonly used editions at this time are  JDK:  1.1, 1.2.2, and 1.3.  JDK 1.2 and JDK 1.3 are both referred to be Sun as " Java 2".

In writing a Java program, you use a text editor of some kind to make one or more *.java files.   You use a Sun utility known as javac.exe to compile the *.java files into *.class files.  The compiler will pull in a number of Java library or "package" files from your disk and link them into your code.  And then you execute the *.class files and see a program running by using either (i) a Sun "virtual machine" utility known as java.exe or (ii) a web browser.  The Java libraries, javac.exe, and java.exe all come as part of the JDK.

The various Java JDK (sometimes also called SDK for Software Devloper's Kit) are downloadable from the Web at Sun's java.sun.com site.   A JDK includes, among other things, the javac.exe and java.exe utilites as well as some libraries of *.java code files.  Note that it's okay to install several JDK on your machine, in separate directories.

Another point to mention here is that when you go to the Sun download page, you have a choice of getting the SDK or the Runtime Environment.  Normally you would want the SDK.

The "SDK "includes all the libraries, the javac.exe compiler, the java.exe virtual machine, and a plug-in to (maybe) make the Java files work with your current web browser.

The "Runtime Environment" leaves out the javac.exe compiler, but includes some of the libraries, the virtual machine, and the plug-in.

Download the JDK 1.3 for Windows From Sun. This is a whopping 30 Meg download.

(If you want a different version or a different platform, go to this page instead:Download a JDK From Sun. )

But, hold on, before you do the long download, figure out which IDE you are going to use (see below).  The thing is, many of the IDE's come with a version of JDK and Java documentation on the disk, so maybe you don't have to do the huge download.

There is another thing you can download, this is the "Documentation".  You definitely would like to get this too To get documentation for the Java SDK, you can download the documentation, which is in HTML format (and a whopping 17 Meg) .  Alternately, you can browse the documentation online at Paintings & Links JDK 1.3 Documentation or  Paintings & Links Java 1.1 Documentation.

By the way, if you can't manage the huge downloads, you can buy these wares directly from Sun on CD.  Also note that some Sun downloads are only availabe during business hours on weekdays.

The Sun Java SDK does not come with any type of IDE (integerated developer environment).  For simple programs you you can use the Windows Notepad accessory or the Wordpad accessory. 

An Integrated Development Environment (IDE)

If you plan to write Java programs of any size, you really need an IDE (integrated development environemnt) that has integrated help, which lets you build and run from the edit interface, which highlights errors, and which has some debugging capability.   There is no perfect Java IDE at this time.  Here are a couple of options.  You'll notice, by the way, links to a number of Java IDE on the page for Download the JDK 1.3 for Windows From Sun.

There are various goals in choosing your compiler. Unfortunately these goals are incompatible.
(1) An IDE (Integrated Development Environment) similar to, for instance Visual C++; that is, a multi-windowed environment in which we can edit, compile, and run our programs, with ability to pop up help files and perhaps even do some WYSIWYG (What you see is what you get) editing of our GUI (Graphical User Interface).
(2) Write Java applets that are useable by a wide range of browsers.
(3) Run in a stable fashion on your machine.
(4) Support the latest features of Java.
(5) Don't have to do  huge downloads over the Web.
(6) Pay very little.

(1) is satisfied by all except the TextPad solution.

(2) is a subtle issue. Only JDK 1.1 makes java programs that run on most existing web browsers. JDK 1.2 and 1.3 don't run on older versions of browsers (this applies both to Navigator and to Netscape) without the user having to download a huge (12 Meg) browser plug-in.  Sun Forte won't let you use JDK 1.1.

(3) fails for Sun Forte, unless you have 128 Meg of RAM.  I think Visual Cafe is OK with less RAM, but I'm not totally sure.

(4) fails for Microsoft J++, which only goes up to JDK 1.1.  All the others are OK with higher versions.

(5) is an issue for either the WordPad or Forte solutions, as you get these free over the Web.

(6) Visual J++ costs $30 (at SJSU) and Cafe costs I think $99.  Wordpad and Forte are free.  But remember: you get what you pay for.

Microsoft J++

This Java compiler is  on some machines in our lab, but is being phased out in favor of Forte.  In addition, you can rent an installation disk for J++ from the SJSU bookstore for $30 for four days, and install J++ on your home machine. A nice thing about commercial software is that you get the whole thing on one CD and don't have to do any downloads.  JDK, IDE, documentation, all on one handy disk.

J++ is a very nice IDE.  It seems to run easily on most machines, and doesn't require that you have a lot of RAM.  The catch is that J++  doesn't support JDK 1.2 and higher.  Given that JDK 1.2 doesn't run on many browsers, this isn't as a big a handicap as you might think.  And for the code we're going to be writing, JDK 1.1 is close enough to JDK 1.2 for most programs.

Even so, it is annoying not to have the latest and greatest version of Java and for many people, Microsoft's pig-headed and quixotic refusal to support JDK 1.2 and higher is a show-stopper.

Sun Forte

Sun provides a free Java IDE called Forte, which is a fairly serviceable IDE for Java development.  You can download it (I think only on weekdays) from Sun's Forte For Java Community Edition Download page.  You need at least 128 Meg of RAM on your computer to run this program. If you run it with low amounts of RAM it will crash unexpectedly, taking your code with it. If you don't have 128 Meg of RAM, don't bother getting the download --- OR get some more RAM. Related to the fact that the thing uses a lot of RAM is that it feels a little wobbly when it runs.  Forte is on our lab machines. I find the program too slow and too unergonomic (you can't use the mouse wheel to scroll and there aren't many hotkey shortcuts, you have to click a lot) to really be useable.

Last time I checked, the download is 7 Meg, which unzips to 23 Meg.   It has an odd user interface (designed in Czechoslovakia), but perhaps I'll get used to it.  I'm not sure if the download includes a JDK and documentation or not, it's possible that these may be separate downloads.  But if you plan to use Forte, download it first and then see what else you might need.

One way around the brutally large downloads is to buy the wares on CD from Sun.  But if you're buying software, you would do better to buy a commercially devolped Java compiler instead of this one.

Borland JBuilder

I've used this some.  It's not bad.  It has the same high-RAM requirement as Sun Forte, but it supports and JDK you like.  Check it out at the JBuilder site, I think you can get a free trial version.

Visual Cafe

I've never tried Visual Cafe, but I have this notion that it might be good, and will check it out soon.  My impression is that combines the good features of Microsoft J++ (it comes complete on a CD with no downloads, feels solid, has a standard user interface, and runs with a modest amount of RAM) with the good features of Sun Forte (supports JDK 1.2 and 1.3!).  If I were buying a compiler myself, I think I would try this one, but maybe that's just because I already know all the others have something wrong with them.  The catch here is a list price of $99 for the Standard version. 

The Asteroids Applet
This was the main project in the Fall, 1999 semester. I don't know if we'll do anything this gnarly in Fall 2000.

Homework #1. due Monday, Sept 25, start of class. Late homework is graded down by 20%. Late homework will not be accepted at all after Friday Sept 29.

Give me a floppy disk with NO DIRECTORIES OR SUBDIRECTORIES ON IT.
ALL FILES MUST BE IN THE ROOT (not in a subdirectory).

USE LOWER CASE NAMES FOR YOUR MAIN CLASSES AND FILES.

FILES I NEED ARE *.JAVA and *.CLASS

THESE ARE THE FILES THAT SHOULD BE ON YOUR DISK IN THE ROOT.

makename.java
makename.class
blackjack.java
blackjack.class

EACH PROGRAM MUST TYPE YOUR NAME AND DATE ONSCREEN.

5 points for putting the right files on disk in the right place.

Homework 1.1.  5 points.

This is based on Jia problem 3.3. Use code from Jia Example 3.5 to read and write a file and code from Example 3.7 to break a string into tokens. We need to take an input line like Jordan:Michael:223458989 and return a string like mj8989@mathcs.sjsu.edu

Write a makename.java file and compile to makename.class. The effect of the program should be that if I run the following three-line batch file on your disk, then I will see an input file students.txt converted to an output file studmail.txt as described in Jia Excercise 3.3. YOUR PROGRAM MUST ALSO WRITE YOUR NAME AND THE DATE TO THE SCREEN WHEN IT RUNS. I will supply the students.txt file to test with. While developing your program, it is useful for you to have a students.txt with three names or so and have the following three lines in a single grade1.bat batch file that you can run from the DOS prompt.

del studmail.txt
java makename students.txt studmail.txt
type studmail.txt

Homework 1.2.  7 points.

Skeleton of blackjack.java.

Extend the skeleton code to get a blackjack program that will show the play of two players playing blackjack. The players should be named "Gambler" and "Dealer". They play as many rounds as you request on the command line, default is 7. In each round, both players get hands from the Deck. The Gampler plays and if his total is not over 21 (i.e. if he's not busted) then the Dealer must play and try and do better. The goal in blackjack is to get as close to 21 as possible without going over. An Ace counts as 11, but if the total is over 21, you can count the Ace as 1. An Ace counted as 11 is called “soft” becasue you can still change it to a 1.  Face cards count 10. The Player has a play(Deck deck) method which keeps checking the hand total and deciding whether to draw from the Deck. More on this in class.  The default play method you should implement  to make the play strategy be a looop that does this: keep drawing cards from the deck as long as your total is 16 or less.  If your total is 17, draw a card if your hand is soft (includes an ace counted as 11), otherwise do not draw.  If your total is 18 or higher do not draw.

 

The play is as follows: the Gambler plays.  If he busts (is over 21), the Dealer wins.  Otherwise the Dealer plays.  If the Dealer busts, the player wins.  Otherwise the one with the total closer to 21 wins and the one with the lower number loses.  If they have the same total it is a tie and neither wins nor loses.

 

For full credit, track how many times Gambler Wins and how many times Gambler Loses, and at the end of the run, print out a line of the form

Gambler’s win probability is 0.45. 

The probablility number is computed as wins/(wins+losses).  The ties are ignored.

The output for the DOS command
java blackjack
should look like this sample output, except the special symbols for the suits will appear instead of the letters C,H,D,S

To make this easier for you, I am including a skeleton with much of the code already in place. You only have to fill in some missing parts of the code, the places with a comment saying //Your code.

Don't forget that the program must start and end by PRINTING YOUR NAME AND THE DATE.

Homeword 1.3  3 points.

Write a blackjackcheat program that’s a slight variation of blackjack.  In this program we have two players, Dealer and Cheater.  Dealer uses the same play strategy outlined above.  Cheater is able to use a new Deck method Card peek() that shows the top card without removing it form the Deck.  Cheater’s play is to always peek and draw the next card as long as it does not put Cheater’s total over 21.  Program runs “silently” with no printed output except the final line of the form
Cheater’s win probability is ....
AS before the probability is wins/(wins+losses) with ties being ignored.
You only get full credit for this one if the number you get is in fact the number that I myself compute to be correct one.  The reason for silent running, is to make it faster to do a run of, like, 1000 rounds to home in on the correct probability. Write a Cheater class to extend the Player class of the blackjack example and overload its play method.

In each round, as before, Cheater plays, Dealer plays