Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED HELP WITH JAVA provided with text files to check the output Objectives: Arr

ID: 3881677 • Letter: N

Question

NEED HELP WITH JAVA

provided with text files to check the output

Objectives:

ArrayList

File I/O

Getting Started

Your application will read in data from four text files: year1.txt, year2.txt, year3.txt, and year4.txt. These can be downloaded as a zip file here - unzip it and place the four files in the top of your project in Eclipse.

To get you started, we've provided a test class, Lab2.java. Your final submission must include this class exactly as it appears here. The only permitted modification to this code is to add Javadoc comments as needed. Once your application is completed, running Lab2.java with the given data files will result in the exact output shown below.

In addition to this class, you will also create School.java and Student.java. All three of these classes must be in the default package in your project.

Lab2.java

public class Lab2 {
public static void main( String[] args ) {
   School hogwarts = new School("Hogwarts School of Witchcraft & Wizardry");

   hogwarts.importStudents("year1.txt");
   hogwarts.importStudents("year2.txt");
   hogwarts.importStudents("year3.txt");
   hogwarts.importStudents("year4.txt");

   System.out.println( hogwarts );
}
}

TEXT FILES

year1.txt

Colin Creevey,Gryffindor
Luna Lovegood,Ravenclaw
Ginny Weasley,Gryffindor
Stewart Ackerley,Ravenclaw
Malcolm Baddock,Slytherin

year2.txt

Harry Potter,Gryffindor
Susan Bones,Hufflepuff
Draco Malfoy,Slytherin
Hermione Granger,Gryffindor
Ronald Weasley,Gryffindor

year3.txt

Cho Chang,Ravenclaw
Katie Bell,Gryffindor
Felicity Eastchurch,Ravenclaw
Oliver Rivers,Hufflepuff
Graham Montague,Slytherin

year4.txt

Cedric Diggory,Hufflepuff
Lee Jordan,Gryffindor
Terence Higgs,Slytherin
Duncan Inglebee,Ravenclaw
Zacharias Smith,Hufflepuff

Student.java

The Student class will represent a student at the school, and will therefore be responsible for the student's name, year information, and the name of their house.

Requirements:

-Class variables: name of the student, year (numeric value from 1 to 7), name of their house.

-Constructor - must take 3 parameters in order: student name, year, and house.

-A standard toString method - refer to the sample output for the format.

-Getter and setter methods for all class variables.

School.java

The School class will represent an entire school, meaning it will track the total enrollment of the school, all students in attendance, the name of the school, and the names of all houses.

Requirements:

-Class variables: name of the school, enrollment (numeric value), an ArrayList of Students, and a String array of house names.

-Constructor - must take only 1 parameter, the name of the school

-Method importStudents - takes only 1 parameter, the name of the file to be read in. Returns nothing, but populates data within the school.

-Method getHouse - takes only 1 parameter, the name of a house, and returns an ArrayList of all students in that house.

-A standard toString method - refer to the sample output for the format.

-Getter and setter methods for all class variables.

Output Hogwarts School of Witchcraft &Wizardry; Enrollment: 20 Gryffindor: - Colin Creevey (Year:1) - Gryffindor - Ginny Weasley (Year:1) - Gryffindor - Harry Potter (Year:2) - Gryffindor - Hermione Granger (Year:2) - Gryffindor - Ronald Weasley (Year:2)- Gryffindor - Katie Bell (Year:3) - Gryffindor - Lee Jordan (Year:4)- Gryffindor Hufflepuff: - Susan Bones (Year:2)Hufflepuff - Oliver Rivers (Year:3) -Hufflepuff - Cedric Diggory (Year:4) Hufflepuff - Zacharias Smith (Year:4) - Hufflepuff Ravenclaw: - Luna Lovegood (Year:l) - Ravenclaw - Stewart Ackerley (Year:1) - Ravenclaw - Cho Chang (Year:3) Ravenclaw - Felicity Eastchurch (Year:3) - Ravenclaw Duncan Inglebee (Year:4) - Ravenclaw Slytherin: - Malcolm Baddock (Year:1) - Slytherin - Draco Malfoy (Year:2)- Slytherin - Graham Montague (Year:3)- Slytherin - Terence Higgs (Year:4) - Slytherin

Explanation / Answer

Student.java

/**

* @author

*

*/

public class Student {

private String name;

private int year;

private String nameOfHouse;

/**

* @param name

* @param year

* @param nameOfHouse

*/

public Student(String name, int year, String nameOfHouse) {

this.name = name;

this.year = year;

this.nameOfHouse = nameOfHouse;

}

/**

* @return

*/

public String getName() {

return name;

}

/**

* @param name

*/

public void setName(String name) {

this.name = name;

}

/**

* @return

*/

public int getYear() {

return year;

}

/**

* @param year

*/

public void setYear(int year) {

this.year = year;

}

/**

* @return

*/

public String getNameOfHouse() {

return nameOfHouse;

}

/**

* @param nameOfHouse

*/

public void setNameOfHouse(String nameOfHouse) {

this.nameOfHouse = nameOfHouse;

}

@Override

public String toString() {

return "- "+name+" (Year: "+year+") - "+nameOfHouse+" ";

}

}

