jective: Create a C++ console application that utilizes the core concepts of des
ID: 3624386 • Letter: J
Question
jective: Create a C++ console application that utilizes the core concepts of designing and creating classes, objects, properties and methods.
Overview: Create and test a class that models a resistor.
Resistor Class UML Diagram:
Class: ResistorClass
+ string m_cResistorName;
- double m_dResValue;
- double m_dTolerance;
- double m_dMinResistance;
- double m_dMaxResistance;
+ void DisplayResistor(void )
+ void EnterResistance (void)
+ void AddSeries (ResistorClass Resistor1, ResistorClass Resistor2)
Resistor Class Member Specifications:
Member Variables
Specification
string m_cResistorName;
Stores the resistors name
double m_dResValue;
Stores the resistors nominal value
double m_dTolerance;
Stores the resistor's ohm tolerance as a decimal value
double m_dMinResistance;
Stores the resistor's minimum resistance in ohms
double m_dMaxResistance;
Stores the resistor's maximum resistance in ohms
Member Functions
Specification
void DisplayResistor(void)
A function that displays the resistor's data members in the following format:
Values for ResistorOne are:
Resistor Nominal Value = 1000.0
ohmsResistorTolerance = 10.0 %
Mininimum Resistance = 900.0 ohms
Maximum Resistance = 1100.0 ohms
NOTE: All decimal points must be aligned and upper and lowercase letters shown. Also, note that the resistor's tolerance is stored as a decimal (0.10) it is displayed as a percentage (10%).
(See chapter 3 in our text book for details on the output formatting functions: setw(), left, right, setpercision, fixed, showpoint...)
void EnterResistance (void)
This function prompts the user to enter new values for both m_dResValue and m_dTolerance and then uses the display resistor function to display the current values of the resistor object.
The resistor value must be in the following range:
m_dResValue > 0 && m_dResValue < 10,000,000
The value entered for tolerance must be a decimal number and not a whole number.
The program should force the user to continue data entry until acceptable values are entered (use a loop).
After valid data has been entered for the nominal and tolerance members the function should calculate new values for m_dMinResistance and m_dMaxResistance. The formula's these calculations are as follows:
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
After all data entry and calculations are completed the DisplayResistor function will be called.
NOTE: Even though tolerance will be stored as a decimal the value it should be displayed in percentage format. For example, a value of 10% tolerance would be entered as .10<ENTER> , would be displayed as 10.0% and would be stored in the member variable m_dTolerance as 0.10.
void AddSeries (ResistorClass Resistor1, ResistorClass Resistor2)
This function adds the values of two Resistor Class objects storing the result in a third Resistor Class object.
The objects to be summed are passed as arguments to the AddSeries function. The results are saved to the calling object's data members.
The sum of the two resistor objects' nominal values will be stored in m_dResValue.
The new tolerance value will be the larger of the two resistor objects' m_dTolerance members (you'll need an if-statement).
New values for m_dMinResistance and m_dMaxResistance will be calculated using the following formulas:
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
Exercise 2: Test the Resistor Class (20 points)
Objective: Create a program that tests each member of the Resistor Class to ensure that they work properly.
Complete the following Tasks:
1. Create a main function and instantiate three Resistor Class objects: Resistor1, Resistor2 and Resistor3.
2. The program will then call the EnterResistance function for Resistor1, Resistor2, Resistor3.
3. The program should then call Resistor3's AddSeries function. The function call will look like this: Resistor3.AddSeries(Resistor1, Resistor2);
4. The program will now display the current values for Resistor1, Resistor2 and Resistor3 using the DisplayResistor function.
5. End program
jective: Create a C++ console application that utilizes the core concepts of designing and creating classes, objects, properties and methods.
Overview: Create and test a class that models a resistor.
Resistor Class UML Diagram:
Class: ResistorClass
+ string m_cResistorName;
- double m_dResValue;
- double m_dTolerance;
- double m_dMinResistance;
- double m_dMaxResistance;
+ void DisplayResistor(void )
+ void EnterResistance (void)
+ void AddSeries (ResistorClass Resistor1, ResistorClass Resistor2)
Resistor Class Member Specifications:
Member Variables
Specification
string m_cResistorName;
Stores the resistors name
double m_dResValue;
Stores the resistors nominal value
double m_dTolerance;
Stores the resistor's ohm tolerance as a decimal value
double m_dMinResistance;
Stores the resistor's minimum resistance in ohms
double m_dMaxResistance;
Stores the resistor's maximum resistance in ohms
Member Functions
Specification
void DisplayResistor(void)
A function that displays the resistor's data members in the following format:
Values for ResistorOne are:
Resistor Nominal Value = 1000.0
ohmsResistorTolerance = 10.0 %
Mininimum Resistance = 900.0 ohms
Maximum Resistance = 1100.0 ohms
NOTE: All decimal points must be aligned and upper and lowercase letters shown. Also, note that the resistor's tolerance is stored as a decimal (0.10) it is displayed as a percentage (10%).
(See chapter 3 in our text book for details on the output formatting functions: setw(), left, right, setpercision, fixed, showpoint...)
void EnterResistance (void)
This function prompts the user to enter new values for both m_dResValue and m_dTolerance and then uses the display resistor function to display the current values of the resistor object.
The resistor value must be in the following range:
m_dResValue > 0 && m_dResValue < 10,000,000
The value entered for tolerance must be a decimal number and not a whole number.
The program should force the user to continue data entry until acceptable values are entered (use a loop).
After valid data has been entered for the nominal and tolerance members the function should calculate new values for m_dMinResistance and m_dMaxResistance. The formula's these calculations are as follows:
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
After all data entry and calculations are completed the DisplayResistor function will be called.
NOTE: Even though tolerance will be stored as a decimal the value it should be displayed in percentage format. For example, a value of 10% tolerance would be entered as .10<ENTER> , would be displayed as 10.0% and would be stored in the member variable m_dTolerance as 0.10.
void AddSeries (ResistorClass Resistor1, ResistorClass Resistor2)
This function adds the values of two Resistor Class objects storing the result in a third Resistor Class object.
The objects to be summed are passed as arguments to the AddSeries function. The results are saved to the calling object's data members.
The sum of the two resistor objects' nominal values will be stored in m_dResValue.
The new tolerance value will be the larger of the two resistor objects' m_dTolerance members (you'll need an if-statement).
New values for m_dMinResistance and m_dMaxResistance will be calculated using the following formulas:
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
Exercise 2: Test the Resistor Class (20 points)
Objective: Create a program that tests each member of the Resistor Class to ensure that they work properly.
Complete the following Tasks:
1. Create a main function and instantiate three Resistor Class objects: Resistor1, Resistor2 and Resistor3.
2. The program will then call the EnterResistance function for Resistor1, Resistor2, Resistor3.
3. The program should then call Resistor3's AddSeries function. The function call will look like this: Resistor3.AddSeries(Resistor1, Resistor2);
4. The program will now display the current values for Resistor1, Resistor2 and Resistor3 using the DisplayResistor function.
5. End program
Explanation / Answer
Dear user,
Here is the code below:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Class Declaration
class ResistorClass {
public:
string m_cResistorName; //resistor name
void DisplayResistor (void);
void EnterResistor();
void AddSeries (ResistorClass Resistor1, ResistorClass Resistor2);
private:
double m_dResValue; //Stores the resistor nominal value
double m_dTolerance; // Stores the resistor ohm tolerance as a decimal value
double m_dMinResistance; // Stores the resistor mininum resistance in ohms
double m_dMaxResistance; // Stores the resistor maximum resistance in ohms
};
int main (void)
{
//creating three objects of resistor class
ResistorClass resistor1;
ResistorClass resistor2;
ResistorClass resistor3;
resistor1.m_cResistorName = "Resistor One";
resistor1.EnterResistor();
resistor2.m_cResistorName = "Resistor two";
resistor2.EnterResistor();
resistor3.m_cResistorName = "Resistor three";
resistor3.EnterResistor();
resistor3.AddSeries(resistor1,resistor2);
//displaying individual resistor values
resistor3.DisplayResistor();
//pause system for a while
system("pause");
return 0;
}
//Member functions
void ResistorClass::EnterResistor()
{
bool Error = false;
//Enter value for resistance
cout << "Enter the Nominal Resistor Value: ";
cin >> m_dResValue;
//Enter value for tolerance
cout << "Enter the Resistor Tolerance Value: ";
cin >> m_dTolerance;
{
while (!Error)
{
//condition to validate m_dResValue
if (m_dResValue <= 0 || m_dResValue > 10000000)
{
cout << " Resistance Value Error: " << endl;
cout << "Enter the Nominal Resistor Value: ";
cin >> m_dResValue;
Error = false;
}
else Error = true;
//condition to validate m_dTolerence
if (m_dTolerance > 1)
{
cout << " Error: Tolerance Value " << endl;
Error = false;
cout << "Entry must be a decimal value <= 1 " << endl;
cout << "Enter the Resistor Tolerance Value: ";
cin >> m_dTolerance;
}
else Error = true;
}
//
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
}
}
void ResistorClass::DisplayResistor ()
{
//Displays all Resistor object data members
//sets the output parameters
cout << setprecision(2);
cout << fixed << showpoint;
//displays the output
cout << " ";
cout << setprecision(5) << fixed << showpoint << setfill(' ');
cout << "Values for " << m_cResistorName << " are: ";
cout << left << setw(25) << "Resistor Nominal Value = "
<< right << setw(10) << m_dResValue << " ";
cout << left << setw(25) << "ohms Resistor Tolerance = "
<< right << setw(10) << m_dTolerance << " ";
cout << left << setw(25) << "Minimum Resistance = "
<< right << setw(10) << m_dMinResistance << " ";
cout << left << setw(25) << "Maximum Resistance = "
<< right << setw(10) << m_dMaxResistance << " ";
}
void ResistorClass::AddSeries(ResistorClass Resistor1, ResistorClass Resistor2)
{
m_dResValue=Resistor1.m_dResValue+Resistor2.m_dResValue;
if(Resistor1.m_dTolerance>Resistor2.m_dTolerance)
m_dTolerance=Resistor1.m_dTolerance;
else
m_dTolerance=Resistor2.m_dTolerance;
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.