Calculate Distance Between Two Points - All Numbers And Return Values Should Be Of Type Double
Jul 8, 2014
Write method distance to calculate the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points.
Hints:
- The distance between two points can be calculated by taking the square root of
( x2 - x1 )2 + ( y2 - y1 )2
- Use Math class methods to compute the distance.
- Your output should appear as follows:
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: 1
Enter Y1: 1
Enter X2: 4
Enter Y2: 5
Distance is 5.000000
Type the end-of-file indicator to terminate
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter
Or Enter X1: ^Z
View Replies
ADVERTISEMENT
Oct 18, 2014
I've just been having a go at an exercise where I have to create and use a class called Point, with two fields of type double. I have to create some methods for it, one of which is a distanceTo(Point) method, that calculates the distance to another point. I've tried to keep the distanceTo(Point) method short so have created some other methods to use within the method. My question is about the getDistance() method that I've made. As you can see below, I've given it two parameters, which are references to values within two Point objects (this.x and otherPoint.x).
double distanceTo(Point otherPoint) {
double distanceX = getDistance(this.x, otherPoint.x);
double distanceY = getDistance(this.y, otherPoint.y);
return calculateLength(distanceX, distanceY);
}
View Replies
View Related
Sep 27, 2013
I have been having difficulty with the weeks concepts in my online Java class, the program is to be as followed:
For this exercise you will implement a class called Pair, that represents a pair of two numbers.The Pair class should include the following constructor and methods:
CONSTRUCTORS
public Pair(double num1, double num2) -- Creates an object that represents a pair of double values
METHODS
public double getAverage() -- Returns the average value of the two numbers
public double getDistance() -- Returns the absolute vale of the distance between the two numbers
public double getMaximum() -- Returns the maximum value of the two numbers
public double getMinimum() -- Returns the minimum vale of the two numbers
Write a class called PairTest that tests your Pair implementation. The PairTest should prompt the user for the two values, create a Pair object with the values and then print the average, distance, maximum, and minimum of the pair. The input / output should look like the following:
Enter the first number: 5.5
Enter the second number: 3.0
Average: 4.25
Distance: 2.5
Maximum: 5.5
Minimum: 3.0
NOTE: For this exercise, your solution should not use any conditional statements. Instead you should use the methods provided by thejava.util.Math.
So far I have:
import java.lang.Math;
import java.util.Scanner;
public class Main
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
{
System.out.println("Please enter a value for the first number");
[Code] ....
View Replies
View Related
Apr 2, 2014
How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.
When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.
// Here is what i tried to do:
Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.
Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).
Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.
class TwoDPoint {
int x = 2;
int y = 4;
}
class TestTwoDPoint {
public static void main(String args[]) {
TwoDPoint obj1 = new TwoDPoint();
System.out.println(obj1.x);
System.out.println(obj1.y);
[Code] ....
View Replies
View Related
Mar 14, 2009
hey just having some trouble with a homework question:
For this question you will use the Point class from the Java API, which represents points in 2-dimensional space, each of which has an x and y coordinate. You must write a program called Distance, which does the following:
1. Reads in the coordinates (separated by spaces) of two points
2. Creates two Point objects with the values entered by the user
3. Uses the distance method of the Point class to calculate the distance between the two points
4. Prints out the distance
Details on how to create Point classes can be found in the Java API documentation. However, for this question the only two methods you need to know about it are the following:
- Point(int x, int y) - Constructor; creates a new point
- double distance(Point other) - Calculates the distance between this point and another point
I think what i have so far will work, im jsut having problems creating 2 new objects . it points at new and says incompatible types. And also points at +distance and says cannot be de reference from a static context.
import java.awt.Point;
import java.util.Scanner;
public class Distance{
private int pointX;
private int pointY;
private double distance;
public Distance(int x,int y)
[Code] .....
View Replies
View Related
May 1, 2015
Write a program that draws 20 circles, with the radius and location of each circle determined at random...... Two circles overlap if the distance between their center points is less than the sum of their radii...
There may be many problems with the code in general but what I'm struggling with is the distance and the totalradius portion. Visually, its inaccurate.
import java.awt.*;
import javax.swing.*;
public class CircleTest extends JPanel {
Circle []circles;
Circle []circleCenter;
Circle []all;
private int distance, totalradius, dx, dy;
private int radius,x,y;
[Code] ....
View Replies
View Related
Sep 7, 2014
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1:
public class date {
public int day;
public int month;
public int year;
}
// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}
View Replies
View Related
Mar 17, 2015
I am working on this new project where we are using the great distance formula but every time I run my ending result is NaN. I was researching, and people say its because you divide by 0. I think I have my formula correct.
Java Code:
public class testingFormula {
public static void main(String[] args) {
double lat = 34.01858902;
double lon = -118.2817688;
double lat2 =33.94530869;
double lon2 = -118.3994904;
[Code] ....
View Replies
View Related
Feb 20, 2015
Write method distance, which calculates the distance between two points (x1, y1) and (x2, y2). All numbers and returned values should be of type double. Incorporate this method into an program that enable the user to enter the coordinates of the points, then calculate and display the distance by calling the method –distance.
I've tried numerous times to make it work and I'm on the right path, however I'm missing some things in the code to make my results look like this later on, which I've attached onto this post.
View Replies
View Related
Mar 13, 2015
I need to modify the drawShape method to calculate the distance from the starting point (the diameter) of any shape regardless of how many sides I give it, but I have absolutely no clue where to begin with this. The ultimate goal of the program is to calculate the value of pi using the shape that is drawn.
Here is the code:
public class PiTurtle extends Turtle
{
private double mySize;
private int mySides;
private double diameter = 0;
final static double startX = 590.0;
final double startY;
public PiTurtle(int nSides)
[Code] .....
View Replies
View Related
Dec 11, 2013
I need to know the sinus or angle between two points (x1,y1) and (x2,y2).
Any formula to calculate this?
View Replies
View Related
Mar 13, 2015
I need to modify the drawShape method to calculate the distance from the starting point (the diameter) of any shape regardless of how many sides I give it, but I have absolutely no clue where to begin with this. The ultimate goal of the program is to calculate the value of pi using the shape that is drawn.
Java Code:
public class PiTurtle extends Turtle
{
private double mySize;
private int mySides;
private double diameter = 0;
[code]....
View Replies
View Related
Feb 1, 2015
Basically I'm looking for a way to make one object follow another. For example, if I move object A to one area of the screen I want object B to to move to object A's location but I also want object B to move at a fixed speed (movement variable). How do I go about doing this?
Both the x and y coordinates of object B would need to know the coordinates of object A to calculate the distance between the two and to determine how much of which axis to increment/decrement (if that makes sense?) with the inclusion of the speed variable. In other words I'm just trying to create a homing object.
View Replies
View Related
Apr 13, 2014
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Hours Traveled ");
int hoursTraveled = input.nextInt();
System.out.println("Enter MPH ");
int mph = input.nextInt();
[Code] .....
View Replies
View Related
Mar 26, 2014
I want to write a Utility to calculate the shipping charges based on the product weight in pounds and distance in miles. How we can go about it.
View Replies
View Related
Jul 11, 2014
I'm having an issue with figuring out how to structure and call a method using a string prompt. My prof has given us specific methods to create, and the one I'm having issues with is blic static double getOperand(String prompt). What I have currently is this:
public static double getOperand(String operand1, String operand2){
Scanner input = new Scanner(System.in);
System.out.println("What is the first operand?");
operand1 = input.toString();
System.out.println("What is the second operand?");
operand2 = input.toString();
return operand1, operand2;
}
which is obviously not working. When I asked my prof he sent me this, "The String paramater in the getOperand() method is intended to have the prompt value that is passed to it. Within the method, the prompt is then displayed, what the prompt is asking for is input, and then returned to the method call.For example, if the method is public double getOperand(String prompt) within the method you would display the prompt, as in System. out. println(prompt), use the Scanner class to create an object, perhaps called input, and then use the input object to get the value asked for by the prompt. Once you have that value, which in this example would be a double, then return the value to the method that called it.
The call could be something like this: double value = getOperand("What is the first operand?");". But that only works a little. Clearly I'm missing something and no matter what I google, or how many times I read the chapter, I am just not getting this. Everything else up to this point has been easy. and I can create regular methods just fine, the menu method works, but I can't figure out this string method and all the rest build off this method.
View Replies
View Related
Aug 22, 2014
I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000
public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);
[Code] ....
View Replies
View Related
Jul 9, 2014
What's that diameter? Create a new method for the circle class called diameter. Add this method to the circle class described on page 15-1. It should return a double that is the diameter of the circle. No parameters are passed to this method.
In a tester class, test the performance of your new diameter method as follows:
(Your project should have two classes, Tester and Circle.)
here is what i have so far:
public class Circle
{
public Circle (double r)
{
radius = r;
}
public double area()
{
double a = Math.PI * radius * radius;
[Code] ....
View Replies
View Related
Nov 6, 2013
This is what he wants: He wants us to prompt the user to input x values and y values (that will be entered into an array when clicking one of the two buttons (this one will say: Add point)). When they click the second button (draw lines), it should take all the points and draw a line that connects all the points together using a method that we write. In the method we have to call the paint method up which the lines will be drawn. The lines drawn should be touching every point added. It shouldn't have parameters. It will also use Graphics painter = getGraphics(); We can't use frames either.
I have everything up until the method understood.
How to write a method that will draw a line from points inputted that is called up in the action listener when the second button (draw lines) is pressed.
View Replies
View Related
Nov 9, 2014
I was struggling to use BufferedReader to extract some data and then perform some calculations and then have the results as outputs.
I haven't quite solved that issue but in order to progress, I hard coded some values into my application and proceeded with the actual calculation loops etc.
Currently, the value out put from one of my calculations is given as:
1.1704926E7
How can I make the console show it in a natural way. I've performed the calculation manually and it should be 11704926.5 I don't want to lose that .5!
View Replies
View Related
Aug 12, 2014
I am trying to use double data type in a for loop for precise operations and just to see if there could be any problem doing that I tested a small code :
public class doubleLimit {
public static void main(String[] args){
for(double i=-0.1;i<=0;i+=0.01)
System.out.println(i); }}
The output I was expecting is :
-0.1
-0.09
-0.08
-0.07
-0.06
-0.05
-0.04
-0.03
-0.02
-0.01
0.00
But the output of the code is :
-0.1
-0.09000000000000001
-0.08000000000000002
-0.07000000000000002
-0.06000000000000002
-0.05000000000000002
-0.040000000000000015
-0.030000000000000013
-0.02000000000000001
-0.01000000000000001
-1.0408340855860843E-17
Why is the code not working the way I expected, I think it has something to do with any property of double but I am not sure.
View Replies
View Related
Feb 21, 2014
I have doubt in generics,
List<int> c=new ArrayList<int>();
why we cannot use primitive data type like int,double.
View Replies
View Related
Dec 16, 2014
How to get a double data type in order to display in the view through a servlet:
objReferencias.setImprevistos(request.("imprevistos"));
Question:
request.getDouble("imprevistos));? or
request.getDoubleParameter("imprevistos));?
I've been searching but I have not found the appropriate answer.
View Replies
View Related
Apr 20, 2014
I have a simple doubt
float k = 0;
k+=0.2;
k=k+0.2; // here compilation error
compliation error Type mismatch: cannot convert from double to float
My question is why not a complilation error at k+=0.2;
View Replies
View Related
Dec 6, 2014
Im trying to do this
if( 2/2 ) {
system.out.println( "number is not event" );
} else {
system.out.println( "number is event" );
}
However if statement requires a return of type boolean. So im forced to this instead
if( (2/2) != 0 ) {
system.out.println( "number is not event" );
} else {
system.out.println( "number is event" );
}
Is there a way to achieve the former method, without typecasting, if you had to typecast how do you do it?
View Replies
View Related
Dec 14, 2014
we have deployed application on web sphere server and using servlets and jsp only.
View Replies
View Related