How to call this Queue Data Structure method - Java

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 805 Views - Last Post: 11 August 2012 - 08:44 PM Rate Topic: -----

#1 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 03:34 PM

Hi, I've been given this question:

What does the call
selectWinner(q,3)
return, if queue is the queue of strings ("first", "second", "third", "fourth)
and the method selectWinner is as given below?


public static String selectWinner(Queue<String> q, int k) {
		if ( q.isEmpty() ) return null;
		while ( q.size() > 1 ) {
		for (int i = 0; i < k; i++) {
		q.enqueue(q.dequeue());
		}
		q.dequeue();
		}
		return q.front();
		}


I've tried calling the method in eclipse with:

import java.util.Queue;

public class dstQueueTest{
	
	public static String selectWinner(Queue<String> q, int k) {
		if ( q.isEmpty() ) return null;
		while ( q.size() > 1 ) {
		for (int i = 0; i < k; i++) {
		q.enqueue(q.dequeue());
		}
		q.dequeue();
		}
		return q.front();
		}
	
public static void main(String[] args){

selectWinner(("first", "second", "third", "fourth"), 4);

    }
}



I'm not sure how to call the method, but I tried the above ^ and I get a syntax error. To be honest, I thought it was the same as calling any other method, so I passed in what I thought was the arguments: the given string and the number of strings that are there (I didn't know what else to do).

Does anyone have any ideas?

This post has been edited by TechSyndrome: 11 August 2012 - 03:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to call this Queue Data Structure method - Java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 03:38 PM

The method signature tells you how to call it, but you may not be familiar with generics:

public static String selectWinner(Queue<String> q, int k)

The signature specifies that the method parameters are to be a single Queue of String objects and a single int, in that order.

Does that help?
Was This Post Helpful? 1
  • +
  • -

#3 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 03:42 PM

View PostGregBrannon, on 11 August 2012 - 03:38 PM, said:

The method signature tells you how to call it, but you may not be familiar with generics:

public static String selectWinner(Queue<String> q, int k)

The signature specifies that the method parameters are to be a single Queue of String objects and a single int, in that order.

Does that help?


Ahh! I think I see what you mean! At least I hope so. So for example, the method signature in this method is: selectWinner(Queue<String> q, int k). BUT, do I insert the given strings in here --><String>?

This post has been edited by TechSyndrome: 11 August 2012 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 03:48 PM

No. Queue<String> is a single object.

Are you familiar with arrays? Say you have an array of Strings:

String[] stringArray = { "this", "that", "other" };

To pass the Array of Strings, stringArray, to the method,

public static String selectWinner( String[] myArray )

you'd write:

String winner = selectWinner( stringArray );

Notice that I pass the name of the Array of Strings to the method rather than a bunch of Strings. The Queue<String> object is similar. You pass the name of a Queue of Strings to the method.
Was This Post Helpful? 2
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2149
  • View blog
  • Posts: 3,303
  • Joined: 21-June 11

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 03:51 PM

You can't create a queue by just writing (element1, element2, ...). I can see how the way the assignment is phrased kinda sorta suggest that, but it's not the case.

To create a Queue, you'll need to create new queue using the new keyword (followed by the name of a class that implements the Queue interface) and then add items to the queue by using the add method. Then you can selectWinner with that queue as its argument.

That said I don't think you're supposed to run the code to find the answer. You're supposed to run it in your head. And given the fact the java.util.Queue doesn't actually have the methods enqueue and dequeue, you won't get it to run even if you fix the syntax error.

PS: The next time you ask about a compilation error (or any other sort of error message for that matter), please post the entire error message (including the line number) here. That makes it easier for us to find where the error is.
Was This Post Helpful? 1
  • +
  • -

#6 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:00 PM

Thank you @GregBrannon & @sepp2k! I'm reading through both of your comments now, and will reply ASAP, I'm following your advice and trying it in Eclipse now!

@sepp2k yes, you're absolutely right, I should've entered the full compilation error and will remember to do so in future posts. Here it is:


Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error on token "(", Name expected after this token

	at dstQueueTest.main(dstQueueTest.java:18)




P.S how do I box the code like you did? It makes it much easier for everyone to read!
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2149
  • View blog
  • Posts: 3,303
  • Joined: 21-June 11

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:05 PM

View PostTechSyndrome, on 12 August 2012 - 01:00 AM, said:

P.S how do I box the code like you did? It makes it much easier for everyone to read!


[ il ]The Code[ /il ] (without the spaces).
Was This Post Helpful? 1
  • +
  • -

#8 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:08 PM

View PostGregBrannon, on 11 August 2012 - 03:48 PM, said:

No. Queue<String> is a single object.

Are you familiar with arrays? Say you have an array of Strings:

String[] stringArray = { "this", "that", "other" };

To pass the Array of Strings, stringArray, to the method,

public static String selectWinner( String[] myArray )

you'd write:

String winner = selectWinner( stringArray );

Notice that I pass the name of the Array of Strings to the method rather than a bunch of Strings. The Queue<String> object is similar. You pass the name of a Queue of Strings to the method.


Thank you. So would the first part be something like this: Queue<String> q = {"first", "second", "third", "fourth"}? Or do I create an object like I would with Scanner?

View Postsepp2k, on 11 August 2012 - 04:05 PM, said:

View PostTechSyndrome, on 12 August 2012 - 01:00 AM, said:

P.S how do I box the code like you did? It makes it much easier for everyone to read!


[ il ]The Code[ /il ] (without the spaces).


Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#9 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:15 PM

View PostGregBrannon, on 11 August 2012 - 03:48 PM, said:

No. Queue<String> is a single object.

Are you familiar with arrays? Say you have an array of Strings:

String[] stringArray = { "this", "that", "other" };

To pass the Array of Strings, stringArray, to the method,

public static String selectWinner( String[] myArray )

you'd write:

String winner = selectWinner( stringArray );

Notice that I pass the name of the Array of Strings to the method rather than a bunch of Strings. The Queue<String> object is similar. You pass the name of a Queue of Strings to the method.


@GregBrannon I appreciate that you asked whether I actually know about arrays. You didn't assume anything and that's so important to the way a lot of people like me learn. Thank you. This is why I love dream in code - the whole community is like this! And yes, I do know about arrays - just need more practice with it.

Ok, bk to your advice.

Like this?

	Queue<String> q = new Queue<String>();
	
	q.add("first");
	q.add("second");
	q.add("third");

Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:19 PM

Looks good to me.
Was This Post Helpful? 0
  • +
  • -

#11 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:26 PM

View PostGregBrannon, on 11 August 2012 - 04:19 PM, said:

Looks good to me


Thank, as sepp2k pointed out, [ il ]java.util.Queue[ il ]doesn't actually have the methods [ il ]enqueue[ il ] and [ il ]dequeue[ il ]. So I Google it and tried importing the class: java.lang.Object, but to no avail...

I've also tried to run the program:

import java.util.Queue;
import java.util.*;

public class dstQueueTest{
	
	public static String selectWinner(Queue<String> q, int k) {
		if ( q.isEmpty() ) return null;
		while ( q.size() > 1 ) {
		for (int i = 0; i < k; i++) {
		q.enqueue(q.dequeue());
		}
		q.dequeue();
		}
		return q.front();
		
		
		}
	
public static void main(String[] args){
 
Queue<String> q = new Queue<String>();
	
	q.add("first");
	q.add("second");
	q.add("third");
	q.add("fourth");
	
}

}


And received this error:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "(", Name expected after this token

at dstQueueTest.main(dstQueueTest.java:18)

This post has been edited by TechSyndrome: 11 August 2012 - 04:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2149
  • View blog
  • Posts: 3,303
  • Joined: 21-June 11

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:29 PM

View PostTechSyndrome, on 12 August 2012 - 01:15 AM, said:

Like this?

	Queue<String> q = new Queue<String>();


That won't work because Queue is an interface not a class. It has to be something like Queue<String> q = new LinkedList<String>(); (LinkedList being a class that implements the Queue interface).
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:32 PM

I'm sorry. Java Queue is an interface and cannot be instantiated. I thought you were using another custom Queue based on the class' methods. Were you given a custom Queue class to use for this assignment?
Was This Post Helpful? 0
  • +
  • -

#14 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:36 PM

View PostGregBrannon, on 11 August 2012 - 04:32 PM, said:

I'm sorry. Java Queue is an interface and cannot be instantiated. I thought you were using another custom Queue based on the class' methods. Were you given a custom Queue class to use for this assignment?


No need to apologise, Sir. It's an paper-based exam question, so I wasn't given any code except for what I provided.

import java.util.Queue;
import java.util.*;
import java.util.LinkedList;

public class dstQueueTest{
	
	public static String selectWinner(Queue<String> q, int k) {
		if ( q.isEmpty() ) return null;
		while ( q.size() > 1 ) {
		for (int i = 0; i < k; i++) {
		q.enqueue(q.dequeue());
		}
		q.dequeue();
		}
		return q.front();
		
		
		}
	
public static void main(String[] args){
 
Queue<String> q = new LinkedList<String>();
	
	q.add("first");
	q.add("second");
	q.add("third");
	q.add("fourth");
	
	
	
}

}


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "(", Name expected after this token

at dstQueueTest.main(dstQueueTest.java:18)

Was This Post Helpful? 0
  • +
  • -

#15 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: How to call this Queue Data Structure method - Java

Posted 11 August 2012 - 04:47 PM

I imported java.util.LinkedList and it still doesn't recognise the dequeue and front method
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2