3.10 Lecture 2
Introduction to Systems Programming - Lectures - Class 2.pdf
Precondition
It is a condition that must be met before starting the execution of a method, or constructor.
- It is an obligation of the invoker (the client of the method) to ensure that the precondition holds, before calling the method.
- If the precondition does not hold, the method (and consequently the object that contains it) may behave in an incorrect way.
- Enforcing the satisfaction of preconditions protects the state of objects.
- It supports/enable encapsulation
Assertions
Java supports assertions, which will allow us to easily implement precondition checking. If the condition of an assertion is false, the program will stop, and an error message will be shown.
For example
Abstraction and Modularization
Abstraction: The ability of ignoring details of the parts to focus at a higher level of the problem.
Modularization: The process of dividing the whole in well defined parts, that can be built and analyzed separately, and whose interaction is also given a well defined way.
Clock
We are trying to use modularization to simplify this task
First we need to implement two digit displays, then we are going to create two variables to that class and combine them
public class NumberDisplay {
private int value;
private int limit;
//CONSTRUCTORS
//METHODS
}
public class ClockDisplay {
private NumberDisplay hours;
private NumberDisplay minutes;
//CONSTRUCTORS
//METHODS
}
When a variable stores an object, the object is not stored in the variable directly, but rather an object reference is stored in the variable.
43 Vincent
42 Vincent since b references the same object as a, and that object's name was changed, it also prints "Vincent".
Note
Classes define types. A class name can be used as the type for a variable.
Variables that have a class as their type can store objects of that class.
There are three type of comments in java
-
// This is a single-line comment
-
Another is
/* This is a multi-line comment. It spans multiple lines. */
-
Also we have Javadoc Comments, it can be complied by java, then we will have the document
Used for generating documentation for classes, methods, and fields. It supports special tags like
@param
,@return
, and@author
./** * This method adds two numbers and returns the result. * @param a First number * @param b Second number * @return Sum of a and b * @throws ArithmeticException if b is zero */
Logic Operators in Java
AND ( &&
) OR ( ||
) NOT ( !
)
Explanation of Project "Clock"
Firstly, we can implement the class of NumberDisplay
easily.
Then we need to use this class as a modular, and create two objects (minute
, hour
), then combine these two, we'll obtain a clock. This process is modularization
Let's call this class as ClockDisplay
. We want to invoke the methods in NumberDisplay
How can we do?
Creating objective types 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();
}
public int xxx()
{
return hours.xxx();
}
}
-
It creates a new object of the named class (here, NumberDisplay).
-
It executes the constructor of that class.
-
If we want change the status of objects like
hours
, we should invoking methods inNumberDisplay
on it
We create two objects, hours
and minutes
, using object types. This is different from primitive types; we cannot use regular arithmetic expressions, such as int
, or the connection operator +
, as we do with String
.
Instead, we should invoke methods in NumberDisplay
, and the methods will operate on objects like hours,minutes
The grammar of invoking methods and constructor (creating new objects) is same with what we are doing in code pad
Note
Internal method vs External method
When we invoke external method, the syntax is <objectName>.<methodName>(<parameter if needed>);
But if we define a method in a class and we want to invoke it in the same class, this is internal method
The syntax is <methodName>(<parameter if needed>);
Multiple constructors
Java allows us to write serval different constructors.
We can choose freely the way that we construct the objects
Example:
public ClockDisplay()
{
//....
}
public ClockDisplay(int hour, int minute)
{
//....
}
this
keyword
-
Reason: Name Overloading
- Occurs when the same name is used for different entities
- Example: fields and parameters having identical names
-
Constructor Example
this.from = from; this.to = to; this.message = message;
-
Variable Distinction
- When fields and parameters share names, they exist as separate variables
- Java resolves variable references by using the one from the closest enclosing block
- Parameters (being in the constructor block) take precedence over fields (defined at class level)
-
The this Keyword
-
this
refers to the current object -
this.from
explicitly refers to the field named "from" in the current object - Allows access to fields when parameters have the same name
-
-
Statement Interpretation
-
this.from = from;
means: "assign the parameter named 'from' to the field named 'from'"
-
Using debugger
Setting Breakpoints
- Set a breakpoint at the first line of printNextMailItem method
- Breakpoints are flags that pause execution at specific lines
- Can be set via Tools menu or by clicking in the margin next to code
- Class must be compiled before setting breakpoints