Hello. I\'m currently working on the last section to my assignment and I\'m running into issues.
I\'ll post a screenshot of the directions provided to us, my 5 classes, and a screenshot of my
console output. Can you help me fix my code? I don\'t know where it\'s going wrong and I can\'t
figure it out. Please leave comments on what I did wrong so I can learn from it. Thank you so
much!
Here is the instructions from the pdf for reference on what I\'m trying to accomplish.
package campus;
public class Person {
private String id;
private String lastName;
private String firstName;
public Person(String id, String last, String first)
{
this.id = id;
this.lastName = last;
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getId() {
return id;
}
public String toString()
{
return id + \": \" + firstName + \" \" + lastName;
}
}
------
---Student.java---
package campus;
public class Student extends Person {
private String major;
private int level;
private Person obj;
public Student(String id, String last, String first, String focus, int level) {
super(id, last, first);// calling parent constructor
this.major = focus;
this.level = level;
obj = new Person(id, last, first);
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
@Override
public String toString() { // override toString() method
return \"id: \" + obj.getId() + \", Name: \" + obj.getFirstName() + \" \" + obj.getLastName()
+ \", major is \"
+ this.major + \" and their level is \" + this.level;
}
}
------
---Faculty.java---
package campus;
public class Faculty extends Person {
private String dept;
private String rank;
private Person obj;
public Faculty(String id, String last, String first, String focus, int level) {
super(id, last, first);// calling parent constructor
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getRank() {
return rank;
}
public void setRank(int level) {
this.rank = rank;
}
@Override
public String toString() { // override toString() method
return \"id: \" + obj.getId() + \", Name: \" + obj.getFirstName() + \" \" + obj.getLastName()
+ \", deptartment is \"
+ this.dept + \" and their rank is \" + this.rank;
}
}
------
---Section.java---
package campus;
public class Section {
private String id;
private Faculty instructor;
private Student[] enrolled;
private int numofEnroll;
private int capacity;
private String location;
private String time;
private String semester;
public Section(String id, Faculty instructor, int capacity, String location, String time, String
semester) {
}
public Section(String id, int capacity) {
this.id = id;
this.capacity .
Historical philosophical, theoretical, and legal foundations of special and i...
Hello. Im currently working on the last section to my assignment a.pdf
1. Hello. I'm currently working on the last section to my assignment and I'm running into issues.
I'll post a screenshot of the directions provided to us, my 5 classes, and a screenshot of my
console output. Can you help me fix my code? I don't know where it's going wrong and I can't
figure it out. Please leave comments on what I did wrong so I can learn from it. Thank you so
much!
Here is the instructions from the pdf for reference on what I'm trying to accomplish.
package campus;
public class Person {
private String id;
private String lastName;
private String firstName;
public Person(String id, String last, String first)
{
this.id = id;
this.lastName = last;
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getId() {
return id;
}
2. public String toString()
{
return id + ": " + firstName + " " + lastName;
}
}
------
---Student.java---
package campus;
public class Student extends Person {
private String major;
private int level;
private Person obj;
public Student(String id, String last, String first, String focus, int level) {
super(id, last, first);// calling parent constructor
this.major = focus;
this.level = level;
obj = new Person(id, last, first);
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
@Override
public String toString() { // override toString() method
return "id: " + obj.getId() + ", Name: " + obj.getFirstName() + " " + obj.getLastName()
+ ", major is "
+ this.major + " and their level is " + this.level;
}
3. }
------
---Faculty.java---
package campus;
public class Faculty extends Person {
private String dept;
private String rank;
private Person obj;
public Faculty(String id, String last, String first, String focus, int level) {
super(id, last, first);// calling parent constructor
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getRank() {
return rank;
}
public void setRank(int level) {
this.rank = rank;
}
@Override
public String toString() { // override toString() method
return "id: " + obj.getId() + ", Name: " + obj.getFirstName() + " " + obj.getLastName()
+ ", deptartment is "
+ this.dept + " and their rank is " + this.rank;
}
}
------
---Section.java---
package campus;
public class Section {
private String id;
private Faculty instructor;
4. private Student[] enrolled;
private int numofEnroll;
private int capacity;
private String location;
private String time;
private String semester;
public Section(String id, Faculty instructor, int capacity, String location, String time, String
semester) {
}
public Section(String id, int capacity) {
this.id = id;
this.capacity = capacity;
this.numofEnroll = 0;
this.enrolled = new Student[capacity];
}
public String getId() {
return id;
}
public Faculty getInstructor() {
return instructor;
}
public void setInstructor(Faculty instructor) {
this.instructor = instructor;
}
public Student[] getEnrolled() {
return enrolled;
}
public int getNumofEnroll() {
return numofEnroll;
}
public int getCapacity() {
return capacity;
}
public String getLocation() {
return location;
}
5. public void setLocation(String location) {
this.location = location;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSemester() {
return semester;
}
public void enrollStudent(Student newStudent) {
// check is capacity is equal to enrolled list
if (numofEnroll == capacity) {
System.out.println("Section capacity is full");
} else {
enrolled[numofEnroll] = newStudent;
numofEnroll++;
}
}
public void removeStudent(Student newStudent) {
// first check if the student is exist in the section or not
boolean found = false;
for (int i = 0; i < enrolled.length; i++) {
if (enrolled[i].getId().equalsIgnoreCase(newStudent.getId())) {
// found, now delete the student
enrolled[i] = null;
found = true;
}
}
if (!found)
System.out.println("requested Student is not found in the section");
}
public void displayStudents() {
for (int i = 0; i < enrolled.length; i++) {
6. if (enrolled[i] != null) {
System.out.println(enrolled[i]);
}
}
}
public void printRoster() {
System.out.println("Section: " + id + " " + "Instructor: " + instructor + " Capacity: " +
capacity
+ " Enrolled: " + numofEnroll + " Location: " + " Semester: " + semester + "
Meeting time: "
+ time + " Student's ID Student's Name Student's Major Student's Level");
}
}
------
---TestCampus.java---
package campus;
public class TestCampus {
public static void main(String[] args) {
Student stu1 = new Student("w3098537", "Wareberg", "James", "CSIS", 4);
Student stu2 = new Student("w1111111", "Damon", "Matt", "Theatre Arts", 3);
Student stu3 = new Student("w2222222", "Wahlberg", "Mark", "Theatre Arts", 3);
Student stu4 = new Student("w3333333", "Rowling", "Joanne", "English", 4);
Student stu5 = new Student("w4444444", "Beckham", "David", "Exercise and Sport
Science", 3);
Student stu6 = new Student("w555555", "Obama", "Barack", "Political Science", 5);
Faculty fac1 = new Faculty("w3029699", "Dai", "Ruxin", "CSIS", 5);
Section section = new Section("235-1", fac1, 5, "North Hall", "8:00am", "Spring");
section.printRoster();
section.enrollStudent(stu1);
section.enrollStudent(stu2);
section.enrollStudent(stu3);
section.enrollStudent(stu4);
section.enrollStudent(stu5);
section.enrollStudent(stu6);
section.displayStudents();
// now delete the student from enroll list
7. System.out.println("removing student: " + stu1);
section.removeStudent(stu1);
System.out.println(" All Student in the Section:");
System.out.println("---------------------------");
section.displayStudents();
}
}
------
My output when I compile the program. 4. Define a class Section, which has a Faculty as
instructor and a collection of students enrolled. The class should contain: Instance variables: id
(String), instructor (Faculty), enrolled (Student0), numo Enroll (int), capacity (int), location
(String), time (String), semester (String) Constructor public Section (String id Faculty instructor,
int capacity String location, String time, String semester) Note: the above constructor is used to
create a section with determined id, instructor, capacity, location, time and semester, but no
enrolled student yet.
Solution
Following mistakes found in your code:
1. In Section.java class the two filed constructor is not called to initialize below files
this.id = id;
this.capacity = capacity;
this.numofEnroll = 0;
this.enrolled = new Student[capacity];
Due to uninitialized id,capacity Y enrolled filed cause Null pointer exception.
2. Also in Person.java & Student.java class you had created instance of Person class as
composition
but its not good practice to have it as inheritance gives you those filed.
3. In Faculty.java class setRank method had mistakes.
I think this will solve your problem.
Following is corrected java code:
//Person.java file
package campus;
public class Person {
private String id;
private String lastName;
8. private String firstName;
public Person(String id, String last, String first) {
this.id = id;
this.lastName = last;
this.firstName = first;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getId() {
return id;
}
public String toString() {
return id + ": " + firstName + " " + lastName;
}
}
//Student.java file
package campus;
public class Student extends Person {
private String major;
private int level;
public Student(String id, String last, String first, String focus, int level) {
super(id, last, first);// calling parent constructor
this.major = focus;
this.level = level;
}
public String getMajor() {
9. return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
@Override
public String toString() { // override toString() method
return super.getId() + "tt" + super.getFirstName() + " " + super.getLastName() +
"tt"+ this.level+"tt" +this.major;
}
}
//Faculty.java file
package campus;
public class Faculty extends Person {
private String dept;
private int rank;
//The dept & rank parameters initialized
public Faculty(String id, String last, String first, String dept, int rank) {
super(id, last, first);// calling parent constructor
this.dept=dept;
this.rank=rank;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public int getRank() {
10. return rank;
}
// Here setRank was wrong. Corrected it
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() { // override toString() method
return "id: " + super.getId() + ", Name: " + super.getFirstName() + " " +
super.getLastName() + ", deptartment is "
+ this.dept + " and their rank is " + this.rank;
}
}
//Section.java file
package campus;
public class Section {
private String id;
private Faculty instructor;
private Student[] enrolled;
private int numofEnroll;
private int capacity;
private String location;
private String time;
private String semester;
public Section(String id, Faculty instructor, int capacity, String location, String time, String
semester) {
//Here calling to two field constructor to create
//student enrolled array with size capacity.
this(id, capacity);
this.instructor=instructor;
this.location=location;
this.time=time;
this.semester=semester;
}
public Section(String id, int capacity) {
this.id = id;
11. this.capacity = capacity;
this.numofEnroll = 0;
this.enrolled = new Student[capacity];
}
public String getId() {
return id;
}
public Faculty getInstructor() {
return instructor;
}
public void setInstructor(Faculty instructor) {
this.instructor = instructor;
}
public Student[] getEnrolled() {
return enrolled;
}
public int getNumofEnroll() {
return numofEnroll;
}
public int getCapacity() {
return capacity;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSemester() {
return semester;
12. }
public void enrollStudent(Student newStudent) {
// check is capacity is equal to enrolled list
if (numofEnroll == capacity) {
System.out.println("Section capacity is full");
} else {
enrolled[numofEnroll] = newStudent;
numofEnroll++;
}
}
public void removeStudent(Student newStudent) {
// first check if the student is exist in the section or not
boolean found = false;
for (int i = 0; i < enrolled.length; i++) {
if (enrolled[i].getId().equalsIgnoreCase(newStudent.getId())) {
// found, now delete the student
enrolled[i] = null;
found = true;
}
}
if (!found)
System.out.println("requested Student is not found in the section");
}
public void displayStudents() {
for (int i = 0; i < enrolled.length; i++) {
if (enrolled[i] != null) {
System.out.println(enrolled[i]);
}
}
}
public void printRoster() {
System.out.println("Section: " + id + "t" + "Instructor: Name-" + instructor.getFirstName()
+" and Dept-"+instructor.getDept()+ " Capacity: " + capacity
+ "tEnrolled: " + numofEnroll + " Location: "+location + "tSemester: " + semester + "
Meeting time: "
+ time);
13. }
}
//TestCampus.java
package campus;
public class TestCampus {
public static void main(String[] args) {
Student stu1 = new Student("w3098537", "Wareberg", "James", "CSIS", 4);
Student stu2 = new Student("w1111111", "Damon", "Matt", "Theatre Arts", 3);
Student stu3 = new Student("w2222222", "Wahlberg", "Mark", "Theatre Arts", 3);
Student stu4 = new Student("w3333333", "Rowling", "Joanne", "English", 4);
Student stu5 = new Student("w4444444", "Beckham", "David", "Exercise and Sport
Science", 3);
Student stu6 = new Student("w555555", "Obama", "Barack", "Political Science", 5);
Faculty fac1 = new Faculty("w3029699", "Dai", "Ruxin", "CSIS", 5);
Section section = new Section("235-1", fac1, 5, "North Hall", "8:00am", "Spring");
section.enrollStudent(stu1);
section.enrollStudent(stu2);
section.enrollStudent(stu3);
section.enrollStudent(stu4);
section.enrollStudent(stu5);
section.enrollStudent(stu6);
System.out.println("-------------------------------------------------------------------------------------------
-");
section.printRoster();
System.out.println(" Student's IDttStudent's NamettStudent's LeveltStudent's
Major");
section.displayStudents();
// now delete the student from enroll list
System.out.println("Removing student:");
System.out.println("Student's IDttStudent's NamettStudent's LeveltStudent's Major");
System.out.println(stu1);
section.removeStudent(stu1);
System.out.println(" All Student in the Section:");
System.out.println("-------------------------------------------------------------------------------------------
-");
14. System.out.println("Student's IDttStudent's NamettStudent's LeveltStudent's
Major");
section.displayStudents();
}
}
Steps to execute the program:
1. Complie all java file using javac command
2. Run progarm using java command
Output of the program:
Section capacity is full
--------------------------------------------------------------------------------------------
Section: 235-1 Instructor: Name-Ruxin and Dept-CSIS
Capacity: 5 Enrolled: 5
Location: North Hall Semester: Spring
Meeting time: 8:00am
Student's ID Student's Name Student's Level Student's Major
w3098537 James Wareberg 4 CSIS
w1111111 Matt Damon 3 Theatre Arts
w2222222 Mark Wahlberg 3 Theatre Arts
w3333333 Joanne Rowling 4 English
w4444444 David Beckham 3 Exercise and Sport Science
Removing student:
Student's ID Student's Name Student's Level Student's Major
w3098537 James Wareberg 4 CSIS
All Student in the Section:
--------------------------------------------------------------------------------------------
Student's ID Student's Name Student's Level Student's Major
w1111111 Matt Damon 3 Theatre Arts
w2222222 Mark Wahlberg 3 Theatre Arts
w3333333 Joanne Rowling 4 English
w4444444 David Beckham 3 Exercise and Sport Science