School.java

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;

public class School {

private String nameOfSchool;

private int enrollment;

private ArrayList<Student> students=new ArrayList<Student>();

private String[] houseNames={"Gryffindor","Hufflepuff","Ravenclaw","Slytherin"};

/**

* Constructor with nameOfSchool as parameter

*/

public School(String nameOfSchool) {

this.nameOfSchool = nameOfSchool;

}

/**

* @param inputFile

* Reads the contents from the file and sets the details to Student object

*/

public void importStudents(String inputFile){

try

{

FileReader fr = new FileReader(inputFile);

BufferedReader myInfile = new BufferedReader(fr);

Scanner scan=new Scanner(myInfile);

while(scan.hasNextLine()){

String[] stdDetails=scan.nextLine().split(",");

String name=stdDetails[0];

String nameOfHouse=stdDetails[1];

int year=Integer.parseInt(inputFile.substring(inputFile.length()-5, inputFile.length()-4));

Student student=new Student(name, year, nameOfHouse);

students.add(student);

}

scan.close();

}

catch (IOException e) {

System.err.println("Didn't open " + inputFile);

}

}

/**

* @param nameOfHouse

* @return

* Filters the students based on house name and returns the list of students with given nameOfHouse

*/

public ArrayList<Student> getHouse(String nameOfHouse){

ArrayList<Student> houseStudents=new ArrayList<Student>();

for (Student student : students) {

if(student.getNameOfHouse().equalsIgnoreCase(nameOfHouse)){

houseStudents.add(student);

}

}

return houseStudents;

}

/**

* @return

*/

public String getNameOfSchool() {

return nameOfSchool;

}

/**

* @param nameOfSchool

*/

public void setNameOfSchool(String nameOfSchool) {

this.nameOfSchool = nameOfSchool;

}

/**

* @return

*/

public int getEnrollment() {

return enrollment;

}

/**

* @param enrollment

*/

public void setEnrollment(int enrollment) {

this.enrollment = enrollment;

}

@Override

public String toString() {

StringBuffer sb=new StringBuffer();

sb.append(nameOfSchool+" ");

sb.append("Enrollment: "+students.size()+" ");

for (int i = 0; i < houseNames.length; i++) {

sb.append(houseNames[i]+" ");

for (Student student : getHouse(houseNames[i])) {

sb.append(student.toString());

}

}

return sb.toString();

}

}

Lab2.java

public class Lab2 {

public static void main(String[] args) {

School hogwarts = new School("Hogwarts School of Witchcraft & Wizardry");

hogwarts.importStudents("D:/workspace/Test/src/lab2/year1.txt");

hogwarts.importStudents("D:/workspace/Test/src/lab2/year2.txt");

hogwarts.importStudents("D:/workspace/Test/src/lab2/year3.txt");

hogwarts.importStudents("D:/workspace/Test/src/lab2/year4.txt");

System.out.println(hogwarts);

}

}

TEXT FILES

year1.txt

Colin Creevey,Gryffindor
Luna Lovegood,Ravenclaw
Ginny Weasley,Gryffindor
Stewart Ackerley,Ravenclaw
Malcolm Baddock,Slytherin

year2.txt

Harry Potter,Gryffindor
Susan Bones,Hufflepuff
Draco Malfoy,Slytherin
Hermione Granger,Gryffindor
Ronald Weasley,Gryffindor

year3.txt

Cho Chang,Ravenclaw
Katie Bell,Gryffindor
Felicity Eastchurch,Ravenclaw
Oliver Rivers,Hufflepuff
Graham Montague,Slytherin

year4.txt

Cedric Diggory,Hufflepuff
Lee Jordan,Gryffindor
Terence Higgs,Slytherin
Duncan Inglebee,Ravenclaw
Zacharias Smith,Hufflepuff

Sample Output:

Hogwarts School of Witchcraft & Wizardry
Enrollment: 20
Gryffindor
- Colin Creevey (Year: 1) - Gryffindor
- Ginny Weasley (Year: 1) - Gryffindor
- Harry Potter (Year: 2) - Gryffindor
- Hermione Granger (Year: 2) - Gryffindor
- Ronald Weasley (Year: 2) - Gryffindor
- Katie Bell (Year: 3) - Gryffindor
- Lee Jordan (Year: 4) - Gryffindor
Hufflepuff
- Susan Bones (Year: 2) - Hufflepuff
- Oliver Rivers (Year: 3) - Hufflepuff
- Cedric Diggory (Year: 4) - Hufflepuff
- Zacharias Smith (Year: 4) - Hufflepuff
Ravenclaw
- Luna Lovegood (Year: 1) - Ravenclaw
- Stewart Ackerley (Year: 1) - Ravenclaw
- Cho Chang (Year: 3) - Ravenclaw
- Felicity Eastchurch (Year: 3) - Ravenclaw
- Duncan Inglebee (Year: 4) - Ravenclaw
Slytherin
- Malcolm Baddock (Year: 1) - Slytherin
- Draco Malfoy (Year: 2) - Slytherin
- Graham Montague (Year: 3) - Slytherin
- Terence Higgs (Year: 4) - Slytherin