JAVA HELP PLEASE!!! You have been given a person class. You are to write an inst
ID: 3721802 • Letter: J
Question
JAVA HELP PLEASE!!!
You have been given a person class. You are to write an instructor class derived from the Person class. Each instructor has a name and an ArrayListof courses he/she teaches. There are four instructors created for you in your main. On each line in your input file there are two strings. The first is the instructors name, and the second is a course. As you input this data add the course to the ArrayList for that instructor. If the input line is:
mehta CMPSC203
then add CMPSC203 to the ArrayList for mehta.
In order to do this, you need to add a method called addCourse to the instructor class. If s is an instructor, then s.add("CMPSC321") adds CMPSC321 to the ArrayList for s1.
You also need a WriteOutput method in the Instructor Class. This will write the name and then all the courses in the ArrayList.
MAIN:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.*;
public class Homework {
public static void main(String[] args) {
//try/catch for input file
//create ArrayLists, initially empty
ArrayList<String> a1=new ArrayList<String>();
ArrayList<String> a2=new ArrayList<String>();
ArrayList<String> a3=new ArrayList<String>();
ArrayList<String> a4=new ArrayList<String>();
//construct instructors
Instructor s1=new Instructor("Appel", a1);
Instructor s2=new Instructor("AlSaeed", a2);
Instructor s3=new Instructor("Mehta", a3);
Instructor s4=new Instructor("Vanderhyde", a4);
//get input and add courses to instructor's ArrayLists
//output names and courses
s1.WriteOutput();
s2.WriteOutput();
s3.WriteOutput();
s4.WriteOutput();
}
}
INSTRUCTOR CLASS:
import java.util.ArrayList;
public class Instructor {
private ArrayList<String> courses;
//constructor
//method addCourse
//method WriteOutput
}
PERSON CLASS:
public class Person {
protected String name;
public Person(String n)
{
name = n;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString()
{return name;}
public void WriteOutput()
{System.out.println(name);}
}
Explanation / Answer
Below is your code. Please let me know in comments if you have any issues
Person.java
public class Person {
protected String name;
public Person(String n) {
name = n;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
public void WriteOutput() {
System.out.println(name);
}
}
Instructor.java
public class Instructor extends Person{
private ArrayList<String> courses;
// constructor
public Instructor(String name,ArrayList<String> list) {
super(name);
courses = list;
}
// method addCourse
public void addCourse(String course) {
courses.add(course);
}
// method WriteOutput
public void WriteOutput() {
super.WriteOutput();
System.out.print("Courses : ");
for (String str : courses) {
System.out.print(str+" ");
}
System.out.println();
}
}
Homework.java
public class Homework {
public static void main(String[] args) {
// try/catch for input file
try {
// create ArrayLists, initially empty
ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>();
ArrayList<String> a3 = new ArrayList<String>();
ArrayList<String> a4 = new ArrayList<String>();
// construct instructors
Instructor s1 = new Instructor("Appel", a1);
Instructor s2 = new Instructor("AlSaeed", a2);
Instructor s3 = new Instructor("Mehta", a3);
Instructor s4 = new Instructor("Vanderhyde", a4);
// get input and add courses to instructor's ArrayLists
Scanner sc = new Scanner(new File("instructors.txt"));
while(sc.hasNextLine()) {
String line = sc.nextLine();
String words[] = line.split(" ");
if(s1.getName().equalsIgnoreCase(words[0])) {
s1.addCourse(words[1]);
} else if(s2.getName().equalsIgnoreCase(words[0])) {
s2.addCourse(words[1]);
} else if(s3.getName().equalsIgnoreCase(words[0])) {
s3.addCourse(words[1]);
} else if(s4.getName().equalsIgnoreCase(words[0])) {
s4.addCourse(words[1]);
}
}
// output names and courses
s1.WriteOutput();
s2.WriteOutput();
s3.WriteOutput();
s4.WriteOutput();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
instructors.txt
mehta CMPSC203
mehta CMPSC242
AlSaeed CPM34234
vanderhyde HGM12345
Output
Appel
Courses :
AlSaeed
Courses : CPM34234
Mehta
Courses : CMPSC203 CMPSC242
Vanderhyde
Courses : HGM12345
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.