Results 1 to 4 of 4
  1. #1
    Moonlightkid is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Collection - java

    I got a question regarding what type of collection class should i used based on the scenario given but i am not sure whether i am correct?
    Can any1 correct me if i am wrong.

    1. When a hotel is fully booked, it holds a waiting list of customers in case of
    cancellations. Once a cancellation is received, the newly available room is allocated
    to the first person on the waiting list, who is then deleted from the list. On a
    particular day the first three customers added to the list are Ali Baba, Bibi Batek and
    Charlie Chan.

    class used : HashSet

    2. A video shop wants to store the titles of videos available for hire. It must be possible
    to list all the titles available but no particular order is required. Although the store
    may have more than one copy of a particular film, the title should only appear once.
    Titles available include: Iron Man, Crystal Skull and Dr Doplenty.
    class used : Collection - ArrayList

    3. An ICT203 tutor wants to store her students' myUniSIM names as strings in such a
    way that she can easily look up an individual student, as in the table below. There are
    no two students in her group with the same name.
    Studentís name myUniSIM name
    Charlie Dickens Charles Dickens
    Margie Hector Margaret Hector
    Terry Martin Terence Martin

    class used : HashMap

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by Moonlightkid View Post
    1. When a hotel is fully booked, it holds a waiting list of customers in case of
    cancellations. Once a cancellation is received, the newly available room is allocated
    to the first person on the waiting list, who is then deleted from the list. On a
    particular day the first three customers added to the list are Ali Baba, Bibi Batek and
    Charlie Chan.

    class used : HashSet
    A HashSet doesn't guarantee you the same order next time you access the collection.

    I'd use a LinkedList, since the size of the collection is very dynamic, hence it's nicer to use than an ArrayList. It's easy to add/delete objects anywhere in the collection, and since it's a linked list it doesn't have to loop through the entire collection, as opposed to an ArraryList.
    2. A video shop wants to store the titles of videos available for hire. It must be possible
    to list all the titles available but no particular order is required. Although the store
    may have more than one copy of a particular film, the title should only appear once.
    Titles available include: Iron Man, Crystal Skull and Dr Doplenty.
    class used : Collection - ArrayList
    Here an HashSet (or HashMap, not sure) makes more sense, since you don't care about the sort order.
    3. An ICT203 tutor wants to store her students' myUniSIM names as strings in such a
    way that she can easily look up an individual student, as in the table below. There are
    no two students in her group with the same name.
    Studentís name myUniSIM name
    Charlie Dickens Charles Dickens
    Margie Hector Margaret Hector
    Terry Martin Terence Martin

    class used : HashMap
    Makes sense. A HashMap filters doubles.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post you are off to a good start

    Quote Originally Posted by Moonlightkid View Post
    I got a question regarding what type of collection class should i used based on the scenario given but i am not sure whether i am correct? Can any1 correct me if i am wrong.
    Not really, but you will probably get copious observations. Correctness depends on detailed and thorugh consideration of problem domain, but you are off to a good start

    Quote Originally Posted by Moonlightkid View Post
    1. When a hotel is fully booked, it holds a waiting list of customers in case of cancellations. Once a cancellation is received, the newly available room is allocated to the first person on the waiting list, who is then deleted from the list. On a particular day the first three customers added to the list are Ali Baba, Bibi Batek and
    Charlie Chan.
    You keep using List in your application description, therefore you should give Collection type List deep contemplation. HashSet removes the key from code scope, therefore I suggest consideration of HashMap. Also, private static final Number totalRoomCount = ( room count, probably an int or long or Interger or Long ) Problem with linked list is if any link ever breaks, list from there on is lost. If Bibi Batek takes a hike to the Pulkovo Observatory, we have to set and remove or whatever.

    Quote Originally Posted by Moonlightkid View Post
    2. A video shop wants to store the titles of videos available for hire. It must be possible to list all the titles available but no particular order is required. Although the store may have more than one copy of a particular film, the title should only appear once. Titles available include: Iron Man, Crystal Skull and Dr Doplenty.
    HashMap, keyed on class Video's String member variable Title, and which class has a countOfItemsOnHand member variable. You then do HashMap.get(video.title) and ++ or -- the item count if not zero......if zero can only ++
    Quote Originally Posted by Moonlightkid View Post
    3. An ICT203 tutor wants to store her students' myUniSIM names as strings in such a way that she can easily look up an individual student, as in the table below. There are no two students in her group with the same name.
    class Student{
    String name.... with get name and set name or with set name in the constructor and then:
    if( HashMap.contains(Student.name)){ do something ();/// }
    else{ add name or declare error }

    Try not to fall into using exceptions to handle student name not in HashMap, possibly handle add already present throws exception but that is not an actual exceptional condition. What is exceptional is trying to read / view or write / add / modifiy without authorization....

    That is what an exceptional condition actually is.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    If you want to order the backlog, isn't a Queue the right structure? So that you maintain the original order of when the reservations come in.

    If you have many classes of service, say Suites, Rooms, and Closets, you may want to have separate Queue structures for each, or you may want one and check to match type requested to type available each time through the loop.

Similar Threads

  1. Java Collection Framework (Lists)
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 05-16-2008, 02:44 PM
  2. How to use Collection in Spring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 01:45 PM
  3. Java Collection Framework (Sets)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 12-07-2007, 08:50 PM
  4. Garbase Collection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-30-2007, 10:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •