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 instance variables to your class.
You will be implementing a class to do some simple calculations.
Annother application programmer has built a user interface (and isn't
it a beauty!?!???) 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.)
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
- 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 getRandom which takes no explicit parameters and returns an int.
- this method must return the value of the intance 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
submenu. 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 the text colors might be different for you)
Type in Calculations.java. 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 as you have learned in the last
two labs, and use the online submission program to submit it to me.
- 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.
Due Friday Oct 12th at 11:59pm (note holiday weekend between now and then)