Sample Problems

Design Patterns and Class Diagrams

Be able to translate a text-based description of a system to a class diagram to code and back:

A picture containing screenshot, text, white, line

Description automatically generated

Also, look for how design patterns can be used.

Problem

Olah is a first-person shooter game in which a player moves through a network of hostile locations. When he enters a location, all of the monsters hiding within attack him. Different monsters can have different styles of attacking. Some bite, some crush, and some poison. Additional attack styles may be available in future releases. Additionally, a monster can dynamically change its attack style. Some monsters have a composite attack styles that may combine biting, crushing, stabbing, and other composite attack styles.  Olah must also include Zap, a legacy attack style from an earlier version. Unfortunately, the Zap class doesn't implement any interfaces in the latest version.

a. Draw a class diagram showing how you would design Olah. Use any relevant design patterns.

b. Implement your design. (No need for algorithmic details.)

Problem

A pattern catalog contains the following patterns:

A. Publisher-Subscriber, B. Farmer-Chef, C. Adapter
D. Strategy, E. Alarm Clock, F. Abstract Factory
G. Composite, H. Model-View-Controller, I. Proxy
J. Master-Slave

Match the problem with the letter of the pattern that would typically be used to solve it:

      i.          You need to represent an organizational hierarchy.

    ii.          Employees need to be alerted when meetings on a calendar come up.

  iii.          A recipe for vegetable soup needs to be independent of where the vegetables are obtained.

   iv.          A soldier needs to be able to change battle tactics quickly.

     v.          Bank customers, employees, and administrators need different user interfaces to the same banking application.

Problem

A financial application wants to provide a toolkit that includes calculator implementing the following interface:

interface ICalculator {
  float getFutureValue(float r);
  float getMonthlyPayment(float r, float t);
  float getRate(float amt);
}

class ToolKit {
  ICalculator calculator;
  // etc.
}

Fortunately, a financial calculator with the required functionality exists:

class FinancialCalculator {
  double computeFutureValue(double r);
  double computeMonthlyPayment(double r, double t);
  double computeRate(double amt);
}

Unfortunately, this class does not implement the required interface. Without modifying any of the above code, show (i.e., by writing some Java code) how you would fix this problem.

Customizing Frameworks

When customizing a framework, don't over-do it. Know what you've inherited and only override the methods you need to.

Problem: Customizing Echo

A firewall proxy blocks certain requests considered harmful to the client. Implement a reusable FirewallHandler class that extends ProxyHandler and has a block method that returns true if the client request is in a list of blocked requests. In these cases the message "your command has been blocked" is sent back to the client. Test your implementation by blocking division and subtraction commands from reaching the Math Server.

Problem: Customizing SimStation

Ants randomly move around sugar scape searching for sugar pods to eat. When an ant eats a sugar pod its fitness increases by 1. As fitness increases, so does size and speed. Here are screen shots at clock = 28 seconds and clock = 50 seconds; the ants are the red dots (some bigger and faster than others) and the sugar pods are the black dots. Notice that the pods are disappearing:

A screenshot of a computer

Description automatically generated with medium confidence     A screenshot of a computer screen

Description automatically generated with medium confidence

In this implementation the simulation (SugarScape) maintains a set of random points representing where sugar pods are located. Ants move about randomly changing their directions. When an ant's position coincides with the position of a sugar pod, the ant eats the sugar pod (removing it from the set) and increments its fitness by 1. Do not allow two ants to eat the same sugar pod! Assume an ant's fitness is initially 3 and that fitness also represents an ant's speed and size.

       i.           Implement the Ant class

    ii.           Implement the SugarScapeView class

  iii.           Implement the SugarScapeFactory class

Problem: Customizing mvc

Clock Simulator simulates the hour hand of a clock:

A screenshot of a computer

Description automatically generated with medium confidence

Each time the user clicks the Tick button or selects Tick from the Edit menu, the hour is displayed and the hour hand moves.

Hints:

·        Use trig to compute the points on the unit circle representing 1:00, 2:00, etc.

·        Here's a start to ClockView.java. It includes a useful method called transform that transforms points on the unit circle in the usual coordinate system to points on the circumference of a circle of radius VIEW_SIZE/2 in the center of the

Problem: Reflection

A software company requires each class to implement the Testable interface:

interface Testable {
  public Boolean runTests(); // = true if implementing class passes all tests
}

Complete the implementation of the following test driver:

class TestDriver {
     public Integer runTests(Set<String> classNames) { ??? } // = # that failed
}

Solutions

1.      Financial Toolkit

2.      FirewallHandler.java

3.      ClockPanel.java

4.      SugarScape.java

5.      Reflection.java

6.      Olah

A picture containing diagram, text, plan, technical drawing

Description automatically generated