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

DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
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...
DO THIS IN C#. Design and implement a program (name it MinMaxAvg) that defines three methods...
DO THIS IN C#. Design and implement a program (name it MinMaxAvg) that defines three methods as follows: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. In the main method, test all three methods with different input value read from the...
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...
Design a class named Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT