#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Need help with Java Queue( just 2 methods!)


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

    public class Brickstore {

    Queue<Float> queue1= new LinkedList<Float>();

    // Adds new batch of brick to the store. Amount amount of bricks in the batch, price price of one brick
    public static void store(int amount, float price)
    {



    for(int i=0;i<=amount;i++)
    { queue1.add(price);}

    }




    //Removes amount bricks from the store. Returns the purchase value of removed bricks.
    //Raises exception when there is no enough bricks in store.
    private float pull(int amount) throws NoSuchElementException {

    //if (queue.size() < amount)
    //throw new NoSuchElementException ;


    for(float PurchaseValue=0,i=1;i<=amount;i++){

    PurchaseValue +=queue.remove().getValue();
    queue.remove(); }

    return PurchaseValue;
    }
    }
    //
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    class Brickstore is public, should be declared in a file named Brickstore.java
    All public classes need to be declared in a file with the same name as the class name.
    cannot find symbol
    symbol : class NoSuchElementException
    Make sure you have the import statement for the package the class is in.

    cannot find symbol
    symbol : variable queue
    The compiler can not find the definition for queue. Is it defined in scope (within same {}s) as where it is being used? The same for most of the cannot find symbol errors.

    Please edit your post and wrap the code in code tags to preserve formatting and get highlighting.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Hi Norm,


    Thanks for the quick reply. Any idea where should i declare that Queue Queue variable so both methods can access it? Declare it as a class variable?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Yes, as a class variable if more than one method needs access to it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    Yes, as a class variable if more than one method needs access to it.
    Ok now I have created Queue of Float objects inside the class, but now i get "non-static variable queue1 cannot be referenced from a static context" Any idea on that?

    and this error:

    PurchaseValue +=queue1.remove().getValue();
    Main.java:31: inconvertible types
    found : <nulltype>
    required: float

    Any idea how to remove Prices (float objects) off the Queue data structure and get their values summed?
    queue.remove() only removes the object, but how to get the value of it????


    Code:
    import java.util.LinkedList;
    import java.util.Queue;
     
    public  class Brickstore {
     
    Queue<Float> queue1= new LinkedList<Float>();
     
    // Adds new batch of brick to the store. Amount  amount of bricks in the batch, price  price of one brick
    public static void store(int amount, float price)
    {
     
     
     
    for(int i=0;i<=amount;i++)
    {    queue1.add(price);} 
     
    }
     
     
     
     
    //Removes amount bricks from the store. Returns the purchase value of removed bricks.
    //Raises exception when there is no enough bricks in store.
    private float pull(int amount) throws NoSuchElementException {
     
    //if (queue1.size() < amount) throw new NoSuchElementException ;
     
     
    for(float PurchaseValue=0,i=1;i<=amount;i++){
     
    PurchaseValue +=queue1.remove().getValue();
    queue1.remove(); }
     
    return PurchaseValue;
    }
    }
    //






    clone edit download copy to clipboard private user's delete
    compilation info hide
    Main.java:4: class Brickstore is public, should be declared in a file named Brickstore.java
    public class Brickstore {
    ^
    Main.java:24: cannot find symbol
    symbol : class NoSuchElementException
    location: class Brickstore
    private float pull(int amount) throws NoSuchElementException {
    ^
    Main.java:15: non-static variable queue1 cannot be referenced from a static context
    { queue1.add(price);}
    ^
    Main.java:31: cannot find symbol
    symbol : method getValue()
    location: class java.lang.Float
    PurchaseValue +=queue1.remove().getValue();
    ^
    Main.java:31: inconvertible types
    found : <nulltype>
    required: float
    PurchaseValue +=queue1.remove().getValue();
    ^
    Main.java:34: cannot find symbol
    symbol : variable PurchaseValue
    location: class Brickstore
    return PurchaseValue;
    ^
    6 errors
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    non-static variable queue1 cannot be referenced from a static context
    A non-static variable is one that only exists in an instance of the class. You need to create an instance of the class to access a class variable.
    A static context is the code inside of a static method which is not associated with an instance of the class.
    cannot find symbol
    symbol : class NoSuchElementException
    Where is NoSuchElementException defined? The compiler can not find its definition.
    cannot find symbol
    symbol : method getValue()
    where is the method getVaue() defined? The compiler can not find its definition with the class of the object returned by the remove() method. Read the API doc to see what is returned.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    A non-static variable is one that only exists in an instance of the class. You need to create an instance of the class to access a class variable.
    A static context is the code inside of a static method which is not associated with an instance of the class.
    Where is NoSuchElementException defined? The compiler can not find its definition.

    where is the method getVaue() defined? The compiler can not find its definition with the class of the object returned by the remove() method. Read the API doc to see what is returned.
    Hi Norman, can you give me the example how to do it? Any workarounds? how to make these two methods work without errors? shoud i import more classses? and how to extract the value out of that queue? i am getting desperate, spent 8 hours on this and can not get through, can you show how to do it correctly?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    For the static vs non-static problem:
    either make everything static (often a poor choice with potential problems)
    or move all the code out of the static method into methods in the class and create an instance of the class.

    I don't why the code is calling the getValue() method for the object returned by the remove method. Break that compound statement up into two parts:
    first part: save the object returned by the remove() in a variable.
    second part: call a method using that variable
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Ok i figured it out, however, I had to use Queue argument in the second method float pull. How can avoid using Queue object argument in that second method, but somehow let the method access the Queue? Should the Queue queue be a class variable and if so should it be just declared, or created?


    Code:
    import java.util.*;
    
    
    public class Brickstore implements Queue {
    
    
    public static void main(String [ ] args){
    
    Brickstore Brickstore1= new Brickstore();
    Brickstore1.store(10,5);
    System.out.println(4);
    
    
    }
    
    
    // Adds new batch of brick to the store. Amount  amount of bricks in the batch, price  price of one brick
    public static void store(int amount, float price)
    {
    
    Queue<Float> queue= new LinkedList<Float>();
    
    for(int i=0;i<=amount;i++)
    {	queue.add(price);} 
    
    }
    
    
    
    
    //Removes amount bricks from the store. Returns the purchase value of removed bricks.
    //Raises exception when there is no enough bricks in store.
    public float pull(Queue<Float> queue, int amount) throws NoSuchElementException {
    
    //if (queue.size() < amount) throw new NoSuchElementException ;
    
    float PurchaseValue=0;
    for(int i=1;i<=amount;i++){
    
    PurchaseValue +=queue.element();
    queue.remove(); }
    
    return PurchaseValue;
    }
    }
    
    //
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    and finally i think I manage to do it What do you think?(you can ignore the main method)

    Code:
    import java.util.*;
    
    
    public class Brickstore implements Queue {
    
    
    public static void main(String [ ] args){
    
    Brickstore Brickstore1= new Brickstore();
    Brickstore1.store(10,5);
    System.out.println(Brickstore1.pull(4));
    
    
    }
    
    private Queue<Float> queue;
    
    // Adds new batch of brick to the store. Amount  amount of bricks in the batch, price  price of one brick
    public static void store(int amount, float price)
    {
    
    Queue<Float> queue= new LinkedList<Float>();
    
    for(int i=0;i<=amount;i++)
    {	queue.add(price);} 
    
    }
    
    
    
    
    //Removes amount bricks from the store. Returns the purchase value of removed bricks.
    //Raises exception when there is no enough bricks in store.
    public float pull(int amount) throws NoSuchElementException {
    
    //if (queue.size() < amount) throw //newNoSuchElementException ;
    
    float PurchaseValue=0;
    for(int i=1;i<=amount;i++){
    PurchaseValue +=queue.element();
    queue.remove(); }
    
    return PurchaseValue;
    }
    }
    
    //
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What do you think?
    Does it compile, execute and do what you want?

    The code is poorly formatted.
    There shouldn't be any code on a line following a {
    All }s should be alone on a line and NOT hidden at the end of a statement.
    Statements should be indented to show their logic nesting levels.
    Last edited by NormR; January 11th, 2013 at 06:57 PM.

IMN logo majestic logo threadwatch logo seochat tools logo