Question: (2a) Model a physical entity as a Java Class (70 points) focus on Encapsulation : This is an Object Modeling/Programming exercise albeit around 1 (maybe
(2a) Model a physical entity as a Java Class (70 points) focus on Encapsulation:
This is an Object Modeling/Programming exercise albeit around 1 (maybe 2) Object(s). Using the Radio Class implementation provided as a point of reference, think of a common (or maybe, not so common) physical device or appliance thats worthy of modeling as an Object. Consider the following:
The basic operations/functionality of your device/appliance become public methods of the implementing Class (e.g., on, off, go, start, stop, )
The internal structure (or how you envision the internal structure) of your device/appliance become the data members of the implementing Class. This would include the internal piece-parts of the device and well as state components. For example, modeling a bread toaster might require a heat coil as well as a power on/off indicator.
In the spirit of modularity, reusability, etc., private/protected methods can and should be used to implement sub-tasks performed by public methods.
In addition to Class members supporting (a) (b) (c) above, you also need to implement:
Either a public void main(String [] ){} method or a second launch Class whose only job will be to:
Create an instance of your modeled Class
Invoke a sequence of methods on that instance that demonstrates the functionality and basic usefulness of what you built.
A String toString() method allowing you to display the state of the Object Instance.
What do we mean by the state of the Instance? The collective value of all data members at a moment in time is considered the instances state. Thats a valid definition of state, but for this lab, customize the Class toString() method to display values of data members that are considered significant to the device/applications operation.
Note: Refer to the Radio Class toString() and main() methods as a guide. Also refer to the Radios execution output below showing that a single instance has been created, and taken through a series of operations (method invocations). At critical points, a toString() representation of the instances state is displayed to the console using System.out.println()or the like.
Replace the output below with yours!
| New Instance Radio Instance: [SerialNumber=1411918066216:43514652, powerState=false, selectedVolume=0, selectedStation=0.0, selectedBand=null, amPresets=null, fmPresets=null, firstTimeOn=null, lastTimeOn=null, selectedBalance=0, selectedBassLevel=0, selectedTrebleLevel=0]
Turned On Radio Instance: [SerialNumber=1411918066216:43514652, powerState=true, selectedVolume=5, selectedStation=770.0, selectedBand=AM, amPresets=[563.0, 1080.0, 773.0, 730.0, 1192.0, 584.0, 608.0, 843.0], fmPresets=[99.0, 92.0, 101.0, 99.0, 88.0, 103.0, 92.0, 92.0, 92.0, 105.0, 99.0, 93.0], firstTimeOn=Sun Sep 28 11:27:46 EDT 2014, lastTimeOn=Sun Sep 28 11:27:46 EDT 2014, selectedBalance=0, selectedBassLevel=2, selectedTrebleLevel=3]
Changed Station Radio Instance: [SerialNumber=1411918066216:43514652, powerState=true, selectedVolume=5, selectedStation=92.3, selectedBand=FM, amPresets=[563.0, 1080.0, 773.0, 730.0, 1192.0, 584.0, 608.0, 843.0], fmPresets=[99.0, 92.0, 101.0, 99.0, 88.0, 103.0, 92.0, 92.0, 92.0, 105.0, 99.0, 93.0], firstTimeOn=Sun Sep 28 11:27:46 EDT 2014, lastTimeOn=Sun Sep 28 11:27:46 EDT 2014, selectedBalance=0, selectedBassLevel=2, selectedTrebleLevel=3]
Assign Preset [1] to + FM Radio Instance: [SerialNumber=1411918066216:43514652, powerState=true, selectedVolume=5, selectedStation=101.1, selectedBand=FM, amPresets=[563.0, 1080.0, 773.0, 730.0, 1192.0, 584.0, 608.0, 843.0], fmPresets=[101.1, 92.0, 101.0, 99.0, 88.0, 103.0, 92.0, 92.0, 92.0, 105.0, 99.0, 93.0], firstTimeOn=Sun Sep 28 11:27:46 EDT 2014, lastTimeOn=Sun Sep 28 11:27:46 EDT 2014, selectedBalance=0, selectedBassLevel=2, selectedTrebleLevel=3]
Turned Off Radio Instance: [SerialNumber=1411918066216:43514652, powerState=false, selectedVolume=5, selectedStation=101.1, selectedBand=FM, amPresets=[563.0, 1080.0, 773.0, 730.0, 1192.0, 584.0, 608.0, 843.0], fmPresets=[101.1, 92.0, 101.0, 99.0, 88.0, 103.0, 92.0, 92.0, 92.0, 105.0, 99.0, 93.0], firstTimeOn=Sun Sep 28 11:27:46 EDT 2014, lastTimeOn=Sun Sep 28 11:27:46 EDT 2014, selectedBalance=0, selectedBassLevel=2, selectedTrebleLevel=3] |
/** * Not all applications are designed and implemented to process repetitive data, * interact with a user through a web browser, etc. * Sometimes, we model a physical object in software - to perhaps: * - simulate a design of a product before we build it * - control a physical device through user provided controls - or an event/time driven control * * So this is an attempt to MODEL the structure and operation of an appliance we've all used: a radio * likely a car radio. * */ package edu.cuny.csi.csc330.radio; import java.util.*; import edu.cuny.csi.csc330.lab1.*; import edu.cuny.csi.csc330.lib.Randomizer; // Radio Class definition public class Radio { static int tester = 9; /** * group Band radio band information into nested inner Classes * static data and methods specific to AM/FM banding */ public static class Bands { static int tester = Radio.tester; public static double [] generateInitialPresets(int size, double from, double to) { Randomizer randomizer = new Randomizer(); double [] presets = new double[size]; for(int i = 0 ; i < presets.length ; i++ ) { presets[i] = randomizer.generateInt(from, to); } return presets; } static public class AM { public final static String name = "AM"; public final static int MAX_SELECTIONS = 8; public final static double LOW_STATION = 530; public final static double HIGH_STATION = 1200; // create initial AM BAND settings - RANDOM public static double [] generateInitialPresets() { return Bands.generateInitialPresets(MAX_SELECTIONS, LOW_STATION, HIGH_STATION); } } public static class FM { public final static String name = "FM"; public final static int MAX_SELECTIONS = 12; public final static double LOW_STATION = 88.30; public final static double HIGH_STATION = 107.10; // create initial FM BAND settings - RANDOM public static double [] generateInitialPresets() { return Bands.generateInitialPresets(MAX_SELECTIONS, LOW_STATION, HIGH_STATION); } } } ///////////////////////////////////////// // Misc static constants protected static final int MIN_VOLUME = 0; protected static final int MAX_VOLUME = 20; protected static final int DEFAULT_VOLUME = 5; protected static final String DEFAULT_BAND = Bands.AM.name; protected static final double DEFAULT_STATION = 770; protected static final int DEFAULT_BALANCE = 0; protected static final int DEFAULT_TREBLE = 3; protected static final int DEFAULT_BASS = 2; // Object instance properties ... private boolean powerState; private int selectedVolume; private double selectedStation; private String selectedBand; private double[] amPresets; private double[] fmPresets; private Date firstTimeOn; private Date lastTimeOn; private String serialNumber; private int selectedBalance; private int selectedBassLevel; private int selectedTrebleLevel; // the default and only constructor public Radio() { init(); } /** * initialize - Only gets invoked by constructor */ private void init() { // serial number Randomizer randomizer = new Randomizer(); Integer irand = randomizer.generateInt(11111111, 99999999); this.serialNumber = new Date().getTime() + ":" + irand.toString(); } /** * turn on the radio instance */ public void on() { Date now = new Date(); // all the things we need to do the 1st time we turn on the instance ... if(firstTimeOn == null) { // create pre-sets arrays - have the inner classes do it for us amPresets = Bands.AM.generateInitialPresets(); fmPresets = Bands.FM.generateInitialPresets(); firstTimeOn = now; // selectedVolume & station selectedVolume = DEFAULT_VOLUME; selectedBand = DEFAULT_BAND; selectedStation = DEFAULT_STATION; // tone and balance selectedBalance = DEFAULT_BALANCE; selectedTrebleLevel = DEFAULT_TREBLE; selectedBassLevel = DEFAULT_BASS; } powerState = true; lastTimeOn = now; } /** * turn off the radio instance */ public void off() { powerState = false; } /** * is the radio powerState on/off?? * @return */ public boolean isOn() { return powerState == true; } public int getVolume() { return selectedVolume; } public void setVolume(int volume) { this.selectedVolume = volume; } public void decreaseVolume(int volume) { this.selectedVolume -= volume; } public void increaseVolume(int volume) { this.selectedVolume += volume; } public double getSelectedStation() { return selectedStation; } public void setSelectedStation(double selectedStation) { this.selectedStation = selectedStation; } public String getSelectedBand() { return selectedBand; } public void setSelectedBand(String selectedBand) { this.selectedBand = selectedBand; } public double[] getFmPresets() { return fmPresets; } public Date getFirstTimeOn() { return firstTimeOn; } public int getSelectedBalance() { return selectedBalance; } public void setSelectedBalance(int selectedBalance) { this.selectedBalance = selectedBalance; } public int getSelectedBassLevel() { return selectedBassLevel; } public void setSelectedBassLevel(int selectedBassLevel) { this.selectedBassLevel = selectedBassLevel; } public int getSelectedTrebleLevel() { return selectedTrebleLevel; } public void setSelectedTrebleLevel(int selectedTrebleLevel) { this.selectedTrebleLevel = selectedTrebleLevel; } // Assign Band Presets - to current band public void assignToPreset( int position, double station) { if(Bands.AM.name.equals(this.selectedBand)) { this.amPresets[position - 1] = station; } else if(Bands.FM.name.equals(this.selectedBand)) { this.fmPresets[position - 1] = station; } } // Select from Preset - from current band public void selectFromPreset(int position) { if(Bands.AM.name.equals(this.selectedBand)) { this.selectedStation = this.amPresets[position - 1]; } else if(Bands.FM.name.equals(this.selectedBand)) { this.selectedStation = this.fmPresets[position - 1]; } } @Override public String toString() { return "Radio Instance: " + "[SerialNumber=" + serialNumber + ", powerState=" + powerState + ", selectedVolume=" + selectedVolume + ", selectedStation=" + selectedStation + ", selectedBand=" + selectedBand + ", amPresets=" + Arrays.toString(amPresets) + ", fmPresets=" + Arrays.toString(fmPresets) + ", firstTimeOn=" + firstTimeOn + ", lastTimeOn=" + lastTimeOn + ", selectedBalance=" + selectedBalance + ", selectedBassLevel=" + selectedBassLevel + ", selectedTrebleLevel=" + selectedTrebleLevel + "] "; } /** * @param args */ public static void main(String[] args) { // Create instance of - power up & down ... other ops, and // display the powerState of the Radio instance Radio radio = new Radio(); System.out.println("New Instance " + radio + " "); // turn it on radio.on(); System.out.println("Turned On " + radio + " "); // select FM Band & a station radio.setSelectedBand(Radio.Bands.FM.name); radio.setSelectedStation(92.3); System.out.println("Changed Station " + radio + " "); // assign a station to a preset on FM radio.assignToPreset(1, 101.1); radio.selectFromPreset(1); System.out.println("Assign Preset [1] to + " + radio.getSelectedBand() + " " + radio + " "); // Turn off radio radio.off(); System.out.println("Turned Off " + radio + " "); } }
**************************************************************************************************************************************
I am trying to build a printer and scanner so please can you design the code to be a printer and scanner. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
