Demo - Aggregation

 

image-20240510-162253.png

 

TextBook Class

package AggregationDemoOne; /** * This class stores information about a textbook. */ public class TextBook { private String title; // Title of the book private String author; // Author's last name private String publisher; // Name of publisher /** * This constructor accepts arguments for the title, author, and publisher. */ public TextBook(String textTitle, String auth, String pub) { title = textTitle; author = auth; publisher = pub; } /** * Copy constructor */ public TextBook(TextBook object2) { title = object2.title; author = object2.author; publisher = object2.publisher; } /** * The set method sets each field. */ public void set(String textTitle, String auth, String pub) { title = textTitle; author = auth; publisher = pub; } /** * The toString method returns a string containing * the textbook information. */ public String toString() { // Create a string representing the object. String str = "\tTitle: " + title + "\n\tAuthor: " + author + "\n\tPublisher: " + publisher; // Return the string. return str; } }

The TextBook class is a Java class that represents a textbook with its title, author, and publisher. Here's a breakdown of its functionality:

  1. Instance Variables: The class has three private instance variables: title, author, and publisher. These variables represent the title of the textbook, the author's name, and the publisher's name, respectively. The private keyword means that these variables can only be accessed directly within the TextBook class.

  2. Constructor: The TextBook constructor takes three parameters: textTitle, auth, and pub. These parameters are used to initialize the title, author, and publisher instance variables respectively.

  3. Copy Constructor: This is a special type of constructor that creates a new TextBook object as a copy of an existing TextBook object. It takes as a parameter another TextBook object and copies its field values into the new object. This is useful when you want to create a new object that is identical to an existing object but don't want changes to one to affect the other.

  4. set Method: The set method is used to update the title, author, and publisher fields of an existing TextBook object. It takes three parameters and assigns each to the appropriate instance variable.

  5. toString Method: This method returns a String that represents the TextBook object. It constructs a string containing the title, author, and publisher fields. This method is called automatically when a TextBook object is used where a string is expected (for example, in a print statement). It allows TextBook objects to be printed in a meaningful way.

Instructor Class

package AggregationDemoOne; /** * This class stores information about an instructor. */ public class Instructor { private String lastName; // Last name private String firstName; // First name private String officeNumber; // Office number /** * This constructor accepts arguments for the last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * Copy constructor */ public Instructor(Instructor object2) { lastName = object2.lastName; firstName = object2.firstName; officeNumber = object2.officeNumber; } /** * The set method sets each field. */ public void set(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The toString method returns a string containing * the instructor information. */ public String toString() { // Create a string representing the object. String str = "\tLast Name: " + lastName + "\n\tFirst Name: " + firstName + "\n\tOffice Number: " + officeNumber; // Return the string. return str; } }

The Instructor class is a Java class that represents an instructor with a last name, first name, and office number. Here's a breakdown of its functionality:

  1. Instance Variables: The class has three private instance variables: lastName, firstName, and officeNumber. These variables represent the instructor's last name, first name, and office number respectively. The private keyword means that these variables can only be accessed directly within the Instructor class.

  2. Constructor: The Instructor constructor takes three parameters: lname, fname, and office. These parameters are used to initialize the lastName, firstName, and officeNumber instance variables respectively.

  3. Copy Constructor: This is a special type of constructor that creates a new Instructor object as a copy of an existing Instructor object. It takes as a parameter another Instructor object and copies its field values into the new object. This is useful when you want to create a new object that is identical to an existing object but don't want changes to one to affect the other.

  4. set Method: The set method is used to update the lastName, firstName, and officeNumber fields of an existing Instructor object. It takes three parameters and assigns each to the appropriate instance variable.

  5. toString Method: This method returns a String that represents the Instructor object. It constructs a string containing the lastName, firstName, and officeNumber fields. This method is called automatically when an Instructor object is used where a string is expected (for example, in a print statement). It allows Instructor objects to be printed in a meaningful way.

Course Class

package AggregationDemoOne; /** * This class stores information about a course. */ public class Course { private String courseName; // Name of the course private Instructor instructor; // The instructor private TextBook textBook; // The textbook /** * This constructor accepts arguments for the * course name, instructor, and textbook. */ public Course(String name, Instructor instr, TextBook text) { // Assign the courseName. courseName = name; // Create a new Instructor object, passing // instr as an argument to the copy constructor. instructor = new Instructor(instr); // Create a new TextBook object, passing // text as an argument to the copy constructor. textBook = new TextBook(text); } /** * getName method */ public String getName() { return courseName; } /** * getInstructor method */ public Instructor getInstructor() { // Return a copy of the instructor object. return new Instructor(instructor); } /** * getTextBook method */ public TextBook getTextBook() { // Return a copy of the textBook object. return new TextBook(textBook); } /** * The toString method returns a string containing * the course information. */ public String toString() { // Create a string representing the object. String str = "Course name: " + courseName + "\nInstructor Information:\n" + instructor + "\nTextbook Information:\n" + textBook; // Return the string. return str; } }

 

The Course class in the provided Java code represents a course with a name, instructor, and a textbook. Here's an overview of its functionality:

  1. Instance Variables: The class has three private instance variables: courseName, instructor, and textBook. These variables hold the course name, the Instructor object for the course, and the TextBook object for the course respectively. The private keyword indicates that these variables can only be accessed directly within the Course class.

  2. Constructor: The Course constructor takes three parameters: name, instr, and text. These parameters are used to initialize the courseName, instructor, and textBook instance variables respectively. However, instead of directly assigning instr and text to instructor and textBook, the constructor creates new copies of these objects using their respective copy constructors. This is to prevent what is known as "aliasing" problems, where changes to objects outside of this class could inadvertently affect the state of the Course object.

  3. getName Method: This is a getter method for the courseName field. It doesn't take any arguments and returns the courseName.

  4. getInstructor Method: This method returns a copy of the instructor object. It uses the Instructor class's copy constructor to prevent aliasing problems.

  5. getTextBook Method: This method works similarly to the getInstructor method, but returns a copy of the textBook object to avoid aliasing issues.

  6. toString Method: This method overrides the toString method from the Object class. It creates and returns a string representation of the Course object, including the courseName, instructor (by calling the instructor object's toString method), and textBook (by calling the textBook object's toString method). This method is usually called when you need a user-friendly representation of the object, for example, for debugging or logging.

This class encapsulates all the details of a course, including its name, the instructor teaching it, and the textbook used for it. It provides a complete, high-level abstraction for a course in a larger program such as a university management system.

Driver

Here's how it works:

  1. Instructor Object Creation: An Instructor object named myInstructor is created with the parameters "Roark", "Kevin", and "MLH201". These parameters represent the instructor's last name, first name, and office number respectively.

  2. TextBook Object Creation: A TextBook object named myTextBook is created with the parameters "Starting Out with Java", "Gaddis", and "Addison-Wesley". These parameters represent the textbook's title, author, and publisher respectively.

  3. Course Object Creation: A Course object named myCourse is created with the parameters "Intro to Java", myInstructor, and myTextBook. These parameters represent the course's name, the instructor for the course (represented by the myInstructor object), and the textbook for the course (represented by the myTextBook object).

  4. Display Course Information: Finally, the System.out.println(myCourse); line displays the course information to the console. This is possible because the Course class has a toString() method that provides a string representation of a Course object.

Running this code will create objects for an instructor and a textbook, use them to create a course, and then display the information of the course, including the details of the instructor and the textbook.

 

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark