Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-01-2008, 11:11 AM
Member
 
Join Date: Sep 2008
Posts: 1
Moonlightkid is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-01-2008, 03:43 PM
ojn ojn is offline
Member
 
Join Date: Sep 2008
Location: Stockholm, Sweden
Posts: 54
ojn is on a distinguished road
Send a message via MSN to ojn
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.
Quote:
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.
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-01-2008, 07:34 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 880
Nicholas Jordan is on a distinguished road
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-02-2008, 07:04 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 533
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Collection Framework (Lists) Java Tutorial Java Tutorials 1 05-16-2008 03:44 PM
How to use Collection in Spring Java Tip Java Tips 0 03-29-2008 02:45 PM
Java performance Issues (III) - Garbage collection JavaForums Java Blogs 0 01-30-2008 03:01 PM
Java Collection Framework (Sets) Java Tutorial Java Tutorials 0 12-07-2007 09:50 PM
Garbase Collection Java Tip Java Tips 0 11-30-2007 11:37 AM


All times are GMT +3. The time now is 09:02 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org