Review Exercises

advertisement
Fondamenti di C++ - Cay Horstmann
Review Exercises
R3.1
An object is a value that can be created, stored and manipulated. A class is a programmer
defined data type. An object can be thought of as an instance of, or a distinct occurrence
of, a particular class definition. The class definition determines what can be done with
and to the object.
R3.3
A member function is a function of a class that is applied to an instance of that class, i.e.
an object, using the dot notation. For example, the statement
wakeup_time.add_seconds(10000); is an application of the member function
add_seconds to the Time object called wakeup_time. Member functions are not allowed
to be used by themselves, they must always be applied to an object. A nonmember
function is not a member of any class, hence it has no implicit parameter. Nonmember
functions are not applied to objects.
R3.5
A Circle object is constructed using a center object of class Point and
double. There is also a default constructor that makes a (rather useless)
a radius of type
circle with center
(0,0) and radius 0.
R3.7
Lunch time (there was an error in the first printing-see errata)
The current time
The top right corner of the graphics window in the
default coordinate system
Your instructor as an employee (make a guess for the
salary)
A circle filling the entire graphics window in the
default coordinate system
A line representing the x-axis fom -10 to 10.
Time(12,0,0);
Time();
Point(10,10);
Employee("Clueless, I.M.",
57000);
Circle(Point(0,0), 10.0);
Line(Point(10,0),Point(10,0));
R3.9
Time now();
Point p = (3,4);
p.set_x(-1);
Can't use () for default constructed variable
No constructor called, cannot assign (3,4) to left hand
side.
No set_x(int) member function in Point class
Copyright © 2003 - The McGraw-Hill Companies srl
1
Fondamenti di C++ - Cay Horstmann
There is no defined method for Time objects to be
output in this way.
There are only hour, minutes, and seconds parameters
Time due_date(2004, 4, 15); for the Time class' constructor. In particular the value
for hours should be between 0 and 23
due_date.move(2,12);
move(int, int) method not defined in Time class.
The member method seconds_from(Time) is being
seconds_from(millenium);
called without a implicit parameter.
The Employee constructor is being called with two
strings and a number. It will only accept one string,
Employee harry("Hacker",
"Harry", 35000);
use Employee harry("Hacker, Harry", 35000);
instead
harry.set_name("Hacker,
set_name(string) is not a member function of class
Harriet");
Employee
cout << Time
R3.11
Assume that the instructions are executed sequentially
Time t;
t = Time(20,0,0)
t.add_seconds(1000);
t.add_seconds(-400);
current time
8:00:00 pm
2 hours, 46 minutes and 40 seconds later, that is 10:46:40 pm
33 minutes and 20 seconds earlier, that is 10:13:20 pm
R3.13
Point, Line, Circle, Message and GraphicsWindow
R3.15
Plot grades from 0.0 to 4.0 along the x-axis, the number of students along the yaxis:cwin.coord(0, nstudent, 4, 0);
R3.17
Point p1 = Point(-2, 2);
Point p2 = Point(0, -2);
Point p3 = Point(0, 2);
Point p4 = Point(-2, -2);
Line l1 = Line(p1, p2);
Line l2 = Line(p3, p4);
Point p5 = Point(0, 2);
Point p6 = Point(2, 2);
Point p7 = Point(1, 2);
Copyright © 2003 - The McGraw-Hill Companies srl
2
Fondamenti di C++ - Cay Horstmann
Point p8 = Point(1, -2);
Line l3 = Line(p5, p6);
Line l4 = Line(p7, p8);
cwin << l1;
cwin << l2;
cwin << l3;
cwin << l4;
R3.19
The x coordinates of the intersection points are too far apart. The points are not on both
the circle and the line, just on the line, outside the circle.
Programming Exercises
P3.1
#include <iostream>
using namespace std;
#include "ccc_time.h"
int main()
{
const int SECONDS_PER_MINUTE = 60;
cout << "Enter the time your next assignment is due (hour min): ";
int hour;
int min;
cin >> hour >> min;
Time due_time(hour, min, 0);
Time now;
int diff = due_time.seconds_from(now) / SECONDS_PER_MINUTE;
cout << "The assignment is due in " << diff << " minutes." << "\n";
return 0;
}
P3.3
#include <cmath>
using namespace std;
#include "ccc_win.h"
int ccc_win_main()
{
/* prompt for the center of the circle */
Copyright © 2003 - The McGraw-Hill Companies srl
3
Fondamenti di C++ - Cay Horstmann
Point center = cwin.get_mouse( "Please click on a circle center
point" );
cwin << center; /* display the center point */
Point boundary = cwin.get_mouse( "Please click on a circle boundary
point" );
cwin << boundary; /* display the boundary point */
double center_x = center.get_x(); /* extract the x,y coordinates of
the center point */
double center_y = center.get_y();
double boundary_x = boundary.get_x(); /* extract the x,y coordinates
of the boundary point */
double boundary_y = boundary.get_y();
/* calculate the circle radius using the distance formula */
double radius = sqrt(pow(center_x - boundary_x, 2) +
pow(center_y - boundary_y, 2));
Circle c(center, radius); /* define the new circle */
cwin << c; /* display the circle */
return 0;
}
P3.5
#include <cmath>
using namespace std;
#include "ccc_win.h"
int ccc_win_main()
{
Point p1 = cwin.get_mouse( "Please click on the first point" );
cwin << p1; /* display the first point */
Point p2 = cwin.get_mouse( "Please click on the second point" );
cwin << p2; /* display the second point */
Line line(p1, p2);
/* define the Line object */
cwin << line;
/* calculate the line length using the Pythagorean formula */
double length = sqrt(pow(p1.get_x() - p2.get_x(),2) +
pow(p1.get_y() - p2.get_y(),2));
/* line midpoint is the average of the x and y coordinates defining
the
line endpoints
*/
Copyright © 2003 - The McGraw-Hill Companies srl
4
Fondamenti di C++ - Cay Horstmann
Point midpoint((p1.get_x() + p2.get_x())/2, (p1.get_y() +
p2.get_y())/2);
cwin << Message(midpoint, length);
return 0;
}
P3.7
#include <iostream>
using namespace std;
#include "ccc_empl.h"
int main()
{
string first_name;
cout << "Enter the first name: ";
cin >> first_name;
string last_name;
cout << "Enter the last name: ";
cin >> last_name;
double salary = 0;
cout << "Enter the salary: ";
cin >> salary;
/* construct the Employee object and name the object the_employee
*/
Employee the_employee( last_name + ", " + first_name, salary );
/* give the employee a 5% salary increment */
the_employee.set_salary( the_employee.get_salary() * 1.05 );
/* output the new information using Employee member functions
*/
cout << "The employee " << the_employee.get_name() <<
$"
<< the_employee.get_salary() << "\n";
return 0;
}
P3.9
#include "ccc_win.h"
int ccc_win_main()
{
Circle face= Circle(Point(0,0), 6);
Copyright © 2003 - The McGraw-Hill Companies srl
" now earns
5
Fondamenti di C++ - Cay Horstmann
Circle eye1= Circle(Point(2,2), 0.5);
Circle eye2= Circle(Point(-2,2), 0.5);
Line mouth = Line(Point(-2.5, -2), Point(2.5,-2));
cwin << face;
cwin << eye1;
cwin << eye2;
cwin << mouth;
return 0;
}
P3.11
#include "ccc_win.h"
int ccc_win_main()
{
Point a = cwin.get_mouse("Click on a point to be an endpoint of the
first line segment.");
cwin << a;
Point b = cwin.get_mouse("Click on the other endpoint of the first
line segment.");
cwin << b;
Line l1 = Line(a, b);
cwin << l1;
Point c = cwin.get_mouse("Click on a point to be an endpoint of the
second line segment.");
cwin << c;
Point d = cwin.get_mouse("Click on the other endpoint of the second
line segment.");
cwin << d;
Line l2 = Line(c, d);
cwin << l2;
/* Solve the system of equations for t */
double
- (
/
( (
+ (
t = ((-( d.get_y() - b.get_y() ) / -( d.get_y() - c.get_y() )
d.get_x() - b.get_x() ) / ( d.get_x() - c.get_x() ))
a.get_x() - b.get_x() ) / -( d.get_x() - c.get_x() )
a.get_y() - b.get_y() ) / ( d.get_y() - c.get_y() )));
/* Compute and display the point of intersection (if any) */
Point intersection = Point( t * a.get_x() + (1-t) * b.get_x(),
t * a.get_y() + (1-t) * b.get_y() );
cwin << intersection;
return 0;
}
P3.13
#include "ccc_win.h"
Copyright © 2003 - The McGraw-Hill Companies srl
6
Fondamenti di C++ - Cay Horstmann
int ccc_win_main()
{
double x = 0; /* the coordinates of the first ring center */
double y = 0;
const double DISTANCE_BETWEEN_RING_CENTERS = 4; /* arbitrary choice
here
but must be greater than
the
ring radius */
const double RING_RADIUS = 2.5;
Circle loop(Point(x, y), RING_RADIUS);
cwin << loop; /* first ring (top middle) */
loop.move(x - DISTANCE_BETWEEN_RING_CENTERS, 0); /* top left ring */
cwin << loop;
loop.move(x + 2 * DISTANCE_BETWEEN_RING_CENTERS, 0); /* top right
ring */
cwin << loop;
/* The placement of the ring centers form the endpoints of
equilateral
triangles. The distance between the apex of an equilateral
triangle and the base is given as sqrt(3) * base / 2
*/
loop.move(x - (DISTANCE_BETWEEN_RING_CENTERS / 2.0),
y - DISTANCE_BETWEEN_RING_CENTERS * sqrt(3.0) / 2.0);
/* bottom right ring */
cwin << loop;
loop.move(x - DISTANCE_BETWEEN_RING_CENTERS, 0);
bottom left ring */
cwin << loop;
return 0;
}
P3.15
#include "ccc_win.h"
int ccc_win_main()
{
const double PI = 3.141592653589793;
double
double
double
double
data1
data2
data3
data4
=
=
=
=
cwin.get_double("Enter
cwin.get_double("Enter
cwin.get_double("Enter
cwin.get_double("Enter
the
the
the
the
first value: ");
second value: ");
third value: ");
fourth value: ");
double total = data1 + data2 + data3 + data4;
double theta_1 = 2 * PI * data1 / total;
Copyright © 2003 - The McGraw-Hill Companies srl
/*
7
Fondamenti di C++ - Cay Horstmann
/* express the angles as
proportion of the 2 Pi
radians within a circle
*/
double theta_2 = 2 * PI * data2 / total;
double theta_3 = 2 * PI * data3 / total;
const double RADIUS = 4;
const Point ORIGIN = Point(0,0);
cwin << Circle(ORIGIN, RADIUS); /* draw pie outline */
double x = RADIUS * cos(theta_1); /* use polar representation */
double y = RADIUS * sin(theta_1);
cwin << Line(ORIGIN, Point(x,y)); /* first pie wedge */
x = RADIUS * cos(theta_1 + theta_2);
y = RADIUS * sin(theta_1 + theta_2);
cwin << Line(ORIGIN, Point(x,y)); /* second pie wedge */
x = RADIUS * cos(theta_1 + theta_2 + theta_3);
y = RADIUS * sin(theta_1 + theta_2 + theta_3);
cwin << Line(ORIGIN, Point(x,y)); /* third pie wedge */
x = RADIUS * cos(0);
y = RADIUS * sin(0);
cwin << Line(ORIGIN, Point(x,y)); /* fourth pie wedge */
return 0;
}
P3.17
#include <iostream>
using namespace std;
#include <string>
#include "ccc_time.h"
int main()
{
Time t1 = Time();
cout << "Type the following sentence as quickly as you can:\n";
cout << "The quick brown fox jumps over the lazy dog.\n";
string s;
getline(cin, s);
Time t2 = Time();
double et = t2.seconds_from(t1);
cout << "That sentence took you " << et << " seconds to type.";
Copyright © 2003 - The McGraw-Hill Companies srl
8
Fondamenti di C++ - Cay Horstmann
return 0;
}
P3.19
#include "ccc_win.h"
int ccc_win_main()
{
Line leftRoof = Line(Point(-4,0), Point(0, 3));
cwin << leftRoof;
Line rightRoof = Line(Point(4,0), Point(0, 3));
cwin << rightRoof;
Line ceiling = Line(Point(-4,0), Point(4,0));
cwin << ceiling;
Line floor = Line(Point(-4,-5), Point(4,-5));
cwin << floor;
Line
cwin
Line
cwin
leftWall = Line(Point(-4,-5), Point(-4,0));
<< leftWall;
rightWall = Line(Point(4,-5), Point(4,0));
<< rightWall;
Line
cwin
Line
cwin
Line
cwin
leftDoor = Line(Point(-3,-5), Point(-3,-1));
<< leftDoor;
rightDoor = Line(Point(-1,-5), Point(-1,-1));
<< rightDoor;
topDoor = Line(Point(-3,-1), Point(-1,-1));
<< topDoor;
Line
cwin
Line
cwin
Line
cwin
Line
cwin
leftWindow = Line(Point(1,-3), Point(1,-1));
<< leftWindow;
rightWindow = Line(Point(3,-3), Point(3,-1));
<< rightWindow;
bottomWindow = Line(Point(1,-3), Point(3,-3));
<< bottomWindow;
topWindow = Line(Point(1,-1), Point(3,-1));
<< topWindow;
return 0;
}
Copyright © 2003 - The McGraw-Hill Companies srl
9
Download