Question

In: Computer Science

Use C++ for this program    Define a Line Class.    Constraint: Line must be in...

Use C++ for this program

   Define a Line Class.
   Constraint: Line must be in First Quadrant
               for X-Y Coordinate System.

   Create the following methods:
   1) Parameterized Constructor
   2) Horizontal Line Constructor
   3) Vertical Line Constructor
   4) Default Constructor  
   5) Compute Length of Line.

    TEST CASES: Find Length of Line

    (a) Point 1: (5,6) Point 2: (15,6)
  
    (b) Point 1: (7,8) Point 2: (7,20)

    (c) Point 1: (9,10) Point 2: (19,20)

    (d) Default Line of 10 units at (0,0).

A quick question, would this be put in a header file and called into a source file?

Solutions

Expert Solution

//Line Class

#include <bits/stdc++.h>
using namespace std;
  
class Line {
   private:
// x and y cordinates of two point Point1 and Point2
int xCordinate_Point1, yCordinate_Point1, xCordinate_Point2, yCordinate_Point2;
  
   public:
   // 1) Horizontal Constructor where cordinate of first points are parameters and length of line
  
Line(int xCordinate_Point1,int yCordinate_Point1,int length){
      
       //CHECKING Line must be in first qudrant
      
if(xCordinate_Point1 < 0 || yCordinate_Point1 < 0){
           cout<<"Line is Not in First Quadrant"<<endl;
       }
       else{
          // this keyword holds reference of current object
          
this->xCordinate_Point1 = xCordinate_Point1;
           this->yCordinate_Point1 = yCordinate_Point1;
          // for second cordinate of horizontal line y cordinate remains same while for x length is added
      
   this->xCordinate_Point2 = xCordinate_Point1 + length;
           this->yCordinate_Point2 = yCordinate_Point1;
       }
      
   }
  
   // 2) Vertical Constructor where cordinate of first points are parameters and length of line
   /*Here one extra parameter is added just to differentite from Horizontal Constructor so
   that ambiguity can be removed and constructor can be overloaded in parameter vertical
   v is passed to indicate vertical */
  
Line(int xCordinate_Point1,int yCordinate_Point1,int length,char vertical){
      
      //CHECKING Line must be in first qudrant
     
if(xCordinate_Point1 < 0 || yCordinate_Point1 < 0){
           cout<<"Line is Not in First Quadrant"<<endl;
           return;
       }
       else{
          // this keyword holds reference of current object
         
this->xCordinate_Point1 = xCordinate_Point1;
           this->yCordinate_Point1 = yCordinate_Point1;
          // for second cordinate of vertical line x cordinate remains same while for y length is added
      
   this->xCordinate_Point2 = xCordinate_Point1;
           this->yCordinate_Point2 = yCordinate_Point1+length;
       }
   }
  
  
   // 3) Parameterized Constructor where cordinates of points are parameters
  
Line(int xCordinate_Point1,int yCordinate_Point1,int xCordinate_Point2,int yCordinate_Point2){
      
       //CHECKING Line must be in first qudrant
      
if(xCordinate_Point1 < 0 || yCordinate_Point1 < 0 || xCordinate_Point2 < 0 || yCordinate_Point2 < 0){
           cout<<"Line is Not in First Quadrant"<<endl;
           return;
       }
      
      // this keyword holds reference of current object
     
this->xCordinate_Point1 = xCordinate_Point1;
       this->yCordinate_Point1 = yCordinate_Point1;
       this->xCordinate_Point2 = xCordinate_Point2;
       this->yCordinate_Point2 = yCordinate_Point2;
   }
  
   /* 4) Default Constructor where cordinates of points are not passed and no initialisation so only
   object reference is created but object is not initialized*/
  
Line(){
   }
  
   // 5) Compute length function
  
double computeLength(){
  
       // this keyword holds reference of current object
       // using distance formulae distance = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
      
int x_difference = (this->xCordinate_Point1 - this->xCordinate_Point2);
       int y_difference = (this->yCordinate_Point1 - this->yCordinate_Point2);
       int square_sum = x_difference * x_difference + y_difference * y_difference;
       //taking square root
      
return pow(square_sum,0.5);
   }
};
  
int main(){
   // creating line object and finding the distance between points
  
   // PART-(a)
   //Point 1: (5,6) Point 2: (15,6)
  

  
   Line *line1 = new Line(5,6,15,6);
   cout<<"Length between Point 1: (5,6) Point 2: (15,6) is "<<line1->computeLength()<<endl;
  
   // PART-(b)
   //Point 1: (7,8) Point 2: (7,20)
  

   Line *line2 = new Line(7,8,7,20);
   cout<<"Length between Point 1: (7,8) Point 2: (7,20) is "<<line2->computeLength()<<endl;
  
   // PART-(c)
   //Point 1: (9,10) Point 2: (19,20)
  

   Line *line3 = new Line(9,10,19,20);
   cout<<"Length between Point 1: (9,10) Point 2: (19,20) is "<<line3->computeLength()<<endl;
  
   // PART-(d)
   //Default Line of 10 units at (0,0).
  

   Line *line4 = new Line(0,0,10);
   cout<<"Default Line of 10 units at (0,0) is drawn"<<endl;
}

//OUTPUT TERMINAL

For Answer of Quick Question :

Yes It can be done as this class can be used in source file and required methods can be called from there.


Related Solutions

C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
(MUST BE DONE IN C (NOT C++)) For this program, remember to use feet and inches....
(MUST BE DONE IN C (NOT C++)) For this program, remember to use feet and inches. First, ask the user for the name of students they have in their class. Then, using a loop, you will ask for each student’s height. However, you will have to use two separate variables, one for feet and one for inches. Then, you will have to call two functions. The first function will check if the values entered are valid (check if number of...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program as follows: Ask the user for an integer representing a number of integers to be sorted. Create an array of integers of the size provided by user. Initialize the array to zeros. Ask the user for and populate the array with user input. Output the array elements on one line separated by a space. Write a function name “supersort” that takes an integer pointer...
Q1: Constraint: Use concept of dynamic allocation for implementation Statement: In a class there are N...
Q1: Constraint: Use concept of dynamic allocation for implementation Statement: In a class there are N students. All of them have appeared for the test. The teacher evaluated the test and noted marks according to their roll numbers. Marks of each students has to be incremented by 5. Print list of marks of students before and after increment.
c++ programing. Objectives Further practice with classes Use of inheritance Use compositions Define a class called...
c++ programing. Objectives Further practice with classes Use of inheritance Use compositions Define a class called Person with private members name of type string, and money of integer type. The class has the public member functions set(), getMoney(), print(), and a parameterized constructor with default values. Define a class called Patient with private data members name of type string, durationOfTreatment, and TreatmentCost of type integer. The class has the public member functions set(), getCost(), getDuration(), print(), and a parameterized constructor...
in Python, Define the class called Line which represents a line with equation ?=??+? with input...
in Python, Define the class called Line which represents a line with equation ?=??+? with input slope ? and intercept ? to initialize the instances. It should include: attributes named ? and ? to represent slope and intercept. method named intersect to return the list, containing coordinates of the intersection point of two lines. support for + operator to compute the addition of two equations. The sum of two Line objects ?=?1?+?1 and ?=?2?+?2 is defined as the line ?=(?1+?2)?+?1+?2...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT