Lab 5 En

1. We need to model a robot responsible for assembling a car. For the sake of simplicity, we consider that a car has only three components: a Car Body, an Engine and a Brake.

  • An Engine can be a TSI 1.2 Engine, a TSI 1.8 Engine or a Diesel Engine.
  • A Brake can be a normal brake or a brake supporting the Hill Hold Control Feature.
  • The Car Body can be a Simple Car Body, a Sport Body or a Combi Body.

The Robot provides a single functionality: assemblyCar(). For example, in the next fragment it is provided a possible implementation for assembling a Combi TSI 1.2 Car with a normal Brake:

     class Robot {
          public void assemblyCar() {
		Engine e = new TSI12Engine();
		Brake b = new NormalBrake();
		CarBody c = new CombiBody();

		System.out.println(“Assembly the car”);
          }
     }

What is wrong with the provided implementation? Modify the implementation using the Abstract Factory Pattern. Give the implementations for your factories for assembling

  • a Combi TSI 1.2 Car with a normal Brake
  • a Sport TSI 1.8 Car with a Brake supporting the Hill Hold Control Feature.

2. We need to implement using the Strategy Pattern a system that provides the basic operations with integers (+, -, /, *). The two integer values are read and afterwards the user enters the given operation and the result is displayed.

3. In the code given below we can find a sequence of duplicated code between the two methods belonging to the implemented two classes. Unfortunately the two methods (makeStuff) are not identical and have minor differences. Refactor the code in order to remove the duplicated code - you are allowed to modify the given classes (e.g. add new attributes and new methods) but you are not allowed to add new classes.

 class Worker {
  protected int x;

  public Worker(int _x) { x = _x; }

  public int makeStuff() {
  	int z;
    	x++; z = x*x; 
	System.out.println("Values are:" + x + " and " + z);
	return x + z; 
  }
 }

 class SpecializedWorker extends Worker {
  private int y;

  public SpecializedWorker(int _x, int _y) { super(_x); y = _y; }  

  public int makeStuff() {
  	int z;
    	x += y; z = x*x + y*y; 
	System.out.println("Values are:" + x + " and " + z);
	return x + z; 
  }
 }

4. What design pattern should be used in order to create arithmetic expressions like:

  • 3
  • 3 + 5
  • 3 * 7
  • 3 * (7 - 5)
  • (3 + (7 + 5))*3

Implement the hierarchy of expressions that provides a service for printing an expression as well as a service for computing the value of an expression.

5. Implement a system that allows us to model different types of pizzas. Details about the components of a pizza should be printed and each pizza has a cost. The basic pizza that is served is SimplePizza. The components of this pizza are ketchup and mozzarella and the cost of a simple pizza is 10RON. We have different ingredients that can be added to a pizza and we can add any existing ingredient (or any possible combination) to a SimplePizza. For the sake of simplicity current we have only the next ingredients:

  • Ham - 5 RON
  • Extra cheese - 5 RON
  • Mushroom - 4 RON
  • Salami - 5 RON

6. Exercise.