Class Three
Wizard:
This class is a subclass of Hogwarts because it shows that Hogwarts has wizards. The purpose of this class is to provide methods that would relate to a wizard attending the school of Hogwarts. For example, there is a method that tells you what house they are in, what type of wand they have, and even how much power the wand has. For more information about the different methods in this class, refer to the comments made in my code.
Code
import javax.swing.JOptionPane;//this imports the JOptionPane for user data
import java.util.*;//imports ArrayList
public class Wizard
{//opens class
private String firstName;//creates an instance variable called firstName
private String lastName;//creates an instance variable called lastName
private int age;//creates an instance variable called age
private String wandType;//creates an instance variable called wandType
private String house;//creates an instance variable called house
private boolean isAProfessor;//creates an instance variable called isAProfessor
private double wandPower;//creates an instance variable called wandPower
private boolean injured;//creates an instance variable called injured
private String location;//creates an instance variable called location
private String[] classes;//creates an instance variable called classes
public Wizard()//creates a zero-argument constructor
{
firstName = "Dean";
lastName = "Thomas";
age = 16;
wandType = "Dragon hearstring";
house = "Gryffindor";
isAProfessor = false;
wandPower = Math.pow((Math.random()*51),2);
injured = false;
location = "Their house common room";
classes = new String[3];
classes[0] = " -Transfiguration ";
classes[1] = " -Potions";
classes[2] = " -Defense Against the Dark Arts ";
//sets the instance variables to default values
}
public int getClassLength()
{
return classes.length;
//returns the length of the array classes
}
public Wizard (String firstName, String lastName, int age,
String wandType, String house, boolean isAProfessor, boolean injured, String location, String[] classes
)//creates a multi-argument constructor
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.wandType = wandType;
this.house = house;
this.isAProfessor = isAProfessor;
this.wandPower = (int)(Math.random()*51);//casts the value to an integer by rounding it
this.injured = injured;
this.location = location;
this.classes = classes;
//resets the instance variables to its default values
}
public void setFirstName (String firstName)
{
this.firstName = firstName;
}//a setter method for the firstName of the object
public String getFirstName()
{
return firstName;
}//a getter method that returns the firstName value
public void setLastName(String lastName)
{
this.lastName = lastName;
}//a setter method for the lastName of the object
public String getLastName()
{
return lastName;
}//a getter method that returns the lastName value
public void setAge(int age)
{
this.age = age;
}//a setter method for the age of the object
public int getAge()
{
return age;
}// a getter method that returns the age value
public void setHouse(String house)
{
this.house = house;
}//a setter method for the hogwarts house the object is in
public String getHouse()
{
return house;
}//a getter method that returns the hogwarts house that the object is in
public void setWandType(String userWand)
{
switch(userWand)
{
case "1":
wandType = "Phoenix feather";
//if the user enter the number 1 then the wand is equals to the Phoenix feather type
break;//exits the switch statement if 1 is entered
case "2":
wandType = "Unicorn hair";
//if the user enter the number 2 then the wand is equals to the Unicorn hair type
break;//exits the switch statement if 2 is entered
case "3":
wandType = "Dragon heartstring";
//if the user enter the number 3 then the wand is equals to the Dragon heartstring type
break;//exits the switch statement if 3 is entered
}
}//a setter method for the wandType of the object
public String getWandType()
{
return wandType;
}//a getter method that returns the type of wand the object has
public void setWandPower(double wandPower)
{
this.wandPower = (Math.random()*51);
}//a setter method that sets the power of the wand to a random number between 0 and 50
public double getWandPower()
{
return wandPower;
}//a getter method that returns the power of the wand
public void setInjured(String injury)
{
if( injury.equalsIgnoreCase( "false" ))
{
injured = false;
}//if the parameter injury is equal to the string false then the student is not injured
else if ( injury.equals( "true" ))
{
injured = true;
}//if the parameter injury is equal to the string true then the student is injured
else
{
throw new IllegalArgumentException("Error");
}//if the parameter injury is not equal to true or false then the if statement will throw an Error
}//a setter method that states whether the student is injured or not
public boolean getInjured()
{
return injured;
}//returns whether the object is injured or not
public boolean IsAProfessor()
{
if (getAge()>18)
{
isAProfessor = true;
}//if the object's age is greater than 18 then they are a professor
else if (getAge()<=18)
{
isAProfessor = false;
}//if the object's age is less than or equal to 18 then they are not a professor
return isAProfessor;
}//a setter method for that determines whether or not the object is a Professor
public void setLocation (String location)
{
this.location = location;
}//a setter method for the location of the object
public String getLocation ()
{
return location;
}//a method that returns the location of the object
public void setClasses (String[] classes)
{
this.classes = classes;
}//a setter method for the array classes
public String[] getClasses()
{
return classes;
}//a getter method that return the array classes
public String getTheClassElements()
{
String output = new String();
for (String elements : classes)
{
output = output + elements;
}
return output;
}//a method that returns the contents of the array classes
public void selectionSortClasses()
{
for(int x = 0; x<classes.length-1; x++)
{
for(int y = x+1; y<classes.length; y++)
{
if(classes[x].compareTo(classes[y]) > 0)
{
String result = classes[y];
classes[y] = classes[x];
classes[x] = result;
}
}
}
}//this method uses selection sort to sort the array classes.
public String toString()
{
String output = new String();//creates a new String
output += "Name: " +getFirstName()+ " " +getLastName()+ "\nAge: " +getAge()+ "\nHogwarts house: "
+getHouse()+ "\nWand type: " +getWandType()+ "\nWand power: "
+getWandPower()+ "\nAre they a professor at hogwarts?: " +IsAProfessor()+ "\nInjured: " + getInjured() + "\nLocation: " + getLocation()
+"\nClasses: " +getTheClassElements();
return output;
}//a toString that returns all the getter methods in the class and organizes them in a neat fashion
}//end of class