I am having issues with my computer science homework and am not sure where to st
ID: 672170 • Letter: I
Question
I am having issues with my computer science homework and am not sure where to start. Could someone walk me through this?
"Create two classes, Radio.java and SimRadio.java. Radio.java should be built according to the class diagram included below. Your radio will be able to store its current station, your favorite station and the volume that it’s at now. SimRadio.java will contain the main method. In the main method, you should create an instance of your Radio and then utilize its methods in the following way:
1. Prompt the user for a station number.
2. Prompt the user for a volume.
3. Prompt the user for a favorite station.
4. Set the values in the radio based on the information from the user.
5. Print out the current information in the Radio (using the getX methods).
6. Prompt the user for a change in volume.
7. Change the volume by the specified amount.
8. Print out the current information in the Radio (using the getX methods).
9. Change the station to the favorite.
10. Print out the current information in the Radio (using the getX methods).
UML Diagram:
Class Radio
Instance Variables
int
volume
float
current_station
float
favorite_station
Method
description
Radio ( )
constructor
void setVolume ( int )
changes the volume to be the value in the parameter
int getVolume ( )
returns the current volume of the radio
void setCurrentStation ( float )
changes the current station to be the parameter
float getCurrentStation ( )
returns the current station
void setFavoriteStation ( float )
changes the value saved for the favorite station
float getFavoriteStation ( )
returns the current favorite station
void increaseVolume ( int )
increases the volume on the radio by the value in the parameter
void decreaseVolume ( int )
decreases the volume on the radio by the value in the parameter"
Class Radio
Instance Variables
int
volume
float
current_station
float
favorite_station
Method
description
Radio ( )
constructor
void setVolume ( int )
changes the volume to be the value in the parameter
int getVolume ( )
returns the current volume of the radio
void setCurrentStation ( float )
changes the current station to be the parameter
float getCurrentStation ( )
returns the current station
void setFavoriteStation ( float )
changes the value saved for the favorite station
float getFavoriteStation ( )
returns the current favorite station
void increaseVolume ( int )
increases the volume on the radio by the value in the parameter
void decreaseVolume ( int )
decreases the volume on the radio by the value in the parameter"
Explanation / Answer
/* Package for Using Scanner object to read inputs from user */
import java.util.*;
class Radio
{
int volume;
float current_station;
float favorite_station;
/*Constructor */
Radio()
{
// Not mention anything for constructor in question.
}
/*Setter methods for current station, fav station and volume */
void setVolume(int v)
{
volume=v; // remember here Volume=v will automatically convert to this.volume=v;
// 'this' keyword is used for current object.
}
void setCurrentStation( float s )
{
current_station=s;
}
void setFavoriteStation (float fs )
{
favorite_station=fs;
}
/*Getter methods for Current station, fav Station and Volume */
float getCurrentStation ( )
{
return current_station;
}
float getFavoriteStation ( )
{
return favorite_station;
}
int getVolume ( )
{
return volume;
}
/* Increase or Decrease in Volume. */
void increaseVolume ( int iv)
{
volume=volume+iv;
}
void decreaseVolume ( int dv )
{
volume=volume-dv;
}
}
public class SimRadio {
public static void main(String[] args)
{
float station_no,fav_station;
int vol;
int increaseVol,decreaseVol;
/* Creating object to call members of Radio class. Thats why We need to create object of Radio Class.*/
Radio r = new Radio();
Scanner s= new Scanner(System.in);
/* Prompting user for values*/
System.out.println("Enter Station number");
station_no=s.nextFloat();
System.out.println("Enter Volume");
vol=s.nextInt();
System.out.println("Enter Favourite Station");
fav_station=s.nextFloat();
/* Setting values in corresponding fields using setter methods */
r.setCurrentStation(station_no);
r.setFavoriteStation(fav_station);
r.setVolume(vol);
/* Printing values using getter methods*/
System.out.println("Current Station is :" + r.getCurrentStation());
System.out.println("Favourite Station is :" + r.getFavoriteStation());
System.out.println("Current Volume is :" + r.getVolume());
/* Increase or decrease in Volume*/
System.out.println("Enter amount for increase the volume");
increaseVol=s.nextInt();
r.increaseVolume(increaseVol);
System.out.println("Current Station is :" + r.getCurrentStation());
System.out.println("Favourite Station is :" + r.getFavoriteStation());
System.out.println("Current Volume is :" + r.getVolume());
System.out.println("Enter amount for Decrease the volume");
decreaseVol=s.nextInt();
r.decreaseVolume(decreaseVol);
System.out.println("Current Station is :" + r.getCurrentStation());
System.out.println("Favourite Station is :" + r.getFavoriteStation());
System.out.println("Current Volume is :" + r.getVolume());
/* Setting current station to Favourite Station*/
System.out.println("Setting your current station to favourite station");
r.favorite_station=r.current_station;
System.out.println("Current Station is :" + r.getCurrentStation());
System.out.println("Favourite Station is :" + r.getFavoriteStation());
System.out.println("Current Volume is :" + r.getVolume());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.