Question

In: Computer Science

WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...

WRITE IN C++

Add to the Coord class

Edit the provided code in C++

Write a member function named

     int distance2origin()

that calculates the distance from the (x, y, z) point to the origin (0, 0, 0)

the ‘prototype’ in the class definition will be

     int distance2origin()

outside the class definition

            int Coord :: distance2origin()

{

                        // your code

}

_______________________________________________________________________________________________________

/**************************************************
*
*      program name:       Coord02
*      Author:             
*      date due:           10/19/20
*      remarks:
*
*
***************************************************/

/******************************************
*     library includes
******************************************/
#include            // needed for cin and cout

/******************************************
*     pre-processor
******************************************/
#define PI 3.14159
using namespace std;

class Coord {
private:
    int xCoord;
    int yCoord;
    int zCoord;

public:
    Coord(int xCoord, int yCoord, int zCoord){
        this->xCoord=xCoord;
        this->yCoord=yCoord;
        this->zCoord=zCoord;
    }

    inline void setXCoord(int xCoord){
        this->xCoord=xCoord;
    }
    inline void setYCoord(int yCoord){
        this->yCoord=yCoord;
    }
    inline void setZCood(int zCoord){
        this->zCoord=zCoord;
    }
    inline int getXCoord(){
        return this->xCoord;
    }
    inline int getYCoord(){
        return this-> yCoord;
    }
    inline int getZCoord(){
        return this->zCoord;
    }

    void display(){
        cout<xCoord<yCoord<zCoord<

Please Explain Everything in detail. Thank you.

Solutions

Expert Solution

Code:

Output:

Raw code:

#include <iostream>
#include <cmath>
#define PI 3.14159
using namespace std;
class Coord {
private:
    int xCoord;
    int yCoord;
    int zCoord;

public:
    // constructor to initialize it's class variables
    Coord(int xCoord, int yCoord, int zCoord){
        this->xCoord=xCoord;
        this->yCoord=yCoord;
        this->zCoord=zCoord;
    }
    // setter methods to set the values of class variables
    void setXCoord(int xCoord){
        this->xCoord=xCoord;
    }
    void setYCoord(int yCoord){
        this->yCoord=yCoord;
    }
    void setZCood(int zCoord){
        this->zCoord=zCoord;
    }
    // getter methods to return the values of the class variables
    int getXCoord(){
        return this->xCoord;
    }
    int getYCoord(){
        return this->yCoord;
    }
    int getZCoord(){
        return this->zCoord;
    }
    // displaying the point information
    void display(){
        cout << getXCoord() << getYCoord() << getZCoord();
    }
    // declaring the method
    int distance2origin();
};
int Coord :: distance2origin(){
    // finding the distance between a point to the origin using the formula
    // p1 = (x1,y1,z1)
    // p2 = (x2,y2,z2)
    // distance between p1 and p2 = ( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 )^0.5
    // Here the p2 is origin that means x2,y2,z2 = 0,0,0
    // distance between p1 and origin  = ( (0-x1)^2 + (0-y1)^2 + (0-z1)^2 )^0.5
    //                                 = ( (-x1)^2 + (-y1)^2 + (-z1)^2 )^0.5
    //                                 = ( (x1)^2 + (y1)^2 + (z1)^2 )^0.5
    int distance = sqrt( pow(getXCoord(),2) + pow(getYCoord(),2) + pow(getZCoord(),2) );
    return (distance);
}
int main(int argc, char const *argv[])
{
    Coord c1 = Coord(13,42,5); // creating an object
    cout << "Distance between a point (13,42,5) to origin (0,0,0) is : " << c1.distance2origin() << "\n"; // printing the result
    return 0;
}

NOTE:
   If You Have Any Doubts Feel Free To Comment In The Comment Section.
   Do Vote (LIKE).


Related Solutions

Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will...
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x)...
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x) d) remove(x) e) splice(x) f) min() g) max() h) pred() i) succ() j) floor() k) ceil()
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
code in c++ using the code given add a hexadecimal to binary converter and add a...
code in c++ using the code given add a hexadecimal to binary converter and add a binary to hexadecimal converter #include <iostream> #include <string> #include<cmath> #include<string> using namespace std; int main() { string again; do { int userChoice; cout << "Press 2 for Decimal to Binary"<< endl; cout << "Press 1 for Binary to Decimal: "; cin >> userChoice; if (userChoice == 1) { long n; cout << "enter binary number" << endl; cin>>n; int decnum=0, i=0, remainder; while(n!=0) {...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new behavior as well: add(Section s) this behavior will add a section to the Person’s schedule. The Person’s display() function will also need to be modified to display() the Person’s Schedule. Schedule class has Properties: An array of Sections and a Count. Constructors: only a default constructor is needed. Behaviors: add() and display(). This is my schedule class class Schedule     {         private int...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT