PROJECT 6: MIRROR CALCULATIONS
DUE by midnight 11/04/08
INTRODUCTION
This is the first of a 3-project series. The next two projects, pro7 and pro8 will expand on this assignment. Therefore, it is important that you do well in pro6.
This project is your introduction to object-oriented programming (OOP). Here, you will develop your first object class, the Mirror class. The definition of the Mirror class and a detailed explanation is given later in this text. Even though the main focus is on OOP, pro6 also involves arrays and file processing, topics that have been covered in pro5. In pro5, your program read data from an ASCII file. In pro6, it will create a data file and write to it as well.
BACKGROUND
In this assignment, you will write a program that calculates the height and the distance of the image of an object that is placed in front of a mirror. The mirror can be concave or convex.
The following figure shows a concave mirror with an object (pencil) placed in front of it. The image of the object is inverted and falls between the focal point (f) and the radius (r).
Figure 1. Image formation in a concave mirror.
The next figure shows the image formation in a convex mirror. Note that the image is not inverted and it is on the other side of the mirror.
Figure 2. Image formation in a convex mirror.
You will use the following formulas to compute the height of the image and its distance from the mirror.
The focal point of the mirror is given by
for concave mirrors:
for convex mirrors:
The image distance, image height and the magnification factor can be found from the following:
where f is the focal point of the mirror, dobject is the distance of the object from the mirror, hobject is the height of the object and m is the magnification factor.
PROGRAM DETAILS
The main menu will look like the following:
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Option 1 is for entering the information pertaining to the specific mirror used in calculations. This includes the type of the mirror (concave or convex) and its radius. There will be only one active mirror at any given time.
Option 2 loads a bunch of objects into the object database from a data file. Here, object refers to the physical entity placed in front of the mirror, not a C++ class object. From this point forward, to differentiate between the two, we will refer to them as either physical objects or C++ objects.
A physical object is described by its height and its distance from the mirror. Therefore, the data file contains two columns of numbers; the first column represents the distance, whereas the second column gives the height.
Your program will maintain a physical object database. In this project, this database will consist of two arrays:
double distance[100];
double height[100];
In the next project (pro7), you will be asked to create a new C++ object class just for the physical objects. But for now, you will use the two arrays given above. Your program will read the physical object distances and heights from the specified file and copy them to the arrays.
Option 3 computes the image height and distance for each physical object in the database. The objects are assumed to be placed in front of the current mirror. Note that the mirror properties can be changed via option 1. After each computation, the user will have the option of saving the results (image height and distance) to an output file. For the format of the file refer to the ‘Notes’ section and the sample run below.
Option 4 displays an image file previously created using option 3.
Finally, option 5 clears the physical object database.
MIRROR CLASS
You will implement a Mirror class to represent the mirrors used in the calculations. In this program, it will be sufficient to declare only one Mirror object. The properties of the Mirror object can be changed anytime using the set methods (setters) listed below.
The following shows the class definition for the Mirror class:
class Mirror {
private:
double radius;
int type; // concave/convex
public:
Mirror(void); // constructor
Mirror(double radius, int type); // another constructor
void setType(int type); // sets the type
void setRadius(double radius); // sets the radius
double getRadius(void); // returns the radius
int getType(void); // returns the type
double getFocal(void); // computes the focal point
double getImageDistance(double distance); // computes the image distance
double getImageHeight(double distance ,double height);
double getMagnification(double distance, double height); // computes the // magnification
void print(void); // prints info about the mirror
};
In the class definition above distance and height refer to the physical objects distance and height.
The Mirror object has two data fields: radius and type. Note that the type field is an integer. Use the following integer constants for the value of this field
const int CONCAVE=0;
const int CONVEX =1;
For example,
Mirror m;
m.setType(CONCAVE);
will set the type to zero (or CONCAVE). By using the words CONCAVE and CONVEX, you will not have to remember that 0 corresponds to concave and 1 to convex.
This is not a complicated object class. It mostly consists of getter and setter methods. The formulas given on page 2 will be coded in getFocal(), getImageDistance(), getImageHeight() and getMagnification() methods.
NOTES
1. Use the following precision and widths in your program:
(a) a fixed precision of 3 digits after the decimal point
(b) a width of 8 for object distance, 6 for object height, 8 for image distance, 6 for image height and 13 for magnification.
Make sure that the input file has a blank line at the end.
To read an entire line from the output file use the getline method of the ifstream class:
Ex. file.getline(buf,80);
where file is an ifstream object and buf is a char[].
- Use cin when reading in the file name.
Ex. cin >> fname;
where fname is a char[]
.
Implement the following error messages:
"ERROR: Incorrect mirror type!"
"ERROR: File does not exist or corrupted file!"
"ERROR: No mirror info stored!"
"ERROR: File cannot be created!"
"ERROR: Database is empty!"
"ERROR: Incorrect choice!"
SAMPLE RUN
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 2
ERROR: No mirror info stored!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 3
ERROR: Database is empty!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 5
ERROR: Database is empty!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 1
0 - Concave
1 - Convex
Choose Mirror Type: 3
ERROR: Incorrect mirror type!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 1
0 - Concave
1 - Convex
Choose Mirror Type: 0
Enter the Radius of the Mirror: 0.1
Mirror Info:
Type : Concave
Radius: 0.100
Focal : 0.050
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 2
Please enter the file name: dummy.txt
ERROR: File does not exist or corrupted file!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 2
Please enter the file name: pro6_1.txt
Object
Distance Height
0.500 1.500
3.200 2.300
3.000 1.000
1.500 6.000
4 objects read
4 objects in the database
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 3
Type : Concave
Radius: 0.100
Focal : 0.050
Image Mirror
Distance Height Magnification
0.056 -0.167 0.111
0.051 -0.037 0.016
0.051 -0.017 0.017
0.052 -0.207 0.034
Save Results (Y/N)?: n
Results not saved!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 5
Database cleared!
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 1
0 - Concave
1 - Convex
Choose Mirror Type: 1
Enter the Radius of the Mirror: 0.3
Mirror Info:
Type : Convex
Radius: 0.300
Focal : -0.150
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 2
Please enter the file name: pro6_1.txt
Object
Distance Height
0.500 1.500
3.200 2.300
3.000 1.000
1.500 6.000
4 objects read
4 objects in the database
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 3
Type : Convex
Radius: 0.300
Focal : -0.150
Image Mirror
Distance Height Magnification
-0.115 0.346 0.231
-0.143 0.103 0.045
-0.143 0.048 0.048
-0.136 0.545 0.091
Save Results (Y/N)?: y
Please enter the file name to save: out.txt
Results saved succesfully in: out.txt
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 4
Please enter the file name: out.txt
Type : Convex
Radius: 0.300
Focal : -0.150
Object Image Mirror
Distance Height Distance Height Magnification
0.500 1.500 -0.115 0.346 0.231
3.200 2.300 -0.143 0.103 0.045
3.000 1.000 -0.143 0.048 0.048
1.500 6.000 -0.136 0.545 0.091
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 2
Please enter the file name: pro6_2.txt
Object
Distance Height
0.500 1.500
3.200 2.300
3.000 1.000
1.500 6.000
0.500 0.465
0.360 0.630
0.988 0.170
1.008 8.111
4 objects read
8 objects in the database
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 3
Type : Convex
Radius: 0.300
Focal : -0.150
Image Mirror
Distance Height Magnification
-0.115 0.346 0.231
-0.143 0.103 0.045
-0.143 0.048 0.048
-0.136 0.545 0.091
-0.115 0.107 0.231
-0.106 0.185 0.294
-0.130 0.022 0.132
-0.131 1.051 0.130
Save Results (Y/N)?: y
Please enter the file name to save: out2.txt
Results saved succesfully in: out2.txt
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 4
Please enter the file name: out2.txt
Type : Convex
Radius: 0.300
Focal : -0.150
Object Image Mirror
Distance Height Distance Height Magnification
0.500 1.500 -0.115 0.346 0.231
3.200 2.300 -0.143 0.103 0.045
3.000 1.000 -0.143 0.048 0.048
1.500 6.000 -0.136 0.545 0.091
0.500 0.465 -0.115 0.107 0.231
0.360 0.630 -0.106 0.185 0.294
0.988 0.170 -0.130 0.022 0.132
1.008 8.111 -0.131 1.051 0.130
Mirror Program
0 - Quit
1 - Enter Mirror Info
2 - Read Object Data from File
3 - Compute Image Data and Save to File
4 - Display a Saved Image File
5 - Clear Database
Choice: 0