Demo - Aggregation
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:
Instance Variables: The class has three private instance variables:
title
,author
, andpublisher
. 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 theTextBook
class.Constructor: The
TextBook
constructor takes three parameters:textTitle
,auth
, andpub
. These parameters are used to initialize thetitle
,author
, andpublisher
instance variables respectively.Copy Constructor: This is a special type of constructor that creates a new
TextBook
object as a copy of an existingTextBook
object. It takes as a parameter anotherTextBook
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.set Method: The
set
method is used to update thetitle
,author
, andpublisher
fields of an existingTextBook
object. It takes three parameters and assigns each to the appropriate instance variable.toString Method: This method returns a String that represents the
TextBook
object. It constructs a string containing thetitle
,author
, andpublisher
fields. This method is called automatically when aTextBook
object is used where a string is expected (for example, in a print statement). It allowsTextBook
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:
Instance Variables: The class has three private instance variables:
lastName
,firstName
, andofficeNumber
. 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 theInstructor
class.Constructor: The
Instructor
constructor takes three parameters:lname
,fname
, andoffice
. These parameters are used to initialize thelastName
,firstName
, andofficeNumber
instance variables respectively.Copy Constructor: This is a special type of constructor that creates a new
Instructor
object as a copy of an existingInstructor
object. It takes as a parameter anotherInstructor
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.set Method: The
set
method is used to update thelastName
,firstName
, andofficeNumber
fields of an existingInstructor
object. It takes three parameters and assigns each to the appropriate instance variable.toString Method: This method returns a String that represents the
Instructor
object. It constructs a string containing thelastName
,firstName
, andofficeNumber
fields. This method is called automatically when anInstructor
object is used where a string is expected (for example, in a print statement). It allowsInstructor
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:
Instance Variables: The class has three private instance variables:
courseName
,instructor
, andtextBook
. These variables hold the course name, theInstructor
object for the course, and theTextBook
object for the course respectively. Theprivate
keyword indicates that these variables can only be accessed directly within theCourse
class.Constructor: The
Course
constructor takes three parameters:name
,instr
, andtext
. These parameters are used to initialize thecourseName
,instructor
, andtextBook
instance variables respectively. However, instead of directly assigninginstr
andtext
toinstructor
andtextBook
, 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 theCourse
object.getName Method: This is a getter method for the
courseName
field. It doesn't take any arguments and returns thecourseName
.getInstructor Method: This method returns a copy of the
instructor
object. It uses theInstructor
class's copy constructor to prevent aliasing problems.getTextBook Method: This method works similarly to the
getInstructor
method, but returns a copy of thetextBook
object to avoid aliasing issues.toString Method: This method overrides the
toString
method from theObject
class. It creates and returns a string representation of theCourse
object, including thecourseName
,instructor
(by calling theinstructor
object'stoString
method), andtextBook
(by calling thetextBook
object'stoString
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:
Instructor Object Creation: An
Instructor
object namedmyInstructor
is created with the parameters"Roark"
,"Kevin"
, and"MLH201"
. These parameters represent the instructor's last name, first name, and office number respectively.TextBook Object Creation: A
TextBook
object namedmyTextBook
is created with the parameters"Starting Out with Java"
,"Gaddis"
, and"Addison-Wesley"
. These parameters represent the textbook's title, author, and publisher respectively.Course Object Creation: A
Course
object namedmyCourse
is created with the parameters"Intro to Java"
,myInstructor
, andmyTextBook
. These parameters represent the course's name, the instructor for the course (represented by themyInstructor
object), and the textbook for the course (represented by themyTextBook
object).Display Course Information: Finally, the
System.out.println(myCourse);
line displays the course information to the console. This is possible because theCourse
class has atoString()
method that provides a string representation of aCourse
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