This question already has an answer here:
Division between integers in Java
(1 answer)
Closed 8 years ago.
I am doing an assignment for my first JAVA programming. So far it has been going well but I am stuck at the last part with using math functions.
double radius;
double diameter;
double volume;
System.out.print("Enter a diameter of a sphere: ");
diameter = keyboard.nextDouble();
radius = diameter / 2;
volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);
System.out.print("Volume of the sphere is " + volume + ".");
I am trying to use the formula for finding a volume of a sphere using diameter input variable. But I keep getting just the PI as an output.
This is what I am supposed to do.
Add a line that prompts the user to enter the diameter of a sphere.
Read in and store the number into a variable called diameter (you will need to
declare any variables that you use).
The diameter is twice as long as the radius, so calculate and store the radius in an
appropriately named variable.
The formula for the volume of a sphere is
V = 4/3 * PI * radius^3
Convert the formula to Java and add a line which calculates and stores the value
of volume in an appropriately named variable. Use Math.PI for PI and Math.pow
to cube the radius.
Print your results to the screen with an appropriate message.
What am I doing wrong ? Also how do I make it so the volume displays with E notation?
I think you might be getting an incorrect ans because of 4/3 in Integer Math give 1 and not 1.33 Try changing you code to what is give below. I tried and worked fine for me.
radius = diameter / 2.0d;
volume = (4.0d / 3.0d) * Math.PI * Math.pow(radius, 3);
Related
I have this method for calculating regular polygon area:
public double getArea() {
return (sideLength *
sideLength * sides) /
(4 * Math.tan(180 / (double) sides));
}
for sideLength and sides both being equal to 10 it returns -219.816218.
"
However this online calculator: https://www.omnicalculator.com/math/regular-polygon-area
returns 769.4. What is wrong with my method? The formula I use is specified here
.
The arguments to trigonometric functions are defined on radians, not degrees. Use Math.toRadians to convert the angle in degrees to radians - like this:
Math.tan(Math.toRadians(180 / (double) sides))
Or do this computation in radians to start with.
Math.tan(Math.PI / sides)
Use the following return statement
return (sideLength * sideLength * sides) / (4 * Math.tan((180 / sides) * 3.14159 / 180));
Here, *(3.14159 / 180) is added to convert the area from degree converted to radians
The problem is that the Math.tan function uses radians as it's default unit of measure. Use this instead:
(4*Math.tan(Math.PI/180 * 180/(double) sides))
I want to be able to add an exact distance to some GPS coordinates.
I have a longitude and a latitude and I want to add a distance, let's say 30 meters.
I found below formula but when I test it, it does not seem to be that accurate because the resulting long and lat are 37m away from the beginning coords.
public static Coordinates addDistanceToCoordinates(String lat, String lon, double distance) {
Double latitude = Double.valueOf(lat);
Double longitude = Double.valueOf(lon);
double lat0 = cos(Math.PI / 180.0 * latitude);
double x = longitude + (180/Math.PI)*(distance/6378137)/cos(lat0);
double y = latitude + (180/Math.PI)*(distance/6378137);
return Coordinates.builder().lon(x).lat(y).build();
}
If you have a center (x,y) and you move on the x axis by 30 meters and on the y axis by another 30 meters your distance from the center won't be 30.
It will be Math.sqrt( Math.pow(x, 2) + Math.pow(y, 2) );.
Specifically, there are an infinite number of points that are 30 meters distant from the center (or your initial coordinates).
If you want to move in only one direction, then you can simply add/subtract 30 meters in either of your axis.
As you already did:
double x = longitude + (180/Math.PI)*(distance/6378137)/cos(lat0);
or
double y = latitude + (180/Math.PI)*(distance/6378137);
but not both...
You are still better off by using angles in your calculations, which will turn handy when you move on both axis.
By knowing which direction you are headed to, for example 50° from the x axis,
double a = Math.toRadians(50); // degrees
double x = longitude + (180/Math.PI) * (30 / 6378137)/cos(lat0) * Math.cos(a);
double y = latitude + (180/Math.PI) * (30 / 6378137) * Math.sin(a);
Coming back to your question, if you want to move on the x axis and the y axis by the same distance and end up exactly 30 meters away from the center, then your angle will be double a = Math.toRadians(45); (if you head North-East) *
In fact you will obtain for both (30 / 6378137) * Math.cos(a) and (30 / 6378137) * Math.sin(a) a result of x1 = y1 = 3.325924707417923e-06.
If you then apply Pythagoras
double finalDistance = Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2)) * 6378137;
you will find finalDistance to be 30 meters from your initial coordinates.
*
The correct calculation would be Math.toRadians(45 * (2 * n - 1)); | where n = [1, 2, 3, 4]
Your code adds 30 meters north and 30 meters east, resulting in 42.4 meters northeast.
The calculation is assuming earth as a sphere instead of an ellipsoid, but that's mostly OK, can make a difference of max. 0.2 percent. It uses the biggest earth diameter (equator - equator) instead of some mean value, which will result in points too close to the starting point most of the time, but agian, taht can give an error of maybe 0.2 percent.
The calculation assumes the lat/lon grid to be rectangular, which is OK as long as the distances are short and you stay away from north or south pole.
So, all of this doesn't explain the 20 percent error you are reporting. The problem must be outside of the code you showed us.
The most suspicious remaining aspect is the string conversion. You'll need at least 5 decimal places for lat / lon degree values to get a 1 meter resolution.
Or maybe the tool that told you about the 37 meters isn't correct or somehow incompatible with the data...
case R.id.bTanx:
temp=(float) (number/0.0174532925);
num=Math.tan(temp);
display.setText("Your Result is " + num);
Guys I'm not able to get "Your Result is 1" when number = 45 ,by this code.Please help.
As tan(45)=1 in degrees.i have converted it.but no desired result.
To convert degrees to radian you first need to convert the degrees to a factor (of the circles circumference) by dividing by 360 degrees. Next you multiply by 2PI rad (which is the circumference of a 'unit circle').
When looking at the units you do this: degrees / degrees * radians = radians
So where you divide by 0.017 (2*PI / 360), you need to multiply instead:
temp = (float) (number * 0.0174532925);
Furthermore it is nicer (more clear) if you do not use 'magic numbers' and add comments (so people know what you are doing):
// Convert to rad
temp = (float) (number * 2 * Math.PI / 360);
And/or even use the available Java functionality:
// Convert to rad
temp = Math.toRadians(number);
I have converted the formula into Java provided here. But accuracy is a problem. We are using GPS coordinates.
We are using iPhone provided GPS location that is upto 10 decimal point accuracy.
/*
* Latitude and Longitude are in Degree
* Unit Of Measure : 1 = Feet,2 = Kilometer,3 = Miles
*/
//TODO 3 Change Unit of Measure and DISTANCE_IN_FEET constants to Enum
public static Double calculateDistance(double latitudeA,double longitudeA,double latitudeB,double longitudeB,short unitOfMeasure){
Double distance;
distance = DISTANCE_IN_FEET *
Math.acos(
Math.cos(Math.toRadians(latitudeA)) * Math.cos(Math.toRadians(latitudeB))
*
Math.cos(Math.toRadians(longitudeB) - Math.toRadians(longitudeA))
+
Math.sin(Math.toRadians(latitudeA))
*
Math.sin(Math.toRadians(latitudeB))
);
return distance;
}
FYI : public static final int DISTANCE_IN_FEET = 20924640;
And then I use Math.round(distance); to convert to long.
For actual 25 feet, I am getting 7 feet output.
You need the haversine formula.
Here's my java implementation:
/**
* Calculates the distance in km between two lat/long points
* using the haversine formula
*/
public static double haversine(
double lat1, double lng1, double lat2, double lng2) {
int r = 6371; // average radius of the earth in km
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = r * c;
return d;
}
According to Wikipedia, 10 feet is close to the limit of what you can expect from normal GPS accuracy. (And that assumes you are getting a good GPS signal.) This post on http://gis.stackexchange.com provides more detailed numbers. It basically says that 3 meter horizontal resolution is the best you are likely to get with an ordinary GPS receiver.
So if you are comparing locations provided by two different (ordinary) receivers with best case ( 3 meter / 10 feet ) resolution on each, you are still not going to be able to tell reliably if one receiver is within 10 feet of the other.
(Note your 10 digits of "precision" may well be illusory. The real issue is how accurate the locations are; i.e. how big the error bars are ... on two different devices.)
So in that case what other alternatives I have?
The linked articles mention WAAS and Carrier Phase GPS (CPGPS). Another option is WPS.
So I'm trying to calculate my bmi using the following method.
double bmi = weight / ((height * 100) * (height * 100));
bmi = Math.round(bmi * 100.0) / 100.0;
From the first line I get an answer that looks like this:
2.3457310760477412E-7
which is why I want to round this to one or two decimals. But this results in bmi being 0.0 instead.
I have also tried decimalformat which also returns 0. What am I doing wrong?
EDIT - SOLUTION
The problem was my formula! Centimeter should be divided by 10 not multiplied!
Thanks!
//André
The number you are getting from your first line, 2.3457310760477412E-7 is already kinda small. It is roughly equal to 2.345/10000000 or 0.00000023457. And this is very close to zero. Math works as expected, rounding the number to the closest integer - 0.
The error is either in the input variables or in the fomula you are using. Maybe you have your height given in centimeters instead of meters?
double weight = 89.0;
double height = 169.0;
double bmi = (weight / ((height * height) / 100)) * 100;
To clarify, weight and height are metric (KG/CM) - you need to convert the height in CM to meters (so you divide by 100) then the output is multiplied by 100 to give the BMI.