Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Could some one please explain this java code for me. thanks

Options
  • 14-01-2010 3:18am
    #1
    Closed Accounts Posts: 175 ✭✭


    Could some one please explain/comment this code for me, have uml exam tomorrow which includes some java, have spent two hours trying to understand this code.

    public class Manager

    {private Vector theAccounts ;
    public void addAccount (account acc)
    {theAccounts.addElement (acc);}
    public void removeAccount (Account acc)
    {theAccounts.removeElement (acc)}
    }

    Thanks


Comments

  • Registered Users Posts: 1,083 ✭✭✭Rulmeq


    Could some one please explain/comment this code for me, have uml exam tomorrow which includes some java, have spent two hours trying to understand this code.

    public class Manager {
    // This is declaring a variable that will hold a list of accounts.
    // A vector is a synchronised implementation of java.util.List
    private Vector theAccounts;

    // This is a utility method that wraps the Vector's behaviour
    public void addAccount (account acc) {
    theAccounts.addElement (acc);
    }

    // Another utility method that wraps the Vector's behaviour
    public void removeAccount (Account acc) {
    theAccounts.removeElement (acc)
    }
    }
    Thanks
    I've added comments above.

    This is actually pretty bad coding, they declare theAccounts as a Vector instead of a List or a collection, and then they use the legacy methods of addElement/removeElement. This code would have looked out of place in the late 90s. Also it's a pretty pointless attempt to add an unnecessary layer of abstraction, anyone who writes Java should know how to handle the default collections, and this was perhaps some sort of misguided attempt at replicating the compile time checks that are provided by generics.

    All of the above code would be better expressed as such:

    List<Account> accounts = new ArrayList<Account>();

    The Manager class as it stands it completely redundant.


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Well the likelihood is that this code comes from an example question rather than some code he has written himself. In addition your use of a collection is only applicable if the class is only ever going to be a collection of Account objects.

    OP: Is there any specific aspect you're having trouble understanding? What's the full question?


  • Registered Users Posts: 1,083 ✭✭✭Rulmeq


    ronivek wrote: »
    In addition your use of a collection is only applicable if the class is only ever going to be a collection of Account objects.

    Which is exactly what the Manager class is trying to implement. Also it can probably be inferred from the name of the collection as well.

    I'm pretty much going to go out on a limb here and say that it is code from an example question. My point remains it's anachronistic code, and it's use should be discouraged.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    The question wasn't about the code, it's a UML exam. It could be pseudo code and the question still be valid.

    Were you supposed to draw a class for this?


  • Closed Accounts Posts: 9 dannystaple


    What you want here is an entity diagram, with a box representing the class, with a division at the top with the classes name, and then further division showing its methods and using the symbols to mark up their scope (public or private). I strongly suspect that people here will not draw the diagram for you, so you are encouraged to research ti and learn for yourself.

    You could look online for UML templates for common drawing programs, I know that there is one for "dia" that might help.


  • Advertisement
Advertisement