Knowee
Questions
Features
Study Tools

For the base public class Person example provided in the Unit 3 Revisiting the Employee Class Program tutorial, what if you need to add a subclass for Volunteers who have a host employee and are assigned to a project.What code segment could implement this subpublic class and create an instance of it called volunteer_1?public class Volunteer extends Person { private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project) { this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer{ private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project){ super(firstName,lastName,jobTitle); this.host = host; this.project = project; } public String getHost(){ return "Host " + this.host; } public void setHost(String newhost){ this.host = newhost; } public String getProject(){ return "Project " + this.project; } public void setProject(String project){ this.project = project; } } Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer extends Person { private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(firstName,lastName,jobTitle); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project){ this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer extends Person{ private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(firstName,lastName,jobTitle,host,project); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project) { this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");

Question

For the base public class Person example provided in the Unit 3 Revisiting the Employee Class Program tutorial, what if you need to add a subclass for Volunteers who have a host employee and are assigned to a project.What code segment could implement this subpublic class and create an instance of it called volunteer_1?public class Volunteer extends Person { private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project) { this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer{ private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project){ super(firstName,lastName,jobTitle); this.host = host; this.project = project; } public String getHost(){ return "Host " + this.host; } public void setHost(String newhost){ this.host = newhost; } public String getProject(){ return "Project " + this.project; } public void setProject(String project){ this.project = project; } } Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer extends Person { private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(firstName,lastName,jobTitle); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project){ this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer extends Person{ private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(firstName,lastName,jobTitle,host,project); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project) { this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct code to implement the Volunteer subclass and create an instance of it called volunteer_1 would be:

public class Volunteer extends Person {  
    private String host;  
    private String project;  

    public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) {    
        super(firstName,lastName,jobTitle);    
        this.host = host;    
        this.project = project;  
    }   

    public String getHost() {    
        return "Host " + this.host;  
    }   

    public void setHost(String newhost) {    
        this.host = newhost;  
    }   

    public String getProject() {    
        return "Project " + this.project;  
    }   

    public void setProject(String project){    
        this.project = project;  
    } 
} 

Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");

This code first defines the Volunteer class as a subclass of Person. It then adds two additional fields, host and project, to store the host employee and assigned project for each volunteer. The constructor for the Volunteer class takes these two additional parameters and uses the super keyword to call the constructor of the Person class for the remaining parameters. The get and set methods for the host and project fields are then defined. Finally, an instance of the Volunteer class is created with the name volunteer_1.

This problem has been solved

Similar Questions

Design a class named Person and its two subclasses named Student and Employee.  Make Faculty and Staff subclasses of Employee. A person has a name,  address, phone_number, and e-mail address. A student has a status (freshman, sophomore, junior, or senior).  An employee  has an office, salary. A faculty member has office_hours and a rank. A staff member has a title. Override the toString() method in each of these classes to display their details. Write a Java application and subsequent pseudocode to implement/simulate the same.

For the subclass Employee of the base class Person example provided in the tutorial, what if we need to keep track of the department for each employee? What code segment could be used to properly initialize, and add methods to set and get this value? a.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title, department) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear self.department = department def get_department(self): return "Department: " + str(self.department) b.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear self.department = department def get_department(self): return "Department: " + str(self.department) c.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear emp_department = department def get_department(self): return "Department: " + str(self.department) d.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear self.department = department def get_department(): return "Department: " + str(self.department)

21. Write a Java program that creates a class hierarchy for employees of a company. The base class should be Employee, with subclasses Manager, Developer, and Programmer. Each subclass should have properties such as name, address, salary, and job title. Implement methods for calculating bonuses, generating performance reports, and managing projects

Main.java:3: error: class Person is public, should be declared in a file named Person.javapublic class Person { ^Main.java:15: error: class Student is public, should be declared in a file named Student.javapublic class Student extends Person { ^Main.java:24: error: class Employee is public, should be declared in a file named Employee.javapublic class Employee extends Person { ^Main.java:34: error: class Faculty is public, should be declared in a file named Faculty.javapublic class Faculty extends Employee { ^Main.java:44: error: class Staff is public, should be declared in a file named Staff.javapublic class Staff extends Employee { ^5 errors

How would you instantiate the Employee class from a main method located in another class?  public class Employee{    private String name;    private double salary;     public Employee(String n, double s){      name = n;      salary = s;    } }  Employee emp1 = new Employee("Syam", 50000);  Employee emp1 = new Employee(50000);  Employee emp1 = new Employee();  Employee emp1 = new Employee(50000, "Syam");

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.