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

Chapter 10 Thinking in Objects

10.8 Designing the course


Class

1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Chapter 10 Thinking in Objects

10.8 Designing the course Class

Programming exercises 10.4 and a modified version of 10.12

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

-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
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;

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;
} 6
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
public int getNumberOfStudents() { Code of class
}
return numberOfStudents;
Course
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

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 {

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

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 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:

 A Java project name CourseStudents containing


the code of the classes Student and Course along
with the application shown in slides 5 to 8.

 Two Java projects containing the code of


programming exercise 10.4 and a modified
version of programming exercise 10.12.
9
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807

You might also like