Question
The class Assignment7Tester is provided. Obtain this class and bring it into a Java project, along with CanadianStudent, CanadianStudentUnder65, FeeCalculator, ForeignStudent, MyDate, SeniorStudent, Sort, Sortable,
The class Assignment7Tester is provided. Obtain this class and bring it into a Java project, along with CanadianStudent, CanadianStudentUnder65, FeeCalculator, ForeignStudent, MyDate, SeniorStudent, Sort, Sortable, Student, and University from last week (solutions are posted online obtain them and understand them if you did not do well on last weeks assignment). You will make very minor modifications to this code, as well as creating two new classes: FinalUserFrame and OutputFrame. Sample screenshots of the programs behaviour are at the end of this document.
You will make one change to the Sort class. You will modify the sortAnything method such that it now accepts a boolean isDescending. If isDescending is false, sortAnything will behave normally (sorting the student in ascending order). If isDescending is true, however, sortAnything will sort in descending order instead.
Class University:
Modify sortStudents such that it takes a boolean, isDescending, and uses that when calling Sorts method sortAnything.
Class FinalUserFrame:
FinalUserFrame extends JFrame and has a label, a text field, a text area, and three buttons (see the layout below). It should use a flow layout manager and its dimensions should initially be 700 x 400. The label should say Enter name of Country. The input field (which is the text field) should allow 20 characters. The text field (called countryInputField) should allow input such that when the user inputs the name of a country, the text area (called outputField) should show how many students in the university are from that country. For instance, if Canada is entered into the input field, the output field should show Number of Students from Canada is 4. The text area should be 15 x 50. As mentioned, there are three buttons. The first is a Show Students button. When this button is clicked, the entire (unsorted) list of students is shown in the output field. This list should always remain unsorted, the students should be shown in the order in which they were created.
Sort
public class Sort { public static void sortAnything(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; } } }
University
public class University { private Student [] listOfStudents; private static int howManyStudents = 0;
public University(int maximumNumberOfStudents){ listOfStudents = new Student [maximumNumberOfStudents]; }
public boolean insertStudent(Student aStudent){ boolean result = false; int i=0; while(listOfStudents[i] != null){ i++; } listOfStudents[i] = aStudent; if(i==10){ result = false; }else{ result = true; } howManyStudents++; return result; }
public int numberOfStudents(String nameOfCountry){ int canadianStudent = 0; int foreignStudent = 0; int result = 0;
if(nameOfCountry.equals("Canada")){ for(Student list: listOfStudents){ if(list instanceof Student){ if (list.findCountry().equals("Canada")){ canadianStudent++; } } result = canadianStudent; } } if(nameOfCountry.equals("China")){ for(Student list2: listOfStudents){ if(list2 instanceof Student){ if (list2.findCountry().equals("China")){ foreignStudent++; } } result = foreignStudent; } } return result; }
public String toString(){ String Student = "Number of students in University = " + howManyStudents + " ";
for(Student s: listOfStudents){ if(s != null){ Student += s.toString() + " "; } } return Student; }
public void sortStudents(){ Sort.sortAnything(listOfStudents, howManyStudents); }
}
public abstract class CanadianStudent extends Student{
public CanadianStudent(String studentName){ super(studentName); } public CanadianStudent(String fullname, int numberOfCoursesTaken){ super(fullname, numberOfCoursesTaken); }
public String findCountry(){ return "Canada"; } }
CanadaStudentUnder65
public class CanadianStudentUnder65 extends CanadianStudent{ private int numberofcourses;
public CanadianStudentUnder65(String studentName) { super(studentName); }
public CanadianStudentUnder65(String studentName, int numberOfCoursesTaken) { super(studentName, numberOfCoursesTaken); numberofcourses = numberOfCoursesTaken; } public double computeFees(){ if (numberofcourses<= 4){ return 800.0; }else{ return numberofcourses*200.0; } } public boolean lessThan(Sortable anotherStudent){ if(anotherStudent instanceof SeniorStudent){ return false; } if(anotherStudent instanceof ForeignStudent){ return false; } if(anotherStudent instanceof CanadianStudentUnder65){ return super.lessThan(anotherStudent); } return false; }
}
feeCalculator
public interface FeeCalculator { public double computeFees(); }
ForeignStudent
public class ForeignStudent extends Student{ private String countryOfOrigin; private int numberofcourses;
public ForeignStudent(String studentName, int numberOfCoursesTaken, String countryOfOrigin, MyDate dateOfEntryToCanada){ super(studentName, numberOfCoursesTaken); this.countryOfOrigin = countryOfOrigin; numberofcourses = numberOfCoursesTaken;
}
public double computeFees(){ return numberofcourses*1000.0; }
public String findCountry(){ return countryOfOrigin; }
public boolean lessThan(Sortable anotherStudent){
if(anotherStudent instanceof SeniorStudent){ return false; } if(anotherStudent instanceof ForeignStudent){ return super.lessThan(anotherStudent); } if(anotherStudent instanceof CanadianStudentUnder65){ return true; } return false; /*Student temp; temp = (Student) anotherStudent; if(anotherStudent instanceof ForeignStudent){ if(getStudentName().compareTo(temp.getStudentName() ) < 0){ return true; }else{ return false; } }else { return false; }*/ } }
MyDate
import java.util.StringTokenizer;
public class MyDate {
public int day; public int month; public int year;
public MyDate(String date){ StringTokenizer myTokens = new StringTokenizer(date, "/"); this.day = Integer.parseInt(myTokens.nextToken()); this.month = Integer.parseInt(myTokens.nextToken()); this.year = Integer.parseInt(myTokens.nextToken()); }
public MyDate(MyDate copy){ if (copy == null) { System.out.println("Fatal error."); System.exit(0); } day = copy.day; month = copy.month; year = copy.year; }
public boolean lessThan(MyDate copy){ if(this.year
return true; } else if(this.year>copy.year) { return false; } else if(this.year==copy.year && this.month { return true; } else if(this.year==copy.year && this.month>copy.month) { return false; } else if(this.year==copy.year && this.month==copy.month && this.day { return true; } else if(this.year==copy.year && this.month==copy.month && this.day>copy.day) { return false; } else { return false; } }
public boolean equals(MyDate copy){ if(this.year==copy.year && this.month==copy.month && this.day==copy.day) { return true; } else { return false; } }
public String toString (){
String Lettermonth="";
if (month == 1){ Lettermonth = "January"; } else if (month == 2){ Lettermonth = "Febuary"; } else if (month == 3){ Lettermonth = "March"; } else if (month == 4){ Lettermonth = "April"; } else if (month == 5){ Lettermonth = "May"; } else if (month == 6){ Lettermonth = "June"; } else if (month == 7){ Lettermonth = "July"; } else if (month == 8){ Lettermonth = "August"; } else if (month == 9){ Lettermonth = "September"; } else if (month == 10){ Lettermonth = "October"; } else if (month == 11){ Lettermonth = "November"; } else{ Lettermonth = "December"; }
if (this.year%100 == 0) { return ("\"" + Lettermonth + " " + this.day + ", '" + "0" + this.year%100 + "\""); } return("\"" + Lettermonth + " " + this.day + ", '" + this.year%100 + "\"");
} }
SeniorStudent
public class SeniorStudent extends CanadianStudent{
private double Pension; public SeniorStudent(String studentName, int numberOfCoursesTaken, double pension) { super(studentName, numberOfCoursesTaken); Pension = pension;
}
public double getPension(){ return Pension; }
public double computeFees(){ return 50.0; }
public String toString(){ String pension = Double.toString(Pension); return super.toString() + pension; }
public boolean lessThan(Sortable anotherStudent){ if(anotherStudent instanceof SeniorStudent){ return super.lessThan(anotherStudent); } if(anotherStudent instanceof ForeignStudent){ return true; } if(anotherStudent instanceof CanadianStudentUnder65){ return true; } return false;
} }
Sortable
public interface Sortable { public boolean lessThan(Sortable anObject); }
Student
public abstract class Student implements FeeCalculator, Sortable{
private int studentNumber, numberOfCoursesTaken; private static int i; private String studentName;
public abstract String findCountry();
public Student(String studentName){ this.studentName = studentName; studentNumber++; }
public boolean lessThan(Sortable anotherStudent){ if(anotherStudent instanceof Student){ Student temp; temp = (Student) anotherStudent; if(getStudentName().compareTo(temp.getStudentName()) < 0){ return true; }return false; }return false; }
public Student(String studentName, int numberOfCoursesTaken){ this.studentName = studentName; this.numberOfCoursesTaken = numberOfCoursesTaken; i++; studentNumber = i; }
protected int getNumberOfCoursesTaken(){ return numberOfCoursesTaken; }
protected String getStudentName(){ return studentName; }
public String toString(){ String result;
if(this.computeFees() == 50.0){ result = "Student #:" + Integer.toString(studentNumber) + ", Name:" + studentName + " is from " + findCountry() + "; pays fees $" + computeFees() + " senior citizen who gets pension $"; }else{ result = "Student #:" + Integer.toString(studentNumber) + ", Name:" + studentName + " is from " + findCountry() + "; pays fees $" + computeFees();
} return result; } }
CanadianStudent
public abstract class CanadianStudent extends Student{
public CanadianStudent(String studentName){ super(studentName); } public CanadianStudent(String fullname, int numberOfCoursesTaken){ super(fullname, numberOfCoursesTaken); }
public String findCountry(){ return "Canada"; } }
Tester
public class Assignment7Tester { public static void main( String args[]){ University ourUniversity; Student aStudent; MyDate dateOfEntryToCanada; FinalUserFrame aFrame; 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); aFrame = new FinalUserFrame(ourUniversity); aFrame.setVisible(true); } }
If anything is missing let me know please
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started