Class Two
Hogwarts:
This class is a subclass to School. Its purpose is to serve as the school of Hogwarts and provide methods that are specific to the fictional school. For example there is a method that tells you whether a student or teacher is part of Dumbledore's Army and a method that can tell you who is the wisest wizard at school based on their age. This class also implements the interface Enrollment which provides a method that relates to enrolling in the school of Hogwarts. For more information about the different methods in this class, refer to the comments made in my code.
Code
import java.util.ArrayList;//imports ArrayList
public class Hogwarts extends School implements Enrollment
{//opens class
private ArrayList <Wizard> wizards;//creates an private ArrayList called wizards
private ArrayList <String> dumbledoresArmy;//creates a private ArrayList called dumbledoresArmy
private Wizard [] students;//creates a private array called students
public Hogwarts()//creates zero argument constructor
{
wizards = new ArrayList <Wizard>();//initializes the ArrayList wizards
dumbledoresArmy = new ArrayList <String>();//initializes the ArrayList dumbledoresArmy
students = new Wizard [7];//initializes the array students
wizards.add(new Wizard("Harry", "Potter", 16, "Phoenix feather", "Gryffindor", false, false, "Gryffindor common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Hermione", "Granger", 16, "Dragon Heartstring", "Gryffindor",false, false, "Gryffindor common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Ron", "Weasley", 16, "Unicorn hair", "Gryffindor", false, false, "Gryffindor common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Severus", "Snape", 36, "Dragon heartstring","Slytherin", true, false, "Potion's classroom", new String [] {"Does not", " take ", "classes."}));
wizards.add(new Wizard("Draco", "Malfoy", 16, "Unicorn hair","Slytherin", false, false, "Slytherin common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Luna", "Lovegood", 16, "Unicorn hair","Ravenclaw", false, false, "Ravenclaw common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Neville", "Longbottom", 16, "Unicorn hair", "Gryffindor",false, false, "Gryffindor common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Cedric", "Diggory", 16, "Unicorn hair","Hufflepuff", false, false, "Hufflepuff common room", new String [] {"Transfiguration, ", " Potions, ", "Defense Against the Dark Arts."}));
wizards.add(new Wizard("Voldemort", "", 69, "Phoenix feather", "Slytherin", false, false, "Secret location", new String [] {"Does not", " take ", "classes."}));
wizards.add(new Wizard("Minerva", "McGonagall", 75, "Dragon heartstring", "Gryffindor",true, false, "Transfiguration classroom", new String [] {"Does not", " take ", "classes."}));
wizards.add(new Wizard("Albus", "Dumbledore", 114, "Thestral tail hair", "Gryffindor",true,false, "Dumbledore's office", new String [] {"Does not", " take ", "classes."}));
//adds multiple different Wizard objects into the array wizards
students[0] = wizards.get(0);
students[1] = wizards.get(1);
students[2] = wizards.get(2);
students[3] = wizards.get(4);
students[4] = wizards.get(5);
students[5] = wizards.get(6);
students[6] = wizards.get(7);
//sets the array students to different values in the ArrayList wizards
//this sets the array students to all the students at Hogwarts and not the teachers
dumbledoresArmy.add(wizards.get(0).getFirstName());
dumbledoresArmy.add(wizards.get(1).getFirstName());
dumbledoresArmy.add(wizards.get(2).getFirstName());
dumbledoresArmy.add(wizards.get(5).getFirstName());
dumbledoresArmy.add(wizards.get(6).getFirstName());
dumbledoresArmy.add(wizards.get(7).getFirstName());
//This sets dumbledoresArmy to all the students that are in dumbledore's Army in Hogwarts
}
public String getDumbledoresArmy()
{
String output = new String();
for (int i = 0; i<6; i++)
{
output = output + dumbledoresArmy.get(i) + "\n";
}//for loop that traverses the ArrayList dumbledoresArmy
return output;
}//a method that traverses the ArrayList dumbledoresArmy with a for loop and returns the values
public String getStudents()
{
String output = new String();
for (int i = 0; i<students.length; i++)
{
output = output + students[i]+ "\n";
}
return output;
}//a method that traverses the array
public String getWizards()
{
String wizardArrayValues = "";//creates a new string
int i = 0;//creates a counter for the do-while loop
do
{
wizardArrayValues = wizardArrayValues + wizards.get(i) + "\n";
i++;
}while(i<wizards.size());//a do-while loop that traverses the array wizards for its values
return wizardArrayValues;
}//a method that uses a do-while loop to return the values of the ArrayList wizards
public String toString()
{
String output = new String();//creates a new string called output
output = "Members of Dumbledore's Army: \n" + getDumbledoresArmy() + "\n\nAll wizards: \n" + getWizards();
return output;
}//a toString method that organizes the information and methods made and returns them neatly
public String defeatTheDeathEaters()
{
String output = new String();//creates a new string
int numberOfMembers = 0;//creates a counter for the for loop
for (int i = 0; i < wizards.size(); i++)
{
if(wizards.get(i).getAge()== 16 && ((wizards.get(i).getHouse().equals("Slytherin"))==false))
{
numberOfMembers++;
}//adds a member count to dumbledore's army if the Wizard object is not part of the Slytherin house and is of age 16
}//runs as long as the size of wizards is greater than the value of i
if (numberOfMembers>3)
{
output = output + "Yes! Dumbledore's army has defeated the Death Eaters!";
}//if dumbledore's army has more than three members than it is able to defeat the death eaters
else if (numberOfMembers<=3)
{
output = output + "No. Dumbledore's army has failed to defeat the Death Eaters.";
}//if dumbledore's army has less than or exactly three members than it is not able to defeat the death eaters
return output;
}//a method that states whether or not Dumbledore's Army defeated Voldemort's death eaters
public String getHarrysLocation()
{
return wizards.get(0).getLocation();
}//a method that returns the location of Harry Potter
public void apparationForHarry()//one part of the pair of the overloaded method
{
if (wizards.get(0).getLocation().equals("Gryffindor common room"))
{
wizards.get(0).setLocation("Dining hall");
}//if Harry is in the Gryffindor common room then he apparates to the dining hall when this method is called
}//this method simulates apparation for the character Harry Potter and changes his location depending on the conditions in the if-else statement
public void apparationForHarry(String location)//the second part of the pair of the overloaded method
{
if(wizards.get(0).getLocation().equals(location))
{
wizards.get(0).setLocation("Hogsmeade");
}//if Harry's location is equal to what it is set to in the parameter, then he apparates to Hogsmeade
else
{
wizards.get(0).setLocation("Dumbledore's office");
}//if Harry's location is not equal to what it is set to in the parameter, then he apparates to Dumbledore's office
}//this method states that depending on what the location is set to, Harry's location changes
public String Battle(Wizard x, Wizard y)
{
String battleResults = new String();
if (x.getWandType().equals("Phoenix feather") && y.getWandType().equals("Dragon heartstring"))
{
y.setInjured("true");//sets the loser to injured
battleResults = x.getFirstName() + " has won this battle. "+x.getFirstName()+" has a phoenix feather wand which beats " + y.getFirstName() + "'s wand, which was the dragon heartstring type.";
}//The phoenix feather wand type beats all other wand types in a battle
else if (x.getWandType().equals("Phoenix feather") && y.getWandType().equals("Unicorn hair"))
{
y.setInjured("true");//sets the loser to injured
battleResults = x.getFirstName() + " has won this battle. "+x.getFirstName()+" has a phoenix feather wand which beats " + y.getFirstName() + "'s wand, which was the unicorn hair type.";
}//The phoenix feather wand type beats all other wand types in a battle
else if (((x.getWandType().equals("Phoenix feather") && x.getWandPower()>y.getWandPower())&& (y.getWandType().equals("Phoenix feather") && y.getWandPower()<x.getWandPower())))
{
y.setInjured("true");//sets the loser to injured
battleResults = x.getFirstName() + " has won the battle. "+x.getFirstName()+" has a phoenix feather wand, like " + y.getFirstName() + ", but " +x.getFirstName()+ "'s wand had more wand power.";
}//if two wizards have the same wand types, then their wand power determines their results.
else if ((x.getWandType().equals("Phoenix feather") && x.getWandPower()<y.getWandPower()) && (y.getWandType().equals("Phoenix feather") && y.getWandPower()>x.getWandPower()))
{
x.setInjured("true");//sets the loser to injured
battleResults = y.getFirstName() + " has won the battle. "+y.getFirstName()+" has a phoenix feather wand, like " + x.getFirstName() + ", but " + y.getFirstName() + "'s wand had more wand power.";
}//if two wizards have the same wand types, then their wand power determines their results.
else if (x.getWandType().equals("Dragon heartstring") && y.getWandType().equals("Phoenix feather"))
{
x.setInjured("true");//sets the loser to injured
battleResults = y.getFirstName() + " has won this battle. "+y.getFirstName()+" has a phoenix feather wand which beats " + x.getFirstName() + "'s wand, which is the dragon heartstring type.";
}//Phoenix feather beats the dragon heartstring type
else if (x.getWandType().equals("Dragon heartstring") && y.getWandType().equals("Unicorn hair"))
{
y.setInjured("true");//sets the loser to injured
battleResults = x.getFirstName() + " has won this battle. "+x.getFirstName()+" have a dragon heartstring wand which beats " + y.getFirstName() + "'s wand which is the unicorn hair type.";
}//dragon heartstring beats the unicorn hair type
else if (((x.getWandType().equals("Dragon heartstring") && x.getWandPower()> y.getWandPower()) && (y.getWandType().equals("Dragon heartstring") && y.getWandPower()<x.getWandPower())))
{
y.setInjured("true");//sets the loser to injured
battleResults = x.getFirstName() + " has won the battle. "+x.getFirstName()+" has a dragon hearstring wand, like " + y.getFirstName() + ", but " +x.getFirstName()+ "'s wand had more wand power.";
}//if two wizards have the same wand types, then their wand power determines their results.
else if ((x.getWandType().equals("Dragon heartstring") && x.getWandPower()<y.getWandPower()) && (y.getWandType().equals("Dragon heartstring") && y.getWandPower()>x.getWandPower()))
{
x.setInjured("true");//sets the loser to injured
battleResults = y.getFirstName() + " has won the battle. "+x.getFirstName()+" has a dragon hearstring wand, like " + x.getFirstName() + ", but " +y.getFirstName()+ "'s wand had more wand power.";
}//if two wizards have the same wand types, then their wand power determines their results.
else if (x.getWandType().equals("Unicorn hair") && y.getWandType().equals("Phoenix feather"))
{
x.setInjured("true");//sets the loser to injured
battleResults = y.getFirstName() + " has won this battle. "+y.getFirstName()+" has a phoenix feather wand which beats " + x.getFirstName() + "'s wand, which is the unicorn hair type.";
}//phoenix feather beats the unicorn hair wand type
else if (x.getWandType().equals("Unicorn hair") && y.getWandType().equals("Dragon heartstring"))
{
x.setInjured("true");//sets the loser to injured
battleResults = y.getFirstName() + " has won this battle. "+y.getFirstName()+" has a dragon heartstring wand which beats " + x.getFirstName() + "'s wand which is the unicorn hair type.";
}//dragon heartstring beats the unicorn hair wand type
else if (((x.getWandType().equals("Unicorn hair") && x.getWandPower()> y.getWandPower()) && (y.getWandType().equals("Unicorn hair") && y.getWandPower()<x.getWandPower())))
{
y.setInjured("true");//sets the loser to injured
battleResults = x.getFirstName() + " has won the battle. "+x.getFirstName()+" has a unicorn hair wand, like " + y.getFirstName() + ", but " +x.getFirstName()+ "'s wand had more wand power.";
}//if two wizards have the same wand types, then their wand power determines their results.
else if ((x.getWandType().equals("Unicorn hair") && x.getWandPower()<y.getWandPower()) && (y.getWandType().equals("Unicorn hair") && y.getWandPower()>x.getWandPower()))
{
x.setInjured("true");//sets the loser to injured
battleResults = y.getFirstName() + " has won the battle. "+y.getFirstName()+" has a unicorn hair wand, like " + x.getFirstName() + ", but " +y.getFirstName()+ "'s wand had more wand power.";
}//if two wizards have the same wand types, then their wand power determines their results.
return battleResults;
}//this method returns the results of a battle between to Wizard objects depending on their wand type and sometimes their wand power, depending on the situation
public String wisestWizard()
{
int max = wizards.get(0).getAge();//sets the maximum value to the first age value in thea ArrayList wizards
String wisestWizard = "";//creates an empty string
for (int i = 0; i<wizards.size(); i++)
{
if( wizards.get(i).getAge() > max )
{
max = wizards.get(i).getAge();
}//if the wizard of index i's age is greater than the maximum, it gets set as the new maximum value
}//this for loops sets the maximum value to the largest age
for ( int index = 0; index<wizards.size(); index++)
{
if (max == wizards.get(index).getAge())
{
wisestWizard = wisestWizard + wizards.get(index).getFirstName() + " " + wizards.get(index).getLastName() + " is the wisest wizard.";;
}//if the wizard has the largest age, then they are the wisest wizard
}//this loop sets the wisest wizard to the wizard with the largest age
return wisestWizard;
}//find the wizest wizard depending on the oldest age (finds maximum age in the ArrayList)
public String wizardStudentClasses()
{
String output = new String();//creates a new string
for (int i = 0; i<students.length; i++)
{
output = output + students[i].getFirstName() + ": \n";
for (int index = 0; index < students[i].getClassLength(); index++)
{
output = output + students[i].getClasses()[index];
}//traverses the student of index i's classes and sets the output equal to their classes
output+="\n";//adds a line to the output for organization purposes
}//traverses the array students
return output;
}//this method uses a nested for loop to return the classes each student at Hogwarts takes
public String takingOWLExams()
{
String output = new String();//creates a new string
ArrayList <String> passingWizards = new ArrayList <String> ();//creates a new ArrayList called passingWizards
for (int i = 0; i<students.length; i++)
{
if (students[i].getFirstName().contains("a")||students[i].getLastName().contains("a"))
{
passingWizards.add(students[i].getFirstName());
}//if the student of index i's first name or last name contains the letter "a", they pass their exams. I chose the letter "a" because it represents an "A" grade in this situation
}//traverses the array students
System.out.println("Students that passed their OWL Exams:" );//prints out this line for organization purposes
for (int i = 0; i<5; i++)
{
output = output+ passingWizards.get(i) + "\n";
}//traverses the ArrayList passingWizards and sets it to the output
return output;
}//this method returns the names of the students who passed their OWL exams
public void enrollForClass()
{
System.out.println("Enrollment and tuition for Hogwarts is free.");
}//this method comes from the interface Enrollment that prints this statement to let students of Hogwarts know that tuition is free.
public String ronAndHermioneLove()
{
String coupleName = new String();//creates a new String
coupleName = (wizards.get(2).getFirstName().concat(wizards.get(1).getFirstName().substring(4)));//concatenates ron's name with the ending of hermione's name
return coupleName;
}//this method returns ron and hermione's couple name because they are the two love birds at Hogwarts
}