04 More On Java
04 More On Java
Object-Oriented Programming
Outline
Readings:
HFJ: Ch. 3, 4.
GT: Ch. 3, 4.
Characters: char
if (b.equals(c)) { // true };
Method equals()
Pre-defined classes: Integer m1 = new Integer(10);
Integer m2 = new Integer(10);
Ready to use System.out.print(m1.equals(m2));
User-created classes:
equals() must be defined, otherwise, it always returns false
object:Dog
name: “Bruno”
breed: null
size:
main’s stack frame
dog
object:Dog
name: “Bruno”
breed: null
size:
main’s stack frame
dog
class Dog {
...
void bark(int numOfBarks) {
while (numOfBarks > 0) {
System.out.println("ruff");
numOfBarks--; A method uses parameters.
} A caller passed arguments
}
Đại học Công nghệ - ĐHQG HN More on Java 16
Parameter passing & return value
class Dog {
something like
...
int numOfBarks = 3;
void bark(int numOfBarks) {
happens at this point
while (numOfBarks > 0) {
System.out.println("ruff");
numOfBarks--;
}
}
Đại học Công nghệ - ĐHQG HN More on Java 17
Parameter passing & return value
class Dog {
...
int getSize() {
return size;
}
m
n 50
class Date {
int year, month, day;
public Date(int y, int m, int d) {
year = y; month = m; day = d;
}
public void copyTo(Date d) {
d.year = year; Date temp =
d.month = month; new Date(d2.year, d2.month, d2.day);
d.day = day; d3 = temp;
}
public Date copy() {
return new Date(year, month, day);
}
...
}
Đại học Công nghệ - ĐHQG HN More on Java 23
Method overloading
void bark() {
if (size > 14)
dog1.bark(); //this dog's size get compared
System.out.println("Ruff! Ruff!");
else
dog2.getBigger(); //this dog's size get increased
System.out.println("Yip! Yip!");
}
void getBigger() {
size += 5;
where is the object reference
}
and dot notation?
}
void bark() {
if (this.size > 14)
dog1.bark(); //this dog's size get compared
System.out.println("Ruff! Ruff!");
else
dog2.getBigger(); //this dog's size get increased
System.out.println("Yip! Yip!");
}
void getBigger() {
this.size += 5;
}
}
methods
often omitted
parameter passing and return value
calling constructor from inside constructor
class MyInteger {
private int value;
public boolean greaterThan (MyInteger other) {
return (this.value > other.value);
}
public boolean lessThan (MyInteger other) {
return (other.greaterThan(this));
}
public MyInteger increment() {
value++;
return this;
}
}
Details:
HFJ. Ch.14 / GT. Ch.12
In this slide:
standard input / output stream
System.in
An InputStream object
// read a word
String s = sc.next());
// read an integer
int i = sc.nextInt();
} catch(IOException e) {
e.printStackTrace();
}
}
...
Đại học Công nghệ - ĐHQG HN More on Java 35
Write to a text file. Example
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
...
public static void main(String args[]) {
int i = 1; long l = 10;
try {
// create a printwriter to write output to a file stream
PrintWriter out = new PrintWriter(new FileWriter("test.data"));
// write to file
out.println("Hello " + i + " " + l);
out.close();
} catch(IOException e) {
e.printStackTrace();
}
}
...