CSC 012

Introduction to Computer Science

Summary of 2/11 Lecture

 

Objects and Methods

(or Nouns and Verbs)

The object oriented style involves constructing an object and then sending it messages to accomplish a desired task.  In my case, you could say that I have an extensive list of messages that I respond to.  In the Java style, this could be described as follows:

Object:        Bill

Methods:     walk()
                    talk()
                    cook();
                    teach();

and so forth.

The authors have provided several fun classes with which to experiment.

The Trick Mouse Class

The Trick Mouse class provides 3 methods:  hitWall(), speak( :String), and setTitle ( :String).  The notation :String suggests that the method requires an argument of type String (i.e., anything between double quotes ").   In class, we constructed several programs to illustrate its use.

//File: TestMouse.java
import CSLib.TrickMouse;
public class TestMouse
{
    public static void main (String [] args)
    {
        TrickMouse bill = new TrickMouse(); //Construct a TrickMouse
                                                                  //named bill
   
        bill.setTitle("Bill Goes Crazy!"); //Change the title of the window
                                                       //to "Bill Goes Crazy"

        bill.hitWall();     //Tell bill to hit the wall
    }
}

wall.jpg (5540 bytes)

We then tested the other TrickMouse methods.

//File: TestMouse2.java
import CSLib.TrickMouse;
public class TestMouse2
{
    public static void main (String [] args)
    {
        TrickMouse bill = new TrickMouse(); //Construct a TrickMouse
                                                                 //named bill
   
        bill.setTitle("Bill Goes Crazy"); //Change the title of the window
                                                      //to "Bill Goes Crazy"
        bill.speak("Where's the wall?!!!!");          //Tell bill to speak
    }
}

speak.jpg (5646 bytes)

First Look at Inheritance

Finally, we developed a somewhat complicated script that had our mouse hitting the wall, responding with anguish, and then vowing never to do that again.   This required resizing the TrickMouse window, relocating it, and adding a pause in the proceedings so that we could savor the results.  The first two tasks were accomplished by exploiting one of the most useful features of Objected Oriented Programming languages, namely inheritance.  We observed from the TrickMouse documentation that these objects inherit the methods associated with the Component class, among which were:

public void setSize(int width, int height)

and

public void setLocation(int x, int y)

In the latter, we noted that the coordinate system is centered at the upper left hand corner of the screen and runs to the right and down.

It was the authors' Timer class that gave us the method needed to slow down the action so that we could observe the evolution of our little script:

public static void pause(long msec)

Putting these all together, we arrived at the following:

//MyMouse.java
import CSLib.*; //imports ALL classes in the CSLib package
public class MyMouse
{
    public static void main (String [] args)
    {
        //construct a TrickMouse object called mickey
        TrickMouse mickey = new TrickMouse();
        mickey.setTitle ("Mickey hits the wall!");
        mickey.setSize (350, 200);
        mickey.hitWall();
       
        //static void pause (long msec) ==> "class" method
        Timer.pause (3000); //pause 3 seconds
       
        TrickMouse minnie = new TrickMouse();
        minnie.setTitle ("Mickey yells!!");
        minnie.setSize (350, 200);
        minnie.setLocation (325, 250);
        minnie.speak ("Ouch, that hurt!!!!");
       
        Timer.pause (3000);
       
        TrickMouse mighty = new TrickMouse();
        mighty.setTitle ("Mickey ponders the future..");
        mighty.setSize (350, 200);
        mighty.setLocation (650, 500);
        mighty.speak ("I'll have to be more careful...");
    }
}

 

Back to CSC 012 Home Page