Question
Identify the output of following Java code. Justify the syntax. File Name:Instuctor.java /** This class stores data about an instructor. */ public class Instructor {
- Identify the output of following Java code. Justify the syntax.
File Name:Instuctor.java
/**
This class stores data about an instructor.
*/
public class Instructor
{
private String lastName; // Last name
private String firstName; // First name
private String officeNumber; // Office number
/**
This constructor initializes the last name,
first name, and office number.
@param lname The instructor's last name.
@param fname The instructor's first name.
@param office The office number.
*/
public Instructor(String lname, String fname,
String office)
{
lastName = lname;
firstName = fname;
officeNumber = office;
}
/**
The copy constructor initializes the object
as a copy of another Instructor object.
@param object2 The object to copy.
*/
public Instructor(Instructor object2)
{
lastName = object2.lastName;
firstName = object2.firstName;
officeNumber = object2.officeNumber;
}
/**
The set method sets a value for each field.
@param lname The instructor's last name.
@param fname The instructor's first name.
@param office The office number.
*/
public void set(String lname, String fname,
String office)
{
lastName = lname;
firstName = fname;
officeNumber = office;
}
/**
toString method
@return A string containing the instructor
information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Last Name: " + lastName +
" First Name: " + firstName +
" Office Number: " + officeNumber;
// Return the string.
return str;
}
}
File Name: TextBook.java
/**
This class stores data 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 initializes the title,
author, and publisher fields
@param textTitle The book's title.
@param auth The author's name.
@param pub The name of the publisher.
*/
public TextBook(String textTitle, String auth,
String pub)
{
title = textTitle;
author = auth;
publisher = pub;
}
/**
The copy constructor initializes the object
as a copy of another TextBook object.
@param object2 The object to copy.
*/
public TextBook(TextBook object2)
{
title = object2.title;
author = object2.author;
publisher = object2.publisher;
}
/**
The set method sets a value for each field.
@param textTitle The book's title.
@param auth The author's name.
@param pub The name of the publisher.
*/
public void set(String textTitle, String auth,
String pub)
{
title = textTitle;
author = auth;
publisher = pub;
}
/**
toString method
@return A string containing the textbook
information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Title: " + title +
" Author: " + author +
" Publisher: " + publisher;
// Return the string.
return str;
}
}
File name:Course.java
/**
This class stores data 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 initializes the courseName,
instructor, and text fields.
@param name The name of the course.
@param instructor An Instructor object.
@param text A TextBook object.
*/
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
@return The name of the course.
*/
public String getName()
{
return courseName;
}
/**
getInstructor method
@return A reference to a copy of this course's
Instructor object.
*/
public Instructor getInstructor()
{
// Return a copy of the instructor object.
return new Instructor(instructor);
}
/**
getTextBook method
@return A reference to a copy of this course's
TextBook object.
*/
public TextBook getTextBook()
{
// Return a copy of the textBook object.
return new TextBook(textBook);
}
/**
toString method
@return A string containing the course information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Course name: " + courseName +
" Instructor Information: " +
instructor +
" Textbook Information: " +
textBook;
// Return the string.
return str;
}
}
File name:CourseDemo.java
/**
This program demonstrates the Course class.
*/
public class CourseDemo
{
public static void main(String[] args)
{
// Create an Instructor object.
Instructor myInstructor =
new Instructor("Kramer", "Shawn", "RH3010");
// Create a TextBook object.
TextBook myTextBook =
new TextBook("Starting Out with Java",
"Gaddis", "Scott/Jones");
// Create a Course object.
Course myCourse =
new Course("Intro to Java", myInstructor,
myTextBook);
// Display the course information.
System.out.println(myCourse);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started