A brief description of the method you used to confirm


Must work in C++ Visual studio 2008:

Deliverables 
Submit to Week 3's Dropbox, a single Notepad file containing the source code for exercise 1 and 2. Your source code should use proper indentation and be error free. Be sure that your last name and the lab number is part of the file name. For example: YourLastName_Lab1.txt.

Each program should include a comment section that minimally includes your name, the lab and exercise number and description of what the program accomplishes.

Submit to Week 3's Dropbox a lab report (a Word document) containing the following information:

Your name and the exercise number 

Specification - A brief description of what the program accomplishes including its input, key processes and output. 

Test Plan - A brief description of the method you used to confirm your program worked properly. If necessary, include a clearly labeled table with test cases, predicted results and actual results. 

Summary & Conclusions:

Summary: Write a statement summarizing your predicted and actual output; identify and explain any differences. 

Conclusions: Write at least one non-trivial paragraph that explains in detail either a significant problem you had and how you solved it, or, if you had no significant problems, something you learned by doing the exercise.

This week submit only one lab report that covers both exercises. 


Grading 

Your lab grade will be based upon:

The formatting of your source code

The use of meaningful identifiers

The extent of internal documentation

The degree to which an exercises' specifications are meet and the completeness of your lab report.

L A B 
Walkthrough: Creating a Class (not graded but required) 


Objective: The code in this walk through will provide you with a program that illustrates the core concepts of creating classes, object, properties, methods and using those items in a program.

In this walkthrough, I'll build a simple class that will be used to represent a box. At each step in the process I'll discuss the design options and explain the choices I have made. 

The first step in the process of creating a class is to have a clear goal in mind. What will the class achieve? For my example, I am creating a box class. Each object of this class will store basic information about a box and perform basic calculations associated with boxes. 

To start the design process, the first choice I must make is the class name. I want to choose a descriptive name that represents both the nature of my class and ensures that I can quickly tell that the data type being used to define an item is a class and not another variable type. So, I'll choose the name "BoxClass." It is a common practice to capitalize the first letter of a class name to make it stand out. At this point in our class construction we have:

class BoxClass { 
}; 


We must now decide what attributes and behaviors we wish to encapsulate in our class. Based on our initial goals we know that we'll have to create member variables that describe our box. These variables will include height, width and length. Any other information we require for our box can be calculated based on these members. We have another choice to make. What data type should we use for our variables? I'll choose integers because this is only an example and integers are compact and easy to work with. So, the class member variables will be:

class BoxClass {
int theLength;
int theWidth;
int theHeight;
}; 

It would be a good idea to provide initial values for our variables, but that can't be done with in the class declaration. The compiler will send you nasty message if your try. We'll learn next week how to take care of variable initialization. For now, we will have to accept the fact that when our object is instantiated the member variables will contain garbage values.

It is time to consider data hiding and access specifiers. When we add member functions to our class they will be using the data contained in our member variables. I want to design my class so that any value entered for a member variable will be valid and not crash or conflict with the operations performed by the class' member functions. To ensure that I control the data going into and being retrieve from the member variables (I don't want any negative numbers) I will make the member variables private. This means that they cannot be directly accessed outside of the class definition. By default all members are private unless they are defined beneath the public access specifier. I'm planning to include public members in my class so I'll needed to have both access specifiers (private and public) in my class:

class BoxClass {
private:
int theLength; //private member variables
int theWidth;
int theHeight;
public:
}; 

At this point, I need to define a function that will allow me to initialize or change the values of the member variables. It will have to be a public function otherwise there would not be anyway of using the function outside the class definition. I'll create a function (setBoxSize) with three integer parameters and an integer return type. The function return value will be an error number. 

class BoxClass {
private:
int theLength; 
int theWidth;
int theHeight;
public:
int setBoxSize(int Length, int Width, int Height); 
}; 

It would also be nice to be able to retrieve the value of the member variables outside of the class. This can not be accomplished because the member variables are private. So, I'll create another function that will retrieve these values. I could do this in one of three ways: 1. I could create three value returning functions -- one function for each member variable, 2. I could create a single value returning function with a parameter used by the function to determine which member variable I want returned --- width, height, or length Or 3. I could create a void function that has three pass by reference parameters. The pass by reference parameters will allow me to use variables as an arguments to the function and retrieve the values from the function into those same variables. I'll choose the last option (pass by reference parameters) because it is the cleanest solution requiring the least amount of code. The function's name will be getBoxSize.

class BoxClass {
private:
int theLength; 
int theWidth;
int theHeight;
public:
int setBoxSize(int Length, int Width, int Height);
void getBoxSize(int &Length, int &Width, int &Height); //used to return values
}; 

As noted above, I could have just as easily created three separate functions to return the length, width and height members. Here is how that would look:

class BoxClass {
private:
int theLength; 
int theWidth;
int theHeight;
public:
int setBoxSize(int Length, int Width, int Height);

int getLength(void); //accessor function
int getWidth(void); //accessor function
int getHeight(void); //accessor function
}; 

If you are not perfectly comfortable with using pass-by-reference parameters then using the three accessor functions would be the way to go. Actually, you could use both techniques and allow your class user to choose whatever function suit their needs.

At this point, we have a fully fleshed out class declaration (variables and prototypes). But, the class does not accomplish a whole lot. I want to add more functionality. To do so, I'll add two public member functions. The first will calculate the boxes volume (boxVolume) and the other will calculate the boxes surface area (boxSurfaceArea).

class BoxClass {
private:
int theLength; 
int theWidth;
int theHeight;
public:
int setBoxSize(int Length, int Width, int Height);

void getBoxSize(int &Length, int &Width, int &Height); 
int getLength(void); //accessor function
int getWidth(void); //accessor function
int getHeight(void); //accessor function

int boxVolume(void);
int boxSurfaceArea(void);
}; 

I'm satisfied with my class declaration, but the class still needs a class definition. The class definition contains the implementation details for the class functions. Providing the function implementation will be your task. 

Sample Exercise: Implementing the Box Class (not graded but required) 


Objective: Utilize the core concepts of creating classes, object, properties and methods by completing the implementation of the box class functions defined in the above walkthrough. 

Steps:

Create a new Visual Studio project and source code file. 
Copy and paste the BoxClass declaration from the above walkthrough into a source code file. 
Provide the implementation of the box class member functions.
Use this UML diagram to guide your class design:

Class: BoxClass 
- int theLength
- int theHeight
- int theWidth 
+ int boxVolume(void);
+ int boxSurfaceArea(void);
+ void setBoxSize(int Len, int Width, int Height);
+ void getBoxSize(int &Length, int & Width, int &Height)
+int getLength(void); 
+int getWidth(void); 
+int getHeight(void); 


Here are the specifications for the class members:

Member Variables Specification 
int theLength; The length of the box. 
int theHeight; The height of the box. 
int theWidth; The width of the box. 



Member Functions Specification 
int boxVolume(void); A function that returns the volume of the box. volume = length * height * width 
int boxSurfaceArea(void); A function that returns the surface area of the box. surface area = 2 * ((length * width) + (length * height) + (height * width)) 
int setBoxSize(int Len, int Width, int Height); A function that sets the length, width an height of the box. All arguments must be positive integers. Validate the arguments passed to the function and return: 0 - no errors, 1 - width error, 2 - height error, 3 - length error. 
void getBoxSize(int &Length, int & Width, int &Height) A function that returns the length, width and height via reference parameters 
int getLength(void) A function that returns the length member 
int getWidth(void); A function that returns the width member 
int getHeight(void); A function that returns the height member 

Utilize the core concepts of creating classes, properties and methods by creating the class declaration and function implementation for the Box Class. Create a Box Class object and tests each member function to ensure that they work properly.

To check your work review the Implementation Solution.

Exercise 1: Implement a Resistor Class (20 points) 


Objective: 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 , 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);

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: A brief description of the method you used to confirm
Reference No:- TGS0134508

Expected delivery within 24 Hours