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

Objectives: ArrayList File I/O UML diagrams Task: Hogwarts is going digital...an

ID: 3881330 • Letter: O

Question

Objectives:

ArrayList

File I/O

UML diagrams

Task: Hogwarts is going digital...and they need your help!

The school for witchcraft and wizardry needs a program to keep track of their enrollment and houses.

If you're unfamiliar with Harry Potter, Hogwarts is the name of a school instructing young wizards and witches. The school has four "houses", and each student enrolled in the school belongs to one. (There's also a bit about sorting, but we'll assume everyone has a house already). Students are additionally identified by what year they are presently in - new students are in their first year, and so on.

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 );
}
}

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

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.

Rubric:

(10pts) Student.java

(30pts) School.java

(40pts) Correctness - Your program will be tested using different data files.

(10pts) Javadoc comments

(10pts) UML diagram

Submissions which do not compile will receive a maximum of 10 points in total.

Zip text files given (4)

year1

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

year2

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

year3

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

year4

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

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//Student.java

public class Student {

      private String name;

      private int year;

      private String house;

      /**

      * constructor

      *

      * @param name

      *            - student name

      * @param year

      *            - year (1-7)

      * @param house

      *            - house the student belongs to

      */

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

            this.name = name;

            this.year = year;

            this.house = house;

      }

      /**

      * method to return a string representation of student data

      */

      @Override

      public String toString() {

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

      }

      /**

      * getters and setters

      */

      public String getName() {

            return name;

      }

      public void setName(String name) {

            this.name = name;

      }

      public int getYear() {

            return year;

      }

      public void setYear(int year) {

            this.year = year;

      }

      public String getHouse() {

            return house;

      }

      public void setHouse(String house) {

            this.house = house;

      }

}

//School.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class School {

      private String name;

      private int enrollment;

      ArrayList<Student> students;

      /**

      * Constructor

      *

      * @param name

      *            - name of school

      */

      public School(String name) {

            this.name = name;

            /**

            * initializing the list

            */

            students = new ArrayList<Student>();

      }

      /**

      * method to import student data from a file

      *

      * @param fileName

      */

      public void importStudents(String fileName) {

            File file = new File(fileName);

            try {

                  Scanner scanner = new Scanner(file);

                  /**

                  * getting year from file name (you didnt specify how to identify

                  * the year)

                  */

                  int year = stripYearFromFileName(fileName);

                  while (scanner.hasNext()) {

                        String line = scanner.nextLine();

                        /**

                        * splitting line by comma

                        */

                        String[] fields = line.split(",");

                        /**

                        * Creating a Student using the values

                        */

                        Student s = new Student(fields[0].trim(), year,

                                    fields[1].trim());

                        /**

                        * Adding to the list

                        */

                        students.add(s);

                        /**

                        * incrementing the count

                        */

                        enrollment++;

                  }

            } catch (FileNotFoundException e) {

                  System.out.println("File not found");

            }

      }

      /**

      * method to return a list of students in the specified house

      */

      private ArrayList<Student> getHouse(String nameOfHouse) {

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

            /**

            * looping through all students

            */

            for (Student s : students) {

                  if (s.getHouse().equalsIgnoreCase(nameOfHouse)) {

                        /**

                        * if the student is a member of the house specified in the

                        * argument, add to the list to be returned

                        */

                        list.add(s);

                  }

            }

            return list;

      }

      @Override

      public String toString() {

            String data = name + " Enrollment: " + enrollment + " ";

            data += "Gryffindor: ";

            /**

            * Getting all students in Gryffindor house

            */

            for (Student s : getHouse("Gryffindor")) {

                  data += "-" + s + " ";

            }

            data += "Hufflepuff: ";

            /**

            * Getting all students in Hufflepuff house

            */

            for (Student s : getHouse("Hufflepuff")) {

                  data += "-" + s + " ";

            }

            data += "Ravenclaw: ";

            /**

            * Getting all students in Ravenclaw house

            */

            for (Student s : getHouse("Ravenclaw")) {

                  data += "-" + s + " ";

            }

            data += "Slytherin: ";

            /**

            * Getting all students in Slytherin house

            */

            for (Student s : getHouse("Slytherin")) {

                  data += "-" + s + " ";

            }

            return data;

      }

      /**

      * As there is no info about year of a student in the file, this helper

      * method is used to strip year value from the filename considering the file

      * name would be year1.txt, year2.txt etc

      *

      * @param fName

      *            - name of file

      * @return - integer value representing year

      */

      private int stripYearFromFileName(String fName) {

            fName = fName.replaceAll("year", "");

            fName = fName.replaceAll(".txt", "");

            int year = Integer.parseInt(fName.trim());

            return year;

      }

}

//Lab2.java

public class Lab2 {

      public static void main(String[] args) {

            /**

            * Creating the school

            */

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

            /**

            * Importing students from year 1 to year 4

            */

            hogwarts.importStudents("year1.txt");

            hogwarts.importStudents("year2.txt");

            hogwarts.importStudents("year3.txt");

            hogwarts.importStudents("year4.txt");

            /**

            * Displaying the students list in school

            */

            System.out.println(hogwarts);

      }

}

//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

//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