Jack Madden
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.
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.
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.
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 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.
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 |
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) veh; Car c
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…
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.
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
The + operator has different meanings dependant on the operation of the +.
The + operator in a statement such as
= "Hello " + "World!"; a
Will produce an answer of “Hello World!”.
While the statement of
return "Answer: " + 42;
Will produce a return of “Answer: 42”.
Objects can create other objects using the new operator.
The operator does two things…
So a statement like this…
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
Remaining fields omitted
public ClockDisplay()
{
= new NumberDisplay(24);
hours = new NumberDisplay(60);
minutes updateDisplay();
}
Methods omitted}
will produce two NumberDisplay’s, each having there own set of fields.
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.
methodName(parameterList)
Methods can call other methods of the same class upon themselves.
.methodName(parameterList) object
Methods can call methods of other classes using dot notation.
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.
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