Objectives:

The primary purpose of this assessment is to ensure that you have the required background understanding to be able to cope with the remainder of this course. You should have gained all the necessary skill from your pre-requisite course, which has been revised in the first few weeks learning materials for this course.

By completing all tasks in this weeks lab, you should have demonstrated you have the appropriate skills and understanding of topics such that you are able to:

  • Write class definitions in Java including instance variables and methods;
  • Write methods that receive parameters and which return values as a result of their execution
  • Write constructors;
  • Apply the principles of encapsulation and information hiding;
  • Use suitable relational expressions to selectively run different parts of code;
  • Ensure that classes have an appropriate toString() method; and
  • Write simple driver classes to test that another class is operating correctly

Tasks Part A

Stage 1:

1. Start a new Java Project named lab02.

2. Create a package for Part A named lab02a, into which you will place the code files.

3. First, define a new class named Booking, which will be used to represent basic facts about a booking of a hotel room. Ensure the class contains definitions for the following attributes (see table on next page for example data):

  • The room number, which is in the form of the level number, followed by a hyphen, followed by a number distinguishing the room from other rooms on the same floor
  • How many nights the booking is for
  • The base-rate of the room per night
  • A total amount incurred for items taken from the in-room fridge, which must be paid for at the end.

4. Ensure the class has a parameterised constructor that accepts values for assigning to each of the attributes except for the attribute relating to expenses from the fridge which will initially be zero.

5. Ensure that the class defines accessors for all attributes.

6. Ensure that there is a mutator for the total fridge expenses attribute only no other attributes are to have a mutator.

7. Write a toString() method that will return a String describing details about the Booking, in the manner as shown in the following example (all in one line):

Room 7-17 (3 nights @ $180 per night), extra fees: $1.8499999

In the example, due to the way floating-point numbers are represented on the particular computer that ran the code, there was a slight rounding error this is acceptable.

8. Write a method named getTotalCosts() which will return the total amount due so far (based on the expected duration of stay and the extra expenses relating to items taken from the fridge). Please note that: if any booking is for more than 2 nights, then the third and subsequent nights are to be discounted by 15 percent, i.e. the above example of $180 per night means the first two nights cost $180, but the third night is only $153. Thus the value which would be returned for the above example is: $514.8499999

Stage 2:

Create a second class in the lab02a package, named BookingMain, which will be used to test that the Booking class (from stage 1 of this task) is functioning correctly.

1. Define 5 variables to be of the Booking class type. (If you are comfortable using arrays, you may use an array instead).

2. Assign to each variable, a new Booking object constructed using hard-coded values given to the constructor using data from the following table: See image.

3. Using the relevant mutator method, adjust the expenses relating to items from the fridge to be as given in the second table above note that in some cases, more than one items expenses is given, and your code should accept these in separate calls of the method to accumulate a total.

4. Demonstrate that toString() method works, by displaying the result for each of the Bookings

5. Using a range of other separate print statements, display individual pieces of information about different bookings attributes.

6. In at least one case, demonstrate that the mutator method works to actually change the fridge expenses, by showing the result before, and then after, calling of the mutator method.

Tasks Part B

1. Create a new package, for Part B, named lab02b, into which you will place the code files for this Part.

The purpose of the Card class will be to represent a playing card. Each playing card has both a value and a suit.

There are 4 suits: Spades, Hearts, Clubs and Diamonds.

There 13 possible values for a card: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen and King

Stage 1:

You are to write the Card class following the principles as taught this week, to enable objects to be created to represent playing cards:

a) Ensure that there is a suitable constructor that accepts two arguments, which should both be integers. One represents the card value, the other represents the suit type.

b) Ensure that there are accessors for the cards value, and the cards suit

c) Provide mutators to alter (separately from each other) both the cards value and the cards suit

d) Write a toString() method so that a String representation of the card is returned. Suitable representations would be similar to the following: Ace of Spades, 5 of Clubs, King of Clubs, 10 of Diamonds.

Stage 2:

In order to check that your Card class (from stage 1 of this Part) is working correctly, you need to write a test driver class, named CardTest (in the lab02b package).

1. Create 4 card objects of different suits and values, and assign them to separate variables (or an array, if you feel comfortable).

2. Display the result of calling toString() for each of the cards

3. Using one of the cards:

a. Show that you can change the value of the card to another valid value

b. Show that an attempt to change the value of a card to an invalid value will not have any effect that the card will remain unchanged.

c. Show that you can change the suit of the card to another valid value

d. Show that an attempt to change the suit of a card to an invalid value will not have any effect that the card will remain unchanged.

Challenge Tasks

Challenge tasks are for the people who feel that they are on top of the material at the moment and want a bit more of a challenge. If you dont feel up to it at the moment you might be in a few weeks time save them up for then. You wont get much immediate help from tutors one of the challenges is to find answers for yourself . However, you are welcome, in fact encouraged, to discuss them with your classmates inside and outside of the lab session.

1. Create a program with a JFrame object (similar to the challenge task from lab 1).

2. Explore the add method of the JFrame class.

3. Create a new JButton object (see the Java API) and use the add method to place this object into the window.

4. Use the following line of code to add the object to the frame.

jf.add (jb, "North");
What happens?

5. Experiment with South, East, West and Center.

6. Create new JButton objects and add them to different parts of the frame.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.