Question

In: Computer Science

Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle...

Lab 7   - Rectangle class-   (Lec. 7)

1.) Create a new project and

name it:    Rectangle

/* OUTPUT:

Enter the width of the court: 60

Enter the length of the court: 120

The width of the court is 60 feet.

The length of the court is 120 feet.

The area of the court is 7200 square feet.

Press any key to continue . . . */

2.) Create the following 3 files:

  • Rectangle.h
  • Rectangle.cpp
  • Source.cpp

3.) Write a program that produces

this output:

Rectangle.h

4.)In the Rectangle.h file, write a class specification for a Rectangle class.

            2 private data members:      _width (int) and _length (int)

            7 public member functions:

      Default constructor - Assigns ‘safe’ values to the data members.

      Descructor

      setWidth – An int value, passed from main(), is assigned to _width.

      setLength - An int value, passed from main(), is assigned to _length.

      getWidth – Returns the object’s width.

      getLength – Returns the object’s length.

      getArea – Calculates the area of the Rectangle object and returns the value.

Rectangle.cpp

5.) In the Rectangle.cpp file, write a function implementation for each function

prototype listed in the Rectangle class specification.

Source.cpp

6.)In main(), declare an object of Rectangletype, and name it:    tennisCourt

7.) In main(), declare three variables of double data type, and

                and name them:   width, length and area

6.) Prompt the user to enter a width and length (see output).

7.) Read in the user’s inputs and assign them to the data members of tennisCourt,

             by using the ‘set’ member functions.

8.)Then assign the tennis court’s width, length and area to variable in main() by

             using the ‘get’ member functions.

9.)Display the width, length, and area. (see output)

Solutions

Expert Solution

//Source.cpp

#include<iostream>
#include"Rectangle.h"

int main()
{
Rectangle tennisCourt;
double width = 0, length = 0, area = 0;

std::cout<<"Enter the width of tennis court: ";
std::cin>>width;
tennisCourt.setWidth(width);
std::cout<<"\nEnter the length of tennis court: ";
std::cin>>length;
tennisCourt.setLength(length);
std::cout<<"\nThe width of Tennis court is "<<tennisCourt.getWidth()<<" feet.";
std::cout<<"\nThe length of Tennis court is "<<tennisCourt.getLenght()<<" feet.";
area = tennisCourt.getArea();
std::cout<<"\nThe Area of Tennis court is "<<area<<" square feet.";
}

//Rectangle.h

class Rectangle{
private:
int _length;
int _width;

public:

Rectangle();
void setWidth(int width);
void setLength(int length);
int getWidth();
int getLenght();
int getArea();
~Rectangle();
};

//Rectangle.cpp

#include"Rectangle.h"

Rectangle::Rectangle(){
this->_length = 0;
this->_width = 0;
}

void Rectangle::setLength(int length){
this->_length = length;
}

void Rectangle::setWidth(int width){
this->_width = width;
}

int Rectangle::getLenght(){
return this->_length;
}

int Rectangle::getWidth(){
return this->_width;
}

int Rectangle::getArea(){
return this->_length * this->_width;
}

Rectangle ::~Rectangle(){

}


Related Solutions

(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
An exceptions lab. Create a new class named EnterInfo Request user enter name and age Accept...
An exceptions lab. Create a new class named EnterInfo Request user enter name and age Accept a string and an int Use custom Exception to warn user when name is blank Use custom Exception to warn user when age is invalid 0 > age > 120 Use general Exception to warn user when age is not an int Loop through until user enters all valid data
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types described above Create and Add...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT