Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Programming (Java) Assignment: Guests with Subclassing at a Bed and Breakfast Imagine that you run a bed and breakfast, or a youth hostel, that

Computer Programming (Java) Assignment:

Guests with Subclassing at a Bed and Breakfast

Imagine that you run a bed and breakfast, or a youth hostel, that gets a lot of foreign visitors. You are to write an assignment that enables you to remember the names of your guests and greet them in their native language. In this assignment, you will create a class called Guest, that serves that purpose. Then you will create subclasses of the Guest class for different languages. Write a class called Guest that has three variables of type string. One variable is called name, one is called nightGreeting and one is called morningGreeting. Define a constructor that takes the name as an argument, and sets the nightGreeting and morningGreeting strings in English. Define three setters for the name, morning, and night strings. Define two additional methods called greetNight and greetMorning that each return a String that contains a sentence to say Good night, and Good morning to the guest. The returned sentence is composed of the night and morning strings, respectively, followed by a space and then the name, and ending with a period. Your Guest class should work with the following implementation of main. (Notice that this example demonstrates two different versions of a for loop used in conjunction with a list type.) The output appears immediately below the code.

public static void main(String[] args) {

ArrayList guestList = new ArrayList();

// Register new guests

guestList.add(new Guest("Mary"));

guestList.add(new Guest("Jaap"));

guestList.add(new Guest("Heidi"));

guestList.add(new Guest("Jean Claude"));

// Bed time sequence

System.out.println("Time to turn out the lights.");

for (int i = 0; i < guestList.size(); i++)

{

Guest visitor = guestList.get(i);

System.out.println(" " + visitor.greetNight());

}

// Breakfast sequence

System.out.println("Time for breakfast!");

for (Guest visitor : guestList)

{

System.out.println(" " + visitor.greetMorning());

}

}

Time to turn out the lights.

Good night Mary.

Good night Jaap.

Good night Heidi.

Good night Jean Claude.

Time for breakfast!

Good morning Mary.

Good morning Jaap.

Good morning Heidi.

Good morning Jean Claude.

Once the above code is working, define a number of subclasses of the Guest class, each for a different language. To create a subclass, replace java.lang.Object with Guest in the superclass field when you create the class in Eclipse. You should then see extends Guest appear after the class name in the source. Each new subclass should override the constructor by defining its own version of the constructor. The constructor for a particular language must call the original constructor using a special word for this purpose, super.

Since the original constructor takes the name as an argument, it should appear as: super(name); Then call setNightGreeting() and setMorningGreeting() to change the greetings to a language other than English. To use the new subclasses, revise the main above to use a different subclass for each guest who does not speak English. For example, the line for Jaap would become: guestList.add(new DutchGuest("Jaap")); The previous output should now look like the following. For your assignment, you must create subclasses for at least 3 different languages. You may use any three from English, Dutch, German, and French as shown here or two languages from here and one language of your own. (Note that in German the nouns are capitalized.)

Time to turn out the lights.

Good night Mary.

Slaap lekker Jaap.

Gute Nacht Heidi.

Bonne nuit Jean Claude.

Time for breakfast!

Good morning Mary.

Guetemorgen Jaap.

Guten Morgen Heidi.

Bonjour Jean Claude.

Finally, to finish this assignment, create a new class, called GuestFactory, that contains a single public static method that returns a Guest object, called getGuestInstance(). The getGuestInstance method takes two strings for a name and a language. It uses the language string to decide which subclass of Guest to use for creating the object to be returned. If the language is not recognized, it should just use the Guest class which defaults to using English greetings. A method that returns a new object, using different subclasses depending on the need, is called a factory method. The method must be static so that it can be called without having an object of its class. Sometimes a factory method is included in the base class. Here it goes in a separate class so that new subclasses can be added without changing the base class. Once implemented, the code for Jaap, as above, should look like the following: guestList.add(GuestFactory.getGuestInstance("Jaap", "Dutch")); The output should again look as above, except with the guests and languages that you chose. Factory Method is one of the patterns in the Gang of Four book, Design Patterns.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions