Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

FeeCalculator and Sortable interfaces using java

you are going to make several classes that take advantage of two interfaces: FeeCalculator and Sortable. You will use the Sortable interface defined in the lab, as well as the Sort, Student, and Assignment6Tester classes I have provided. You will also need the MyDate class you have previously defined.
There are five main classes you will define: CanadianStudent, CanadianStudentUnder65, ForeignStudent, SeniorStudent, and University. You will also design a very simple interface, FeeCalculator. The Student class here is different from what you have made in the past. The tester class simply makes a University object, adds various students to the university, prints the array of students that the array has, sorts the array, and prints the array again.
Interface FeeCalculator:
This interface has one method called computeFees. It takes no arguments and returns a double.
Class University:
This class has an array of Student objects, a maximumNumberOfStudents integer, and an integer for the total number of students in the array. It has one constructor that accepts a maximum number of students for its array. It has a method called insertStudent that returns a boolean and accepts a Student as a parameter. This method adds a student to the array, at the first vacant index of the array. It has another method called numberOfStudents which returns an int and accepts a String, which is the name of a country. This method counts the number of Students from the given country and returns it. University also has a method called sortStudents which uses the Sort class to sort the array of Students. Finally, it has a toString method. The toString method returns “Number of students in university = ” followed by the number of students in the university. Each on a new line, it lists the toString of each student.
Class CanadianStudent:
This abstract class extends Student and has a constructor which accepts a String and an int, which are the student’s name and the number of courses taken, respectively. CanadianStudent overrides the method findCountry in Student, simply by returning “Canada”.
Class CanadianStudentUnder65:
This is a concrete class extending CanadianStudent. It has a constructor which accepts a String and an int, which are the student’s name and number of courses respectively. Another constructor only accepts the name, and sets the number of courses to 5. This class overrides computeFees (obtained from FeeCalculator in Student). For this type of student, if the student is taking at least 4 courses, the fee is $800. Otherwise, the fee is $200 per course. This type of student also overrides the lessThan method, obtained from Sortable in Student. The order for sorting is given below, and each concrete student must have a lessThan method that creates the order provided.
Class SeniorStudent:
This class extends CanadianStudent. This student has an instance variable called pension, which is a double. It has a single constructor, which accepts the student’s name, the number of courses taken, and the pension. The fee for a senior student is $50. This class overrides lessThan, again, which must enforce the order provided below. This class also overrides toString, by appending “, senior citizen who gets pension $” followed by its pension.
Class ForeignStudent:
This class has two instance variables: a String which is the country of origin, and a MyDate which is the date of entry to Canada of the student. ForeignStudent has a single constructor, which takes the name of the student, the number of courses taken, the country of origin, and the date of entry to Canada. The fee for a ForeignStudent is $1000 per course. Finally, ForeignStudent has a lessThan override, as well as an override for findCountry, which simply returns the country of origin of the student.
Sort Order: -senior students come first, followed by foreign students, followed by Canadians that are not seniors
-within the senior students, sort alphabetically by name (ascending)
-within foreign students, sort alphabetically by name (ascending)
-within Canadian non-seniors, sort alphabetically by name (ascending)
-see the sample output below for more information
Sample Output:
Number of students in university = 9
Student #1, Name: John is from Canada, pays fees $800.0
Student #2, Name: Mary is from Canada, pays fees $600.0
Student #3, Name: Tom is from Canada, pays fees $50.0, senior citizen who gets pension $1500.0
Student #4, Name: Xiao is from China, pays fees $3000.0
Student #5, Name: Jagjit is from India, pays fees $5000.0
Student #6, Name: Liz is from Ireland, pays fees $3000.0
Student #7, Name: Hong is from China, pays fees $4000.0
Student #8, Name: Pat is from Canada, pays fees $50.0, senior citizen who gets pension $2000.0
Student #9, Name: Ting is from China, pays fees $5000.0
Number of students from Canada is 4
Number of students from China is 3
************* Now we will sort the students********************
Number of students in university = 9
Student #8, Name: Pat is from Canada, pays fees $50.0, senior citizen who gets pension $2000.0
Student #3, Name: Tom is from Canada, pays fees $50.0, senior citizen who gets pension $1500.0
Student #7, Name: Hong is from China, pays fees $4000.0
Student #5, Name: Jagjit is from India, pays fees $5000.0
Student #6, Name: Liz is from Ireland, pays fees $3000.0
Student #9, Name: Ting is from China, pays fees $5000.0
Student #4, Name: Xiao is from China, pays fees $3000.0
Student #1, Name: John is from Canada, pays fees $800.0
Student #2, Name: Mary is from Canada, pays fees $600.0

Answer:

Tester Class

public class Tester {
	public static void main(String a[]){
		University ourUniversity;
		Student aStudent;
		MyDate dateOfEntryToCanada;
		ourUniversity = new University(10);
		aStudent = new CanadianStudentUnder65("John");
		ourUniversity.insertStudent(aStudent);
		aStudent = new CanadianStudentUnder65("Mary", 3);
		ourUniversity.insertStudent(aStudent);
		aStudent = new SeniorStudent("Tom", 3, 1500.0);
		ourUniversity.insertStudent(aStudent);
		dateOfEntryToCanada = new MyDate("25/05/2009");
		aStudent = new ForeignStudent("Xiao", 3, "China", dateOfEntryToCanada);
		ourUniversity.insertStudent(aStudent);
		dateOfEntryToCanada = new MyDate("12/08/2013");
		aStudent = new ForeignStudent("Jagjit", 5, "India", dateOfEntryToCanada);
		ourUniversity.insertStudent(aStudent);
		dateOfEntryToCanada = new MyDate("05/04/2008");
		aStudent = new ForeignStudent("Liz", 3, "Ireland", dateOfEntryToCanada);
		ourUniversity.insertStudent(aStudent);
		dateOfEntryToCanada = new MyDate("13/11/2011");
		aStudent = new ForeignStudent("Hong", 4, "China", dateOfEntryToCanada);
		ourUniversity.insertStudent(aStudent);
		aStudent = new SeniorStudent("Pat", 2, 2000.00);
		ourUniversity.insertStudent(aStudent);
		dateOfEntryToCanada = new MyDate("21/09/2009");
		aStudent = new ForeignStudent("Ting", 5, "China", dateOfEntryToCanada);
		ourUniversity.insertStudent(aStudent);
		
		System.out.println(ourUniversity);
		System.out.println("Number of students from Canada is " + 
				ourUniversity. numberOfStudents("Canada"));
		System.out.println("Number of students from China is " + 
				ourUniversity. numberOfStudents("China"));
		System.out.println("\n************* Now we will sort the students********************\n");

		ourUniversity.sortStudents();
		System.out.println(ourUniversity);
	}
}

CanadianStudentUnder65 Class

public class CanadianStudentUnder65 extends CanadianStudent {
	
	public CanadianStudentUnder65 (String name, int numOfCourses){
		super (name, numOfCourses);
	}
	public CanadianStudentUnder65 (String name){
		super (name, 5);
	}
	public double computeFees(){
		if (super.getCourses() >= 4)
			return 800.0;
		else
			return super.getCourses()*200.0;
	}
	public String findCountry(){
		return super.findCountry();
	}
	public String getName(){
		return super.getName();
	}
	public boolean lessThan (Sortable anotherStudent){
		if (anotherStudent instanceof ForeignStudent || anotherStudent instanceof SeniorStudent){
			return false;
		}
		else if (super.getName().compareTo(((CanadianStudentUnder65)anotherStudent).getName()) < 0 ){
				return true;
		}
		return false;
	}
}

CandianStudent Class

public abstract class CanadianStudent extends Student{

	public CanadianStudent(String studentName, int numberOfCoursesTaken) {
		super(studentName, numberOfCoursesTaken);
	}
	
	public String findCountry()
	{
		return "Canada"; 
	}
}

FeeCalculator Class

public interface FeeCalculator {
	public double computeFees();
}

ForeignStudent Class

public class ForeignStudent extends Student {

	private String countryOfOrigin;
	private String dateOfEntryToCanada;
	public ForeignStudent(String studentName, int numberOfCoursesTaken, String countryOfOrigin, MyDate dateOfEntryToCanada){
		super (studentName , numberOfCoursesTaken);
		this.countryOfOrigin = countryOfOrigin;
		}
	  
	public String findCountry(){
		return countryOfOrigin;
	}
	public double computeFees(){
		return super.getCourses()*1000.0;
	}
	public String getName(){
		return super.getName();
	}
	public boolean lessThan(Sortable anotherStudent){
		if (anotherStudent instanceof SeniorStudent )
			return false;
		else if (anotherStudent instanceof ForeignStudent){
			if (super.getName().compareTo(((ForeignStudent)anotherStudent).getName())>0)
				return false;}
		return true;
	}
}

MyDate Class

import java.util.StringTokenizer;

public class MyDate{
	private int day;
	private int month;
	private int year;

	public MyDate(String unformattedDate){
		StringTokenizer splitDate = new StringTokenizer(unformattedDate, "/");

		this.day = Integer.parseInt(splitDate.nextToken());
		this.month = Integer.parseInt(splitDate.nextToken());
		this.year = Integer.parseInt(splitDate.nextToken());
	}

	public MyDate(MyDate myDate){
		this.day = myDate.day;
		this.month = myDate.month;
		this.year = myDate.year;
	}

