Question

In: Computer Science

**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor:...

**Java Programming Question**

A class that implements a data type, “Point,” which has the following constructor:

Point(double x, double y, double z)

  • and, the following API:
    • double distanceto(Point q)
      • it returns the Euclidean distance between this and q.
        • The Euclidean distance between (x1, y1, z1) and (x2, y2, z2) is defined as sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2).
    • String toString() – it returns the string representation of the point. An example would be (2.3,4.5,3.0).
    • Write a main method in the class that is used to test it.
      • It should create two Point objects using input provided by the user on the command-line.
      • Then it should print out the two points followed by their Euclidean distance

A sample run would be as follows.

>java Point 2.1 3.0 3.5 4 5.2 3.5
The first point is (2.1,3.0,3.5)
The second point is (4.0,5.2,3.5)
Their Euclidean distance is 2.90

**Java Programming Question**

Solutions

Expert Solution

// do comment if any problem arises

//code

class Point {

    double x, y, z;

    Point(double x, double y, double z) {

        this.x = x;

        this.y = y;

        this.z = z;

    }

    public String toString() {

        return "(" + x + "," + y + "," + z + ")";

    }

    // this function finds sqareroot without using library function

    public double sqrt(double input) {

        double n;

        // value to return

        double ret = input / 2;

        do {

            n = ret;

            ret = (n + (input / n)) / 2;

        } while ((n - ret) != 0);

        return ret;

    }

    // this function returns euclidian distance between this and q

    double distanceto(Point q) {

        double distance = (x - q.x) * ((x - q.x)) + (y - q.y) * ((y - q.y)) + (z - q.z) * ((z - q.z));

        Point temp=new Point(0,0,0);

        return temp.sqrt(distance);

    }

    public static void main(String[] args) {

        double x, y, z;

        x = Double.parseDouble(args[0]);

        y = Double.parseDouble(args[1]);

        z = Double.parseDouble(args[2]);

        // create first point

        Point first = new Point(x, y, z);

        x = Double.parseDouble(args[3]);

        y = Double.parseDouble(args[4]);

        z = Double.parseDouble(args[5]);

        // create second point

        Point second = new Point(x, y, z);

        // print first point

        System.out.println("The first point is " + first);

        // print second point

        System.out.println("The second point is " + second);

        System.out.print("Their Euclidian distance is ");

        // print distance

        System.out.printf("%.2f", first.distanceto(second));

    }

}

Output:

The first point is (2.1,3.0,3.5) The second point is (4.0,5.2,3.5) Their Euclidian distance is 2.91


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor...
in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor consists of an employee’s first name and an employee’s salary, both of which are instance variables. Create accessor and mutator methods for both of these variables Write an equals method that returns true if the salaries are equal with one cent and the names are exactly equal Write a compareTo method that returns 1 if the salary of this employee is greater than the...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable...
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable interfaces The class should have the following private member variables: • the x value of a point: int • the y value of a point: int and the class should have the following public member functions: • default constructor which initializes the point to the origin of the Cartesian coordinate system • explicit constructor which initializes the point to a pair of given value...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
1. Create a class Point which has a template parameter of the type of internal data,...
1. Create a class Point which has a template parameter of the type of internal data, T, and a template parameter for the dimension of the Point(2D, 3D etc.). Store a statically allocated, internal array of type T with dimension n. Ensure to include any constructer(s),destructors, getters or setters you might need. 2. Create a template function which computes the Euclidean distance between 2 points. 3. Instantiate two Point and compute their distance. Instantiate two Point and compute their distance....
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
The following is a Java programming question: Define a class StatePair with two generic types (Type1...
The following is a Java programming question: Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main(): ArrayList> zipCodeState: Contains ZIP code/state abbreviation pairs ArrayList> abbrevState: Contains state abbreviation/state name pairs ArrayList> statePopulation: Contains state name/population pairs Complete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain...
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain some methods. These will be regular methods (not static methods), so the class will have to be instantiated in order to use them. The class containing the main method will be provided and you will write the required methods and run the supplied class in order to test those methods. ? Specifications There are two separate files in this program. The class containing the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT