In: Computer Science
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding.
We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :)
Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst is to aggregate data points and make conclusions based on how that data clusters together. Fundamentally, this is how Pandora selects what song to play next, how Netflix determines what shows to recommend next, how Amazon chooses which products to promote together, how banks determine whether to issue a loan, and how hedge companies decide which stocks to invest into. One critical component that empowers data analysts to make such predictions is to determine how close or far two data points are in relation to one another. This is accomplished using Trigonometry, a field of study in mathematics which observes the relationships of the sides and angles of triangles. By mapping the data points onto a 2d chart, it is then possible to use the distance formula (pythagorean theorem) to calculate the geometric distance between these two points. Your task in this problem is to calculate the distance between two points.
Facts
● Distance formula: distance = √(x x ) 2 − 1 2 + (y y ) 2 − 1 2
● Java Math class contains sqrt and pow methods ○ https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html
● Your solution should have a main method.
● Use Scanner for input & Use System.out for output!
Input The first line of input is the number of test cases. Each line afterwards contains four double values. The first two values represent the first point's coordinates as (x1 ,y1 ). The second set of values represent the second point's coordinates as (x2 ,y2 ).
Output Print the distance between the two points as a double type. Use System.out.println() on your result.
Sample Input
3
0 0 2 2
-1 -1 1 1
0 0 0.5 0.5
Sample Output
2.8284271247461903
2.8284271247461903
0.7071067811865476
Java Implementation
//import all the modules required
import java.util.*;
public class GeometricDistance {
public static void main(String args[]) {
//variable declarations
double x1, x2 , y1, y2 ,first_term
, second_term;
//creating a scanner object to read
the input from user
Scanner sc = new
Scanner(System.in);
// reading the number of test cases
from the user.
int n =sc.nextInt();
// creating an array to store
distance values in the distance array.
double[] distance = new
double[n];
//Running a for loop to take the
input from the user and to calculate distance
for(int i =0;i <n;i++) {
//Reading the
input from the user
x1 =
sc.nextDouble();
y1 =
sc.nextDouble();
x2 =
sc.nextDouble();
y2 =
sc.nextDouble();
//calculating
the square terms first
first_term =
Math.pow((x1-x2),2);
second_term =
Math.pow((y1-y2),2);
// calculating
the square root of the sum of the aboce terms
distance[i] =
Math.sqrt((first_term+second_term));
//storing the
distance value in the array .
}
//running the for loop to display
the distances
for(int i=0;i<n;i++)
//printing the
distances
System.out.println(distance[i]);
//closing the scanner object.
sc.close();
}
}
Output: -