Menu

(Solved) : First Run Attached Classes See Output Examine Code See Composition Works Complete Followin Q37292140 . . .

First run the attached classes and see what the output is thenexamine the code and see how composition works. Complete thefollowing steps to modify some of these classes so that it willalso print out the information on the SI of the class too.

  • Add SI class that has three instance variables (first name,Last name, Office hours)
  • Add all the methods required for this class (includingtoString() method)
  • Modify the course class toString() method so that it will alsoprint out the information on SI.
  • Modify the client so that it works with the additionalclass.

/** * This class stores information about an instructor. */public class Instructor{ private String lastName, // Last name firstName, // First name 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; } /** * 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 = “Last Name: ” + lastName + “nFirst Name: ” + firstName + “nOffice Number: ” + officeNumber; // Return the string. return str; }}

———————————————————————————————————————————-

/** * 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”, “Addison-Wesley”); // Create a Course object. Course myCourse = new Course(“Intro to Java”, myInstructor, myTextBook); // Display the course information. System.out.println(myCourse); }}

—————————————————————————————————————–

/** * This class stores information about a textbook. */public class TextBook{ private String title, // Title of the book author, // Author’s last name 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; } /** * 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 = “Title: ” + title + “nAuthor: ” + author + “nPublisher: ” + publisher; // Return the string. return str; }}

———————————————————————————————————-

/** * 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; 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 = instr; // Create a new TextBook object, passing // text as an argument to the copy constructor. textBook = text; } /** * getName method */ public String getName() { return courseName; } /** * getInstructor method */ public Instructor getInstructor() { // Return a copy of the instructor object. return instructor; } /** * getTextBook method */ public TextBook getTextBook() { // Return a copy of the textBook object. return 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; }}

Expert Answer


Answer to First run the attached classes and see what the output is then examine the code and see how composition works. Complete …

OR