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

generic queue class using java

you will 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 (as mentioned in class) where elements are only added and remove 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 posted online that must be used with this assignment. The expected output and description of GenericQueue is found below.
Class GenericQueue:
It is generic, therefore it needs a type parameter (let’s 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.
Obtain the modified versions of CanadianStudent, Student, CanadianStudentUnder65, and ForeignStudent from the assignment posting, and import these into your project. I have removed the interface implementations from these classes, as we do not need them for this assignment. Import GenericTest as well, which is the tester class for this assignment.
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: John 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: John 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: John 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:
***************************************************

Answer:

CanadianStudent Class

public abstract class CanadianStudent extends Student {

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

CanadianStudentUnder65 Class

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;
		}
	}
}

ForeignStudent Class

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;
	}
	
}

GenericQueue Class

public class GenericQueue<T> {
	
   private int front,back,numItems,maxSize;
   private T[] myQueue;
   
   @SuppressWarnings("unchecked")
   /* Constructor */
   public GenericQueue() {
      maxSize = 100;
      myQueue = (T[]) new Object[maxSize];
      back = -1;
      front = 0;
      numItems = 0;
   }
   
   /*isEmpty method*/
   public boolean isEmpty() {
      return numItems == 0;
   }
   
   /*isFull method*/
   public boolean isFull() {
      return numItems == maxSize;
   }
   
   /*enqueue method*/
   public void enqueue(T item) {
      back++;
      myQueue[back] = item;
      if (back == maxSize - 1) {
         back = -1;
      }
      numItems++;
   }
   
    /*dequeue method*/
   public T dequeue() {
      T s = myQueue[front];
      front++;
      if (front == maxSize) {
         front = 0;
      }
      numItems--;
      return s;
   }
   
   /*toString method*/
   public String toString() {
      String s = "";
      s+="***************************************************\n";
      s+="The queue has "+ numItems+" elements:\n ";
      if (back >= front) {
         for (int i = front; i < back+1; i++) {
            s += myQueue[i] + "\n ";
         }
      }
      else {
         if (back >= 0) {
            for (int i = front; i < back+1; i++) {
               s += myQueue[i] + "\n ";
            }
         }
         for (int i = maxSize - 1; i >= front; i--) {
            s += myQueue[i] + " \n";
         }
      }
      if(back < front){
    	  numItems=0;
    	  s="The queue has "+ numItems+" elements:\n ";
      }
      s+="***************************************************\n";
      return s;
   }
}

GenericTest Class

public class GenericTest{
	
	public static void main(String args[]){
		GenericQueue<Student> studentQueue = new GenericQueue<Student>();
		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("John", 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());
		
	}
}

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;
	}

	@Override
	public double computeFees() {
		return 50.0;
	}
	
	@Override
	public String toString() {
		return super.toString() + ", senior citizen who gets pension $" + pension;
	}	
}

Student Class

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(); 
	}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!