Question

In: Computer Science

This is an entry to Java Question. Please answer with Pseudocode and Java code for better...

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

Solutions

Expert Solution

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: -


Related Solutions

This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
This is a Entry to Java question. Please show me the PseudoCode or Commits first so...
This is a Entry to Java question. Please show me the PseudoCode or Commits first so I can learn to analyze everything a a little bit better. Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result. Your calculator must support addition (+), subtraction (-), multiplication (*), division (/),...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently....
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently. Problem 5: Player Move Dungeon (10 points) (Game Development) You're the lead programmer at a AAA studio making a sequel to the big hit game, Zeldar 2. You've been challenged to implement player movement in dungeons. The game is top-down, with dungeons modeled as a 2d grid with walls at the edges. The player's location is tracked by x,y values correlating to its row...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT