FinalUserFrame Class and OutputFrame Class using java
You will make an application with a GUI that allows the user to display the students in a university, sort them (ascending or descending), and find the number of students attending the university from a given country.
Class Sort:
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 Sort’s 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. Thus, when the “Show Students” button is clicked, the output field
should read:
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
Finally, the other two buttons each launch OutputFrame windows. The first button says “Show Students
sorted (Ascending)” and the other says “Show Students sorted (Descending)”. When clicking either of
these buttons, an OutputFrame window opens with the expected result (student list, sorted, ascending or
descending as requested). For instance, when “Show Students sorted (Ascending)” is clicked, the output
in the OutputFrame reads:
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
Similarly, when “Show Students sorted (Descending)” is clicked, an OutputFrame is launched with the
following output:
Number of students in university = 9
Student #2, Name: Mary is from Canada, pays fees $600.0
Student #1, Name: John is from Canada, pays fees $800.0
Student #4, Name: Xiao is from China, pays fees $3000.0
Student #9, Name: Ting is from China, pays fees $5000.0
Student #6, Name: Liz is from Ireland, pays fees $3000.0
Student #5, Name: Jagjit is from India, pays fees $5000.0
Student #7, Name: Hong is from China, pays fees $4000.0
Student #3, Name: Tom is from Canada, pays fees $50.0, senior citizen who gets pension $1500.0
Student #8, Name: Pat is from Canada, pays fees $50.0, senior citizen who gets pension $2000.0
Class OutputFrame:
OutputFrame is also a type of JFrame, but is more basic than FinalUserFrame. OutputFrame only has a
label, a text area, and a button. The label should read “Sorted List (ascending) of Students” if the output is
showing the sorted list in ascending order, or it should read “Sorted List (descending) of Students”
otherwise. In the output area, the text should read the following if the list is sorted ascending:
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
If the list is sorted descending, it should read:
Number of students in university = 9
Student #2, Name: Mary is from Canada, pays fees $600.0
Student #1, Name: John is from Canada, pays fees $800.0
Student #4, Name: Xiao is from China, pays fees $3000.0
Student #9, Name: Ting is from China, pays fees $5000.0
Student #6, Name: Liz is from Ireland, pays fees $3000.0
Student #5, Name: Jagjit is from India, pays fees $5000.0
Student #7, Name: Hong is from China, pays fees $4000.0
Student #3, Name: Tom is from Canada, pays fees $50.0, senior citizen who gets pension $1500.0
Student #8, Name: Pat is from Canada, pays fees $50.0, senior citizen who gets pension $2000.0
The button should read “Close Window”, and it simply closes the OutputFrame window. The output area
should be 20 x 50, and the entire OutputFrame window should be 600 x 500.
Answer:
Tester Class
public class Tester { 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); } }
CanadianStudent Class
public abstract class CanadianStudent extends Student{ public String findCountry(){ return "Canada"; } public CanadianStudent(String studentName, int numberOfCoursesTaken){ super(studentName, numberOfCoursesTaken); } }
CanadianStudentUnder65 Class
public class CanadianStudentUnder65 extends CanadianStudent{ public CanadianStudentUnder65(String studentName, int numberOfCoursesTaken){ super(studentName, numberOfCoursesTaken); } /*set the default value*/ public CanadianStudentUnder65(String studentName){ this(studentName, 5); } public double computeFees(){ int numberOfCoursesTaken; numberOfCoursesTaken = getNumberOfCoursesTaken(); /*full time students*/ if (numberOfCoursesTaken >= 4){ return 800.00; /*part time students*/ } else { return 200.0 * numberOfCoursesTaken; } } public boolean lessThan(Sortable anotherStudent){ Student aCanadianStudentUnder65; if (anotherStudent instanceof ForeignStudent){ return false; } else if (anotherStudent instanceof SeniorStudent){ return false; } else { aCanadianStudentUnder65 = (Student) anotherStudent; return (this.getName().compareTo(aCanadianStudentUnder65.getName()) < 0); } } }
FeeCalculator Class
public interface FeeCalculator { public double computeFees(); }
FinalUserFrame Class
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; public class FinalUserFrame extends JFrame{ private JLabel enterCountry; private JTextField countryInputField; private JTextArea outputArea; private JButton [] myButtons = new JButton[3]; private University univ; public FinalUserFrame(University ourUniversity){ super("Input Frame"); setLayout(new FlowLayout()); univ = ourUniversity; enterCountry = new JLabel("Enter name of Country"); add(enterCountry); countryInputField = new JTextField(20); /*Display how many student from the specific country*/ countryInputField.addActionListener(new ActionListener() { String inputString = ""; @Override public void actionPerformed(ActionEvent e) { inputString = String.valueOf(univ.numberOfStudents(countryInputField.getText())); outputArea.setText("Number of Students from " + countryInputField.getText() + " is " + inputString); countryInputField.setText(""); } }); add(countryInputField); outputArea = new JTextArea(15,50); add(outputArea); for(int i = 0; i < myButtons.length; i++){ switch(i){ /*when click the "Show Students" button*/ case 0: myButtons[i] = new JButton("Show Students"); myButtons[i].addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { outputArea.setText(univ.toString()); } }); break; /*when click the "Show Students sorted(ascending)" button*/ case 1: myButtons[i] = new JButton("Show Students sorted(ascending)"); myButtons[i].addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ OutputFrame newFrame = new OutputFrame("Sorted List(ascending) of Students"); boolean isDescending=false; univ.sortStudents(isDescending); newFrame.displayMessage(univ.toString()); } }); break; /*when click the "Show Students sorted(descending)" button*/ case 2: myButtons[i] = new JButton("Show Students sorted(descending)"); myButtons[i].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OutputFrame newFrame = new OutputFrame("Sorted List(descending) of Students"); boolean isDescending=true; univ.sortStudents(isDescending); newFrame.displayMessage(univ.toString()); } }); break; } add(myButtons[i]); } setSize(700,400); setVisible(true); } }
ForeignStudent Class
public class ForeignStudent extends Student{ private String countryOfOrigin; 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 1000.0 * getNumberOfCoursesTaken(); } public boolean lessThan(Sortable anotherStudent){ String nameOfForeignStudent; ForeignStudent aForeignStudent; if (anotherStudent instanceof ForeignStudent){ nameOfForeignStudent = getName(); aForeignStudent = (ForeignStudent) anotherStudent; return (nameOfForeignStudent.compareTo(aForeignStudent.getName()) < 0); } else if (anotherStudent instanceof SeniorStudent){ return false; } else { 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; } }
OutputFrame Class
import java.awt.FlowLayout; import java.awt.event.*; import javax.swing.*; public class OutputFrame extends JFrame implements ActionListener{ private JLabel messageToUser; JTextArea outputArea; JButton closeWindow; public OutputFrame (String captionForTextArea){ super("Output Frame"); String sortedListOfStudents; setLayout(new FlowLayout()); messageToUser = new JLabel(captionForTextArea); add(messageToUser); outputArea = new JTextArea(20, 50); add(outputArea); closeWindow = new JButton("Close Window"); closeWindow.addActionListener(this); add(closeWindow); setSize(600,500); setVisible(true); } public void displayMessage(String stringToDisplay){ outputArea.setText(stringToDisplay); } public void actionPerformed(ActionEvent e){ this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); } }
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 double computeFees(){ return 50.0; } public boolean lessThan(Sortable anotherStudent){ String nameOfSenior; SeniorStudent aSeniorStudent; if (anotherStudent instanceof SeniorStudent){ nameOfSenior = getName(); aSeniorStudent = (SeniorStudent)anotherStudent; return (nameOfSenior.compareTo(aSeniorStudent.getName()) < 0) ; } else { return true; } } public String toString(){ String output; output = super.toString() + " senior citizen who gets pension $" + pension; return output; } }
Sort Class
public class Sort { public static void sortAnything(Sortable listObjects[], int numObjects,boolean isDescending){ Sortable temp; int indexSmallest, index1, index2; for (index1 = 0; index1 < numObjects - 1; index1++){ indexSmallest = index1; for (index2 = index1 + 1;index2 < numObjects; index2++){ /*Check if the list has to be in descending order*/ if(isDescending==true){ if (listObjects[indexSmallest].lessThan(listObjects[index2])){ indexSmallest = index2; } } /*then the list has to be in ascending order*/ else{ if (listObjects[index2].lessThan(listObjects[indexSmallest])) indexSmallest = index2; } } temp = listObjects[index1]; listObjects[index1] = listObjects[indexSmallest]; listObjects[indexSmallest] = temp; } } }
Sortable Class
interface Sortable{ public boolean lessThan(Sortable anObject); }
Student Class
public abstract class Student implements Sortable, FeeCalculator{ private static int totalStudents = 0; private int studentNumber = 0; private String studentName = null; private int numberOfCoursesTaken; public Student(String studentName, int numberOfCoursesTaken){ totalStudents++; studentNumber = totalStudents; this.numberOfCoursesTaken = numberOfCoursesTaken; this.studentName = studentName; } public abstract String findCountry(); public int getNumberOfCoursesTaken(){ return numberOfCoursesTaken; } public String toString() { String output; output = "Student #:" + studentNumber + ", Name:" + studentName; output += " is from " + findCountry() + "; pays fees $" + computeFees(); return output; } public String getName() { return studentName; } }
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(boolean isDescending){ Sort.sortAnything(listOfStudents, numOfStudents,isDescending); } }
Leave a reply