Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

1

CHAPTER 10 THINKING IN OBJECTS

10.3 The Scope of Variables


10.4 The this Reference
10.7 Object Composition
10.8 Designing the course Class
10.3 THE SCOPE OF VARIABLES
2

• Chapter 5, Methods, discussed local variables and their


scope rules. A variable declared inside a method is
called a local variable. The scope of a local variable
starts from its declaration and continues to the end of the
block that contains the variable. So local variables can
be used only inside the method where they are declared.
• Variables declared inside the class, but not inside any
method in the class are called data fields or class’s
variables. They can be instance (non static) or static
variables. Their scope is the entire class.
10.4 THE THIS REFERENCE
3

• The this keyword is the name of a reference that refers to an object itself.
• One common use of the this keyword is to reference a class’s hidden data
fields.
• Another common use of the this keyword is to enable a constructor to invoke
(call) another constructor of the same class.
10.4 THE THIS REFERENCE (CONTINUED)
4

Using the this keyword to reference the hidden data fields.


If a local variable or a parameter has the same name as a data field, then the local
variable or the parameter takes precedence and the data field with the same name is
hidden (i.e. it cannot be accessed). For example, in the following class , i is defined
both as a data field and as a parameter in the method setI(). Hence the data field i is
hidden in the method, but we can access it using the this keyword as shown below.

public class Foo { Suppose that f1 and f2 are two objects of Foo.
private int i = 5;
Calling f1.setI(10) will execute
this.i = 10, where this refers f1
public void setI(int i) { The above assigns the value 10 of the
this.i = i; parameter i to the data field i.
}
Invoking f2.setI(45) will execute
} data field parameter this.i = 45, where this refers f2
10.4 THE THIS REFERENCE (CONTINUED)
5

What if we do not use the this keyword when we should?


Since in the method setI( ) below we did not use the this keyword before the i on the
left of the assignment operator (=) then both the i on the left of = and on the right of
= are treated as the parameter i. None of them is the data field i.

public class Foo { Suppose that f1 is an object of Foo.


private int i = 5;
Invoking f1.setI(10) will not initialize the
data field i to 10. It will instead set the
public void setI(int i) { parameter i to itself (i.e. change the
i = i; parameter i from 10 to 10).
}

} parameter parameter
10.4 THE THIS REFERENCE (CONTINUED)
6

Using the this keyword to


public class Circle {
private double radius;
enable a constructor to
call another constructor
public Circle(double radius) { of the same class.
this.radius = radius;
} Here the use of the keyword this is a must to be able
to access the data field radius since the parameter
public Circle() { has the same name (radius).
this(1.0);
} Here this is used to call the other constructor.

public double getArea() {


return this.radius * this.radius * Math.PI;
}
} Here the use of this is optional since the method getArea() does not
have neither a parameter nor a local variable called radius.
10.7 OBJECT COMPOSITION
7

• An object can contain another object. The relationship between the


two is called composition and is also known as the “has a”
relationship.
• Example:
In the UML diagram, the rhombus shape is used
to represent the composition relationship.

Class Person Class Address

We assume that in class Person we have a data field of type Address


(Another class) which means that a person object “has an” address
object (composition relationship).
10.7 OBJECT COMPOSITION (CONTINUED)
8

Person Address
- name: String - building: String
- address: Address - street: String
- city: String
+ Person(n: String, a : Address)
+ toString( ): String + Address(b: String, s: String, c: String)
+ toString( ): String

public class Person {

private String name;

private Address address; // This is the statement that creates the composition relationship.

public Person(String n, Address a) {

name = n;

address = a;

public String toString() {

return "name = " + name + " address = " + address.toString();

}
10.8 DESIGNING THE COURSE CLASS
9
UML Diagram of class Course + description of the methods.
Course

-name: String The name of the course.


-students: Student[ ] The students who take the course.
-numberOfStudents: int The number of students (default: 0).

+Course(name: String, size: int) Creates a Course with the specified name.
+getName( ): String Returns the course name.
+addStudent(student: Student): void Adds a new student to the course list.
+getStudents( ): Student[ ] Returns the students for the course.
+getNumberOfStudents( ): int Returns the number of students for the course.
+ print( ): void Outputs the data fields of the class.

In your textbook the type of the array students is String, but here we
changed the type to Student to make the problem more interesting.
10.8 (CONTINUED)
10
UML Diagram of classes Course and Student.

Course Student

-name: String
-id: int
-students: Student[ ]
-name: String
-numberOfStudents: int
+Student(id: int , name: String)
+Course(name: String, size: int)
+getId( ): int
+getName( ): String
+getName( ): String
+addStudent(student: Student): void
+setId(id: int ): void
+getStudents( ): Student[ ]
+setName(name: String): void
+getNumberOfStudents( ): int
+ print( ): void
+ print( ): void

In your textbook the type of the array students is String, but here we
changed the type to Student to make the problem more interesting.
public class Student {
Code of class Student
private
11 int id;
private String name;

public Student(int id, String name) {


this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void print() {
System.out.println("id = " + id + ", name = " + name);
}
}
public class Course {
Code of class Course
private
12 String name;
private Student[] students;
private int numberOfStudents;

public Course(String name, int size) { // size is the array size


this.name = name;
numberOfStudents = 0;
students = new Student[size];
}

public void addStudent(Student student) {


if (numberOfStudents == students.length) // array/course is full
System.out.println("Cannot add more students. The section is full.");
else
{
students[numberOfStudents] = student;
numberOfStudents++;
}
}

public Student[] getStudents() {


return students;
}
public int getNumberOfStudents() { Code of class Course
return
13 numberOfStudents;
}

public String getName() { (Continued)


return name;
}

public void print() {


System.out.println("The course name is " + name);
System.out.println("The number of students is " + numberOfStudents);
System.out.println("The students registered in this course are: ");
for (int i = 0 ; i < numberOfStudents; i++)
students[i].print(); // calling the print( ) method of class Student
}

} // end class Course


Code of Application
public 14class Problem_10_9 {
public static void main(String[] args) {

Course course1 = new Course("Intermediate Programming with Objects", 5);

Student s1 = new Student(1, "A"); // create six students


Student s2 = new Student(2, "B");
Student s3 = new Student(3, "C");
Student s4 = new Student(4, "D");
Student s5 = new Student(5, "E");
Student s6 = new Student(6, "F");

course1.addStudent(s1); // add the six students to the course course1


course1.addStudent(s2);
course1.addStudent(s3);
course1.addStudent(s4);
course1.addStudent(s5);
course1.addStudent(s6);

course1.print(); // calling the print( ) method of class Course

}
} // end Application
10.8 (CONTINUED)
15

As you might have noticed from the UML diagram,


there is a composition (has a) relationship between the
class Course and the class Student. The Course class
has as one of its data fields an array students of type
Student. Hence a course object has many student
objects.
In the next few slides you will find the code of the
classes Student, Course, and an application (Driver
class) to test the two classes.
In your textbook the type of the array students is
String, but here we changed the type to Student to
make the problem more interesting.

You might also like