In java, make an application with a GUI that allows the user to display the stud
ID: 3913636 • Letter: I
Question
In java, 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.
The class Tester 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 given below. You will make very minor modifications to this code, as well as creating two new classes: FinalUserFrame and OutputFrame. Sample screenshots of the program’s behaviour are shown at the end.
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.
Class Tester:
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);
}
}
Class CanadianStudent:
public abstract class CanadianStudent extends Student {
public CanadianStudent(String studentName, int numberOfCoursesTaken) {
super(studentName, numberOfCoursesTaken);
}
@Override
public String findCountry() {
return "Canada";
}
}
Class CanadianStudentUnder65:
public class CanadianStudentUnder65 extends CanadianStudent {
public CanadianStudentUnder65(String studentName, int numberOfCoursesTaken) {
super(studentName, numberOfCoursesTaken);
}
public CanadianStudentUnder65(String studentName) {
this(studentName, 5);
}
@Override
public double computeFees() {
if(numberOfCoursesTaken >= 4) {
return 800.0;
} else {
return numberOfCoursesTaken * 200.0;
}
}
@Override
public boolean lessThan(Sortable anotherStudent) {
if(anotherStudent instanceof ForeignStudent || anotherStudent instanceof SeniorStudent) {
return false;
} else if(anotherStudent instanceof CanadianStudentUnder65) {
return studentName.compareTo(((CanadianStudentUnder65) anotherStudent).studentName) < 0;
} else {
return true;
}
}
}
interface FeeCalculator:
public interface FeeCalculator {
public double computeFees();
}
Class ForeignStudent:
public class ForeignStudent extends Student {
private String countryOfOrigin;
private MyDate dateOfEntryToCanada;
public ForeignStudent(String studentName, int numberOfCoursesTaken, String countryOfOrigin, MyDate dateOfEntryToCanada) {
super(studentName, numberOfCoursesTaken);
this.countryOfOrigin = countryOfOrigin;
this.dateOfEntryToCanada = dateOfEntryToCanada;
}
@Override
public double computeFees() {
return numberOfCoursesTaken * 1000.0;
}
@Override
public boolean lessThan(Sortable anotherStudent) {
if(anotherStudent instanceof SeniorStudent) {
return false;
} else if(anotherStudent instanceof ForeignStudent) {
return studentName.compareTo(((ForeignStudent) anotherStudent).studentName) < 0;
} else {
return true;
}
}
@Override
public String findCountry() {
return countryOfOrigin;
}
}
Class MyDate:
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;
}
}
Class SeniorStudent:
public class SeniorStudent extends CanadianStudent {
private double pension;
public SeniorStudent(String studentName, int numberOfCoursesTaken, double pension) {
super(studentName, numberOfCoursesTaken);
this.pension = pension;
}
@Override
public double computeFees() {
return 50.0;
}
@Override
public boolean lessThan(Sortable anotherStudent) {
if(anotherStudent instanceof SeniorStudent) {
return studentName.compareTo(((SeniorStudent) anotherStudent).studentName) < 0;
} else {
return true;
}
}
@Override
public String toString() {
return super.toString() + ", senior citizen who gets pension $" + pension;
}
}
Class 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;
}
}
}
Class Sortable:
public interface Sortable {
public boolean lessThan(Sortable anObject);
}
Class Student:
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();
}
}
Class University:
public class University {
private Student[] listOfStudents;
private int totalStudents = 0;
private final int maximumNumberOfStudents;
public University(int maximumNumberOfStudents) {
this.maximumNumberOfStudents = maximumNumberOfStudents;
listOfStudents = new Student[maximumNumberOfStudents];
}
public boolean insertStudent(Student aStudent) {
// Safeguard
if(totalStudents >= maximumNumberOfStudents) {
return false;
}
listOfStudents[totalStudents] = aStudent;
totalStudents++;
return true;
}
public int numberOfStudents(String nameOfCountry) {
int total = 0;
for(int i = 0; i < totalStudents; i++) {
if(listOfStudents[i].findCountry().equals(nameOfCountry)) {
total++;
}
}
return total;
}
public String toString() {
String desc = "";
desc += "Number of students in university = " + totalStudents + " ";
for(int i = 0; i < totalStudents; i++) {
desc += listOfStudents[i].toString() + " ";
}
return desc;
}
public void sortStudents() {
Sort.sortAnything(listOfStudents, totalStudents);
}
}
Sample Screenshots:
Explanation / Answer
package chegg.june;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Chegg375 {
}
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);
}
}
class FinalUserFrame extends JFrame {
public FinalUserFrame(University university) {
super("Input Frame");
setLayout(null); // set the layout of this frame as null
// create a JLabel
JLabel lb = new JLabel("<html>Enter the name of the country:</html>");
add(lb);
lb.setBounds(40, 20, 200, 30);
// create a country text field
JTextField tfCountry = new JTextField("");
add(tfCountry);
tfCountry.setBounds(251, 20, 150, 30);
// create a text area to display the contents
JTextArea taContent = new JTextArea(9, 10);
add(taContent);
taContent.setBounds(20, 60, 400, 220);
// create a button to show the number of students from the given country
JButton btnShowStudents = new JButton("<html>Show Students</html>");
btnShowStudents.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// getByCountry returns a list of students who are from same give country
// here we only need to print the number of students from that country
String countryName = tfCountry.getText();
System.out.println("Country is "+countryName);
ArrayList<String> students = university.getByCountry(countryName);
taContent.setText("Number of students from "+countryName+" is "+students.size());
}
});
add(btnShowStudents);
btnShowStudents.setBounds(440, 125, 130, 50);
JButton btnAscending = new JButton("<html>Show Students sorted (Ascending)</html>");
btnAscending.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(btnAscending);
btnAscending.setBounds(40, 301, 200, 40);
JButton btnDescending = new JButton("<html>Show Students sorted (Descending)</html>");
btnDescending.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(btnDescending);
btnDescending.setBounds(250, 301, 200, 40);
setSize(600, 400);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// Class CanadianStudent:
abstract class CanadianStudent extends Student {
public CanadianStudent(String studentName, int numberOfCoursesTaken) {
super(studentName, numberOfCoursesTaken);
}
@Override
public String findCountry() {
return "Canada";
}
}
// Class CanadianStudentUnder65:
class CanadianStudentUnder65 extends CanadianStudent {
public CanadianStudentUnder65(String studentName, int numberOfCoursesTaken) {
super(studentName, numberOfCoursesTaken);
}
public CanadianStudentUnder65(String studentName) {
this(studentName, 5);
}
@Override
public double computeFees() {
if(numberOfCoursesTaken >= 4) {
return 800.0;
} else {
return numberOfCoursesTaken * 200.0;
}
}
@Override
public boolean lessThan(Sortable anotherStudent) {
if(anotherStudent instanceof ForeignStudent || anotherStudent instanceof SeniorStudent) {
return false;
} else if(anotherStudent instanceof CanadianStudentUnder65) {
return studentName.compareTo(((CanadianStudentUnder65) anotherStudent).studentName) < 0;
} else {
return true;
}
}
}
// interface FeeCalculator:
interface FeeCalculator {
public double computeFees();
}
// Class ForeignStudent:
class ForeignStudent extends Student {
private String countryOfOrigin;
private MyDate dateOfEntryToCanada;
public ForeignStudent(String studentName, int numberOfCoursesTaken, String countryOfOrigin, MyDate dateOfEntryToCanada) {
super(studentName, numberOfCoursesTaken);
this.countryOfOrigin = countryOfOrigin;
this.dateOfEntryToCanada = dateOfEntryToCanada;
}
@Override
public double computeFees() {
return numberOfCoursesTaken * 1000.0;
}
@Override
public boolean lessThan(Sortable anotherStudent) {
if(anotherStudent instanceof SeniorStudent) {
return false;
} else if(anotherStudent instanceof ForeignStudent) {
return studentName.compareTo(((ForeignStudent) anotherStudent).studentName) < 0;
} else {
return true;
}
}
@Override
public String findCountry() {
return countryOfOrigin;
}
}
// Class MyDate:
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;
}
@Override
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 return this.day < myDate.day;
}
public boolean equals(MyDate myDate) {
if (myDate == null) return false;
return (this.day == myDate.day) && (this.month == myDate.month) && (this.year == myDate.year);
}
}
// Class SeniorStudent:
class SeniorStudent extends CanadianStudent {
private double pension;
public SeniorStudent(String studentName, int numberOfCoursesTaken, double pension) {
super(studentName, numberOfCoursesTaken);
this.pension = pension;
}
@Override
public double computeFees() {
return 50.0;
}
@Override
public boolean lessThan(Sortable anotherStudent) {
if(anotherStudent instanceof SeniorStudent) {
return studentName.compareTo(((SeniorStudent) anotherStudent).studentName) < 0;
} else {
return true;
}
}
@Override
public String toString() {
return super.toString() + ", senior citizen who gets pension $" + pension;
}
}
// Class Sort:
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;
}
}
}
// Class Sortable:
interface Sortable {
public boolean lessThan(Sortable anObject);
}
// Class Student:
abstract class Student implements FeeCalculator, Sortable {
private static int totalStudents = 0;
private final 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();
@Override
public String toString() {
return "Student #" + studentNumber +
", Name: " + studentName +
" is from " + findCountry()
+ ", pays fees $" + computeFees();
}
}
// Class University:
class University {
private final Student[] listOfStudents;
private int totalStudents = 0;
private final int maximumNumberOfStudents;
public University(int maximumNumberOfStudents) {
this.maximumNumberOfStudents = maximumNumberOfStudents;
listOfStudents = new Student[maximumNumberOfStudents];
}
public boolean insertStudent(Student aStudent) {
// Safeguard
if(totalStudents >= maximumNumberOfStudents) {
return false;
}
listOfStudents[totalStudents] = aStudent;
totalStudents++;
return true;
}
public int numberOfStudents(String nameOfCountry) {
int total = 0;
for(int i = 0; i < totalStudents; i++) {
if(listOfStudents[i].findCountry().equals(nameOfCountry)) {
total++;
}
}
return total;
}
@Override
public String toString() {
String desc = "";
desc += "Number of students in university = " + totalStudents + " ";
for(int i = 0; i < totalStudents; i++) {
desc += listOfStudents[i].toString() + " ";
}
return desc;
}
public void sortStudents() {
Sort.sortAnything(listOfStudents, totalStudents);
}
//////////////////////////////////////////////////////////
//// Added Methods /////
/////////////////////////////////////////////////////////
/**
* Method to return the students details in a array list who belongs to
* the country specified by the countryName
* @param countryName
* @return
*/
public ArrayList<String> getByCountry(String countryName) {
ArrayList<String> students = new ArrayList<>();
for(int i = 0; i < totalStudents; i++) {
if(listOfStudents[i].findCountry().equals(countryName)) {
students.add(listOfStudents[i].toString());
}
}
return students;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.