M4 OO Programming Objects
M4 OO Programming Objects
Objects
Programming 1
-1-
Contents
1. Introduction
• Objects, messages, classes
2. Working with objects
• Creating, using and deleting objects
3. Simple objects
• Classes String and StringBuilder
4. Formatting
• Methods printf and format
-2-
Introduction to
objects
Objects 1.5
Object = foundation of
Object-Oriented Programming (OOP)
-4-
Objects
•Example: An individual ship
– Properties (attributes)
▪ Type: passenger ship, oil tanker, …
▪ Capacity: max cargo tons or max amount of
passengers
▪ Amount of crew currently on board
▪ Length, width, draft
– Behavior
▪ dock, blast horn, lower anchor, turn, …
▪ board passengers, load cargo
•Other examples?
-5-
Which data type to use for ...
•Age? → int
•Percentage? → double
•Name? → String
•Ship?
•Product?
•Person?
•Car?
?
•Trip? Island? Captain?
-6-
Classes & Objects
•A class is a blueprint that describes the properties
and the methods of all objects of a certain type
•An object is a unique instance of a class
-7-
Objects
•Come up with some example of classes and
possible objects of those classes
•Class President
•Objects of class President
barackObama abrahamLincoln
-8-
Objects
-9-
Quiz
•Which of the following are classes and which are
objects?
If it's a class, give examples of possible objects.
If it's an object, what would be a good class name?
- 10 -
Classes & Objects
•A class determines the type of an object
•A class defines the properties and methods
•An object is an instance of a class
Class definition Object (instance of a class)
- 11 -
Method call (message)
object
method
variable stores
parameter(s)
res t
- 12 -
Working with
objects
Creating objects 3.2.2
Or
○ Use one. Some classes in the Java libraries:
■ Scanner
■ String
■ Random
■ StringBuilder
■ …
● Creating an object
○ Scanner keyboard = new Scanner(System.in);
○ Random rand = new Random(10);
○ StringBuilder builder = new StringBuilder();
- 14 -
Creating objects
How do you create an object?
- 15 -
Creating objects
• Some examples:
Scanner keyboard = new Scanner(System.in); A single parameter
Random getal = new Random();
Rectangle box = new Rectangle(10,6); No parameters
2 parameters
Capi letter!
- 16 -
Random
Generating random numbers
Using classes & objects: Random 6,9
- 19 -
Using classes & objects: Random
- 20 -
Using objects: some questions
- 21 -
Using objects: some questions
Ex04.01: Lottery
- 22 -
Exercises
•Ex04.01: Lottery
- 23 -
Referencing
an object
Reference
generator Random
object
Stack Heap
Wo ld it be possib e for a
randomOne re erence to not refer to
Random any object at al ?
object
randomTwo
- 26 -
Heads up!
Will not compile.
java: variable generator might not have been
initialized
Random generator;
int random = generator.nextInt();
generator
Random
generator
object
- 27 -
Reference
int i;
Assignment (of primitive types): content of i is copied to j
int j;
→ i and j have the same content: 7
i = 7;
j = i; // j is now also 7
i++; // i is 8, j is still 7
System.out.println(j); // 7
Player player1;
Player player2;
player1 = new Player("hunter25", 3); // 3 lives
player2 = player1; // player2 also refers to "hunter25"
player1.extraLife();
System.out.println(player2.getLives()); // 4!
Assignment (of reference types): content of player1 (reference to the object) is copied to player2
→ player1 and player2 now refer to the same object
As a consequence, any change on the object via player1 can be observed via player2 and vice versa
- 28 -
package and
import
What is a package? 2.5.1 & 3.2.5
• If
we want to refer to a class by its name only,
an import statement is needed
Replacing Random with *
import java.util.Random; imports all classes from java.util
- 30 -
Package and import
•Using import:
import java.util.Random;
// …
When sing import, just
Random generator = new Random();
the name o the c ass ill
suffice
- 31 -
Package and import
- 32 -
Package naming convention
● reverse.domain.of.company.project.internal
○ guarantees world wide uniqueness
○ all lower case
● example
be.kdg.programming1.m4.myutilities
- 33 -
Garbage Collection
Freeing memory / opposite of new
Garbage collection 8,10
Random
generator
object
- 35 -
Exercises
•Ex04.02: Random
- 36 -
Strings
String 14.1
- 38 -
Creating a String 14.3.1
System.out.println("Hello World");
- 39 -
Reading a String
- 40 -
String methods 14.3
String
- 41 -
String methods examples
Never compare strings using the ==
String male = "John";
operator! (see later)
String female = "Mary";
- 42 -
String methods
Hello Duke!
- 43 -
Quiz: what is the output?
String me = "me";
int number = 2 + 3;
String result = me + " a ";
result += number + "!";
System.out.println("Give " + result);
System.out.println(3 + 7 + " hello world " + 3 + 7);
- 44 -
Quiz: Strings are immutable
•What is the output of the following code?
String s1 = "abc";
s1.concat("def");
System.out.println(s1);
String s2 = s1.concat("def");
System.out.println(s2);
- 45 -
Memory allocation for Strings
Stack Heap
ha t!?
one
a i t,w
"abc" W
two
three "abc"
- 46 -
Comparing Strings
System.out.println(one.equals(two)); true
System.out.println(one.equals(three)); true
System.out.println(two.equals(three)); true
- 47 -
Strings
- 48 -
Exercises
•Ex 04.03-04.05
- 49 -
The StringBuilder class 14.4
- 50 -
Creating String vs StringBuilder 14.4.1
- 51 -
Methods of StringBuilder 14.4
•StringBuilder
- 52 -
Methods of StringBuilder: examples
fork.append('r').append('k');
System.out.println(spoon + " and " + fork);
spoon and fork
- 53 -
Exercises
•Ex 04.06-04.10
- 54 -
Formatting
Formatting data 2.4
Yo can use both printf or format, both methods are identical (except or their name)
- 56 -
Formatting data
a tomatic rounding
- 58 -
Quiz: what is the output?
System.out.printf("%b %c %d %f %s%n"
, true, 'X', 1, 3.14, "abc");
- 59 -
Quiz: what is the output?
System.out.format("%4d%n", 1); 1
System.out.format("%4d%n", 12); 12
System.out.format("%4d%n", 123); 123
System.out.format("%4d%n", 1234); 1234
System.out.format("%4d%n", 12345); 12345
System.out.format("%4d%n", 123456); 123456
- 61 -
Review
1. Introduction
• Objects, messages, classes
2. Working with objects
• Creating, using and deleting objects
3. Simple objects
• Classes String and StringBuilder
4. Formatting
• Methods printf and format
- 62 -