Comparable Class - Create Priority Queue For Airline Passengers
Oct 9, 2014
My assignment was to create a priority queue for Airline Passengers. Here is what I have done so far:
//Driver
package priorityqueuestandby;
import java.util.NoSuchElementException;
import javax.swing.JOptionPane;
public class PriorityQueueStandBy {
public static void main(String[] args) {
[Code] .....
So the part that I cant figure out is:
When a standby passenger is to be enqueued into the priority queue, it must be done so that at the moment of each dequeue operation, the item at the head of the queue is the standby passenger with the longest longevity, and also so that passengers with the same longevity are dequeued in a first-come-first-served fashion.
he says that we need to "Make your program so that it extends Comparable and implements the compareTo() method properly..."
So I was looking at the Comparable class and I could't find a compareTo() method... I am not confident I know how extends works either. I am assuming I need a new class if I am going to be extending another class. Right now I am taking in longevity as a String and converting it to an int because my last ditch effort is going to be to set up a loop that will organize longevity into a/an circular array based on the size of the incoming integer.
I have a class "ExecuteJob" which has Print Q in the form of Priority Q.
You can keep adding job to the Q by calling one of the method in the class. However, and object cant do things simultaneity can it? While im adding a new job to the print queue, can it be executing and existing job in the print Q.
To achieve that, I would need to implement process and threads? I believe am I right? So that adding a job is independent to being removed?
I am in the process of implementing Priority queue, as I understand that there are many data structures you could use to implement. I implemented it with the an array, which it works absolutely fine. However I have limitations on what collections I can use from the collections classes. I fact I cant use any of the collections classes. Meaning I cant use array.
I’m trying to implement Priority Queue using heap. And implementing heap using binary trees. But however I have a few questions which I need to clarify and I cant think of any other way of resolving it. Ofcourse I can implement my own simple array class using linked list.
Inserting into heap would be quite simple, as I just need to find the right last position from left to right leaf to insert the node into the tree. However after inserting, you may want to make sure that leaf node values are > than root node. Therefore, the root node will always be with the highest priority.
I call these steps where you compare from top down as bubbledown and bubbleup. To do this I really need a for each node within the treee node to have attribute pointing to its root node. So in case of bubbleup I always have a pointer for a given node to its root, without it would mean I would to traverse through the entire tree to identify its root. Which I believe is very inefficient.
Or have I taken this completely wrong? Or is it the case that heap are only best with arrays and therefore use array (by implement it using linked list?)
I'm working on a lab for my class. I need to create a Priority Queue in Java using an array list. The kicker is that each node has to have a "handle" which is just an object which contains the index of of the node that it's associated with. I know that sounds weird but we have to implement it this way. Anyway, my priority queue seem to work fine except the handle values apparently aren't updating correctly because I fail the handle test. Also, I run into problems with handles only after extractMin() is called, so I figured the problem would be somewhere in that method but I've been through it a million times and it looks good to me.
Here is my Priority Queue Class:
package lab3; import java.util.ArrayList; /** * A priority queue class supporting operations needed for * Dijkstra's algorithm. */ class PriorityQueue<T> { final static int INF = 1000000000; ArrayList<PQNode<T>> queue;
[Code] ....
I can post the tests too but I don't know if that would do much good. Just know that the queue works as is should, but the handles don't point to the right nodes after extractMin() is utilized.
I was given some code by a professor to add some features to as part of an assignment. However, the code itself doesn't seem to work.
import java.util.HashSet; import java.util.InputMismatchException; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; public class DijkstraPriorityQueue
[Code] ....
The method to find minimum distance is nonfunctional...I receive an error that the types are incompatible. I can't do the assignment if the base code doesn't work to begin with...
I want to create java program in which i want to inherit stack and queue from a linked list class and also make infix to postfix inherit from stack and periority queue from queue class.Ho can i make this program.
Below is the requirements and code. I am getting the error CODELAB ANALYSIS: LOGICAL ERROR(S)We think you might want to consider using: >
Hints:
-Correct solutions that use equals almost certainly also uses high -Correct solutions that use equals almost certainly also uses low
Assume the existence of a Widget class that implements the Comparable interface and thus has a compareTo method that accepts an Object parameter and returns an int . Write an efficient static method , getWidgetMatch, that has two parameters . The first parameter is a reference to a Widget object . The second parameter is a potentially very large array of Widget objects that has been sorted in ascending order based on the Widget compareTo method . The getWidgetMatch searches for an element in the array that matches the first parameter on the basis of the equals method and returns true if found and false otherwise.
public static boolean getWidgetMatch(Widget a, Widget[] b){ int bot=0; int top=b.length-1; int x = 0; int y=0; while (bot >= top)
How do you enforce any class which implements an interface should also implement comparable too? Say for instance you may have an interface
public interface Task { ... } public class DoThis implements Task { ... } public class DoThis1 implements Task { ... }
I want all of the classes which implements the interface Task to implement comparable too. Of course I can just say implements Task, Comparable. But is there something which we could do from interface level, i mean interface Task level?
I have a Queue class (containing a LinkedList plus a few other variables and stats for my project), which works fine with the standard LinkedList, but I'm trying to add my own code for MyLinkedList.
However, I keep getting a NullPointerException at my remove method.
public class MyLinkedList<T> { Node head; public MyLinkedList() { head = null; } public class Node { T contents; Node nextNode;
I reserved one row for first class and another for economy class. This code works just as I want but my question is how can I loop through it so I don't need all the if statements? I tried it many ways. I tried something like this but I cant get it to work.
for (int row=0;row<seat.length;row++{ for (int col=0;col<seat[row].length;col++){ if (seat[0][col]==false){ seat[0][col]=true; System.out.println("You have number 0" + row + in first class);
In the class below I'm trying to create a class that will accept dates in various formats and create a range. The first constructor is easy because I send it the begin date and end date as Date objects. Now I want to send a month(and year) in a constructor and derive the begin and end dates from it. In my constructor that accepts the month/year I need to put the this(startDate, endDate) at the top to be allowed, but the parameters are not built yet.
package com.scg.athrowaway; import java.util.Calendar; import java.util.Date; public class DateRange { private Date startDate; private Date endDate;
class test{//class public static void main(String[]args) { String booking [][]= new String [30] [6] ;//two dimensional array System.out.println("Enter the seat column you want");//column entry char column=Keyboard.readChar(); System.out.println("Enter the seat row you want");//row entry int row=Keyboard.readInt();
I am doing a pretty basic program with a fixed origin city and five destination cities. I am having trouble with entering passenger details. This is what I tried, but then I cannot retain all passenger details to print later on in the final ticket.
System.out.println("Enter flight details of child passengers (below 12 years)"); System.out.println("Enter Name"); for(a = 1; a <= cp; ++a) { //cp is number of child passengers, a is loop variable String name = br.readLine(); System.out.println("1. " + name); }
How to create object for "class B" and call the "function_B" from other different class D where class D has no connection with class A? Here is my program.
public class A(){ void print(){} } class B{ void function_B(){} } class C{ void function_C(){} }
Here, A, B, C are in the same package. But class D is in different package.
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. There is also a MyDate class as explained below. A person has a name, address, phone number, and email address. A student has a status (freshman, sophomore, junior, or senior). Define the status as an integer which can have the value 0 (for "Freshman"),
1 (for "Sophomore"), 2 (for "Junior"), and 3 (for "Senior"),
but don't allow the status to be set to any other values. An employee has an office, salary, and dateHired. The dateHired is a MyDate field, which contains the fields: year, month, and day. The MyDate class does not explicitly inherit from any class, and it should have a no-arg constructor that sets the year, month, and day to the current year, month, and day. The MyDate class should also have a three-argument constructor that gets three int arguments for the year, month and day to set the year, month and day.
A faculty member has office hours and a rank. Define the rank as a String (for values like "Professor" or "Instructor"). A staff member has a title, which is also a String. Use data types for the fields as specified, or where one is not specified, use a data type that is appropriate for the particular field. Write a test program called TestEveryone.java that creates a Person, Student, Employee, Faculty, and Staff object, and invoke their toString() method (you don't need to call the objects' toString() method explicitly).
Note: Your MyDate.java class is the object class that your dateHired field is created from in the Employee.java class.
Do not use the Person, Employee or Faculty classes defined on pages 383 and 384 of the book. Create new ones.Here is the code I have so far concerning the employee and MyDate.
public class Employee extends Person { private String office; private double salary; //private MyDate dateHired; //7 argument constructor for employee public Employee(String name, String phoneNumber, String email, String address, String office, double salary /*MyDate dateHired*/) { super(name, phoneNumber, email, address);
The assignment is to create a SmartString class that implements a SmartStringInterface class (created by professor) and implements a few methods. We are basically taking a string and then taking various substrings and inserting, deleting them and undoing changes as well. Here are the methods in the interface to use along with the parameters.
public interface SmartStringInterface { public void insert(int pos, String sstring); public void delete(int pos, int count); public void undo(); public String toString();
The Undo is supposed to be able to be called multiple times (to be tested using a driver program that we must create) but the part that's got me is that the changes are only supposed to be stored. Currently, I am storing the "new" string after each change onto a stack, so that undo can just pop off the stack and it will revert to the previous string. Professor said that was wrong, so I don't know how to do it. Here is what I have so far (some of the code we have is using default StackADT stuff from our book, so if you need that I can post as well. You can see in the undo method where I currently save the string. We can use multiple stacks if needed, but the less the better. Must use at least 1. The exception code is already coded for us in another file also. I am only having to code these methods and the driver to test.
import java.util.Arrays; public class ArrayStack<T> implements StackADT<T> private final static int DEFAULT_CAPACITY = 100 private int top; private T[] stack;
I have an assignment to complete where I have to develop a Java Console application in Eclipse which accepts regional zone information for premises based upon addresses provided. The program needs to figure out which geographical zone each customer lives in and based upon their age, set a priority rating.
The zones (which is the Belfast and the directions) and sub-zones (which are the Postcode such as "BT1" are:
1.Display a count of addresses within a user defined geographical zone. 2.Display all information for customers within a user defined geographical zone. 3.Display a complete set of captured data. 4.Display a prioritized list of customer details within each geographical zone. 5.Display a count of customers within each geographical zone.
Below is the code I have created so far for it.
package assignment1;
import java.io.BufferedReader; import java.io.InputStreamReader; public class Assignment1 { private static InputStreamReader input = new InputStreamReader(System.in); private static BufferedReader reader = new BufferedReader(input);
[Code] .....
The main issue I'm having is to make sure that data like address and customer counts, and customer details appear only for the selected areas.
I am asked to create a code that if a user enters 1 it will use the object natural comparison form ('default') as written in CompareTo method.But if he chooses to enter something else then another comparison is used.Maybe I just need to use 2 diff comparators? but then what;s the point of defining something as 'default'....
I have the following code that will make linked list and order its elements using self referential objects. but i have the following error: incompatible types
required: ListNode<T#2> found: ListNode<T#1> where T#1,T#2 are type-variables: T#1 extends Comparable declared in method <T#1>insertInOrder(T#1) T#2 extends Comparable declared in class OrderedList
import java.util.*; public class ListNode<T> { ListNode<T> nextNode; T data; public ListNode(T item) { this(item, null);
I have a task to create a Java OOP program, I have a class Team which requires a comparable and iterable interface, the only way I know how to do this is either:
public class Team implements Iterable <Mechanic> or public class Team implements Comparable <Mechanic>
Which of the following classes uses Comparable and Comparator?
QueueTreeSetStackPriorityQueue
In the above question, what does 'uses' mean? Does it mean do above classes implement Comparable and Comparator?
I know that in order to compare any two elements stored in one of the above classes, we need to make the elements' class to implement one of these - either Comparable or Comparator.