Requirement: create one project for each problem; add comments to your code. Sub
ID: 3549208 • Letter: R
Question
Requirement: create one project for each problem; add comments to your code.
Submission: after finishing the all the problems, paste the code and snapshot the output in
===================================================
Problem 1:
class Rectangle
{
private:
double width;
double height;
public:
double getWidth() const;
double getHeight() const;
void setValues(double w, double h); // sets w to width and h to
height
double Area() const; // returns the area of this rectangle
};
i. Write the definition for all member functions, as they are written outside/below
the class interface.
ii. Write main (in application) which does the following (in sequence).
o Create a Rectangle object r1 and set width as 1.0 and height as 2.0.
o Create another Rectangle object r2 and set width = 0.8 and height = 1.5.
o Compare the area of r1 and r2, and print to the terminal a message either
"r1 is bigger" or "r2 is bigger".
Problem 2:
1) Define a class Time, the class view is shown as follows.
Data member: hour, minute
Function members:
getHours( ) // return the current hour
getMinutes( ) // return the current minutes
setHours( ) // set the current hour
setMinutes( ) // set the current minutes
printTime() // print the current time, example: 14:56
2) Implement all the function members.
===================================================
problem 3:
void advance(); -- This method advance the time by 1 minute. Be sure not to
make the 'minute' member variable overflow (>= 60). You can ignore the
overflow of the hour.
Explanation / Answer
class Rectangle
{
private:
double width;
double height;
public:
double getWidth() const;
double getHeight() const;
void setValues(double w, double h); // sets w to width and h to height
double Area() const; // returns the area of this rectangle
};
i. Write the definition for all member functions, as they are written outside/below the class interface.
double Rectangle::getWidth() const
{
return width;
}
double Rectangle::getHeight() const
{
return height;
}
void Rectangle::setValues(double w, double h)
{
width = w;
height = h;
}
double Rectangle::Area() const
{
return width*height;
}
ii. Write main (in application) which does the following (in sequence).
o Create a Rectangle object r1 and set width as 1.0 and height as 2.0.
o Create another Rectangle object r2 and set width = 0.8 and height = 1.5.
o Compare the area of r1 and r2, and print to the terminal a message either "r1 is bigger" or "r2 is bigger".
int main()
{
Rectangle r1;
r1.setValues(1.0,2.0);
Rectangle r2;
r2.setValues(0.8,1.5);
if(r1.Area() > r2.Area())
cout << "r1 is bigger" << endl;
else
cout << "r2 is bigger" << endl;
return 0;
}
1) Define a class Time, the class view is shown as follows. Data member: hour, minute
class Time
{
private:
int hour;
int minute;
public:
int getHours( ) // return the current hour
{
return hour;
}
int getMinutes( ) // return the current minutes
{
return minute;
}
void setHours(int h) // set the current hour
{
hour = h;
}
void setMinutes(int m ) // set the current minutes
{
minute = m;
}
void printTime() // print the current time, example: 14:56
{
cout << hour << " : " << minute << endl;
}
}
problem 3:
void advance(); -- This method advance the time by 1 minute. Be sure not to
make the 'minute' member variable overflow (>= 60). You can ignore the overflow of the hour.
void Time::advance()
{
minute = minute + 1;
if(minute>=60)
{
hour = hour+1;
minute = minute/60;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.