Вы находитесь на странице: 1из 2

WXES1115/WXES1117

Mid Semester Examination Sem. 2, 2014/2015


A sandwich is a food item consisting of one or more types of food placed
on or between slices of bread, or more generally any dish wherein two or
more pieces of bread serve as a container or wrapper for some other food.
The sandwich was originally a portable food item or finger food, which
began its popularity primarily in the Western World, but is now found in
various versions in numerous countries worldwide.
[Source: Wikipedia]
Problem description:
Your task is to create a sandwich from any given list of ingredients. The possible ingredients include:
1. Bread, denoted by character B
2. Cheese, denoted by character C
3. Ham, denoted by character H
4. Lettuce, denoted by character L
5. Tomato, denoted by character T
You must try to make sure the sandwich is delicious. A delicious sandwich should fulfill two criteria: a)
there is no consecutive layers of bread, and b) both top and bottom layers should be bread. For instance,
referring to the figure below, A and B are delicious sandwiches, whereas C and D are not.

Bread

Top layer

Bottom layer

Bread

Lettuce

Bread

Lettuce

Bread

Bread

Tomato

Tomato

Lettuce

Tomato

Cheese

Cheese

Bread

Cheese

Ham

Ham

Tomato

Lettuce

Bread

Bread

Bread

Bread

To assist the right ordering of inserting the layers of ingredients, our top chef has defined the rules below:
1. The bread can be the top, bottom, or between two consecutive non-bread ingredients. However,
the first encountered bread must be added to the bottom layer (as in Sandwich D), and the second
encountered piece of bread should be at the top (as in Sandwich A).
2. All of the cheese, ham, lettuce and tomato layers should be added following the order in which
they are given. For example, if the order of the given ingredients is B, H, C, T, L, B then the
sandwich produced should be same as Sandwich A. ** It is not always the case that the breads are
listed first. H, C, T, L, B, B will produce the same sandwich **
3. If more bread layers were added, it should be inserted in between the layers of non-bread
ingredient, from top to bottom. For example, adding an extra piece of bread to Sandwich A
produces Sandwich B. If all non-bread layers are used, the bread should be placed on top, like
Sandwich C (no matter how many extra layers of bread).

Page 1

WXES1115/WXES1117 Mid Semester Examination Sem. 2, 2014/2015


Question: [only ONE, as promised]
Implement a Sandwich class that inherits the LinkedList class that you have implemented during
your lab.
Requirements:
Your Sandwich class should comprises an add method to add a layer of ingredient and a
showSandwich method to print all of the layers of the sandwich from the top to the bottom.
Sample input

Sample output

BHB

BHB

BHCTLB

BLTCHB

CHLTBB

BTLHCB

CHLTBBB

BTBLHCB

CHLBBBTTT

BTTTLBHCB

BBBCHLT

BTLHBCB

CHLT

TLHC

BBBTTBBHC

BCBHBTBTB

Hints:
1. You may need to implement those necessary methods of LinkedList class.
2. You may want to write a SandwichTester class to test your class using above test cases.

Page 2

Вам также может понравиться