We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,858 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
Page 3 of Article: priority queue and hash tables
i am doing a project with a group i have to deal with orders by kind then linked to the driver who will be taking the first customer. the driver can be free or back in 10 min. i have started with the order list with the customer, i have…

i changed it to this and this and now i am stuck with the comparator prblem i mentioned before

PriorityQueue<Orders1> queue = new PriorityQueue<Orders1>(179, new Comparator<Orders1>(){

                public int compare(Orders1 orderA, Orders1 orderB){

                if(orderA.getKind().equals("urgent") && ! orderB.getKind().equals("urgent")){
                    return 1;

                  if(orderA.getPriority() < orderB.getPriority()){
                    return 1;

                 if(orderB.getPriority() > orderA.getPriority()){

                return 1;

            }
        }
    }


            }


        });
chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Are there any errors? Please post the full text.

Why does the method always return a value of 1? It should return different values depending on A and B.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Orders1 is not an abstract and does not override abstract method compare(java.lang.Object, java.lang.Object) in java.util.Comparator




public class Orders1 implements Comparator{

it is pointing to the c in class
do you mean like this

PriorityQueue<Orders1> queue = new PriorityQueue<Orders1>(179, new Comparator<Orders1>(){

                public int compare(Orders1 orderA, Orders1 orderB){

                if(orderA.getKind().equals("urgent") && ! orderB.getKind().equals("urgent")){
                    return 1;

                  if(orderA.getPriority() < orderB.getPriority()){
                    return 1;

                 if(orderB.getPriority() > orderA.getPriority()){

                return -1;

            }
        }
    }


            }


        });
chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

implements Comparator

Either remove the above or correctly code the class.

Read how to code here:
Click Here

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

i am still trying to be able to use the priority queue but all i am getting is the file being read it is not sorted at all.

import java.io.*;
import java.util.PriorityQueue;
//import java.lang.Comparable;
import java.util.Comparator;
import java.util.*;

public class Orders1 implements Comparator<Orders1>{


    private String Name;
    private String Kind;
    private String PickUp;
    private String DropOff;
    private String TypeOfService;
    private String Requirements;
    private int priority;



    public Orders1(String Name, String Kind, String PickUp, String DropOff, String TypeOfService, String Requirements, int priority) {
        this.Name = Name;
        this.Kind = Kind;
        this.PickUp = PickUp;
        this.DropOff = DropOff;
        this.TypeOfService = TypeOfService;
        this.Requirements = Requirements;
        this.priority = priority;
    }

    /**
     * @return the name
     */
    public String getName() {
        return Name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * @return the kind
     */
    public String getKind() {
        return Kind;
    }

    /**
     * @param kind the kind to set
     */
    public void setKind(String Kind) {
        this.Kind = Kind;
    }

    /**
     * @return the pick up
     */
    public String getPickUp() {
        return PickUp;
    }

    /**
     * @param pick up the pick up to set
     */
    public void setPickUp(String PickUp) {
        this.PickUp = PickUp;
    }

    /**
         * @return the drop off
         */
        public String getDropOff() {
            return DropOff;
        }

        /**
         * @param drop off the drop off to set
         */
        public void setDropOff(String DropOff) {
            this.DropOff = DropOff;
    }

    /**
         * @return the type of service
         */
        public String getTypeOfService() {
            return TypeOfService;
        }

        /**
         * @param type of service the type of service to set
         */
        public void setTypeOfService(String TypeOfService) {
            this.TypeOfService = TypeOfService;
    }

    /**
         * @return the requirements
         */
        public String getRequirements() {
            return Requirements;
        }

        /**
         * @param requirements the requirements to set
         */
        public void setRequirements(String Requirements) {
            this.Requirements = Requirements;
    }

    /**
             * @return the priority
             */
            public int getPriority() {
                return priority;
            }

            /**
             * @param requirements the priority to set
             */
            public void setPriority(int priority) {
                this.priority = priority;
    }



              public int compare(Orders1 orderA, Orders1 orderB){

                            if(orderA.getPriority() < orderB.getPriority())

                            return -1;

                            else if(orderA.getPriority() > orderB.getPriority())

                            return 1;

                            else

                            return 0;



            }

            public String toString(){

            return  "\t" + Name + "\t\t" + Kind + "\t\t" + PickUp + "\t\t" + DropOff + "\t\t" + TypeOfService + "\t\t" + Requirements;
        }





            /*public int compareTo(Object otherKind){

                            if(booked_in_advance() > urgent)

                            return 1;

                            else if(booked_in_advance < urgent)

                            return -1;

                            else

                            return 0;

            }*/

            PriorityQueue<Orders1> priorityQueue = new PriorityQueue<Orders1>();



        public static void main (String []args) throws IOException
            {

            String out="";
                  BufferedReader in = new BufferedReader(new FileReader("companyOrders.txt"));
                String str;
                while ((str = in.readLine()) != null)
                 {
                    out+=str+"\n";
                }
                in.close();
                System.out.print(out);
    }
        //Orders1[] orders1 = new Orders1[100];



            /*  while (!prioritQueue.isEmpty()) {
                      System.out.println(priorityQueue.remove());
 }*/


            //Orders1 o1 = newOrders1("booked in advance",



    }
chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Where is the code and logic that will do any sorting?

Can you print out the contents of the queue that shows what order it is created in?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
 public int compare(Orders1 orderA, Orders1 orderB){

                            if(orderA.getPriority() < orderB.getPriority())

                            return -1;

                            else if(orderA.getPriority() > orderB.getPriority())

                            return 1;

                            else

                            return 0;
chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What code will call the compare() method?
When will it be called?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

i getting about six errors with it like illegal from the w and the !
; expected and others

                while (!prioritQueue.isEmpty()) {
                      System.out.println(priorityQueue.remove());
 }
chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please post the full text of the error messages.

Where in the posted code do you add anything to the queue?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

all the examples i have seen is where you add to the queue but none where you read in a file

chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You do not need a file for testing the queue. Just hardcode some calls to the add() method 4+ times and printout the contents of the queue to see if it is sorted correctly.
Save the reading of the file for the next project. Get the queue to work now.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

i learn by direct examples so don't have to guess and do a run around when there are examples of direct path

chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Examples are OK when you're writing something that has already been written meny times, but far less useful when you are doing anything new. Reading and using the API JavaDoc is an absolutely essential Java skill that you need to start practicing now.

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

Put this in the main method to add some items to the queue for testing:

priorityQueue.add(new Orders1(...)); // add some items to the queue
priorityQueue.add(new Orders1(...));
priorityQueue.add(new Orders1(...));
priorityQueue.add(new Orders1(...));
System.out.println("pQ=" + priorityQueue); // Show its contents

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

from what i have seen in priority queue online they don' have the addElement and remove within their code is it seperate, is it suppose to be there somewhere because this is what i have now and i have an error saying

the method addElement (Orders1[]) is undefined for the type og PriorityQueue<Orders1>



import java.io.*;
import java.util.PriorityQueue;
import java.lang.Comparable;
//import java.util.Comparator;
import java.util.*;
import java.io.FileReader;
import java.util.Scanner;
import java.io.FileInputStream;
//import java.lang.*;

public class Orders1 implements Comparable<Orders1>{


    private String Name;
    private String Kind;
    private String PickUp;
    private String DropOff;
    private String TypeOfService;
    private String Requirements;
    private int priority;



    public Orders1(String Name, String Kind, String PickUp, String DropOff, String TypeOfService, String Requirements, int priority) {
        this.Name = Name;
        this.Kind = Kind;
        this.PickUp = PickUp;
        this.DropOff = DropOff;
        this.TypeOfService = TypeOfService;
        this.Requirements = Requirements;
        this.priority = priority;
    }

    /**
     * @return the name
     */
    public String getName() {
        return Name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * @return the kind
     */
    public String getKind() {
        return Kind;
    }

    /**
     * @param kind the kind to set
     */
    public void setKind(String Kind) {
        this.Kind = Kind;
    }

    /**
     * @return the pick up
     */
    public String getPickUp() {
        return PickUp;
    }

    /**
     * @param pick up the pick up to set
     */
    public void setPickUp(String PickUp) {
        this.PickUp = PickUp;
    }

    /**
         * @return the drop off
         */
        public String getDropOff() {
            return DropOff;
        }

        /**
         * @param drop off the drop off to set
         */
        public void setDropOff(String DropOff) {
            this.DropOff = DropOff;
    }

    /**
         * @return the type of service
         */
        public String getTypeOfService() {
            return TypeOfService;
        }

        /**
         * @param type of service the type of service to set
         */
        public void setTypeOfService(String TypeOfService) {
            this.TypeOfService = TypeOfService;
    }

    /**
         * @return the requirements
         */
        public String getRequirements() {
            return Requirements;
        }

        /**
         * @param requirements the requirements to set
         */
        public void setRequirements(String Requirements) {
            this.Requirements = Requirements;
    }

    /**
             * @return the priority
             */
            public int getPriority() {
                return priority;
            }

            /**
             * @param requirements the priority to set
             */
            public void setPriority(int priority) {
                this.priority = priority;
    }

                public boolean addElement(){


                }


              public int compareTo(Orders1 order){

                            if(order instanceof Orders1){
                                Orders1 orders1 = (Orders1) order;
                                if(this.priority > orders1.getPriority())
                                    return 1;



                                else if(this.priority < orders1.getPriority())

                                    return -1;


                            }
                            return 0;








            }

            public String toString(){

            return  "\t" + Name + "\t\t" + Kind + "\t\t" + PickUp + "\t\t" + DropOff + "\t\t" + TypeOfService + "\t\t" + Requirements;
        }




        public static void main (String []args) throws IOException
            {

           /* String out="";
                  BufferedReader in = new BufferedReader(new FileReader("companyOrders.txt"));
                String str;
                while ((str = in.readLine()) != null)
                 {
                    out+=str+"\n";
                }
                in.close();
                System.out.print(out);
    }*/


            File file = new File("companyOrders.txt");

                    FileReader inputFile = new FileReader(file);

                    Scanner scan = new Scanner(file);


            //Orders1 o1 = newOrders1("booked in advance",
                  Orders1[] orders1 = new Orders1[180];
                    PriorityQueue<Orders1> pq = new PriorityQueue<Orders1>();


                    while(scan.hasNext()){

                    }

                    System.out.println("Before Priority Order");

                    while(scan.hasNext())

                                {  

                                     pq.addElement(orders1);



                                }


                    System.out.println("Put in Priority Order: "); 

                        while(!pq.isEmpty())

                        {

                            System.out.println(pq.remove()+ " ");

                        }



                        System.out.println();

                            }

}




    }
}

i am adding your e.g but getting errors with new saying cannot find symbol
constructor Orders1(java.lang.String

chinee
Junior Poster
118 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

cannot find symbol
constructor Orders1(java.lang.String

You need to study how to call the constructor of a class to create an instance of a class.

i have an error saying

Please post the full text of any error messages. Your one line does not show all the information in the message.

Your attempt at testing the PQ is too complicated. Do it the simple way:
create a PQ
add 4 items to the PQ
print out the contents of the PQ

Nothing more just those 4 steps.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1166 seconds using 2.77MB