Introduction:
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.
Objectives:
- 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
Assignment Specifications
In this lab you will take preexisting files and finish them to create a
Ganan flag. (Why Ganan, because it is one of the Bar flags that
I've not done yet). When you are finished, your program should look
like this when you run it:
Getting
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
unzip Lab2Start.zip
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
ls command.
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)
One of the files that you just unzipped is named Comp101Lab1.java.
This is the file that you will need to work on. So open this file using
xemacs. From the command line type:
emacs Comp101Lab1.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
extensive programs.
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:
javac Comp101Lab1.java
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 Comp101Lab1 &
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. All of your object creation code should be
placed in the setup() method of the Comp101Lab1 class. You will find
two lines of comments that read:
//comp101 lab2 the code for object creation goes here //comp101 lab 2 all object creation code must be above here.
Put all of your object creation code between these two lines of comments.
Put the code to call methods in the paint method in the Comp101Lab1 class.
//comp 101 lab2 put your code for method calls here //all your code should be above hereput all of your method calling code between these two lines of comments.
You need to
- Create three objects, of the class Bar
- The
constructor for this class takes one explicit paramenter, a
string describing the color of the bar. (one green, one yellow and one
blue).
- This classes has a method called
show
which takes
no explicit paramenters and returns no value. You need to call this
show
method once on each object. - You will use some special variables that I have already created for you. They are called
- Store
the new objects that you create in these three variables (you don't
need to give a type since they are already declared for you) and then
call the show method once for each variable.
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.
Submitting.
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
cd ..
zip -r <your last name>Lab2.zip Lab2
Refer to the last lab for a refresher on how to do this.
Due Date.
This lab is due at 11:59pm on the day before class one week from now.