1. Create a new class Faculty that extends class Person. Faculty members belong to departments, and teach courses. Provide appropriate fields and methods to make this class functional.
The following are the codes of my FacultyMember and Department classes:
import java.util.ArrayList;
/**
* Write a description of class Department here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Department
{
private ArrayList members;
/**
* Constructor for objects of class Department
*/
public Department()
{
members = new ArrayList <FacultyMember> ();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addMember(FacultyMember teacher)
{
members.add(teacher);
}
}
/**
* Write a description of class FacultyMember here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FacultyMember extends Person
{
private Course course;
/**
* Constructor for objects of class FacultyMember
*/
public FacultyMember(String initName, boolean gender, Course courseToTeach, String course1)
{
super(initName, gender);
course = courseToTeach;
}
public String getCourse()
{
return course.getCourseTitle();
}
public void setCourse(Course newCourse)
{
course = newCourse;
}
}
2. The Car Park is modeled by class CarPark. CarPark is a collection of parking spaces, which are modeled by class ParkingSpace. Continue to develop the project so that both faculty and students can be allocated parking spaces, and these parking spaces can be stored in the car park.
To do this I modified my CarPark class in order to make it compatible with all the above tasks. Here is its code:
/**
* CarPark models a car park where the parking spaces are allocated
*
* @author (fdaly)
* @version (version 1: nov 2007)
*/
public class CarPark
{
private ParkingPlace[] parkingSpots;
public CarPark(int size)
{
parkingSpots = new ParkingPlace[size];
}
public void addSpot(ParkingPlace spot)
{
if(spot.getPlaceNumber() > parkingSpots.length)
System.out.println("Please choose a parking spot that is anywhere between 0 and " + parkingSpots.length + ".");
parkingSpots[spot.getPlaceNumber()] = spot;
}
}
3. Develop a CarPark method that prints the names and car registration numbers of all allocated parking spaces. Use an overridden toString method in class ParkingSpace so that printing is simplified from class CarPark. Test your method with a car park that has spaces allocated for both faculty and students.
This was the result as asked (Mike is a Student instance, John is a FacultyMember instance):
4. Create a new class that will model a club. The club can have both faculty and student members. Create methods that permit you to add and to remove members from the club. Create a method that can print all male members or all female members of the club.
There is an issue with my BlueJ in that it won't compile the class. It simply freezes; however, I believe that this code is correct, apart from some syntactual mistakes which I cannot fix without the compiler:
import java.util.ArrayList;
/**
* Write a description of class Club here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Club
{
private ArrayList clubMembers;
/**
* Constructor for objects of class Club
*/
public Club()
{
clubMembers = new ArrayList <Person> ();
}
public void printClubDetails(boolean genderList) // true is male, false is female.
{
String gender = "";
for(int i = 0; i<clubMembers.size(); i++)
{
if(genderList && clubMembers.get(i).isMale() == true)
gender += "Male";
System.out.println(clubMembers.get(i).getName() + gender);
if(!genderList && clubMembers.get(i).isFemale() == true)
gender += "Female";
System.out.println(clubMembers.get(i).getName() + gender);
}
}
public void removeMember(int MemberNumber)
{
clubMembers.remove(MemberNumber);
}
public void addMember(Person member)
{
clubMembers.add(member);
}