	public String toString(){
		String twoDigitYear = String.valueOf(this.year).substring(2,4);
		String monthName [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September",
				"October", "November", "December"};
	
		return monthName[month-1] + " " + day + ", " + twoDigitYear;
		
	}

	public boolean lessThan(MyDate myDate){
		if(this.year < myDate.year){
			return true;
		}
		else if (this.year > myDate.year){
			return false;
		}
		else if (this.month < myDate.month){
			return true;
		}
		else if (this.month > myDate.month){
			return false;
		}
		else if (this.day < myDate.day){
			return true;
		}
		else
			return false;
	}

	public boolean equals(MyDate myDate) {
		if (myDate == null) return false;
		if((this.day == myDate.day) && (this.month == myDate.month) && (this.year == myDate.year)){
			return true;
		}
		else
			return false;
	}
}

SeniorStudent Class

public class SeniorStudent extends CanadianStudent{
	private double pension; 
	
	public SeniorStudent(String studentName, int numberOfCoursesTaken,double pension){
		super(studentName,numberOfCoursesTaken); 
		this.pension = pension; 
	}
	
	
	public boolean lessThan(Sortable anObject)
	{
		if(anObject instanceof SeniorStudent){
			return super.lessThan(anObject); 
		}
		
		if(anObject instanceof CanadianStudentUnder65){
			return true;
		}
		else if(anObject instanceof ForeignStudent){
			return true; 
		}
		return false; 
	}
	
	
	public String toString(){
		String result = ""; 
		result += super.toString() + ", senior citizen who gets\nPension:" + pension; 
		return result; 
	}


	@Override
	public double computeFees() {
		return 50.00; 
	}
}

Sort Class

public class Sort {
	public static void sortAll(Sortable listObjects[],  int numObjects){
		Sortable temp;
    		int indexSmallest, index1, index2;
    		for (index1 = 0; index1 < numObjects - 1; index1++){
    			indexSmallest = index1;
       			for (index2 = index1 + 1; 
              			index2 < numObjects; index2++)
       				if (listObjects[index2].
				     lessThan(listObjects[indexSmallest]))
                				indexSmallest = index2;
       			temp = listObjects[index1];
       			listObjects[index1]  = listObjects[indexSmallest];
       			listObjects[indexSmallest] = temp;
    		}
  	}     
}

Sortable Class

public interface Sortable {
	public boolean lessThan(Sortable anObject);
}

Student Class

public abstract class Student implements FeeCalculator, Sortable {

	private static int totalStudents = 0;
	
	private int studentNumber;
	protected int numberOfCoursesTaken;
	protected String studentName;
	
	public Student(String studentName, int numberOfCoursesTaken) {
		totalStudents++;
		this.studentNumber = totalStudents;
		
		this.studentName = studentName;
		this.numberOfCoursesTaken = numberOfCoursesTaken;
	}
	
	public abstract String findCountry();
		
	public String toString() {
		return "Student #" + studentNumber +
				", Name: " + studentName +
				" is from " + findCountry()
				+ ", pays fees $" + computeFees(); 
	}
	
	public boolean lessThan(Sortable anObject){
		if(anObject instanceof Student){
			Student temp = (Student) anObject; 
			if(getName().compareTo(temp.getName()) < 0){
				return true; 
			}
			else{
				return false; 
			}
		}
		return false; 
	}
	
	public String getName(){
		return studentName; 
	}
	public int getClasses(){
		return numberOfCoursesTaken; 
	}

	public double getCourses() {
		return numberOfCoursesTaken;
	}
}

University Class

public class University {
	private Student [] listOfStudents; 
	private int numOfStudents; 
	public University(int maximumNumberOfStudents){
		listOfStudents = new Student[maximumNumberOfStudents]; 
	}
	
	public boolean insertStudent(Student aStudent){
		
		for(int i = 0; i < listOfStudents.length; i++){
			if(listOfStudents[i] == null){
				listOfStudents[i] = aStudent; 
				numOfStudents++; 
				return true;
			}
		}
		return false; 
	}
	
	public int numberOfStudents(String nameOfCountry){ 
		int counter = 0;
		for(Student s: listOfStudents){
			if(s != null){
				if(s.findCountry().equals(nameOfCountry)){
					counter++; 
				}
			
			}
		}
		return counter; 
	}
	
	public String toString(){
		String result = ""; 
		result += "Number of students in university= " + numOfStudents + "\n";
	
		for(Student s: listOfStudents){
			if(s!= null)
			result += s.toString() + "\n"; 
		}
		return result; 
		
	}
	
	public void sortStudents(){
		Sort.sortAll(listOfStudents, numOfStudents);
	}
	
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!