Problem:
Radio
|
-
channels : Channel[]
-
volume : int
|
+ Radio(channels : Channel[], volume : int)
+ setChannels(channels : Channel[]) : void
+
getChannels() : Channel[]
+
setVolume(volume : int) : void
+
getVolume() : int
+
saveChannelsToFile(filename : String) : void
|
Channel
|
- frequency : double
- name : String
|
+ Channel(frequency : double, name : String)
+ setFrequency(frequency : double) : void
+ getFrequency() : double
+ setName(name : String) : void
+ getName() : String
+ toString() : String
|
You
have to implement the previous two classes. The class Channel has two private
variables, a constructor, setters and getters, and a toString mthod. The class
Radio has an array of channels and a volume.
In
the tester class, you are required to read input from a file called
"channels.txt" using a Scanner. The first line in the input file is
the number of channels. Each additional line in the input file represents the
name of the channel and its frequency.
After
reading the number of channels, you need to create an array of Channels and its
length would be the number of channels.
For
each additional line you read, you need to create a new channel and add it to
the array.
After
reading the input, you should create a new Radio object an give it the read
channels array as a parameter. Then, you should call the method
saveChannelsToFile(String filename) to save the channels array to a file called
"radio.txt" using a Formatter.
Output:
2
MixFM
104.4
RadioOne
105.5
MixFM 104.4
RadioOne 105.5
Solution:
/**------------------------Channel.java---------------------*/ public class Channel { private double frequency; private String name; public Channel(double frequency, String name) { this.frequency = frequency; this.name = name; } public double getFrequency() { return frequency; } public void setFrequency(double frequency) { this.frequency = frequency; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /**------------------------Radio.java---------------------*/ import java.io.File; import java.util.Formatter; public class Radio { private Channel[] channels; private int volume; public Radio(Channel[] channels, int volume) { this.channels = channels; this.volume = volume; } public Channel[] getChannels() { return channels; } public void setChannels(Channel[] channels) { this.channels = channels; } public int getVolume() { return volume; } public void setVolume(int volume) { this.volume = volume; } public void saveChannelsToFile(String filename) { try { Formatter formatter = new Formatter(new File(filename)); for (int i = 0; i < channels.length; i++) { formatter.format("%s\n", channels[i].getName()+ " "+channels[i].getFrequency(), true); } formatter.close(); } catch (Exception e) { System.err.println("Could not save file"); } } } /**------------------------RadioTester.java---------------------*/ import java.io.File; import java.io.IOException; import java.util.Scanner; public class RadioTester { public static void main(String args[]) { Scanner scan; try { scan = new Scanner(new File("channels.txt")); int numberOfChannels = scan.nextInt(); Channel[] channels = new Channel[numberOfChannels]; for (int i = 0; i < numberOfChannels; i++) { String name = scan.next(); double frequency = scan.nextDouble(); channels[i] = new Channel(frequency, name); } scan.close(); Radio radio = new Radio(channels, 10); radio.saveChannelsToFile("radio.txt"); } catch (IOException e) { System.err.println("Error reading file"); } } }
liposuction cost Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.
ReplyDeleteBe that as it may in the event that you are new to the game avoid the Warrior as it is the most troublesome class to utilize and on the off chance that you can connect together the combos you will be destructive however it takes practice. Assignment Help
ReplyDelete