Exercise 2

The aim of this exercise is for you to become familiar with using other people's classes and building your own.


Part A - The Radio

You are given a class (defined in Radio.java) written by someone else. It emulates a virtual radio that you can tune in to various FM frequencies and listen to.

You must write a program that does the following using the Radio class:

  1. Prints out the frequency of Classic FM
  2. Prints out the frequency of BBC Radio 3
  3. Prints out what is playing on BRMB
  4. Prints out what is happening on BBC Radio 2
  5. Prints out what can be heard on 92.7 FM
  6. Prints out what can be heard on 94.5 FM

Hints

You should:

  1. Save the class Radio to a file called Radio.java in your java folder
  2. Look at the documentation for the Radio API (this was generated directly from the Radio.java source code)
  3. Examine what constants and methods the Radio class defines
  4. Note the difference between static methods (green dots) and non-static methods (red dots)
  5. Create a new class in its own, separate java file in the same folder
    (perhaps calling it RadioTest, and saving it in RadioTest.java)
  6. Implement the main method
  7. Remember to compile both Radio.java and RadioTest.java
    (javac Radio.java and javac RadioTest.java)
  8. However, you only pass the object with the main method to the Java interpreter when you want to run it
    (so use java RadioTest not java Radio)
  9. Note that a double value (34.3 or 34.3d) is slightly different from a float (34.3f)


Part B - Temperature

The software development house that you work for has been approached by RoastIt (a company that makes heating appliances) who want some software for their new computer controlled range of domestic cookers and scientific kilns. They are planning to give the new range a special controller that allows people to enter the desired cooking temperature in Celsius, Fahrenheit, Gas Marks or Kelvin.

You've been asked to write a part of the system - in particular, the class of Temperature objects. Each temperature object will store a single temperature value in degrees Celsius, and provide a conversion service to and from the other temperature scales. The specification of the methods and data members that the class should have has been provided by the team who are writing the user interface and the control components of the system. It can be found in Temperature.java

To help you ensure that it works correctly, they provided you with TempTest.java - a test driver program.

Hints


Extras

Part A

Add your own favourite radio station to the Radio class and adapt your test program to tune in and listen to it.

Part B

Your software company has now been approached by WeatherSense (a company that builds outdoor thermometers and weather stations) - they would like you to write software for their new range of computerised weather sensors. In particular they want to convert temperatures into common language like "bloody freezing", "freezing", "chilly", "warm", "hot" and "phew what a scorcher!". Add a new method to your Temperature class (maybe public String getCommonLanguage()) that does this and modify TestTemp to check that it works.

Why not define a constructor for the temperature class that can be used for setting the temperature when you create it? You could then create some public final static constants for the BOILING_POINT and FREEZING_POINT of water, ABSOLUTE_ZERO, BODY temperature etc.


If you want to practice these skills some more, try the additional appointments exercise.