Chapter 3 Notes - Object Interaction

October 13, 2021

Jack Madden

Objects

This is about how objects work together. We could cram all the functionality we require into one piece of software, but that would be a bad design. We instead shall build a piece of software that uses three objects and instead arranges for methods to call other methods to increase the software portability.

Aims

  • How to look at a problem from a high level point of view.
  • How we draw a class diagram, showing relationships between classes.
  • How a program can be modularised to allow for different parts of it to communicate.
  • How to create objects from code, and make them work together.
  • How to draw object diagrams.
  • How to use more operators, including the modulo (%).
  • How to use the java keyword this.

Modularization and Abstraction

Modularization and Abstraction complement each other.

Modularization is dividing large problems into smaller parts.

Abstraction is the ability to ignore details of parts, to focus on a higher level of the problem.

Abstraction

To identify sub-components of a piece of software and program them as separate classes, this is know as divide and conquer. We try and then use those sub-components without being concerned about the bigger picture.

Modularization

Modular programming is an ability to divide a large problem into small chunks. Each chunk has its own special role to play in the software.

Class diagrams vs Object diagrams

Class diagrams show us the static picture of the relationships between classes. A class diagram changes when you are modifying the source code and relationships change.

Object diagrams show us the dynamic picture of the classes at any moment in time during the execution of the software. Object diagrams change as the program runs, and what objects have been called or modified.

Primitive types & Object types

Primitive Types

Type name Description Example Literals
byte Byte sized integer (8 bit) 24 -2
short short integer (16 bit) 137 -117
int integer (32 bit) 5409 -2003
long long integer (64 bit) 423226343L 55L
float single precision floating point 34.873F
double double precision floating point 45.85 55e5
char a single character (16 bit) ‘m’
boolean a boolean value (true or false true false

Object types

All types not mentioned already, are object types. These interface types from the Java library plus all user defined types.

An object type holds a reference (pointer) to an object. Classes are the templates for objects. The process is called casting.

Car c = (Car) veh;

Logical operators

Logical operators operate on a boolean values (true or false) and produce a new boolean value as a result. The most important boolean values are as follows…

  • && and
  • || or
  • ! not

So a && b = true if both a and b are true.

The expression a || b = true if we have either a or b as true.

And finally !a = true if a is a false value.

Modulo operator

The modulo operator (%) is common for rolling over of values.

So if we had a pie that was cut into 13 slices. Each one of the 5 people would get two slices of pie. (Because 10 / 5 = 2) Then the modulo would be the 3 that’s left.

This is the modulo

String concatenation

The + operator has different meanings dependant on the operation of the +.

The + operator in a statement such as

a = "Hello " + "World!";

Will produce an answer of “Hello World!”.

While the statement of

return "Answer: " + 42;

Will produce a return of “Answer: 42”.

Objects creating Objects

The new keyword

Objects can create other objects using the new operator.

The operator does two things…

  1. It creates a new object of the names class here
  2. It executes the constructor of that class

So a statement like this…

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;

    Remaining fields omitted

    public ClockDisplay()
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        updateDisplay();
    }

     Methods omitted
}

will produce two NumberDisplay’s, each having there own set of fields.

Overloading

When the compiler is compiling code, including calls to overloaded methods or constructors, it looks for a method or constructor signature compatible with the pattern of name and argument types of the call.

Why would we bother using overloading? For a constructor, the rule is that it must have the same name as its class, so there is only one name possible, and having different signatures means we can construct objects in different ways that suit us.

Internal and External method calls

Internal method calls

methodName(parameterList)

Methods can call other methods of the same class upon themselves.

External method calls

object.methodName(parameterList)

Methods can call methods of other classes using dot notation.

Debugger

A debugger is a software tool that helps show how a program runs. It allows a program to run one step at a time at certain points in the application. Its main focus is to help find bugs, but can also be used to help programmers understand the application.

For a more detailed explanation, see Objects first with java - appendix f

To set a break point, click anywhere to the left in the editor.

To step over a single line of code click the Step button.

To step into a method press the Step into button.

To continue executing the code until the next breakpoint press the Continue button.

To halt the execution, press the Halt button.

The this keyword

A self reference of this

Within an instance method or a constructor, this is a reference to the current object, The object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

So a statement can be written like so

public class Point
{
    private int x;

    public Point(int x)
    {
        this.x = x;
    }
}

To refer to a field of x, the constructor must use this.x

More examples of this

  • Invoke current class constructor
  • Invoke current class method
  • Return the current class object
  • Pass an argument in the method call
  • Pass an argument in the constructor call

Links to other pages

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14