Assignment 1: They Speak Java
ProblemsPage 1 of 7

1. The Radio Station Problem

Interviewer: Wow, you really seem to know your Java! I have 1 last question for you.

Let me give you some background. We currently communicate with Ninjel in Earth by sending/receiving a special kind of signals. However, those signals are expensive to send, so we try not to send unnecessary signals.

However, most of us are very excited to hear updates Ninjel brings. The two major radio stations around here - North Radio Station and South Radio Station are not exceptions.

Because they are in a very tight competition, both radio stations are criticized for sending too many signals to Earth, asking Ninjel if there are any new updates. Most of the times, Ninjel does not have any updates (seems like CMU is not the most happening place), so those signals are wasted.

Question for You

Ninjel currently uses this Java class called NinjelData to store interesting data.

  public class NinjelData {
    // Interesting data
    private String favoriteSong;  // Ninjel's favorite song
    private double temperature;   // in CMU
    private double usbDrivePrice; // for 16GB model
    private String fence;         // What's written on CMU fence
    // Getters for above variables
    public String getFavoriteSong() {...}
    public double getTemperature() {...}
    // Setters for above variables.
    public void setFavoriteSong(String newFavoriteSong) {...}
    public void setTemperature(double newTemperature) {...}

And Radio Stations:

  1. Keep a copy of the most recent data.
  2. Request for a new NinjelData object every 10 minutes using broadcastUpdates.
  3. If any one of the data changed, then broadcast.
  public class NorthRadioStation {
    // Keep a copy of the most up-to-date Ninjel data
    private String mostRecentFavoriteSong;
    private double mostRecentTemperature;
    // This method called every 10 minutes
    public void broadcastUpdates() {
      nD = SignalCenter.requestNinjelDataObject();

      // Check to see if anything has changed
      // since last check
      if (!nD.getFavoriteSong().equals(mostRecentFavoriteSong)
        || nD.getTemperature() != mostRecentTemperature
        || ... ) {
        // If so, send out a broadcast

        // Update current variables
        mostRecentFavoriteSong = nD.getFavoriteSong();
        mostRecentTemperature = nD.getMostRecentTemperature();

Again, the main issue we're having is, an expensive code SignalCenter.requestNinjelDataObject() is called every 10 minutes (in line 11).

To see this in picture, it looks like this:

Spend couple minutes to understand what's happening. Use pen and paper if it's a bit confusing.

Next Page

You: This is clearly a bad design. I can do better! Next Page

Assignment 1:
They Speak Java