Professional Documents
Culture Documents
Chapter10_P2
Chapter10_P2
1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Chapter 10 Thinking in Objects
2
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
10.8 Designing the course Class
UML Diagram of class Course + description of the methods.
Course
+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
3
changed the type to Student to make the problem more interesting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
10.8 (Continued)
UML Diagram of classes Course and Student.
Course Student
-name: String
-id: int
-students: Student[ ]
-name: String
-numberOfStudents: int
+Students(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
4
changed the type to Student to make the problem more interesting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
public class Student {
Code of class
private int id;
private String name; Student
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);
}
} 5
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
public class Course {
Code of class
private String name;
private Student[] students; Course
private int numberOfStudents;
7
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Code of
public static void main(String[] args) {Application
public class Problem_10_9 {
}
} // end Application 8
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Java Projects
You will find posted to Google Classroom with
this lecture the three java projects described
below: