Question

1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML claPart B: Subclass Person into Student Objective: Create subclass of Person with more specificity. In Part A, you implemented tStudent - major: String - gpa: double - enrolledClasses: ArrayList<String> + Student(String name, int id, String major, doubl7) To be able to initialize the fields defined in the Person class, the constructor of the Student class includes 4 fields, t8) Add more statements in the constructor to initialize the other fields of the Student class. Your complete constructor shouLets test to make sure this works. In the Lab6Inheritance.java file, click in the main method, and add code to create a PersSystem.out.println(The class has been dropped); else System.out.println(You are not enrolled in the class); sl.display();Part C: Subclass Person into Professor 1) Follow the same procedure as in Part B to add the Professor class. The UML class di2) Test your Professor class by adding the following statement to the main method, Student sl = new Student (Malick, 900,

I need help for part B and C

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

b)

// Person.java

public class Person {
   private String name;
   private int id;

   /**
   * @param name
   * @param id
   */
   public Person(String name, int id) {
       this.name = name;
       this.id = id;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the id
   */
   public int getId() {
       return id;
   }

   /**
   * @param id
   * the id to set
   */
   public void setId(int id) {
       this.id = id;
   }

   public void display() {
       System.out.println("Name :" + name);
       System.out.println("Id :" + id);

   }

}

=================================

// Student.java

import java.util.ArrayList;

public class Student extends Person {
   private String major;
   private double gpa;
   private ArrayList<String> enrolledClasses;

   /**
   * @param name
   * @param id
   * @param major
   * @param gpa
   */
   public Student(String name, int id, String major, double gpa) {
       super(name, id);
       this.major = major;
       this.gpa = gpa;
       this.enrolledClasses = new ArrayList<String>();
   }

   /**
   * @return the major
   */
   public String getMajor() {
       return major;
   }

   /**
   * @param major
   * the major to set
   */
   public void setMajor(String major) {
       this.major = major;
   }

   /**
   * @return the gpa
   */
   public double getGpa() {
       return gpa;
   }

   /**
   * @param gpa
   * the gpa to set
   */
   public void setGpa(double gpa) {
       this.gpa = gpa;
   }

   /**
   * @return the enrolledClasses
   */
   public ArrayList<String> getEnrolledClasses() {
       return enrolledClasses;
   }

   public void addClass(String name) {
       enrolledClasses.add(name);
   }
public boolean dropClass(String name)
{
   int indx=-1;
   for(int i=0;i<enrolledClasses.size();i++)
   {
       if(enrolledClasses.get(i).equalsIgnoreCase(name))
       {
           indx=i;
       }
   }
  
   if(indx==-1)
   {
       return false;
   }
   else
   {
       enrolledClasses.remove(indx);
       return true;
   }
}
  
public void display()
{
   super.display();
   System.out.println("Major :"+major);
   System.out.println("GPA :"+gpa);
   System.out.println("Enrolled in the following classes:");
   for(int i=0;i<enrolledClasses.size();i++)
   {
       System.out.println(enrolledClasses.get(i));
   }
  
  
}

}

=====================================

// Test.java

public class Test {

   public static void main(String[] args) {

       Student s1=new Student("Malick",900,"CS",3.5);
       s1.addClass("Java Programming");
       s1.addClass("Calculus");
       if(s1.dropClass("Spanish"))
       {
           System.out.println("The class has been dropped");
       }
       else
       {
           System.out.println("You are not enrolled in the class");
       }
      
       s1.display();
      
   }

}

==================================

Output:

You are not enrolled in the class
Name :Malick
Id :900
Major :CS
GPA :3.5
Enrolled in the following classes:
Java Programming
Calculus

===================================

c)

// Professor.java

import java.util.ArrayList;

public class Professor extends Person {
private String department;
private double salary;
private ArrayList<Student> advisees;
   /**
   * @param name
   * @param id
   * @param department
   * @param salary
   */
   public Professor(String name, int id, String department, double salary) {
       super(name, id);
       this.department = department;
       this.salary = salary;
       this.advisees=new ArrayList<Student>();
   }
   /**
   * @return the department
   */
   public String getDepartment() {
       return department;
   }
   /**
   * @param department the department to set
   */
   public void setDepartment(String department) {
       this.department = department;
   }
   /**
   * @return the salary
   */
   public double getSalary() {
       return salary;
   }
   /**
   * @param salary the salary to set
   */
   public void setSalary(double salary) {
       this.salary = salary;
   }
   /**
   * @return the advisees
   */
   public ArrayList<Student> getAdvisees() {
       return advisees;
   }


   public void addAdvisees(Student student)
   {
       advisees.add(student);
   }
  
   public boolean removeAdvisee(String name)
   {
       int indx=-1;
       for(int i=0;i<advisees.size();i++)
       {
           if(advisees.get(i).getName().equalsIgnoreCase(name))
           {
               indx=i;
           }
       }
       if(indx==-1)
       {
           return false;
       }
       else
       {
           advisees.remove(indx);
       }
       return true;
   }

   public void display()
   {
       super.display();
       System.out.println("Department :"+department);
       System.out.println("Salary :$"+salary);
      
       System.out.println("Advisees:");
       for(int i=0;i<advisees.size();i++)
       {
           System.out.println(advisees.get(i).getName());
       }
   }
}

==================================

// TestProg.java

public class TestProg {

   public static void main(String[] args) {

       Student s1 = new Student("Malick", 900, "CS", 3.5);
       Student s2 = new Student("Sumukh", 901, "CS", 3.2);
       Student s3 = new Student("Magarita", 902, "CS", 3.0);

       Professor p1 = new Professor("Marlon", 300, "CS", 80000);
       p1.addAdvisees(s3);
       p1.addAdvisees(s2);
       p1.addAdvisees(s1);

       p1.display();
       if (p1.removeAdvisee("John")) {
           System.out.println("The advisee has been removed");
       } else {
           System.out.println("The advisee cannot be found in the list");
       }

       if (p1.removeAdvisee("Malick")) {
           System.out.println("The advisee has been removed");
       } else {
           System.out.println("The advisee cannot be found in the list");
       }

   }

}

===================================

output:

Name :Marlon
Id :300
Department :CS
Salary :$80000.0
Advisees:
Magarita
Sumukh
Malick
The advisee cannot be found in the list
The advisee has been removed

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
I need help for part B and C 1) Create a new project in NetBeans called...
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
  • Last picture is the tester program! In this Assignment, you will create a Student class and...

    Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...

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

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

  • **Using NetBeans** Lesson Using inheritance to reuse classes. Deliverables person.java student.java studentAthlete.java app.java Using inheritance and...

    **Using NetBeans** Lesson Using inheritance to reuse classes. Deliverables person.java student.java studentAthlete.java app.java Using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

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