GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes code for a recently used menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "code for a recently used menu" Watch "code for a recently used menu" New topic
Author

code for a recently used menu

Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
posted October 10, 2005 07:54 AM
--------------------------------------------------------------------------------
i am trying to create code that will create a most recently used menu with a maximum of 10 items. it also must not allow for the same file to be noted more than once and every time an item is opedned it goes to the top of the list and the others drop down a place.
my problem so far is withthe constructor.
the following are my fields:

// --------------- FIELDS --------------

private static final int MAX_ITEMS = 10;
private int i;

Cansomeone please help with a constructor
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm guessing you're building your own little container to hold the last 10 things. That's a good approach. We can worry about getting it on screen later.

To create an instance your code will probably look like:

where the parameter to the constructor sets the maximum size. The constructor itself looks like:

Does that give you a start or raise more questions?
[ October 10, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
a bit of both lol. so far my codes as follows:
// --------------- FIELDS --------------

private static final int MAX_ITEMS = 10;
private int i;

// ------------- CONSTRUCTOR -------------

public MostRecent(int maxSize)
{ this.maxSize = maxSize;
}

// -------------- METHODS ----------------

/* Update List*/

public void update(NamedItem n)
{ int [] NamedItem = new int[10];
NamedItem[0] = n.getName();
}
but its bringing up 2 errors.
1)this.maxSize ----- cannot find symbol
2) NamedItem[0] = n.getName(); ------ incompatable types
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Do it with a LinkedList ! (jdk 1.4, there's a Queue object in jdk1.5) :


Note that in your code :


You don't declare an instance variable named maxSize !
Choose one of :
- the use of a constant MAX_SIZE
- the use of an instance variable maxSize you can change whan you instanciate your class

variable NamedItem is an array of int.
-> Don't use a class name for a variable !
NamedItem[0] = n.getName() -> You're trying to assign a String to an int !!


Regards,<br />Seb<br /> <br />SCJP 1.4
Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
i tried it with the linked list but the code didnt work for me.
So far this is my code:

public class MostRecent
{
// --------------- FIELDS --------------

private static final int MAX_ITEMS = 10;
private int i;

// ------------- CONSTRUCTOR -------------

public MostRecent(int maxSize)
{ maxSize = MAX_ITEMS;
}

// -------------- METHODS ----------------

/* Update List*/


public void update(NamedItem n)
{ int [] NamedItem = new int[10];
NamedItem[0] = n.getName();
}

/* Display List*/

public void display()
{
System.out.println("---------------------------");
for (i = 0; i < noItems; i++)
{
// print name of ith item to standard output
}
}


at this stage i have no idea where i have gone wrong. any help is much appreciated asap
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
Comments on code posted by Chris Lavery:You are missing the most important field, a data structure to store the menu items. If your menu items are strings, then the simplest approach would be an array of strings. (A linked list would also work, but would be more challenging for a beginner.) You also need some way of keeping track of how many items are in the list. The simplest way to do this would be with an int field, like the noItems field you already have in display().What are you trying to do here? This code doesn't make any sense. You only need an explicit constructor if you need to do some initialization. Do you?This also doesn't make any sense either. Forget the code for a moment. You need to start by getting a clear understanding of the logic that is required. I suggest that you start by forgetting the no-duplicates requirement, and just focus on adding items to the list, moving items down the list, and dropping the last item off once the list is full. When all that is working, you can come back and deal with the no-duplicates rule as well. This is just so that you only have to worry about a bit of the problem at a time.

The update() method needs to do something like this:
  • If the list is already full, discard the last (oldest) item
  • Move all remaining items down one place
  • Add the new item in the first place

  • You can do parts 1 & 2 in one for loop. There is a bit of a trick to this. You don't have to explicitly discard the last item. You just ignore it when you carry out step 2. Once you have this working, you can think about what you need to do to avoid duplicates in the list. This is a little more complicated (but not much more).

    BTW, your display() method is almost there.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791

    I probably introduced this maxSize argument without explaining it. My thought was we could pass in the max size to the constructor rather than code a constant. That way we could use this with 10 max one time and 15 max the next time. MS Office lets you choose how many recent files you want to keep, so maybe we could load this from preferences. Anyhow, it turns out more like:

    Now we can make a new MostRecent instance with any max size we like.

    The other discussions about storing your list of "n" were good, too. It's useful to think about your code in terms of what it will do before you write it. Try making some tests like this:

    Make sure you know what you expect when you add "four" to this thing with max capacity of three.

    You could make your own holder for "n" entries with an array. That would be educational for sure. You could also browse the JavaDoc for List and Set and see if you find something that will do the job. What's really neat is you should be able to switch from an array to a collection and the tests shown above will still pass.

    It's great fun to write one test and then write just enough code to make it pass. Give yourself a chocolate every time it passes! Trying to write the whole program and then figure out whether or not it works is much harder and actually takes much longer. Have fun!
    Chris Lavery
    Ranch Hand

    Joined: Nov 30, 2004
    Posts: 54
    first of all thanks to everyone whose helping me out at the minute. i have been working away but i cannot get past the problem of an error on the public void update(NamedItem n)line.
    the error message saysthat: ( or[ expected although i cannot understand where??
    if you can see any other problems with my code, any help is welcome.


    my code:

    public class MostRecent
    {
    // --------------- FIELDS --------------

    private static final int MAX_ITEMS = 10;
    private int i;

    // ------------- CONSTRUCTOR -------------

    public MostRecent(){
    }

    // -------------- METHODS ----------------

    /* Update List*/

    NamedItem recent = new NamedItem

    public void update(NamedItem n){

    int [] entry = new int[10];
    entry[0] = n.getName();
    }

    /* Display List*/

    public void display()

    {System.out.println("---------------------------");
    for (i = 0;
    i < MAX_ITEMS;
    i++);
    {
    // print name of ith item to standard output
    }
    }
    ;

    }
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    The error message includes the line number with the problem. Please indicate which line the error refers to so that we can help you.

    Layne


    Java API Documentation
    The Java Tutorial
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    After looking at your code, the error appears to be on the line BEFORE the update method:

    You need to change this to something like

    Since you have not posted the code for the NamedItem class, it is difficult to know whether or not you need to pass any variables to the constructor.

    With that said, I wonder if you really need this variable. What is its purpose?

    Layne
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: code for a recently used menu