Review of Previous Lesson
1
Object Orientated Programming Paradigm (OOP)
Own Classes - Exercises
“Has-a” (Aggregation / Composition) = Objects that Contain Objects, final
2
3
LEARNING OBJECTIVE | |
Define instance variables for the attributes to be initialized through the constructors of a class. | |
Declare variables of the correct types to represent primitive data. | |
4
ESSENTIAL KNOWLEDGE | |
An object’s state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This creates a “has-a” relationship between the object and its instance variables. | |
Write your own programs:�Write your own programs from “scratch”.�Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).
5
Trees
6
“Has-a” (Aggregation) = Objects that Contain Objects
7
Can an object contain instance variables that point to other objects?
private Cone branches;
private Cylinder trunk;
8
How many objects are there?
3:
?
Ways to declare & assign a reference variable
….
refVariableName = new ClassName( ) ;
9
Some constructors may require certain information called “actual” parameters written inside the brackets in a specific order separated by ,.
null/nothing
Trees
10
Continued on the next slide.
Tree
Cylinder
Cone
UML Diagram (without variables and methods)
one-to-one
Trees
11
Tree
- branches : Cone
- trunk : Cylinder
- x, y, z : int
+ Tree(heightBranches, radiusBranches, heightTrunk, radiusTrunk : double, x, y, z : int)
+ getVolume() : double
+ getArea() : double
+ grow(double rate) : void
+ toString() : String
UML Diagram (with variables and methods)
Key:
- private
+ public
# protected
constructor
Forest
12
Tree
Cylinder
Cone
UML Diagram (without variables and methods)
Forest
one-to-many
one-to-one
Forest
- trees : Tree[]
+ Forest(size : int)
+ setTree(tree: Tree) : void
+ toString() : String
UML Diagram (with variables and methods)
Place each Tree in the next available location in trees.
Advantages & Disadvantages of “has-a” aggregation
13
Forest
Answer in your comments:
str += trees[i] + "\n\n";
14
Weather Report
15
Continued on the next slide.
Weather Report
16
Continued on the next slide.
Weather Report
17
Continued on the next slide.
Remember that all should instance variables should be private.
Weather Report
18
Continued on the next slide.
dataType[] arrayName;
// Establishes the fact that intArray is an int array variable but no array actually exists yet, only a reference to the array is created.
arrayName = new dataType[arraySize]; // Creates an array.
?
Weather Report
19
Continued on the next slide.
Weather Report
/* Will be used as a return value by some methods to flag an error. The caller will be expected to check the return value before using it. Using the same variable name throughout a program’s code will improve readability and consistency. See next slide for more details.*/
private final int ERRORFLAG = 999;
20
Weather Report - Methods:
return ((year%4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
return 31;
else if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
else if(m == 2)
if (isLeapYear(y))
return 29;
else
return 28;
else
return ERRORFLAG;
21
Weather Report - Methods:
22
// Note that to use this feature effectively main() should check if setTemp returns false and act accordingly:
if ( !jan.setTemp(day, highestTemp) )
/* This still calls .setTemp on jan & checks if it returns false but is a more professional, neater & elegant way to write if ( (jan.setTemp(day, highestTemp) == false ) */
System.out.println("error in input");
/* Also note that jan.setTemp(day, highestTemp) on its own will also compile and execute but as the return value is never examined or stored anywhere it will just be ignored (unused & lost). */
Weather Report - Methods:
23
Continued on the next slide.
Use an String Escape Sequence for line breaks.
For reminder see “nested for” (slide 9).
For reminders on how to store primitive data types in String variables see “Strings” (slide 86 or earlier if necessary).
Weather Report
24
Continued on the next slide.
Weather Report
25
StringArray
26
Continued on the next slide.
StringArray
27
String[] args
for (int j=0; j < args.length; j++ )
System.out.println( "Parameter " + j + ": " + args[j] );
Now try starting the program with command line arguments.
28
String[] args
29
Grade yourself
30