Variable might not have been initialized? Error - java

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;

Related

How to convert radians to degrees

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);
}

Math formula not returning correct answer

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.

Precision in String

I have the following code:
String price="9000.89";
double basePrice =10000;
String dis= String.valueOf(((basePrice - Double
.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
It is giving me the output as "9.991100000000007", but i need it in 2 decimal points. Any idea how to do that?
Expected output : 9.99
String price="9000.89";
double basePrice =10000;
String dis = String.format("%.2f",((basePrice - Double.parseDouble(price)) * 100 / basePrice));
System.out.println(dis);
I would recommend
double dis = ((basePrice - Double.parseDouble(price)) * 100 / basePrice);
System.out.format("%1.2f", dis);
You can round using:
Math.round(number * 100.0) / 100.0;

My separate method keeps returning the wrong value

I'm using my weight as a value of 180 to find my weight in lbs on planets other than earth with a formula to find weight using mass and surface gravity. The problem I'm facing is that whenever I call this method, the weight value returns as 180 all 8 times the method is called. In my code I have weight being calculated with (mass * roundedSG)/433... It shouldn't return as 180. I don't know how to fix this, I've been trying for a couple hours. The for loop is in a different method, I'm doing this for home work by the way, any help would be appreciated to just try and fix this problem of mine. Thanks!
public static double weight(double sGravity, double w)
{
double roundedSG = sGravity / 10;
double mass = (w * 433.59237)/roundedSG;
double weight = (mass * roundedSG)/433.59237;
return weight;
}
for(int i = 0; i < planetSurfaceGravity.length; i++)
{
weightOnPlanets[i] = weight(planetSurfaceGravity[i], weightLbs);
System.out.println(weightOnPlanets[i]);
}
}
I think your math in weight() is incorrect. It looks like weight is actually returning the following:
(w * 433/roundedSG)*roundedSG/433.
The 433s and roundedSGs cancel, and you're just returning w, which I'm guessing is 180?
I renamed your variables and added a constant which makes the code as written a bit clearer:
private static final double GRAMS_PER_POUND = 433.59237;
public static double weight(double sGravity, double weightLbs)
{
double roundedSG = sGravity / 10;
double mass = (weightLbs * GRAMS_PER_POUND)/roundedSG;
double weight = (mass * roundedSG)/GRAMS_PER_POUND;
return weight;
}
So your mass on earth, which is converted to grams, is being divided by the gravity of whatever planet you're checking. Change the mass calculation to be
double mass = weightLbs * GRAMS_PER_POUND;
Then the weight calculation should be ok as is, returning the weight on the other planet converted back from grams to pounds.
You weight method calculation are incorrect. the result of it returns only w which you pass as weightLbs (= 180).
Correct Weight Formula is:
weight = (mass*gravity);
Change from:
double weight = (mass * roundedSG)/433.59237;
To:
double weight = mass * roundedSG;

Java sphere volume calculation

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);

Categories