Answer to Question #192400 in Java | JSP | JSF for Bill

Question #192400

Create Java program that has “supporting class” named “Point3D” which contains:

(a) three class variables, x, y, and z. (b) a default constructor that automatically set the point to be (0, 0, 0). (c) create another form of the default constructor that allows the user to set the three coordinates, such as (126, 44, 39) (d) create method named “distance” that calculates the distance between (0, 0, 0) and another point such as (31, 127, 86); and create another form of the “distance” method that calculates the distance between one “Point3D” object and another “Point3D” object.

In the man() method create three instances of the “Point3D” class: p1, p2, and p3. Let p1 be (0, 0, 0), p2 be (126, 44, 39), and p3 be (25, 219, 74). Ask the user to enter three integers, and then use them to create another Point3D object named “p4”. Calculate the distance between p1 and p2, p1 and (31, 127, 86), p2 and p3 as well as p3 and p4.


1
Expert's answer
2021-05-18T15:08:57-0400


class Point3D{

    private int x, y, z;

// origin value constructor

    Point3D(){

        this.x = 0;

        this.y = 0;

        this.z = 0;

    }

    // pass argument

    Point3D(int x, int y, int z){

        this.x = x;

        this.y = y;

        this.z = z;
    }

    // points
    public double distance(Point3D point3d){
        int x1 = point3d.x;
        int y1 = point3d.y;
        int z1 = point3d.z;
        return Math.sqrt(x1*x1 + y1*y1 + z1*z1);
    }
    // distance calculation
    public double distance(Point3D point1, Point3D point2){
        int x1 = point1.x;
        int y1 = point1.y;
        int z1 = point1.z;
        int x2 = point2.x;
        int y2 = point2.y;
        int z2 = point2.z;
        return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
    }
}
//main.java
import javax.swing.JOptionPane;

class MyProg13{
    public static void main(String[] args){
        Point3D p1 = new Point3D();
        Point3D p2 = new Point3D(126, 44, 39);
        Point3D p3 = new Point3D(25, 219, 74);
        String x = JOptionPane.showInputDialog("Enter x: ");
        String y = JOptionPane.showInputDialog("Enter y: ");
        String z = JOptionPane.showInputDialog("Enter z: ");
        Point3D p4 = new Point3D(Integer.parseInt(x),Integer.parseInt(y),Integer.parseInt(z));
         String result = "p1 to (31, 127, 86): "+ p1.distance(p1, new Point3D(31, 127, 86))+"\n"+
                        "p1 to p2: "+ p1.distance(p2)+"\n"+
                       "p2 to p3: "+ p1.distance(p2, p3)+"\n"+
                        "p3 to p4: "+ p1.distance(p3, p4);
        JOptionPane.showMessageDialog(null,result);
        System.exit(0);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS