Question

In: Computer Science

: Design and implement class Radio to represent a radio object. The class defines the following...

: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods:

Assume that the station and volume settings range from 1 to 10.

  1. A private variable of type int named station to represent a station number. Set to
  2. A private variable of type int named volume to represent the volume setting. Set to 1.
  3. A private variable of type boolean named on to represent the radio on or off. Set to false.
  4. A non-argument constructor method to create a default radio.
  5. Method getStation() that returns the station.
  6. Method getVolume() that returns the volume.
  7. Method turnOn() that turns the radio on.
  8. Method turnOff() that turns the radio off.
  9. Method stationUp() that increments the station by 1 only when the radio is on.
  10. Method stationDown() that decrements the station by 1 only when the radio is on.
  11. Method volumeUp() that increment the volume by 1 only when the radio is on.
  12. Method volumeDown() that decrements the volume by 1 only when the radio is on.
  13. Method toString()to printout a meaningful description of the radio as follows(if the radio is on):

The radio station is X and the volume level is Y. Where X and Y are the values of variables station and volume. If the radio is off, the message is: The radio is off.

Now design and implement a test program to create a default radio object and test all class methods on the object in random order. Print the object after each method call and use meaningful label for each method call as shown in the following sample run.

Sample run:

Turn radio on:

The radio station is 1 and the volume level is 1.

Turn volume up by 3:

The radio station is 1 and the volume level is 4.

Move station up by 5:

The radio station is 6 and the volume level is 4.

Turn volume down by 1:

The radio station is 6 and the volume level is 3.

Move station up by 3:

The radio station is 9 and the volume level is 3.

Turn radio off.

The radio is off.

Turn volume up by 2: The radio is off.

Turn station down by 2: The radio is off.

Solutions

Expert Solution

Requirements include toString() method which is in JAVA. Hence writing a JAVA Program.

JAVA Program:

import java.io.*;

class Radio {
private int station,volume;
private boolean on;
public Radio(){
this.station = 1;
this.volume = 1;
this.on = false;
}
  
public int getStation(){
return this.station;
}
  
public int getVolume(){
return this.volume;
}
  
public void turnOn(){
System.out.println("Turn radio on:");
this.on = true;
}
  
public void turnOff(){
System.out.println("Turn radio off:");
this.on = false;
}
  
public void stationUp(int stat){
System.out.println("Move station up by "+stat+":");
if(this.on)
{
if(this.station+stat>10)
System.out.println("Station Unavailable!!");
else{
this.station+=stat;
}
}
}
  
public void stationDown(int stat){
System.out.println("Move station down by "+stat+":");
if(this.on){
if(this.station-stat<0)
System.out.println("Station Unavailable!!");
else{
this.station-=stat;
}
}
}
  
public void volumeUp(int vol){
System.out.println("Turn volume up by "+vol+":");
if(this.on)
{
if(this.volume+vol>10)
System.out.println("Volume Unavailable!!");
else{
this.volume+=vol;
}
}
}
  
public void volumeDown(int vol){
System.out.println("Turn volume down by "+vol+":");
if(this.on){
if(this.volume-vol<0)
System.out.println("Volume Unavailable!!");
else{
this.volume-=vol;
}
}
}
  
public String toString(){
if(this.on)
return "\tThe radio station is "+getStation()+" and the volume level is "+getVolume()+".\n";
else
return "\tThe radio is off.";
}
   public static void main (String[] args) {
       Radio r = new Radio();
       r.turnOn();
       System.out.println(r);
       r.volumeUp(3);
       System.out.println(r);
       r.stationUp(5);
       System.out.println(r);
       r.volumeDown(1);
       System.out.println(r);
       r.stationUp(3);
       System.out.println(r);
       r.turnOff();
       System.out.println(r);
       r.volumeUp(2);
       System.out.println(r);
       r.stationDown(2);
       System.out.println(r);
   }
}

Output:


Related Solutions

Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT