3.1b Radio Project

Framing the Project (real-world):

You will be creating an object that represents an FM car radio.  You will be simulating the various controls of the radio such as volume control, tuning to various stations, preset stations, and turning it on and off.  Specifically the radio will have:

  • 5 preset stations

  • a volume control

    • minimum level of 0

    • maximum level of 20

  • a mute button

    • if the mute is on the all other controls (methods) will not change the volume

    • when the mute is turned off, the loudest volume it will go back to is 10

  • a tuner control

    • minimum frequency of 87.5

    • maximum frequency of 108.0

  • on / off switch


When the radio has been used and is turned off, the next time the radio is turned on:

  • if the mute is on, it will be turn off

  • the previously set volume level will be used but the volume will not be over 10

  • the current station will be the same station as when the radio was turned off


Program Design


Class Name:  FMRadio()


Fields

Constants:

final double MAX_FREQUENCY = 108.0

final double MIN_FREQUENCY = 87.5

final int MAX_VOLUME = 20

final int MIN_VOLUME = 0

Instance variables (visibility for all instance variables is private):

isOn, type: boolean

  • the status of the radio - true/false for on/off

  • initialize to false (off)

isMuted, type:  boolean

  • the status of the mute feature - true/false for on/off

  • initialize to false (off)

mutedVolume, type: int

  • the volume to set when the mute is turn off

  • initialize to MIN_VOLUME

currentStation, type: double

  • station radio is currently tuned to

  • all stations must be between MAX_FREQUENCY and MIN_FREQUENCY (inclusive)

  • initialize to MIN_FREQUENCY

volume, type: int

  • current volume level

  • volume must be between MAX_VOLUME and MIN_VOLUME (inclusive)

  • initialize to MIN_VOLUME

presetStation[ ], type: double array

  • array with 5 locations

  • each location stores a radio station frequency

  • initialized by resetRadio() in constructor


Constructor:

  • initialize instance variables as indicated above

  • invoke resetRadio()


Methods:

Private:

resetRadio()

  • the volume is set to 0

  • all preset stations and the current station are set to MIN_FREQUENCY

  • the radio is off

  • the mute is off


initPresetStations()

  • set all preset station to MIN_FREQUENCY


Public:

displaySettings()

  • display on the system console, the current settings for the radio

    • on/off

    • station

    • mute on/off

    • volume

    • station settings for each of the 5 presets

increaseVolume()

  • increase the volume by 1

decreaseVolume()

  • decrease the volume by 1

setPresetStation(int presetNumber)

  • save the currentStation to the  presetNumber

  • display a message similar to “Preset 1 set to 98.1”

increaseFrequency()

  • increase the currentStation frequency by 0.1

decreaseFrequency()

  • decrease the currentStation frequency by 0.1

changeStation(int preset)

  • change the current station to the preset station

changeStation(double station)

  • change the currentStation to the station

seekUp()

  • increase the frequency of the station by 0.5

  • if the station is too high, set the station to the minimum frequency

seekDown()

  • decrease the frequency of the station by 0.5

  • if the station is too low, set the station to the maximum frequency

muteVolume()

  • if the radio isMuted

    • set the volume to the mutedVolume

    • set isMuted to false

  • otherwise

    • sets the volume to 0

    • set isMuted to true

powerSwitch()

  • turn on the radio if it is off and off if it is on

Comments