In this lab you will do your first actual java programming, creating
objects and calling methods. You will contine to get used to the Unix
computing environment. A considerable part of this lab is spent in
getting used to the tools you will use to write and build your program.
- Learn how to use xemacs as an easy way to develop java programs without the added weight of an IDE
- Create objects in Java.
- Call methods in Java.
- compile, debug and run a small Java application.
- use the this keyword
In this lab you will take preexisting files and finish them to create
a flag of the Bahamas. (I know the colors are a little off, but
you get the idea). When you are finished, your program should look
like this when you run it:
Copying the files
First you'll need to copy the starter files over to your directory.
cp /home/bscstaff/jsantore/Lab2Start.zip .
and then unzip the files
This has created a folder called Lab2 in your home folder. Make
the Lab2 folder your working folder (using the cd command appropriately
as you learned in the last lab) Then see what files are there using the
Start using xemacs
This section is best done either on campus, or from a genuine highspeed
connection (not the cable modem that is heavily used by everyone else
on the street, or the lowest speed dsl)
One of the files that you just unzipped is named DrawingPanel.java.
This is the file that you will need to work on. So open this file using
xemacs. First make sure you are working in the Lab2 folder from your command line. From the command line type:
emacs DrawingPanel.java &
This may take a few seconds to come up fully. There is some
configuration for using java that gets done when starting emacs. From
off campus emacs may take as long as a minute to startup. You should
see something like the following:
You see that the file is nicely "pretty printed" with comments in blue,
java keywords in a a sort of pale purple and so on. xemacs will
also help you to indent your code later on when you are writing more
The program runs as it is, it just doesn't do very much. Before we can
run any program, we must compile it.
To compile, go back to your command line and type:
recall that this wild card will match all files with a .java suffix in
your current directory. So this command will compile all of the java
files in your directory.
Running the program.
Now lets run your program. We run a
program in java by using the command java and then the name of the
class with the main method in it. In our case
java Lab2 &
You may get a warning about a font, this is only a warning and the font
will just look a little ugly. After running the program you will see a
nice gray window like the one below:
Congradulations, you are now ready to begin writing your part of the program.
Writing Java code.
In order to make your Ganan flag appear, you must create three
objects and call a method on each one. In the DrawingPanel.java file, you will find
two lines of comments that read:
//put your code below this comment
//put your code above this comment
Put all of your java code between these two lines of comments.
You need to
- Create three objects, one of each of the following classes
- Each contructor takes no explicit parameters.
- You must call the do the triangle last.
- declare a variable for each of these objects that you create and
store the object reference in this variable so you can call methods on
the object later.
- Each of these classes has a method called
draw which takes one explicit paramenter of type Graphics and returns no value. You need to call this
draw method once on each object. Again make sure that you do the Triangle object last.
- There is a Graphics object available for you to use for this already. Use the variable 'g' as the parameter that you pass.
When you have created the three objects and called show on each one,
compile your program again, fix any errors in compiling and then run it again. If you see the complete flag from the
first image above, you are done the program. Note that I've run into a
Java bug occasionaly which forces you to minimize and then restore the
window to see the flag sometimes.
To submit your program, zip up the entire Lab2 folder (as you zipped
the lab1 folder last time) and submit using the online submission program that we used for lab 1
you can either use konqueror to zip the files or use the command below.
zip -r <your last name>Lab2.zip Lab2
Refer to the last lab for a refresher on how to do this.
This lab is due at 11:59pm on the day before class one week from now.