Totally stuck with it all!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Totally stuck with it all!!

  1. #1
    Join Date
    Nov 2004
    Posts
    2

    Totally stuck with it all!!

    I recently started an "introduction to object orientated programming" class thinking it would be nice and simple to start with. How wrong I was! I am totally lost with all aspects of Java :S Basically our class uses a programming environment called blue J - I dont know if any of you have heard of it?

    Anyway, basically I was wondering if anyone could help me with these questions:

    First, you will write a class called Hotel in
    Question 1 and then a class called HotelChain, that uses Hotel, in Question 2. You have to create
    one new project, named hotels for instance, within which you will create the two classes.
    If you donít succeed in writing a method from the class Hotel needed in the class HotelChain,
    you can just declare it and then assume it is working fine. Of course, you wonít be able to check
    that HotelChain works perfectly.

    Question 1:
    Letís first write the class Hotel, the objects of which will store an Hotel name, the number of booked
    rooms and which room is free or not. The methods will allow someone to check whether the hotel
    is full or not, to book a room or to free it, etc.

    Question 1.1
    The class hotel has 3 fields: name which stores the name of the hotel as a String; rooms which stores
    which room is booked as a one-dimensional array of boolean; and numBookedRooms which stores the
    number of rooms currently booked as an int. All the fields are private. Do not create any other
    fields.
    What information does the array store ? The array which is going to be created in the constructor,
    is going to have exactly one slot per room, its index being the room number. Hence an hotel with
    150 rooms will have room numbered from 0 to 149. The value of each slot is going to be true if that
    room is booked and false otherwise.
    Declare your class and its fields.

    Question 1.2
    There is only one constructor for this class. It takes two parameters. The first one is the name of
    the hotel that will be stored in the name field. The second one will be the total number of rooms
    in the hotel. The constructor should create an array of the length given by the second parameter.
    1
    Given that there are no room booked yet, all the slots of the array must be initialized to false. It
    should also initialize the field numBookedRooms to 0. You can assume that the values given are valid
    and sensible.
    Implement this constructor.

    Question 1.3
    There is one simple accessor methods, getName, that takes no parameter and returns, the value of
    the field name. There is a more complex accessor, getOccupationRate, that takes no parameter
    and returns a double. It should return, as a figure between 0.0 and 100.0, the percentage of room
    currently booked for this hotel. Remember that the total number of rooms in the hotel is the length
    of our array rooms.
    Warning: If you divide an integer by another integer in java, you do the integer division, i.e.,
    5/2 == 2. You need to type cast any of the two arguments to get the floating point division, i.e.,
    ((double)5)/2 == 2.5;
    Implement these two accessors as public methods.

    Question 1.4
    There is a private mutator method called bookThisRoom, which takes one parameter, an int,
    and returns nothing. This paramater represents a room number (i.e., a slot of the array), and
    your method should book that room, that is to say, it should update the values of rooms and
    numBookedRooms to reflect the fact that the room given in parameter has now been booked.
    Given that this method is private, it can only be called from inside the class. You can therefore
    assume that the parameter given matches an existing room that is free. No checks are needed here.
    However, you must make sure when you call this method to follow these assumptions.
    Implement this private method.

    Question 1.5
    There is a public mutator method freeThisRoom, which takes an int and returns a boolean. The
    first parameter is a room number. If that room was booked, it should free it, do the necessary
    update of the attributes and return true. Otherwise if the parameter is not an existing room or a
    room that has not been booked, it should do nothing and return false.
    Implement this public method.

    Question 1.6
    There is a public mutator method, bookARoom that takes no parameter and returns an int. The
    method should find whether or not there is a free room left in the hotel. If the hotel is fully booked,
    the method returns -1. Otherwise it books a free room and returns the room number. You may or
    may not use the private method bookThisRoom
    Implement this public method.

    Question 2:
    Letís now write the class HotelChain, It should be declared in the same project as it will use objects
    from the class Hotel.
    The objects from the class HotelChain are hotel chains. They store all the hotels which are
    member of a chain, and implement some operations to manage these.

    Question 2.1:
    The class HotelChain has 2 fields: hotels, which is an ArrayList that will be used to store objects
    of the class Hotel and name, that stores the name of the HotelChain as a String. Basically the
    number of objects in hotels will represent the number of hotels belonging to this HotelChain.
    2
    Declare your class and its fields.

    Question 2.2:
    This class has 1 Constructor. It takes one parameter that is the name of the HotelChain as a String,
    and it should initialize the name field with it. It should also create the ArrayList.
    Implement this public constructor.

    Question 2.3:
    There is a mutator called addHotelNaively which takes a parameter of type Hotel and returns a
    boolean. If the Hotel object is not null, it should add it to hotels and return true. It should
    return false otherwise.
    Implement this public method.

    Question 2.4:
    There is a method listHotels that takes no parameter, returns nothing and first prints a sentence
    that states the number of hotels in the chain, and then lists all the hotels in the chain by name, for
    instance, it would print:
    There are 4 hotels in the chain:
    - Madrid Puerta del sol
    - London Bayswater
    - Paris St-Michel
    - Berlin Alexanderplatz
    If there are no hotels yet in the chain, it should not crash.
    Implement this public method.

    Question 2.5:
    Create a public method findThisHotel which takes a parameter of type String and returns a
    variable of type Hotel. If there is an Hotel in the ArrayList hotels that bears the name given as a
    parameter to the method, the method should return it. If there is none, the method should return
    null. This also applies if there are no hotels in the list yet.
    Warning: To compare two Strings, use the equals method from the class String, and not
    the == operator. If I have String s1 = "hello"; String s2 = "hello";, then s1 == s2 is not
    necessarily true, while s1.equals(s2) is. This is because, on Strings, == does an identity test (are
    they the same object ?), while equals does a similarity test (do they have the same value ?).
    Implement this public method.

    Question 2.6:
    Now write a mutator addHotel which takes a parameter of type Hotel and returns a boolean. Add
    the Hotel given in parameter to the chain if and only if it is not null and no Hotels in the chain
    already bears the same name. It returns true if and only if the hotel argument has been added. Use
    the method findThisHotel, and possibly the method addHotelNaively
    Implement this public method.

    Question 2.7:
    The class has yet another accessor, getAverageOccupationRate, the takes no paramater and returns
    a double, that is the average occupation rate of all the hotels of the chain. It should return 0.0 if
    there are no hotels in the chain yet.
    Implement this public method.
    3

    Question 2.8:
    Challenging question: This method, called sortHotels, takes no parameter and returns nothing. It
    should reorder all the Hotel objects in the hotels ArrayList so that the lower its index, the lower
    its name is in alphabetical order.
    Example: Suppose we have:
    Hotel name index
    "London Victoria" 0
    "London Bayswater" 1
    "Paris St-michel" 2
    "Madrid Puerta Del Sol" 3
    "Berlin Alexanderplatz" 4
    Then after calling sortHotels(), we will have
    Hotel name index
    "Berlin Alexanderplatz" 0
    "London Bayswater" 1
    "London Victoria" 2
    "Madrid Puerta Del Sol" 3
    "Paris St-michel" 4
    You can use any strategy you like. According to which strategies you are choosing, looking at
    the API of ArrayList may be very helpful.
    Help: The class String has a method compareTo which takes a String as an argument and
    returns an integer. The value returned is negative if the argument is after, in alphabetical order,
    than the String to which the method is applied to. It returns zero if theyíre the same, and a positive
    value otherwise. For instance, String s1 = "aa"; String s2 = "zz";, then s1.compareTo(s2)
    is negative, but s2.compareTo(s1) is positive.
    Implement this method.


    Any help would be greatful!!! just email me mcfstep@hotmail.com or post on here!
    Thanks!

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    what's the first point you are stuck?
    Since this is just a post of your entire assignment.
    Posting you entire assignment her ein stead of direct questions will get you very little response.
    WHen you ask to-the-point questions about a specific problem however we are better able to help out.

  3. #3
    Join Date
    Nov 2004
    Posts
    2
    Its the assignment I'm stuck with. I have no idea how to start it or do any of it!

  4. #4
    Join Date
    Oct 2004
    Posts
    311
    Ok, I'll help you along by giving you some pointers for the Hotel class.
    I asume you already know how to create a class and a constructor.

    First you need to define the variables.
    Code:
    private String name;
    private boolean[] rooms;
    private int numBookedRooms;
    These you will have to initialise in your constructor.
    Code:
    name="test hotel";
    rooms = new boolean[150];
    numBookedRooms = 0;
    (You'll have to change this so the name of the hotel and the size of the array are taken from the constructor input.

    This should get you started on the class. The rest of the assignment is pretty self explaining I think, so you should be well on your way now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center