focus on Encapsulation : This is an Object Modeling/Programming exercise – albei
ID: 3849926 • Letter: F
Question
focus on Encapsulation:
This is an Object Modeling/Programming exercise – albeit around 1 (maybe 2) Object. Using the Radio Object we reviewed in class as a point of reference, think of a common (or maybe, not so common) physical device or appliance that’s 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 public method sub-tasks.
In addition to Class members supporting (a) through (c) above, you also need to implement:
Either a public void main(String [] ) method or a second “test harness” Class whose only job will be to:
Create an instance of your modeled Class
Invoke a sequence of methods on that instance designed to show that it’s being taken
A “String toString()” method – allowing you to display the “state” of the Object Instance.
What do we mean by the state of the Instance? Normally, the collective value of all data members ‘at a moment in time’ is considered the instance’s state. That’s a very 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/application’s operation.
Note: Refer to the Radio Class’ toString() and main() methods as a guide.
See Rodio’s execution output below – showing that a single instance has been created, and taken through a series of operations (method invocations).
At key points, a “toString()” representation of the instance’s state is displayed to the console using System.out.println()or the like.
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]
output :
Explanation / Answer
// program oblist.cpp
// object and list
#include <iostream>
using namespace std;
// a class declaration part
class wall
{
int length;
int width;
wall *another_wall; // a pointer variable
public:
wall(void);
// a constructor declaration
void set(int new_length, int new_width);
int get_area(void);
void point_at_next(wall *where_to_point);
wall *get_next(void);
};
// a class implementation part
wall::wall(void)
// a constructor implementation {
length = 8;
width = 8;
another_wall = NULL;
}
// this method will set a wall size to the input parameters
void wall::set(int new_length, int new_width)
{
length = new_length;
width = new_width;
}
// this method will calculate and return the area of a wall instance
int wall::get_area(void)
{
return (length * width);
}
// this method causes the pointer to point to the input parameter
void wall::point_at_next(wall *where_to_point)
{
another_wall = where_to_point;
}
// this method returns the wall the current one points to
wall *wall::get_next(void)
{
return another_wall;
}
// main program
void main()
{
wall small, medium, large;
// objects are instantiated, of type class wall
wall *wall_pointer;
// wall *point;
// a pointer to a wall
small.set(5, 7);
large.set(15, 20);
// point = new wall;
// use the defaults value supplied by the constructor
cout<<"Using small.set(5, 7): ";
cout<<"---------------------- ";
cout<<"Area of the small wall surface is "<<small.get_area()<<" ";
cout<<"Using default/constructor value ";
cout<<"------------------------------- ";
cout<<"Area of the medium wall surface is "<<medium.get_area()<<" ";
cout<<"Using large.set(15, 20): ";
cout<<"------------------------ ";
cout<<"Area of the large wall surface is "<<large.get_area()<<" "; small.point_at_next(&medium);
medium.point_at_next(&large);
wall_pointer = &small;
wall_pointer = wall_pointer->get_next();
cout<<"The wall’s pointer pointed to has area "<<wall_pointer
->get_area()<<" ";
// system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.