Question: I'm having trouble getting this code to work. The only error it says when I'm looking at the code, is for the last part, where

I'm having trouble getting this code to work. The only error it says when I'm looking at the code, is for the last part, where we are declaring the community members in the static void. On the line communityMember 0, it says Multiple markers at this line - Line breakpoint:Hierarchy [line: 231] - main(String[]) - No enclosing instance of type Hierarchy is accessible. Must qualify the allocation with an enclosing instance of type Hierarchy (e.g. x.new A() where x is an instance of Hierarchy). For reference, hierarchy is the name of my class file, hierarchy.java.

Here is the code i'm working with

import java.util.Date; public class Hierarchy {

//CommunityMember.java public abstract class CommunityMember { //instance variables private String firstName; private String lastName; //Constructor public CommunityMember(String firstName, String lastName) { this.firstName=firstName; this.lastName=lastName; } //Returns first name public String getFName() { return firstName; } //Returns last name public String getLName() { return lastName; } @Override public String toString() { return "First Name : "+firstName+ "Last Name : "+lastName; } } //Student.java public class Student extends CommunityMember {

//instance variables private int ID; private double gradeAvg; //Constructor public Student(String firstName, String lastName, int ID, double gradeAvg) throws Exception { super(firstName, lastName); //Calling setter methods setID(ID); setAverage(gradeAvg); } //Set id number public void setID(int ID) throws Exception { if(ID<0 || id>9999999) throw new Exception("Invalid ID number"); else this.ID=ID; } //Set average grade points public void setAverage(double gradeAvg) throws Exception { if(gradeAvg<0.0 || gradeAvg>4.0) throw new Exception("Invalid grade average number"); else this.gradeAvg=gradeAvg; } //Returns id public int getID() { return ID; } //Returns grade average public double gradeAvg() { return gradeAvg; } //Override toString metod @Override public String toString() { return super.toString()+"Student ID : "+ID+" Grade Average : "+gradeAvg; } } //Employee.java public class Employee extends CommunityMember { //instance variables private int ID; private double salary;

//Employee public Employee(String firstName, String lastName,int id, double salary) throws Exception { super(firstName, lastName); //Setter methods setID(ID); setSalary(salary); } //Set method to set ID public void setID(int ID) throws Exception { if(ID<0 || id>9999999) throw new Exception("Invalid Employee ID number"); else this.ID=ID; }

//Set method to set salary public void setSalary(double salary) throws Exception { if(salary<0 || salary>100000) throw new Exception("Invalid salary number"); else this.salary=salary; }

//Returns ID public int getID() { return ID; }

//Returns salary public double gradeSalary() { return salary; } //Override toString metod @Override public String toString() { return super.toString()+ "Employee ID : "+ID+" Salary : "+salary; } } //Alumnus.java public class Alumnus extends CommunityMember { //Set gender type private String gender; //Constructor to set first,last and gender type public Alumnus(String firstName, String lastName, String gender) { super(firstName, lastName); this.gender=gender; } //Override toString metod @Override public String toString() { return super.toString()+" Gender : "+gender; } }

//Staff.java public class Staff extends Employee { //instance variable private String department;

//Constructor public Staff(String firstName, String lastName, int id, double salary, String department) throws Exception { super(firstName, lastName, id, salary); this.department=department; } //Override toString @Override public String toString() { return super.toString()+" Department : "+department; } } //Faculty.java public class Faculty extends Employee { //instance variables private String subject; private Date dateOfJoin; //Constructor public Faculty(String firstName, String lastName, int id, double salary, String subject, Date dateOfJoin) throws Exception { super(firstName, lastName, id, salary); this.subject=subject; this.dateOfJoin=dateOfJoin; } //Override toString metod @Override public String toString() { return super.toString()+" Subject : " +subject+" Date of Join : "+dateOfJoin; } } public static void main(String[] args)throws Exception { //Declare an array of CommunityMember CommunityMember[] communitMember=new CommunityMember[5]; communitMember[0]=new Student("Michell", "Johnson", 1212, 4.0); communitMember[1]=new Employee("Hellen", "Keller", 1323, 5500); communitMember[2]=new Alumnus("Kunal", "Malhotra", "Male"); communitMember[3]=new Faculty("BN", "Achari", 4515, 45000, "Computer Scient", new Date(1988, 5, 25)); communitMember[4]=new Staff("Mahn", "Singh", 1000, 5200, "Non-Teaching"); //Print the objects of student System.out.println("Student Details"); System.out.println(communitMember[0].toString()); System.out.println("Employee Details"); //Print the objects of Alumini System.out.println(communitMember[1].toString()); System.out.println("Alumnus Details"); //Print the objects of Faculty System.out.println(communitMember[2].toString()); System.out.println("Faculty Details"); //Print the objects of Staff System.out.println(communitMember[3].toString()); System.out.println("Staff Details"); //Print the objects of Staff System.out.println(communitMember[4].toString());

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!