Comp101 Lab 3
In this lab you will continue your java programming experience writing
a very simple class with several methods. You will take the part of an
applications programmer who is developing a class for another
programmer who will build the actual application.
- Define a class in java
- Define methods in java
- Define a constructor
- add an instance variable to your class.
You will be implementing a class to do some simple calculations.
Annother application programmer has built a user interface (perhaps
there was a reason that your predecessor moved on?) to function as an
extreemly simple calculator. Your
job is to write class that does the calculations according to the
specifications provided. Because the user interface is written for you,
you must write your class exactly as it is specified.
First copy the skeleton file for this lab to your folder. After you login
cp ~jsantore/Lab3Skeleton.zip .
Remember that that final dot is important for the command, its not punctuation.
unzip the file as usual
and then cd into the newly created Lab3 directory.
You have only one file this time. Morover, it will not compile until
you do at least some of your assignment. When everything does compile
and you run the program, you should see a window that looks alot like
The behavior that we expect to see is this:
When you type an integer into the two text boxes, select one of
the radio buttons and press calculate. The result will be displayed on
the label next to the calculate button. (the one that says "No
Calculations yet" in the picture above.) Note that there might be an
apparently inaccurate result with division. We'll talk about integer
division soon. If everything else works and division seems to be off by
one, then you are all set.
Specifications of the class that you must write
You need to write a class with the following specifications
- The class must be named Calculations
- It must have an instance variable of type int. Only use this
instance variable in two places, initialize it in the contructor and
return it in getProfsNum. If you need a variable in one of your other methods, use a local variable.
- The constructor must take one int as its parameter and use that paremeter to initialize the instance variable
- The class must have 5 more methods
- A Method called add which takes two parameters of type int and returns a value of type int
- this method should return the value obtained by adding its two parameters
- A Method called subtract which takes two parameters of type int and returns a value of type int
- this method should return the value obtained by subtracting its second parameter from its first parameter
- A Method called multiply which takes two parameters of type int and returns a value of type int
- this method should return the value obtained by multiplying its two parameters
- A Method called divide which takes two parameters of type int and returns a value of type int
- this method should return the value obtained by dividing its second parameter by its first parameter
- A method called getProfsNum which takes no explicit parameters and returns an int.
- this method must return the value of the instance variable.
- Once you have written all of the methods and the property of the
Calculations class correctly and everything compiles and works then you
When you begin, I suggest that you let xemacs help you out a little.
Once you are in your lab3 folder, open the file with the GUI in xemacs
You don't need to look at this file closely or understand it since it
does many things that you have not yet learned about. Select the
file menu and choose the "JDE New" submenu and then choose "class" from that
xemacs will ask you in the for the name of the new java file.
(in the small text bar at the bottom of xemacs - see image - note that
you won't have the Calculations.java files in your window when you do
yours - I've already written a copy in mine.)
Type in Calculations.java. Remember, you must define your java class in a file
with the same name as the class itself (with the .java extension)
xemacs will now ask you what this class extends. We haven't learned
about extending classes so you can simply type enter to default to
nothing. The same with the next question about what this class
Now you have a simple class outlined for you. It has a constructor (to
which you still need to add the correct parameter ) and nothing more.
Now add the instance variable, fix the contructor and add the rest of
the methods that you need.
When you submit this time, add a readme.txt file to your lab3 folder
that you are building. (In xemacs you create a new file by doing
<file><open file> and typing in a brand new file name. The
readme should contain:
Once you have the readme done, cd into your parent folder, zip up the
entire lab3 folder using the -r flag or using konqueror as you have
learned in the last
two labs. Make sure that you include your last name as part of the file
name. If you used konqueror to zip up the file, use konqueror to rename
to file to include your last name. for example I would use:
- Your name.
- A very brief summary of what you did
- A list of any problems still in your code or anything that you didn't get done.
Once you have the zip file, use the online submission program to submit it to me.
Due Friday Oct 10th at 11:59pm