Question

In: Computer Science

For this task you will create a Point3D class to represent a point that has coordinates...

For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class.
The Point3D class will have the following state and functionality:

  • Three data fields, x, y and z, of type double, represent the point’s coordinates
  • Additional data field, colour, of type String, represents the point's color
  • A no-arg constructor creates a point at position (0, 0, 0) and "Red" colour.
  • Another constructor creates a point with specified coordinates and colour.
  • A getter method is provided for each of the fields.
  • An accessor method named distance returns the distance between the current point and another point passed as an argument.
  • The distance method is overloaded to accept the coordinates of the other point.

Write a TestPoint3D class that will have a main method, and perhaps other methods as required by good design, to test your Point3D class. It will not have user input because this class will stand as a record of the tests you have applied and you will be able to run it whenever you alter your Point3D class. For the TestPoint3D class you will need to do the following:

  • Test the basic state and functionality of the Point3D class. Each of the constructors and each of the methods should be tested using some different data values. The test code should display values so that they can be checked.
  • Write some code to create an array of at least 10 Point3D objects.
  • Write a method max, which will accept the array of points as an argument, and will calculate and display the maximum distance between the points in the array, and the pair of points for which the maximum occurs.
  • Write a method min, which will accept the array of points as an argument, and will calculate and display the minimum distance between the points in the array, and the pair of points for which the minimum occurs.

Solutions

Expert Solution

//Point3D.java

public class Point3D {

      

// instance variables

       private double x;

       private double y;

       private double z;

       private String colour;

      

       // constructors

       public Point3D()

       {

             x = 0;

             y = 0;

             z = 0;

             colour = "Red";

       }

      

       public Point3D(double x, double y, double z, String colour)

       {

             this.x = x;

             this.y = y;

             this.z = z;

             this.colour = colour;

       }

      

// getters

       public double getX()

       {

             return x;

       }

      

       public double getY()

       {

             return y;

       }

      

       public double getZ()

       {

             return z;

       }

      

       public String getColour()

       {

             return colour;

       }

      

       // distance methods

       public double distance(Point3D point)

       {

             return(Math.sqrt(Math.pow(x-point.x,2)+Math.pow(y-point.y, 2)+Math.pow(z-point.z, 2)));

       }

       public double distance(double x, double y, double z)

       {

             return(Math.sqrt(Math.pow(this.x-x,2)+Math.pow(this.y-y, 2)+Math.pow(this.z-z, 2)));

       }

      

}

//end of Point3D.java

//TestPoint3D.java

public class TestPoint3D {

      

       /* method that calculates and displays the maximum distance between two points in the given arrays and the points with the maximum distance*/

       public static void max(Point3D points[])

       {

             double maxDist = 0;

             Point3D point1 =null, point2= null;

            

             for(int i=0;i<points.length;i++)

             {

                    for(int j=i+1;j<points.length;j++)

                    {

                           double dist = points[i].distance(points[j]);

                           if( dist > maxDist)

                           {

                                 point1 = points[i];

                                 point2 = points[j];

                                 maxDist = dist;

                           }

                    }

             }

            

             if(point1 != null && point2 != null)

             {

                    System.out.println("Maximum distance : "+maxDist+" between points ("+point1.getX()+", "+point1.getY()+", "+point1.getZ()+") and "

                                 +" ("+point2.getX()+", "+point2.getY()+", "+point2.getZ()+")");

             }

       }

      

       /* method that calculates and displays the minimum distance between two points in the given arrays and the points with the minimum distance*/

       public static void min(Point3D points[])

       {

             double minDist = Double.MAX_VALUE;

             Point3D point1 =null, point2= null;

            

             for(int i=0;i<points.length;i++)

             {

                    for(int j=i+1;j<points.length;j++)

                    {

                           double dist = points[i].distance(points[j]);

                           if( dist < minDist)

                           {

                                 point1 = points[i];

                                 point2 = points[j];

                                 minDist = dist;

                           }

                    }

             }

            

             if(point1 != null && point2 != null)

             {

                    System.out.println("Minimum distance : "+minDist+" between points ("+point1.getX()+", "+point1.getY()+", "+point1.getZ()+") and "

                                 +" ("+point2.getX()+", "+point2.getY()+", "+point2.getZ()+")");

             }

       }

      

       public static void main(String[] args) {

             Point3D point1 = new Point3D();

             Point3D point2 = new Point3D(2,3,1,"Yellow");

            

             System.out.println("Point1 : ("+point1.getX()+","+point1.getY()+","+point1.getZ()+") Color : "+point1.getColour());

             System.out.println("Point2 : ("+point2.getX()+","+point2.getY()+","+point2.getZ()+") Color : "+point2.getColour());

            

             Point3D points[] = {new Point3D(),new Point3D(1,2,3,"Red"),new Point3D(5,1,3,"Yellow"),new Point3D(8.5,1.24,4,"Blue"),new Point3D(1.5,2,7.5,"Green")

                           ,new Point3D(12,5.5,8,"Magenta"),new Point3D(8,2.4,3.25,"Cyan"),new Point3D(9,12,8,"Purple"),new Point3D(8,0.5,12,"Pink"),new Point3D(0,13.6,7.5,"Orange")};

             max(points);

             min(points);

            

       }

}

//end of TestPoint3D.java

Output:


Related Solutions

Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
Requirements:   You are to write a class called Point – this will represent a geometric point...
Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. A copy...
Requirements:   You are to write a class called Point – this will represent a geometric point...
Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. A copy...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
Objective: Create a program that has a class to represent a cup of coffee that combines...
Objective: Create a program that has a class to represent a cup of coffee that combines the properties: name and caffeine content. The class should also have a method that calculates the number of cups that would be maximally risky to consume in a short period of time. The program should create two instances of the class coffee where the user enters their properties and should output the amount of coffees that consumed would be risky. Requirements: Write a class...
Task use c++ and Create Base class task with virtual method Create a pair of derivative...
Task use c++ and Create Base class task with virtual method Create a pair of derivative classes of architecture, scientist, economist - Define a salary in each of these classes and create a method that prints the salary for each job Create a working object Use dynamic_cast Use typeid Create an object for each job that it will include the number of employees of that type and the method of printing these numbers
public class Point { protected double x, y; // coordinates of the Point //Default constructor public...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public Point() { setPoint( 0, 0 ); } //Constructor with parameters public Point(double xValue, double yValue ) { setPoint(xValue, yValue ); } // set x and y coordinates of Point public void setPoint(double xValue, double yValue ) { x = xValue; y = yValue; } // get x coordinate public double getX() { return x; } // get y coordinate public double getY() { return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT