Class 5

Homework 4

For Class 4, you wrote a program which first read in information about some mids, and then printed out the mids that were in a specific company. This program had three methods, one that built a Mid from user-inputted information, a method that prints the information about a Mid to the screen, and a main method.

Alter Mid.java to have the following methods:

Now rewrite your main function to take advantage of these new capabilities. Due Friday.

What to hand in: Your program must be named HW4.java and include a file named Mid.java. It must compile with this line:

javac HW4.java

You will lose at least 50% of your grade if that does does not compile. Submit your *.java files only. You do not need to submit *.class files.

Objects in Java: Grouping Data

As we've seen, classes/objects can be used like structs to group together hunks of data as a new type, but there are actually many other important properties. As you may have noticed, programs can sometimes get complicated. Object Oriented programming is a way of organizing programs to try to manage this complexity. It does this com allowing classes to hold not just data, but also the methods that operate on the data.

As an example, we're very comfortable with the .length() function that goes along with a String. With the information we have now, we know how to write a function with the signature:

  int length(String s);
  

But that isn't what member functions, like .length() are doing.

In structured/procedural programming, you have functions as the basic units of the program, and they operate on the data. In OOP, the basic units are the objects, and they contain the code that tells those objects how to behave.

Let's look at an example.

  class Point {
    public double x,y;

    public void move(double dx, double dy) {
      x+=dx;
      y+=dy;
    }
  }

Notice how there is a method that is included inside the definition of the class, and how the word static disappeared.

We could have done this the old way of course. Take a look at this for comparison:

  class Point {
    public double x,y;
    public static void move(Point p, double dx, double dy) {
      p.x+=dx;
      p.y+=dy;
    }
  }

The new method above is called a member-method, because it is a part of the class itself. See how the member method modifies the fields of the calling object. It just does it with out mentioning a variable of type Point. So how does it know which Point it is that is moving? That is made clear in the call:

  Point one = new Point();
  Point two = new Point();
  one.move(2.3,-4.2);
  two.move(0.2, 0);

Compare that to:

  Point one = new Point();
  Point two = new Point();
  move(one,2.3,-4.2);
  move(two,0.2,0);

So we see that when we call the member method, the way we call it determines which point it is being called on.

When we mention a variable in the class (called a member field in Java), the method knows this variable is the one that is inside the object that called the method.

Another way to think of it is that every object has its own copy of the method inside it, and you call it by first mentioning the object, then using the dot to get inside the object, and then mentioning the method name. Thus when the method is running, when a variable is mentioned, it first looks locally inside the method. If it doesn't find it, it then looks in the wider scope of the object to find it there.

Constructors

As objects get more complicated, with lots of fields, it becomes more and more irritating to initialize all these fields. For example, in the final project of IC210, you had to initialize the GameObject's x_pos, y_pos, x_vel, y_vel, picture, and alive-ness for every instance of that object.

The answer to this is to write a function which performs all of this setup for you, and returns the newly-built and initialized object. These special functions are called constructors.

The constructor is a member method that has the SAME NAME AS THE CLASS. It has no return type.

Arguments to the constructor are passed when the object is made:

  class Point {
    double x,y;

    public Point(double ex, double why) {
      x=ex;
      y=why;
    }

    public void move(double dx, double dy) {
      x+=dx;
      y+=dy;
    }

    public static void main(String[] args) {
      Point one = new Point(3.2, 5);
      Point two = new Point(0, 0);
      ...

We can overload constructors the same way we overload any other method. Suppose, for example, we often want to make our point be at (0,0). In that case, our Point.java might look like this:

  class Point {
    double x,y;

    public Point() {
      x=0;
      y=0;
    }

    public Point(double ex, double why) {
      x=ex;
      y=why;
    }

    public void move(double dx, double dy) {
      x+=dx;
      y+=dy;
    }

    public static void main(String[] args) {
      Point one = new Point(3.2, 5);
      Point two = new Point();
      ...

Problems