Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

1. Design a class named TwoDPoint to represent a point with x and y coordinates . The class contains: *Two data fields x and y

1. Design a class named TwoDPoint to represent a point with x and y coordinates.

The class contains:

*Two data fields x and y that represent the coordinates.

* No-argument constructor that creates a point (0,0)

*A constructor that constructs a point with specified coordinates

*Two accessor functions

*Override toString() and equals() methods

*clone() method

*A method named distance that returns the distance from this point to the origin: public double distance()

Formula for calculating distance between two-dimensional point (x1, y1) and the origin is: d = sqrt( (x1)2+(y1)2)

2. Create a class named ThreeDPoint to model a point in a three-dimensional space. Let ThreeDPoint be derived from TwoDPoint

with the following additional features:

*A field z that represents the z-coordinate.

*No-argument constructor that creates a point (0,0,0)

* Accessor method for z

*Override toString() and equals() methods

*clone() method

*Override the distance function to return the distance between the point in the three-dimensional space and the origin. Formula for calculating this distance is

d = sqrt( (x1)2+(y1)2+(z1)2) where the coordinates of the point are (x1, y1, z1)

3. Testing in main()

* Test ALL METHODS by calling them. Do not use user input in your tests hard code all data instead.

* Create an array of type TwoDPoint and fill it with 10 object: half of them must be of type TwoDPoint, another half of type ThreeDPoint. Use random number generator to create random points when you are filling your arrays with data. Then in a loop call distance() method for all objects and observe how the correct implementation of the method is being called.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions