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. 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...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
IN JAVA: Write a parent class, Device, which implements ageneric computer device.Create two child...
IN JAVA: Write a parent class, Device, which implements a generic computer device.Create two child classes, Disk and Printer, which specialize a Device with added functionality.Device Task:We are only interested in three things for the time being: the name of the device, an ID number identifying the device, and a flag indicating whether or not it is enabled. Thus, three fields are necessary, a String for the name, a int for the ID, and a boolean for the enabled status.Any...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT