Stacks and Priority Queues

Search stack for tasks for current day and insert into priority queue

Page 1 of 1

9 Replies - 2203 Views - Last Post: 02 April 2009 - 11:27 AM Rate Topic: -----

#1 pansylea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-November 08

Stacks and Priority Queues

Post icon  Posted 30 March 2009 - 08:39 AM

this is just my tester class, i need to search a stack for tasks that are due for completion on the current day and add them to the priority queue. I have gotten the current day stored into a varible, but am having no luck with getting the date from each of the tasks through searching. Any help would be appreciated. Thanks

   
////////////////////////////////////////////////////////////////
   import java.util.ArrayList;
   import java.io.*;
   import java.util.Date;
   import java.text.DateFormat;
   import java.text.SimpleDateFormat;
   import java.text.ParseException;

	public class Tester
   {
	   public static void main(String[] args) throws IOException
	  {
		 int pos = 0;
		 String taskDate = "";
		 String t = "";
	  
		 DateFormat df=new SimpleDateFormat("dd/MM/yyyy");
		 Date date=new Date();
		 String currentDate=df.format(date);
		  
		 ArrayStack as = new ArrayStack( );
	  
		 for ( int i = 0; i < 10; i++ )
		 {
			if (as.push(new Task("Meeting " + i, "Business " + i, 2, "30", "03", 2009)))
			   System.out.println("Task # " + i + " successfully pushed onto stack");
			else
			   System.out.println("Task # " + i + " not pushed - stack full");
		 }
	  
		 System.out.println( "Stack:\n" + as );
	  
		 Task temp;
	  
		 try
		 {
			for ( int i = 0; i < 10; i++ )
			{
			   temp = as.peek( );
			   System.out.println( "peeking " + temp );
			   temp = as.pop( );
			   System.out.println( temp + " popped" );
			}
			System.out.println( "Stack:\n" + as );
		 }
			 catch ( DataStructureException dse )
			{
			   System.out.println( dse.getMessage( ) );
			}
			  
		 for ( int i = 0; i < 5; i++ )
			as.push( new Task( "Assignment Due " + i, "Educational", 3, "01","04",2009 ) );
		 for ( int i = 0; i < 3; i++ )
			as.push( new Task( "Presentation " + i, "Business", 2, "22", "04", 2009 ) );
	  
		 System.out.println( "Stack:\n" + as );
		  
			  
		 PriorityQ thePQ = new PriorityQ(100);
		 try
		 {
			for ( int i = 0; i < 100; i++ )
				t = as.toString();
				pos = t.lastIndexOf("Date: ");	  // Search backwards for a string
				taskDate = t.substring(pos+6); // Extract word following "Date: "
				System.out.println(taskDate);
					System.out.println(currentDate);
		 
		 		if (taskDate == currentDate)
						thePQ.insert(as(i));
		 // 								thePQ.insert(as(i));
		 }
		 
			
			 catch (Exception dse )
			{
			   System.out.println( dse.getMessage( ) );
			}
				   //System.out.println(taskDate); 
					  //if (taskDate == currentDate)
						  //thePQ.insert(i);
		  
	  }  // end main()
   //-------------------------------------------------------------
   }  // end class PriorityQApp


This post has been edited by pansylea: 30 March 2009 - 11:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stacks and Priority Queues

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Stacks and Priority Queues

Posted 30 March 2009 - 11:16 AM

Please post your Task and PriorityQ classes.

Thanks!

This post has been edited by n8wxs: 30 March 2009 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 pansylea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-November 08

Re: Stacks and Priority Queues

Posted 30 March 2009 - 11:37 AM

task class
   import java.util.*;
   import java.io.*;
   import java.util.Scanner;

	class Task implements Comparable
   {
	  private String description;
	  private String category;
	  private int urgency;
	  private String day;
	  private String month;
	  private int year;
   	
	  private String descIn;
	  private String catIn;
	  private int urgencyIn;
	  private String dayIn;
	  private String monthIn;
	  private int yearIn;
   	
	  protected static Scanner keyboard = new Scanner(System.in);	
   	
	   public Task()
	  {
		 description = "";
		 category = "";
		 urgency = 0;
		 day = "";
		 month = "";
		 year = 0;
	  }
   	
	   public Task(String taskDesc, String taskCat, int taskUrgency, String taskDay, String taskMonth, int taskYear)
	  {
		 description = taskDesc;
		 category = taskCat;
		 urgency = taskUrgency;
		 day = taskDay;
		 month = taskMonth;
		 year = taskYear;
	  }
   		
	   public void display()	   
	  {
		 System.out.print("Description: " + description);
		 System.out.print(", Category: " + category + ", Urgency: " + urgency);
		 System.out.print(", Date: " + day + "/" +month+ "/" +year);
	  }
   	 
	   public void setDesc(String descIn) 
	  { description = descIn; }
   	
	   public String getDesc()	  
	  { 
		 return description; }		  
   				
	   public void setCat(String catIn) 
	  { category = catIn; }
   	
	   public String getCat()	  
	  { 
		 return category; }
   		
	   public void setUrgency(int UrgencyIn) 
	  { urgency = UrgencyIn; }
   	
	   public int getUrgency()	  
	  { 
		 return urgency; }
   	
	   public void setDay(String dayIn) 
	  { day = dayIn; }
   	
	   public String getDay()	  
	  { 
		 return day; }
   	
	   public void setMonth(String monthIn) 
	  { month = monthIn; }
   	
	   public String getMonth()	  
	  { 
		 return month; }
   		
	   public void setYear(int yearIn) 
	  { year = yearIn; }
   	  
	   public int getYear() 
	  { 
		 return year; } 	
   	
	   public String getDescIn()	  
	  { 
		 return descIn; }
   		
	   public String getCatIn()	  
	  { 
		 return catIn; }
   		
	   public int getUrgencyIn()	  
	  { 
		 return urgencyIn; }
   		
	   public String getDayIn()	  
	  { 
		 return dayIn; }
   		
	   public String getMonthIn()	  
	  { 
		 return monthIn; }
   	
	   public int getYearIn()	  
	  { 
		 return yearIn; }
   	
	   public int compareTo(Object obj)
	  {
			   if (obj instanceof Task) 
			   {
				  Task tsk = (Task) obj;
				  if (this.year > tsk.getYear())
					 return 1;
				  else if (this.year < tsk.getYear());
				  return -1;
			   }
			   return 0;
	  } 	
   	
	   public String toString() {
			
		 StringBuffer buffer = new StringBuffer();
		 buffer.append("Description: " + description);
		 buffer.append(", Category: " + category + ", Urgency: " + urgency);
		 buffer.append(", Date: " + day + "/" +month+ "/" +year);
			
		 return buffer.toString();
	  }
   	  
	   public void read(){
		 System.out.print("Description : ");descIn=keyboard.nextLine();
		 System.out.print("Category : ");catIn=keyboard.nextLine();
		 System.out.print("Urgency : ");urgencyIn=keyboard.nextInt();
		 System.out.print("Day : ");dayIn=keyboard.nextLine();
		 System.out.print("Month : ");monthIn=keyboard.nextLine();
		 System.out.print("Year : ");yearIn=keyboard.nextInt();
		  
	  //		  boolean goodInput = false;
	  //	   
	  //		  do{		
	  //			 try{
	  //				setDay(dayIn);
	  //				setMonth(monthIn);
	  //				setYear(yearIn);
	  //				goodInput = true;
	  //			 }
	  //				 catch(IllegalArgumentException e){
	  //				   System.out.println("INVALID ARGUMENT PASSED FOR day or month or year");
	  //				   System.out.println(e);
	  //				   System.out.print("RE-ENTER VALID ARGUMENT FOR DAY : ");dayIn=keyboard.nextInt();
	  //				   System.out.print("RE-ENTER VALID ARGUMENT FOR MONTH : ");monthIn=keyboard.nextInt();
	  //				   System.out.print("RE-ENTER VALID ARGUMENT FOR YEAR : ");yearIn=keyboard.nextInt();								
	  //				}
	  //		  }while(!goodInput);
	  //	   }
	  
	  }
   
   }


arrayStack class
   import java.util.ArrayList;
   import java.awt.Color;

	public class ArrayStack
   {
	  private static final int STACK_SIZE = 100; // maximum array size
	  private Task [] stack;			  //
	  private int top;			// last used index; top of the stack
   
	   public ArrayStack( )
	  {
		 stack = new Task[STACK_SIZE];
		 top = -1; // stack is empty
	  }
   
   /** push method
   *   @param	d   double to insert
   *   @return   true if insertion was successful false otherwise
   */
	   public boolean push( Task t )
	  {
		 if ( !isFull( ) ) // is there room to insert?
		 {
			stack[++top] = new Task( t.getDesc( ), t.getCat( ), t.getUrgency( ),t.getDay( ), t.getMonth( ), t.getYear( ) );
			return true;
		 }
		 else
			return false;
	  }
   
   /** pop method
   *   @return   the double deleted
   */
	   public Task pop( ) throws DataStructureException
	  {
		 if ( !isEmpty( ) ) // is there an item to delete?
		 {
			return stack[top--];
		 }
		 else
			throw new DataStructureException
				 ( "Stack empty: cannot pop" );
	  }
   
   /** peek method
   *   @return   the double at the top of the stack
   */
	   public Task peek( ) throws DataStructureException
	  {
		 if ( !isEmpty( ) ) // stack is not empty
			return ( new Task( stack[top].getDesc( ), stack[top].getCat( ), stack[top].getUrgency( ), stack[top].getDay( ), 
			   stack[top].getMonth( ), stack[top].getYear( )) );
		 else
			throw new DataStructureException
				 ( "Stack empty: cannot peek" );
	  }
   
   /** isEmpty method
   *   @return   true if stack is empty, false otherwise
   */
	   public boolean isEmpty( )
	  {
		 return ( top == - 1 );
	  }
   
   /** isFull method
   *   @return   true if stack is full, false otherwise
   */
	   public boolean isFull( )
	  {
		 return ( top == ( STACK_SIZE - 1 ) );
	  }
   
   /** toString method
   *   @return   a String representing the stack
   */
	   public String toString( )
	  {
		 String stackString = "";
	  
		 for ( int i = top; i >= 0; i-- )
			stackString += ( i + ": " + stack[i] + "\n" );
	  
		 return stackString;
	  }
   
   //   public ArrayList<Task> tasks( int s )
   //   {
   //	 ArrayList<Task> result = new ArrayList<Task>( );
   // 
   //	 for ( int i = top; i >= 0; i-- )
   //	 {
   //	   if ( stack[i].getDay( ).equalsIgnoreCase( s ) )
   //		 result.add( stack[i] );
   //	 }
   //	 return result;
   //   }
   }



priority class
class PriorityQ
   {
   private int maxSize;
   private Task [] queArray;		 // unordered array
   private int nItems;
//-------------------------------------------------------------
   public PriorityQ(int s)		  // constructor
	  {
	  maxSize = s;
	  queArray = new Task[maxSize];
	  nItems = 0;
	  }
//-------------------------------------------------------------
   public void insert(Task item)
	  { queArray[nItems++] = item; }
//-------------------------------------------------------------
   public Task peekMin()			// peek at minimum item
	  { return queArray[nItems-1]; }
//-------------------------------------------------------------
   public boolean isEmpty()		 // true if queue is empty
	  { return (nItems==0); }
//-------------------------------------------------------------
   public boolean isFull()		  // true if queue is full
	  { return (nItems == maxSize); }
//-------------------------------------------------------------
   }  // end class PriorityQ


Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Stacks and Priority Queues

Posted 30 March 2009 - 12:11 PM

Thanks for posting the rest.

Besides not having a definition for DataStructureException

I found this:
if (taskDate == currentDate) {
	thePQ.insert(as(i));
}


There is no variable "i" at this point. "i" is used in the preceding for() loop that has exited already.

as(i)
There is no constructor that takes an integer argument. I think you mean
as.pop();

This post has been edited by n8wxs: 30 March 2009 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 pansylea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-November 08

Re: Stacks and Priority Queues

Posted 30 March 2009 - 12:46 PM

sorry i never added the class for the data exception

