January 22, 2019

Fidelity Questions: 

Problem 1:
Simple Question based on Inheritance and methods related to java.lang.Math class.

Description:
You have to implement 2 classes named Point2D and Point3D.
The purpose of the two classes is to find out the distance between two 2D points and two 3D points respectively.
The Structure of the classes should be as follows:

Point2D:

-- Two instance level properties x and y which depicts the x and y coordinate of a point in two-dimensional space.
--A parameterized constructor which takes two parameters and initializes its instance variables with it.
--A method double distanceFrom(Point2D p) which returns the distance between two points in 2-Dimensional space.
--A method void printDistance(double d) which outputs the distance in a single digit format of the largest number than it if the distance is not an integer literal.


Point3D:

--The class extends Point2D
-- Three instance level properties x, y, and z which depicts the x, y and z coordinate of a point in three-dimensional space.
--A parameterized constructor which takes three parameters and initializes its instance variables with it.
--A method double distanceFrom(Point3D p) which returns the distance between two points in 3-Dimensional space.
--A method void printDistance(double d) which outputs the distance in a single digit format of the largest number than it if the distance is not an integer literal.

Implementation:
package hackerrankQuestion.fidelity;
import java.util.Scanner;
public class Solution
{
// The following code is already present.
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int z1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int z2 = sc.nextInt();
Point2D p1 = new Point2D(x1, y1);
Point2D p2 = new Point2D(x2, y2);
Point3D p3D1 = new Point3D(x1, y1, z1);
Point3D p3D2 = new Point3D(x2, y2, z2);
double twoDdistance = p1.distanceFrom(p2);
double threeDdistance = p3D1.distanceFrom(p3D2);
p1.printDistance(twoDdistance);
p3D1.printDistance(threeDdistance);
}
}
//we need to implement the following classes
class Point2D
{
int x, y;
public Point2D(int x, int y)
{
this.x = x;
this.y = y;
}
public double distanceFrom(Point2D p)
{
int subx = p.x - this.x;
int suby = p.y - this.y;
return Math.sqrt(((subx * subx) + (suby * suby)));
}
public void printDistance(double d)
{
System.out.println("2D distance=" + (int) Math.ceil(d));
}
}
class Point3D extends Point2D
{
int z;
public Point3D(int x, int y, int z)
{
super(x, y);
this.z = z;
}
public double distanceFrom(Point3D p)
{
int subx = p.x - this.x;
int suby = p.y - this.y;
int subz = p.z - this.z;
return Math.sqrt(((subx * subx) + (suby * suby) + (subz * subz)));
}
public void printDistance(double d)
{
System.out.println("3D distance=" + (int) Math.ceil(d));
}
}
view raw Solution.java hosted with ❤ by GitHub