Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java, make a generic queue class called GenericQueue. A queue is a data structure similar to a stack. However, a stack is last-in-first-out (LIFO)

In Java, make a generic queue class called GenericQueue. A queue is a data structure similar to a stack. However, a stack is last-in-first-out (LIFO) and a queue is first-in-first-out (FIFO). To elaborate, a stack is like a stack of dishes where elements are only added and removed from the same end of the data structure. A queue, on the other hand, is like a lineup at a grocery store. The first person to enter the line is the first one to get served, and consequently is the first one to get removed from the line. So, instead of push and pop to add and remove elements, a queue has enqueue and dequeue. The enqueue method adds an element to the end of the queue. The dequeue method returns the first element in the queue and removes it from the queue. Several classes are included below that MUST be used with this question. The expected output and description of GenericQueue is found below.

Class GenericQueue:

It is generic, therefore it needs a type parameter, lets call it T. It has an ArrayList instance variable called myQueue. The base type of the ArrayList is T as well. It has a method to check if it is empty, called isEmpty. isEmpty returns true if myQueue is empty, and false otherwise. As described above, GenericQueue has a method called enqueue which accepts a T and adds it to the queue. It also has a method called dequeue which returns a T, the value at index 0 of the queue (if one exists). If no value exists in the queue, return null. The value returned must also be removed from the queue. Lastly, this class has a toString method which first prints *************************************************** on its own line, followed by The queue has followed by the number of elements, followed by elements: on another line. It then prints the toString of all of the elements in the queue on their own line. Finally, it prints *************************************************** once more.

Below you can find CanadianStudent, Student, CanadianStudentUnder65, and ForeignStudent classes, import these into the project. Use GenericTest as well, which is the tester class for this question.

Expected Output:

***************************************************

The queue has 3 elements:

Student #1, Name: Rene is from France, pays fees $4000.0

Student #2, Name: Li is from China, pays fees $3000.0

Student #3, Name: Rakesh is from India, pays fees $2000.0

***************************************************

***************************************************

The queue has 4 elements:

Student #2, Name: Li is from China, pays fees $3000.0

Student #3, Name: Rakesh is from India, pays fees $2000.0

Student #4, Name: Lynne is from Canada, pays fees $800.0

Student #5, Name: Tanya is from Canada, pays fees $800.0

***************************************************

***************************************************

The queue has 5 elements:

Student #3, Name: Rakesh is from India, pays fees $2000.0

Student #4, Name: Lynne is from Canada, pays fees $800.0

Student #5, Name: Tanya is from Canada, pays fees $800.0

Student #6, Name: Chris is from Canada, pays fees $800.0

Student #7, Name: Ryan is from Canada, pays fees $800.0

***************************************************

***************************************************

The queue has 7 elements:

Student #4, Name: Lynne is from Canada, pays fees $800.0

Student #5, Name: Tanya is from Canada, pays fees $800.0

Student #6, Name: Chris is from Canada, pays fees $800.0

Student #7, Name: Ryan is from Canada, pays fees $800.0

Student #8, Name: Bob is from Canada, pays fees $50.0, senior citizen who gets pension $45000.0

Student #9, Name: Tyler is from Canada, pays fees $50.0, senior citizen who gets pension $62000.0

Student #10, Name: Mary is from Canada, pays fees $50.0, senior citizen who gets pension $65000.0

***************************************************

***************************************************

The queue has 4 elements:

Student #7, Name: Ryan is from Canada, pays fees $800.0

Student #8, Name: Bob is from Canada, pays fees $50.0, senior citizen who gets pension $45000.0

Student #9, Name: Tyler is from Canada, pays fees $50.0, senior citizen who gets pension $62000.0

Student #10, Name: Mary is from Canada, pays fees $50.0, senior citizen who gets pension $65000.0

***************************************************

***************************************************

The queue has 0 elements:

***************************************************

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;

}

}

}

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 String toString() {

return super.toString() + ", senior citizen who gets pension $" + pension;

}

}

class Student:

public abstract class Student{

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 abstract double computeFees();

public String toString() {

return "Student #" + studentNumber +

", Name: " + studentName +

" is from " + findCountry()

+ ", pays fees $" + computeFees();

}

}

class GenericTest:

public class GenericTest{

public static void main(String args[]){

GenericQueue studentQueue = new GenericQueue();

studentQueue.enqueue(new ForeignStudent("Rene", 4, "France", new MyDate("12/12/12")));

studentQueue.enqueue(new ForeignStudent("Li", 3, "China", new MyDate("9/9/9")));

studentQueue.enqueue(new ForeignStudent("Rakesh", 2, "India", new MyDate("01/01/01")));

System.out.println(studentQueue.toString());

studentQueue.dequeue();

studentQueue.enqueue(new CanadianStudentUnder65("Lynne", 5));

studentQueue.enqueue(new CanadianStudentUnder65("Tanya", 5));

System.out.println(studentQueue.toString());

studentQueue.dequeue();

studentQueue.enqueue(new CanadianStudentUnder65("Chris", 5));

studentQueue.enqueue(new CanadianStudentUnder65("Ryan", 5));

System.out.println(studentQueue.toString());

studentQueue.dequeue();

studentQueue.enqueue(new SeniorStudent("Bob", 5, 45000));

studentQueue.enqueue(new SeniorStudent("Tyler", 3, 62000));

studentQueue.enqueue(new SeniorStudent("Mary", 4, 65000));

System.out.println(studentQueue.toString());

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

System.out.println(studentQueue.toString());

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

studentQueue.dequeue();

System.out.println(studentQueue.toString());

}

}

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 String findCountry() {

return countryOfOrigin;

}

}

class CanadianStudent:

public abstract class CanadianStudent extends Student {

public CanadianStudent(String studentName, int numberOfCoursesTaken) {

super(studentName, numberOfCoursesTaken);

}

@Override

public String findCountry() {

return "Canada";

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

To find integral of sin(logx) .

Answered: 1 week ago

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

c. What groups were least represented? Why do you think this is so?

Answered: 1 week ago

Question

7. Describe phases of multicultural identity development.

Answered: 1 week ago