Question

3. Suppose the University has two types of academics: students and lecturers. Both of these have names and ids, but students

a) Write the header for a class called Academic and update the Student and Lecturer class headers so that they inherit Academ

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

Answer 3:

(a) The updated classes are as follows:

class Academic {
    private:
        string name;
        string id;
    public:
        Academic(string name, string id);
};

class Lecturer : public Academic {
    public:
        Lecturer(string name, string id);
        void giveLecture();
};

class Student : public Academic {
    public:
        Student(string name, string id);
        void attendLecture();
};

(b): The constructors for the three classes are given below:

Academic::Academic(string _name, string _id)
    : name(_name), id(_id)
{
}

Student::Student(string name, string id)
    : Academic(name, id)
{
}

Lecturer::Lecturer(string name, string id)
    : Academic(name, id)
{
}

Answer 4:

(a) It is assumed that the Person will have a name but id is specific is academics only. The corresponding classes is as follows:

class Person {
    private:
        string name;
    public:
        Person(string name);
};

class Academic : public Person {
    private:
        string id;
    public:
        Academic(string name, string id);
};

(b): The constructors are as follows:

Person::Person(string _name)
    : name(_name)
{
}


Academic::Academic(string _name, string _id)
    : Person(_name), id(_id)
{
}

(c):

If all the methods are declared private, the code would not compile. This would be because the private constructors of the base class would not be accessible by the derived class. Thus, the constructor for Academic could not call the constructor for Person, and the constructors for Student and Lecturer would not be able to call the constructor for Academic. This would cause a compilation error.

When the constructor is private, it also implies that only member functions can create its objects.

If all the variables were declared public, the member variables of the base class would be accessible from the derived classes as well as functions that are not its members. Thus, the string name which is a member of Person, would be accessible by Academic and other classes as well as outside the derived classes as well. Similarly, string id in class Academic, if made public would be accessible anywhere.

Add a comment
Know the answer?
Add Answer to:
3. Suppose the University has two types of academics: students and lecturers. Both of these have...
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
  • 3. Suppose the University has two types of academics: students and lecturers. Both of these have...

    3. Suppose the University has two types of academics: students and lecturers. Both of these have names and ids, but students attend lectures while lecturers give them class Studentí private: string name; string id; public: Student(string name, string id); string getName O; string getIDO; void attendLecture (); class Lecturer< private: string name; string id; publiC: Lecturer (string name, string id); string getName (); string getIDO; void giveLecture(); ti (a) Write the header for a class called Academic and update the...

  • PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

    PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

  • I need help for part B and C 1) Create a new project in NetBeans called...

    I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • How to create a constructor that uses parameters from different classes? I have to create a...

    How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...

  • 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...

  • I need help converting the following two classes into one sql table package Practice.model; impor...

    I need help converting the following two classes into one sql table package Practice.model; import java.util.ArrayList; import java.util.List; import Practice.model.Comment; public class Students {          private Integer id;    private String name;    private String specialties;    private String presentation;    List<Comment> comment;       public Students() {}       public Students(Integer id,String name, String specialties, String presentation)    {        this.id= id;        this.name = name;        this.specialties = specialties;        this.presentation = presentation;        this.comment = new ArrayList<Commment>();                  }       public Students(Integer id,String name, String specialties, String presentation, List<Comment> comment)    {        this.id= id;        this.name...

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