1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Fofino [41]
4 years ago
5

Write a program that will manipulate Rectangle objects for which you will create a Rectangle class.

Computers and Technology
1 answer:
vodomira [7]4 years ago
8 0

Answer:

REC.H ( save this file as "REC.H" )

#define REC_H

int min(int a, int b){

if(a<b) return a;

return b;

}

int max(int a, int b){

if(a>b) return a;

return b;

}

class Rectangle{

std::string name;

int x1,y1, x2, y2;

public:

//to overload cout and cin operator for rectangle object

friend std::ostream & operator << (std::ostream &out, const Rectangle &c);

friend std::istream & operator >> (std::istream &in, Rectangle &c);

int perimeter(){

int length = x2-x1;

int breadth = y1-y2;

return 2*(length+breadth);

}

int area(){

int length = x2-x1;

int breadth = y1-y2;

return length * breadth;

}

// operator overloading thorugh member function

Rectangle operator + (Rectangle const &two) {

Rectangle res;

res.name = "addRect";

res.x1 = min(this->x1, two.x1);

res.y1 = max(this->y1, two.y1);

res.x2 = max(this->x2, two.x2);

res.y2 = min(this->y2, two.y2);

return res;

}

// operator overloading thorugh friend function

friend Rectangle operator - (Rectangle const &one, Rectangle const &two);

};

std::ostream & operator << (std::ostream &out, const Rectangle &c)

{

out << c.name<<" "<< c.x1 << " " << c.y1 <<" "<< c.x2 <<" "<< c.y2 << std::endl ;

return out;

}

std::istream & operator >> (std::istream &in, Rectangle &c)

{

in >> c.name;

in >> c.x1;

in >> c.y1;

in >> c.x2;

in >> c.y2;

return in;

}

Rectangle operator-(Rectangle const &one, Rectangle const &two){

Rectangle res;

res.name = "subRect";

if(one.x1 > two.x2 || one.x2 < two.x1 || one.y1 < two.y2 || one.y2 > two.y1){

res.x1 = res.y1 = res.x2 = res.y2 = 0;

return res;

}

else{

if(one.x1 < two.x1){

res.x1 = two.x1;

res.y1 = min(two.y1, one.y1);

res.x2 = one.x2;

res.y2 = max(two.y2, one.y2);

}

else{

res.x1 = one.x1;

res.y1 = min(two.y1, one.y1);

res.x2 = two.x2;

res.y2 = max(two.y2, one.y2);

}

return res;

}

}

//header file code ends

// below is .cpp code

#include<iostream>

#include<string>

#include "REC.h" //including our REC.h file for accessing the declared class

int main(){

char ch='y';

while(ch=='y' || ch=='Y'){

Rectangle one, two;

std::cout<<"Enter Rectangle 1 (name, x1, y2, x2, y2): ";

std::cin>>one;

std::cout<<"Enter Rectangle 2 : ";

std::cin>>two;

std::cout<<"\nRectangle 1 : "<<std::endl;

std::cout<<one;

std::cout<<"Perimeter = "<< one.perimeter()<<"\tArea = "<< one.area()<<"\n\n";

std::cout<<"Rectangle 2 : "<<std::endl;

std::cout<<two;

std::cout<<"Perimeter = "<< two.perimeter()<<"\tArea = "<< two.area()<<"\n\n";

Rectangle addAns = one+two;

Rectangle subAns = one - two;

std::cout<<"addAns : "<<std::endl;

std::cout<<addAns;

std::cout<<"Perimeter = "<< addAns.perimeter()<<"\tArea = "<< addAns.area()<<"\n\n";

std::cout<<"subAns : "<<std::endl;

std::cout<<subAns;

std::cout<<"Perimeter = "<< subAns.perimeter()<<"\tArea = "<< subAns.area()<<"\n";

std::cout<<std::endl;

std::cout<<"Want to run program again ? Enter 'y' or 'Y' else any key to exit : ";

std::cin>>ch;

}

}

Explanation:

(1) To see how cpp code ends and how to indent code, please have a look on the attached file.

(2) Intuiton of formulae is also explained in deatiled way in the attached files.

You might be interested in
1. The advancement of media and information bring society countless opportunities such<br>as...​
rjkz [21]

Answer:

The advancement of media and information bring society countless opportunities such as quick access to information that helps people during their daily lives and problems.

Thus, for example, nowadays the use of the internet and social networks allows individuals very fast access to different methods of solving everyday problems: a clear example is the tutorial videos where it is explained how to carry out a certain activity or arrangement of a break on a specific asset, providing users with a quick solution to their daily problems.

5 0
3 years ago
PLZ HELP NEEDS TO BE ANSWERED ASAP THANK YOU
Stells [14]

In python:

lst = ([])

largest = 0

while len(lst) != 6:

   user_number = int(input("Enter a number: "))

   lst.append(user_number)

   for i in lst:

       if i > largest:

           largest = i

   print(largest)

I hope this helps

7 0
3 years ago
Define a function UpdateTimeWindow() with parameters timeStart, timeEnd, and offsetAmount. Each parameter is of type int. The fu
NARA [144]

Answer:

Here is a UpdateTimeWindow() method with parameters timeStart, timeEnd, and offsetAmount

// the timeEnd and timeStart variables are passed by pointer

void UpdateTimeWindow(int* timeStart, int* timeEnd, int offsetAmount){

// this can also be written as  *timeStart = *timeStart + offsetAmount;

*timeStart += offsetAmount;  //adds value of offsetAmount to that of //timeStart

// this can also be written as  *timeEnd = *timeEnd + offsetAmount;

  *timeEnd += offsetAmount;  } //adds value of offsetAmount to that of //timeEnd

Explanation:

The function has three int parameters timeStart, timeEnd, and offsetAmount.

First two parameters timeStart and End are passed by pointer. You can see the asterisk sign with them. Then in the body of the function there are two statements *timeStart += offsetAmount; and *End+= offsetAmount; in these statements the offsetAmount is added to the each of the two parameters timeStart and timeEnd.

8 0
3 years ago
Implement a 3-bit counter that counts in two different orders. Your circuit should have a Button named CLK, a Button named RESET
ss7ja [257]

Answer:

gfhfidusbifhgseliuggbdsdbdf

Explanation:

5 0
3 years ago
write a function issorted() that accepts an array of real numbers arr and an integer n as arguments, where n is the size of arr.
lesantik [10]

Answer:

Here is code in java.

import java.util.*;

class Main

{

//main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

       int m;

        // scanner object to read input from user

       Scanner ss=new Scanner(System.in);

       System.out.println(" size  of the array:");

       //read the size of the array

        m=ss.nextInt();

        // create array of the given size

        float arr[]=new float[m];

        /* call function to check whether input are in the ascending sorted order or not */

        Boolean res= issorted(arr,m);

       

      // print the result

       System.out.println("array in the ascending sorted order  : "+res);

     

       

   }catch(Exception ex){

       return;}

}

 // function to check whether input are in the ascending sorted order or not

public static boolean issorted(float[] arr,int n)

{

    int i,j;

    float d;

    Boolean flag=true;

    Scanner ss=new Scanner(System.in);

    System.out.println("enter the elements of the array:");

     // read elements of the array

    for(i=0;i<n;i++)

    {

        arr[i]=ss.nextFloat();

    }

   

    // check elements re in the ascending order or not

    for(j=0;j<n-1;j++)

    {

        if(arr[j]>arr[j+1])

        {

            flag=false;

            break;

        }

    }

     // return the result

    return flag;

}

}

Explanation:

With the help of scanner class object, read the size "m" of array.Create an array of  the given size. Pass array and integer into the function issorted(). Read the elements of the array and then check elements are in the ascending order or not.It will return "true" if elements are in the sorted order else it will return "false".

Output:

enter  size  of the array:                                                                                                                                    

5                                                                                                                                                              

enter the elements of the array:                                                                                                                              

1 2 3 4 7                                                                                                                                                      

array in the ascending sorted order  : true                                                                                                                              

enter  size  of the array:                                                  

5                                                                            

enter the elements of the array:                                              

2 4 9 3 5                                                                  

array in the ascending sorted order  : false

7 0
4 years ago
Other questions:
  • A. When executing System.out.println(a1), the toString() method in the Object class is invoked.
    14·1 answer
  • How many spaces is equal to one tab on a keyboard? When I compose emails, the tab button doesn't indent for me, so how many time
    7·1 answer
  • Select the correct answer.
    8·1 answer
  • Ou have an application running on Oracle Cloud Infrastructure. You identified that the read and write operations are slowing you
    10·1 answer
  • Where can you make changes to AutoCorrect?
    14·2 answers
  • If there are three classes, Shape, Circle and Square, what is the most likely relationship among them?
    15·1 answer
  • George enters the types of gases and the amount of gases emitted in two columns of an Excel sheet. Based on this data he creates
    6·1 answer
  • What is the purpose of requirements gathering and analysis?
    8·1 answer
  • Question 1 (1 point)
    13·1 answer
  • Please help me with this question<br>it on the picture​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!