Return to lecture notes index

15-100 Lecture 6 (Monday January 24, 2004)

Today we will be making our first java class!

Java under the hood
The code that you're tying into the computer isn't quite what the compuer uses to run your program. So how does it get from here to there?

If you've used C or C++ before you might know that the first thing that the C code does is go to a preprocessor. Java however doesn't use the preprocessor very much, it pretty much skips that step and goes directly to something called the compiler.

In java, and may other programming languages including C and C++ what a compiler does is to translate your program into something called assembely language, which is something that your comptuter can understand and use. There is a small problem with assembly code though. It turns out that each diffrent type of computer has a diffrent type of assembly code, so once you've compiled a program on that comptuter it can only be run on a computer that speaks the same language. the obvious problem with this is that you can't move a program from one computer to another and have it still work.

Jave, however, has a solution to that. When the people at Sun, the makers of java made the language they made somthing called the java virtual maching. The java virtual machine acts like a computer, so that when the program is compiled it is compiled to run on the computer 'java virtual machine'. Java virtual machine doesn't really exist as a computer though, what it does is your computer runs a program which runs java virtual machine, which in turn runs your program. Our process as a programmer is going to feed a .java program into the compiler then run the program that it produces. Sun's model is a little diffrent. They made a machine, but it isn't a real machine, its just a virtual machine. One of the disadvantages of this approach is that it makes the program run a little slower. But since hardware is cheap this will only make a niticible diffrence if you have a very optimized program.

What do you write your program on?
In this class you will be allowed to write your program on, and compile it on anything that you want to. In class I will probably use vi, and you will see many other intro-programming courses using CodeWarrior, which you can download from MyAndrew for free, but just use whatever you feel most comfortable with.

Writing the calculator
Now we will once again be working with our old friend the calculator class. First we will name our class and type in the methods we will be using.

Also while reading java you will come across lines starting with "//" or begining with "/*" and ending with "*/" these are called comments in java. They are used by programmers to tell other programmers what the code is doing. The compiler will ignore this code when compiling the pprogram.


/**
This is a calculator which will be able to add, subtract and return
The previous result.
**/
class Calculator{
//this holds the result of the last calculation that the calcultor did //we made it private so that an outside program does not try and change //our result private double result; //This is a type of class called a constructor, every time //a Calculator object is created java invokes this method. //Constructors are always named the same name as the class //they are in and they always public public Calculator(){ } //This method will add two numbers and then return the result to the //user. Since we want the user to be able to use this method we //will make it public, public double subtract(double leftNumber,double rightNumber){ } //this method will subtract rightNumber from leftNumber then //return the result to the user. public double add(double leftNumber,double rightNumber){ } //this method will return the result from the last operation public double getResult(){ }
//this method will clear the calculator by setting the saved //result to 0 public void clear(){ } }

There is no code in any of the methods, so thats the next thing that we'll create, first we'll do the constructor.

The constructor is the method that starts public Calculator{. Before we write the code for this method we should think about what we want it to do. In this case we just want it to set the result to 0, so that the calculator starts out with a cleared result. The finished code for the constructor is below.


public Calculator{
result = 0.0;
}
The next method that we'll work on is the add method public double add(double rightNumber, double leftNumber){. For this method we want the program to add the two numbers that the user gave us, then return that number to the user and save it as the result.

public double add(double rightNumber, double leftNumber){
   result = rightNumber + leftNumber;
   return result;
}
The subtract method is alot like the add method, we are just going to subtract the two number instead of adding them.

public double subtract(double rightNumber, double leftNumber){
    result = rightNumber + leftNumber;
    return result;
}
We want the getResult method to return the last result that the calculator got. In order to do this we just want the method to return the result variable. Where we have been saving the result.

public double getResult(){
     return result;
}
Finally all that we need to do is the clear method. We want this method to do just what it sounds like. In order to do this we just want to set the result variable to 0.

public void clear(){
    result = 0.0;
}

Now we'll bring it all together.

 
/** 
This is a calculator which will be able to add, subtract and return 
The previous result. 
**/ 
class Calculator{
//this holds the result of the last calculation that the calcultor did //we made it private so that an outside program does not try and change //our result private double result; //This is a type of class called a constructor, every time //a Calculator object is created java invokes this method. //Constructors are always named the same name as the class //they are in and they always public public Calculator(){ result = 0.0; } //This method will add two numbers and then return the result to the //user. Since we want the user to be able to use this method we //will make it public, public double subtract(double leftNumber,double rightNumber){ result = leftNumber - rightNumber; return result; } //this method will subtract rightNumber from leftNumber then //return the result to the user. public double add(double leftNumber,double rightNumber){ result = leftNumber + rightNumber; return result; } //this method will return the result from the last operation public double getResult(){ return result; } //this method will clear the calculator by setting the saved //result to 0 public void clear(){ result = 0.0; } }

Using java classes

Now we're going to use the java class that we just created. We're going to create a new class called TestProgram, which will use some of the methods that we just created in calculator. To do this we will use somthing called the main method. Below is an example of the test program.

public class TestProgram{

   //When you run the program java goes step by step through this method
   public static void main(String args[]){
          
	  //creates a new calculator called firstCalc
	  Calculator firstCalc = new Calculator();

          //uses the add method on the calculator we just created
          //and returns the value of the two numbers we passed it 
          //added together, and sets 'a' to point to that value
          double a = firstCalc.add(1,3);

	  //uses the subtract mehtod on firstCalc and returns the value
         // as the variable 'b'.
         double b = firstCalc.subtract(9,1);

         //creates a new calculator
         Calculator secondCalc = new Calculator();

          //second calc adds the number 4 and 5
         double c = secondCalc.add(4,5);

         //returns the result of the last operation second calc did
         double d = secondCalc.getResult();
 
         //clears secondCalc
         secondCalc.clear();
 
         //gets the result of secondCalc
         double e = secondCalc.getResult();

         //returns the result of the last operation firstCalc did
         double f = firstCalc.getResult();

        //prints out the results
        System.out.println("a=" + a);
        System.out.println("b=" + b);
        System.out.println("c=" + c);
        System.out.println("d=" + d);
        System.out.println("e=" + e);
        System.out.println("f=" + f);
   }
}

If you compile and run TestProgram and Calculator this is the output you will get.

a=4
b=8
c=9
d=9
e=0
f=8