I have a Java class and im stumped on this issue. We have to make a volume calculator. You input the diamater of a sphere, and the program spits out the volume. It works fine with whole numbers but whenever I throw a decimal at it, it crashes. Im assuming it has to do with the precision of the variable
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextInt();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
So, like I said if i put in a whole number, it works fine. But I put in 25.4 and it crashes on me.
This is because keyboard.nextInt() is expecting an int, not a float or double. You can change it to:
float sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diamater of a sphere:");
sphereDiam = keyboard.nextFloat();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("The volume is: " + sphereVolume);
nextFloat() and nextDouble() will pickup int types as well and automatically convert them to the desired type.
double sphereDiam;
double sphereRadius;
double sphereVolume;
System.out.println("Enter the diameter of a sphere:");
sphereDiam = keyboard.nextDouble();
sphereRadius = (sphereDiam / 2.0);
sphereVolume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( sphereRadius, 3 );
System.out.println("");
System.out.println("The volume is: " + sphereVolume);
Related
I'm making a trig calculator to practice aviation problems for fun and can't convert radians to degrees properly in java.
I've tried taking altitude divided by Math.tan(angle) and times it by (180 / Math.PI) but this doesn't give me the answer I'm looking for.
The numbers I've tried include alt = 500, angle of approach = 3. My code will store these values and take 500/tan(3) * (180/Pi) and I'm unsure why this isn't the correct trigonometry behind it.
public static void approachPath() {
System.out.println("FINDING THE IDEAL APPROACH PATH . . . ");
System.out.println("What is the altitude of the aircraft:");
double alt = scan.nextDouble();
System.out.println("What is the angle of approach:");
double angleofapproach = scan.nextDouble();
//line my problem occurs on
double approachPath = (alt / Math.tan(angleofapproach)) * (180 / Math.PI);
System.out.println("The ideal approach path is: " + approachPath);
}
I'm expecting the answer 9,541feet so I can move on to writing the rest of the method to find the final approach path in nautical miles.
You were almost right. Just instead of rad to deg, it should be deg to rad.
double angleofapproach = toRad(scan.nextDouble());
double approachPath = (alt / Math.tan(angleofapproach));
// deg to rad
public static double toRad(double deg) {
return deg * (Math.PI / 180);
}
So i'm trying to make a simple calculator to calculate a wilks score. I'm not getting the correct answer which i'm sure is because i'm not setting up the formula correctly. If I pass in a weight of 180, squat of 300, bench of 300, and deadlift of 400 in lbs I should be getting a wilks of 305.78 but i'm getting 2.0414858^-5
Heres my calcWilks method
public double calcWilks(double weight, double squat, double bench, double deadLift) {
double a = -216.0475144;
double b = 16.2606339;
double c = -0.002388645;
double d = -0.00113732;
double e = Math.pow(7.01863, -6);
double f = Math.pow(-1.291, -8);
double x = weight;
double coeff;
double score;
coeff = 500 / (a + (b*x) + (c* Math.pow(x, 2))+ (d* Math.pow(x, 3))
+ (e* Math.pow(x, 4)) + (f* Math.pow(x, 5)));
double total = squat + bench + deadLift;
score = coeff* total;
return score;
}
and heres a link to the actual formaula https://en.wikipedia.org/wiki/Wilks_Coefficient
I'm trying to use the Male formula
Thanks for any help!
The e coefficient's value in the wikipedia page is
7.01863E-06
but you've used
Math.pow(7.01863, -6)
That's not the same thing:
7.01863E-06 = 7.01863 * Math.pow(10, -6).
Just use the value 7.01863E-06 (or 7.01863e-6) directly.
(Same problem with f)
Also, note that the inputs to the formula should be in kilograms; not in pounds, as you state in the question.
I am working on an app and it doesn't really works out at the moment. The app calculates how much calories you burned with walking the stairs. But when I want to round up the amount of calories it says Variable might not have been initialized. So I made the double kcal = 0 . But when I tested my app it just gave the number 0.0 as the calories I burned that day. Do you guys know how to resolve this problem?
Here is my code where I Declare the doubles:
double trap;
double kcal = 0;
double endkcal = Math.round( kcal * 100.0 ) / 100.0;
double keer;
double gewicht;
double nul = 0.13;
String eten = "Je kan nu Niks eten";
And here is the calculating code:
trap = Double.parseDouble(editText_trap.getText().toString());
gewicht = Double.parseDouble(editText_gewicht.getText().toString());
keer = gewicht * nul / 4;
kcal = keer * trap;
textView_kcal.setText(String.valueOf(endkcal) + "kcal");
You should use endkcal = Math.round( kcal * 100.0 ) / 100.0; after you calculate kcal to have correct calculation. Now it is normal to get 0.0 after calculation because kcal is 0.
Apparently your endkcal variable is only initialized (to 0.0) but never updated.
Initialize all of your double variables when you have declared them like this:
double trap=0.0;
double kcal =0.0;
double endkcal = Math.round( kcal * 100.0 ) / 100.0;
double keer=0.0;
double gewicht=0.0;
double nul = 0.13;
I was bored and wanted to practice my java coding skills. I made a program that finds the area of a polygon based on what you know (radius, perimeter, apothem).
Here's a portion:
static void pentagon() {
System.out.println("Select what you know");
System.out.println("[1]Perimeter\n[2]Apothem\n[3]Radius");
info = input.nextInt();
if (info == 1) {
System.out.println("What is the perimeter of the pentagon?");
double per = input.nextDouble();
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 2) {
System.out.println("What is the apothem of the pentagon?");
double apothem = input.nextDouble();
double per = apothem / .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 3) {
System.out.println("What is the radius of the pentagon?");
double rad = input.nextDouble();
double per = rad / .1701301617;
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
}
}
Due to the problem that all those decimals (ratio of apothem to perimeter) I had to figure out myself, I could only code a few useful ones.
If I knew how to use tangents, I could figure this out.
Ex: double apothem = length / tan(360/10/2)
(An apothem of a decagon)
Can someone show me how to code the previous line?
The recomended way would be to use java.lang.Math.tan(double a)
double apothem = 1 / java.lang.Math.tan( (2*java.lang.Math.PI)/(10*2))
unless there is some reason why you need extraordinary precision and this does not provide it. Then you may be able to find some third party alternative.
You're looking for the java.lang.Math class which has all the trig functions along with other useful constants like e and PI
So the apothem of a decagon where each side was length long and the equation = length/ 2 tan(180/ n) would be (after importing the Math class by putting at the top of your java file import java.lang.Math; )
EDIT
As user ajb points out, Math.tan() takes radians so you have to convert degrees to radians so you have to use toRadians() to convert from degrees to radians:
double apothem = length / (2 *Math.tan(Math.toRadians(180/10))
what I'm doing is this, I leave the 5' alone then I turn 3" into a decimal of a foot by dividing by 12 then I divide the numerator by the denominator then I add it all up and multiply by 1.414 it works but I dont know how I would display the foot inches and fraction of a inch
c2c_fdecimal = f_num / f_den;
c2c_fdeci_fft = c2c_fdecimal / 12.0;
deci_of_foot = inchs / 12.0;
total_travel= feet + c2c_fdeci_fft + deci_of_foot;
toff_ftodeci = tkoff_numa / tkoff_dena;
tkoff_inch = tkoff_inch / 12.0;
sub_toff = toff_ftodeci / 12.0 + tkoff_inch;
ans = (total_travel * ffformula) - sub_toff;
//print out measurement format
ansint = (int)ans;
strip_inches = (int) ((ans - ansint) * 12.0);
//print answer
editText2.setText(ansint + " ft" + strip_inches + " in");
Here's how you'd figure out the feet and inches in Java:
double resultInInches; // you start with inches...
int feet = (int)(resultInInches / 12);
double rest = (resultInInches / 12) - feet;
int wholeInches = (int)rest;
rest = rest-wholeInches; // rest now holds the fraction of the inch, eg 0.4141
Now all that's left to do is display rest as a fraction. I'm not familiar with what is or is not permitted in the Android SDK, and there's a bunch of ways to do this (see this answer).