Você está na página 1de 7

Exercise 1: 

Implement a Resistor Class 

#include <iostream>
#include <iomanip>

using namespace std;

class ResistorClass
{
private:
string m_cResistorName;
double m_dResValue;
double m_dTolerance;
double m_dMinResistance;
double m_dMaxResistance;
public:
void displayResistor(void);
void enterResistor(void);
void addSeries( ResistorClass Resistor1, ResistorClass Resistor2);
};

void ResistorClass::displayResistor(void)
{
cout << endl;
cout << "Values for Resistor are:" << endl;
cout << setprecision(5) << fixed << showpoint << "Resistor Nominal
Value = \t" << setw(20) << m_dResValue << endl;
cout << setprecision(5) << fixed << showpoint <<
"ohmsResistorTolerance = \t" << setw(20) << m_dTolerance * 100;
cout << "%" << endl;
cout << setprecision(5) << fixed << showpoint << "Mininimum
Resistance = \t\t" << setw(20) << m_dMinResistance;
cout << " ohms" << endl;
cout << setprecision(5) << fixed << showpoint << "Maximum
Resistance = \t\t" << setw(20) << m_dMaxResistance;
cout << " ohms" << endl;
cout << endl;
}
void ResistorClass::enterResistor(void)
{
double my_dResValue;
double my_dTolerance;

m_dResValue = 0.0;
m_dTolerance = 0.0;

while( m_dResValue == 0.0)


{
cout << "Enter the Nominal Resistance Value: ";
cin >> my_dResValue;

if ( my_dResValue > 0 && my_dResValue < 10000000)


{
m_dResValue = my_dResValue;
}
else
{
cout << "ERROR: Nominal Resistance " << endl;
cout << "Value not between 0 and 10,000,000" << endl;
cout << endl;
cout << endl;
}
}

while( m_dTolerance == 0.0)


{

cout << "Enter the Resistor Tolerance Value: ";


cin >> my_dTolerance;

if ( my_dTolerance > 0 && my_dTolerance < 1)


{
m_dTolerance = my_dTolerance;
}
else
{
cout << "ERROR: Resistance Tolerance" << endl;
cout << "Value must be a decimal number between 0 and 1" <<
endl;
cout << endl;
cout << endl;
}
}

m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);


m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);

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);

int main()
{
ResistorClass resistor1;
ResistorClass resistor2;
ResistorClass resistor3;

resistor1.enterResistor();
resistor2.enterResistor();
resistor3.enterResistor ();

resistor1.displayResistor();
resistor2.displayResistor();
resistor3.displayResistor();

resistor3.addSeries(resistor1, resistor2);

resistor1.displayResistor();
resistor2.displayResistor();
resistor3.displayResistor();

system("pause");

return 0;
}

Summary

This program created is the first stage utilizing object oriented design.
This stage is the consruction of the program before it is tested.
The program will be tested in the second stage.

Conclusion

I did not have any problems with the program. I found it was much easier
by using classes. I learned that there are much simpler ways of writing
programs. I am actually excited about learning this new material.
Exercise 2:  Test the Resistor Class 

#include <iostream>
#include <iomanip>

using namespace std;

class ResistorClass
{
private:
string m_cResistorName;
double m_dResValue;
double m_dTolerance;
double m_dMinResistance;
double m_dMaxResistance;
public:
void displayResistor(void);
void enterResistor(void);
void addSeries( ResistorClass Resistor1, ResistorClass Resistor2);
};

void ResistorClass::displayResistor(void)
{
cout << endl;
cout << "Values for Resistor are:" << endl;
cout << setprecision(5) << fixed << showpoint << "Resistor Nominal
Value = \t" << setw(20) << m_dResValue << endl;
cout << setprecision(5) << fixed << showpoint <<
"ohmsResistorTolerance = \t" << setw(20) << m_dTolerance * 100;
cout << "%" << endl;
cout << setprecision(5) << fixed << showpoint << "Mininimum
Resistance = \t\t" << setw(20) << m_dMinResistance;
cout << " ohms" << endl;
cout << setprecision(5) << fixed << showpoint << "Maximum
Resistance = \t\t" << setw(20) << m_dMaxResistance;
cout << " ohms" << endl;
cout << endl;
}
void ResistorClass::enterResistor(void)
{
double my_dResValue;
double my_dTolerance;

m_dResValue = 0.0;
m_dTolerance = 0.0;

while( m_dResValue == 0.0)


{
cout << "Enter the Nominal Resistance Value: ";
cin >> my_dResValue;

if ( my_dResValue > 0 && my_dResValue < 10000000)


{
m_dResValue = my_dResValue;
}
else
{
cout << "ERROR: Nominal Resistance " << endl;
cout << "Value not between 0 and 10,000,000" << endl;
cout << endl;
cout << endl;
}
}

while( m_dTolerance == 0.0)


{

cout << "Enter the Resistor Tolerance Value: ";


cin >> my_dTolerance;

if ( my_dTolerance > 0 && my_dTolerance < 1)


{
m_dTolerance = my_dTolerance;
}
else
{
cout << "ERROR: Resistance Tolerance" << endl;
cout << "Value must be a decimal number between 0 and 1" <<
endl;
cout << endl;
cout << endl;
}
}

m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);


m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);

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);

int main()
{
ResistorClass resistor1;
ResistorClass resistor2;
ResistorClass resistor3;

resistor1.enterResistor();
resistor2.enterResistor();
resistor3.enterResistor ();

resistor1.displayResistor();
resistor2.displayResistor();
resistor3.displayResistor();

resistor3.addSeries(resistor1, resistor2);

resistor1.displayResistor();
resistor2.displayResistor();
resistor3.displayResistor();

system("pause");

return 0;
}

Summary

This program created is the second stage utilizing object oriented design.
This stage also calculates the nominal value and tolerance value of three
different resistors. The outputs of the calculations are then
displayed showing the outcome.

Conclusion

I did not have any problems with the program. I found it was much easier
by using classes. I learned that there are much simpler ways of writing
programs. I am actually excited about learning this new material.

Você também pode gostar