public class DataStructureException extends Exception
{
  public DataStructureException( String s )
  {
	super( s );
  }
}



		 PriorityQ thePQ = new PriorityQ(100);
		 try
		 {
			for ( int i = 0; i < as.length(); i++ )
				{
				t = as.toString();
				pos = t.lastIndexOf("Date: ");	  // Search backwards for a string
				taskDate = t.substring(pos+6); // Extract word following "Date: "
				System.out.println(taskDate);
					System.out.println(currentDate);
		 
		 		if (taskDate == currentDate)
						thePQ.insert(as.pop());
						}
		 }
		 
			
			 catch (Exception dse )
			{
			   System.out.println( dse.getMessage( ) );
			}
				
				System.out.println("Priority Q: \n" + thePQ);



i still cant get it adding to the priority queue, it's displaying a reference.
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Stacks and Priority Queues

Posted 30 March 2009 - 02:22 PM

Ok. :)

I get the following error:

Quote

init:
deps-clean:
Deleting directory C:\Documents and Settings\Owner\My Documents\NetBeansProjects\dic4\build
Deleting directory C:\Documents and Settings\Owner\My Documents\NetBeansProjects\dic4\dist
clean:
init:
deps-jar:
Created dir: C:\Documents and Settings\Owner\My Documents\NetBeansProjects\dic4\build\classes
Compiling 5 source files to C:\Documents and Settings\Owner\My Documents\NetBeansProjects\dic4\build\classes
C:\Documents and Settings\Owner\My Documents\NetBeansProjects\dic4\src\dic4\Tester.java:63: cannot find symbol
symbol : method length()
location: class dic4.ArrayStack
for (int i = 0; i < as.length(); i++) {
1 error
BUILD FAILED (total time: 0 seconds)

The ArrayStack class does not have a length method.

And,
if (taskDate == currentDate) {
	thePQ.insert(as.pop());
}


you are comparing strings. The Strings class does not have an "==" operator. You need to use if (taskDate.equals(currentDate)) {

See Class String

Quote

i still cant get it adding to the priority queue, it's displaying a reference.

The PriorityQ class does not have a toString() method. So you'll get Object.toString().
Also you are printing the value of the array reference, not the array contents
System.out.println("Priority Q: \n" + thePQ);

This post has been edited by n8wxs: 30 March 2009 - 02:30 PM

Was This Post Helpful? 1
  • +
  • -

#7 pansylea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-November 08

Re: Stacks and Priority Queues

Posted 31 March 2009 - 08:07 AM

i added a length method in the stack class and used the

Quote

if (taskDate.equals(currentDate)) {
. i also added a toString() method in the priority class. but it is printing null records. :(
PriorityQ thePQ = new PriorityQ(100);
		 try
		 {
			System.out.println("Priority Q: \n");
			for ( int i = 0; i < as.length(); i++ )
			{
			   t = as.toString();
			   pos = t.lastIndexOf("Date: ");	  // Search backwards for a string
			   taskDate = t.substring(pos+6); // Extract word following "Date: "
			
			   if (taskDate.equals(currentDate))
				  thePQ.insert(as.pop());
				System.out.println(thePQ.toString());
			}
		 }
			 catch (Exception dse )
			{
			   System.out.println( dse.getMessage( ) );
			}


This post has been edited by pansylea: 31 March 2009 - 08:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 pansylea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-November 08

Re: Stacks and Priority Queues

Posted 31 March 2009 - 10:42 AM

i got it working it was one bit that i was missing, i had as.toString(); but it was meant to be as.peek().toString.

Thanks for all your help

Quote

PriorityQ thePQ = new PriorityQ(100);
try
{
System.out.println("Priority Q: \n");
for ( int i = 0; i < as.length(); i++ )
{
t = as.peek().toString(); //PEEK WAS MISSING
pos = t.lastIndexOf("Date: ");
taskDate = t.substring(pos+6);

if (taskDate.equals(currentDate))
thePQ.insert(as.pop());
}
System.out.println(thePQ.toString());
}
catch (Exception dse )
{
System.out.println( dse.getMessage( ) );
}

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Stacks and Priority Queues

Posted 31 March 2009 - 04:17 PM

It is

t = as[i].toString();

not

t = as.toString();
Was This Post Helpful? 0
  • +
  • -

#10 pansylea  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-November 08

Re: Stacks and Priority Queues

Posted 02 April 2009 - 11:27 AM

How do i sort a priority queue by priority??
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1