Question

JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

JAVA

/**
 * 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;
   }
}

-------------------------------------------------------------------------------------------------------------------------------------------

First run the attached classes and see what the output is then examine the code and see how composition works. Complete the following steps to modify some of these classes so that it will also 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 (including toString() method)

Modify the course class toString() method so that it will also print out the information on SI.

Modify the client so that it works with the additional class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, Please find my implementation.

Please let me know in case of any issue.

############## Instructor.java #############

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;

   }

}

############## TextBook.java ##################

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;

   }

}

############### SI.java ############

public class SI {

   private String firstName;

   private String lastName;

   private int officeHours;

  

   public SI(String firstName, String lastName, int officeHours) {

       super();

       this.firstName = firstName;

       this.lastName = lastName;

       this.officeHours = officeHours;

   }

   public String getFirstName() {

       return firstName;

   }

   public void setFirstName(String firstName) {

       this.firstName = firstName;

   }

   public String getLastName() {

       return lastName;

   }

   public void setLastName(String lastName) {

       this.lastName = lastName;

   }

   public int getOfficeHours() {

       return officeHours;

   }

   public void setOfficeHours(int officeHours) {

       this.officeHours = officeHours;

   }

  

   @Override

   public String toString() {

       return "First Name: "+firstName+"\n"+

               "Last Name: "+lastName+"\n"+

               "Office Hours: "+officeHours;

   }

}

################## Course.java ###################

public class Course

{

   private String courseName; // Name of the course

   private Instructor instructor; // The instructor

   private TextBook textBook;

   private SI si;

   public Course(String name, Instructor instr,

           TextBook text, SI s)

   {

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

      

       si = s;

   }

   /**

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

   }

  

   public SI getSI(){

       return si;

   }

   /**

   * 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

               + "\nSI Information:\n"

               + si;

       // Return the string.

       return str;

   }

}

################### CourseDemo.java #####################

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

      

       SI si = new SI("Pravesh", "Kumar", 6);

       // Create a Course object.

       Course myCourse =

               new Course("Intro to Java", myInstructor,

                       myTextBook, si);

       // Display the course information.

       System.out.println(myCourse);

   }

}

/*

Sample run:

Course name: Intro to Java

Instructor Information:

Last Name: Kramer

First Name: Shawn

Office Number: RH3010

Textbook Information:

Title: Starting Out with Java

Author: Gaddis

Publisher: Addison-Wesley

SI Information:

First Name: Pravesh

Last Name: Kumar

Office Hours: 6

*/

Add a comment
Know the answer?
Add Answer to:
JAVA /** * This class stores information about an instructor. */ public class Instructor { private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

  • (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...

    (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...

  • pls write psuedocode write UML CODE ALSO example 3 files 1 for abstract 1 for bank...

    pls write psuedocode write UML CODE ALSO example 3 files 1 for abstract 1 for bank account and 1 for savings accounts due in 12 hours Lab Assignment 4a Due: June 13th by 9:00 pm Complete the following Programming Assignment. Use good programming style and all the concepts previously covered. Submit the java files electronically through Canvas by the above due date in 1 Zip file Lab4.Zip. (This is from the chapter on Inheritance.) 9. BankAccount and SavingsAccount Classes Design...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • I Need Help with this using Java Programming : Class name fname lname Class variable total...

    I Need Help with this using Java Programming : Class name fname lname Class variable total Number Constructor (no arguments) Constructor (takes two arguments, fname and lname) One getter method to return the fname and lname One setter method for fname and lname Inside Main: Create three objects with the following names Jill Doe John James Jack Smith When creating the first object (Should not be an anonymous object) use the argument-less constructor Then use the setter method to assign...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

  • Public class Person t publie alass EmployeeRecord ( private String firstName private String last ...

    public class Person t publie alass EmployeeRecord ( private String firstName private String last Nanei private Person employee private int employeeID publie EnmployeeRecord (Person e, int ID) publie Person (String EName, String 1Name) thia.employee e employeeID ID setName (EName, 1Name) : publie void setName (String Name, String 1Name) publie void setInfo (Person e, int ID) this.firstName- fName this.lastName 1Name this.employee e employeeID ID: publio String getFiritName) return firstName public Person getEmployee) t return employeei public String getLastName public int getIDO...

  • I have a java class that i need to rewrite in python. this is what i...

    I have a java class that i need to rewrite in python. this is what i have so far: class Publisher: __publisherName='' __publisherAddress=''    def __init__(self,publisherName,publisherAddress): self.__publisherName=publisherName self.__publisherAddress=publisherAddress    def getName(self): return self.__publisherName    def setName(self,publisherName): self.__publisherName=publisherName    def getAddress(self): return self.__publisherAddress    def setAddress(self,publisherAddress): self.__publisherAddress=publisherAddress    def toString(self): and here is the Java class that i need in python: public class Publisher { //Todo: Publisher has a name and an address. private String name; private String address; public Publisher(String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT