How to calculate gravitational force? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am getting the wrong output for the code below. G = 6.674 x 10^-11. I am supposed to get 3.559466666666664e+22 but I am getting 8.0088E44. Can someone please explain what I have wrong on my code? I will appreciate it. The inputs are 2e30 6e24 1.5e11
import stdlib.StdOut;
public class GraviForce {
public static void main(String[] args) {
double m1 = Double.parseDouble(args [0]);
double m2 = Double.parseDouble(args [1]);
double r = Double.parseDouble(args [2]);
double G = 6.674e-11;
double f = G * (m1 * m2) / r * r;
StdOut.println(f);

A simple operator associativity mistake - missing parens:
double f = G * (m1 * m2) / (r * r);

Related

Java Double formatting String output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Not suitable for this site
Improve this question
I'm having some problem formatting decimals of a double. The only error is:
String output = DecimalFormat = (((P * t) / 360) * 0.025);
DecimalFormat df = new DecimalFormat(" ###,###.00");
String output = DecimalFormat = (((P * t) / 360) * 0.025);
DecimalFormat df = new DecimalFormat("###,###.00");
String output = df.format(((30 * 45) / 360) * 0.025);
In your second line, you have DecimalFormat = which should be replaced with df.format The DecimalFormat class doesn't let you format decimals, it's the df object that you created which can format decimals by calling the format method.

Trouble converting exponent equation into Java code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I need to convert a=n×s^2 into Java. My code looks something like this:
public float ABC( float n , float s)
{
a = n*s;
return a*a;
}
However if i print this out it returns the following error:
This method must return a result of type float
because of this I don't think my solution is correct. Would anyone be able to offer any solutions? I'm very new to Java.
You should declare the type of the variable a:
public static float ABC( float n , float s) {
float a = n * s;
return a * a;
}
You need to declare the type for a, and since you are not using any instance variables, you can make the method static.
Furthermore, you could pass the result to Math.pow and cast the resulting double to a float.
public class Question64613567 {
public static void main(String[] args) {
System.out.println(abc(2, 3)); // 36.0
System.out.println(abc2(2, 3)); // 36.0
}
public static float abc(float n, float s) {
float a = n * s;
return a * a;
}
public static float abc2(float n, float s) {
return (float) Math.pow(n * s, 2);
}
}

Java error: illegal start of expression in my code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm getting the error: illegal start of expression and don't know how to fix this. This is all declarations for a program I need to write with guidelines. The program is meant to calculate the cost of a boat. Thanks in advance!
import java.util.Scanner;
public class boat
{
public static void main(String[] args)
{
//declarations
....
double depreciationYear1 = bookValueBeginningYear1 * (2 * 100% / 3);
^
double bookValueBeginningYear2 = bookValueBeginningYear1 - depreciationYear1;
double depreciationYear2 = bookValueBeginningYear2 * (2 * 100% / 3);
^
double bookValueBeginningYear3 = bookValueBeginningYear2 - depreciationYear2;
double depreciationYear3 = bookValueBeginningYear3 * (2 * 100% / 3);
.... ^
double exciseTaxYear1 = 90% * boatPrice/1000 * 25;
^
double exciseTaxYear2 = 80% * boatPrice/1000 * 25;
^
double exciseTaxYear3 = 70% * boatPrice/1000 * 25;
.... ^
double insuranceYear1 = boatPrice * 1% + bookValueBeginningYear1* 3%;
^
double insuranceYear2 = boatPrice * 1% + bookValueBeginningYear2* 3%;
^
double insuranceYear3 = boatPrice * 1% + bookValueBeginningYear3* 3%;
.... ^
(2 * 100% / 3);
instead use:
(2 * 100 / 3); // Just remove the % from all the statement.
Because you are using two operator (%, /) at once.
This is the error into all the statement in the code, which you have marked.
The % is a modulus operator in java, which is used for calculating remainders of two number like 10%4 is 2. You might be getting confused by considering it as percentage.
In Java
% Modulus operator
Divides left hand operand by right hand operand and returns remainder
Example: B % A will give 0
/ Division operator
Divides left hand operand by right hand operand
Example: B / A will give 2
So in your case 2 * 100 % / 3
doesn't make any sense hence it is illegal
Have a quick look here to know the basic java operators

What does *= mean in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm going through a tutorial and I found this operator but I'm not sure what it does.
int number = Integer.parseInt(tfInput.getText());
number *= number;
tfResult.setText(number + "");
Thanks.
a *= b; is equivalent to a = a * b;
You're probably (maybe?) familiar with the += operator. There is a similar operator for all the basic math functions.
+=: a += b; is equivalent to a = a + b;
-=: a -= b; is equivalent to a = a - b;
*=: a *= b; is equivalent to a = a * b;
/=: a /= b; is equivalent to a = a / b;
%=: a %= b; is equivalent to a = a % b;
And please make note of #ruakh's comment:
Note that a *= b evaluates a only once, whereas a = a * b evaluates it
twice. (That doesn't make a difference if a is just a variable or
field name, but if it's a more complicated expression, such as f().x
or f.g.x, that can matter a great deal.)

find sin-1 of user input for ladder hight [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to find the sin of a number that the user inputs say for eg 1.5. I've done it on the calculator and it works but the code is not working.
Here is the code:
package msd1;
import java.util.Scanner;
public class Part3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
double Height = scanner.nextDouble();
double Angle = Height/2;
double asine = Math.asin(Angle);
System.out.println("Arcsine of " + Angle + " = " + asine);
}
}
Your variable names make no sense. A "height" is a length, and dividing a length by 2 doesn't give you an "angle". Furthermore, you don't pass an "angle" to asin, you pass it a number from -1 to +1 and it returns an angle.
In your case, you'd want to want to take the height of the ladder and divide it by its length to give you your sin (between -1 and +1), then take the asin of that value.
Odds are you also want to take the angle returned by asin in radians and convert to degrees.
You might have code like this:
double lengthOfLadder = 2.0;
double height = scanner.nextDouble();
double sine = height / lengthOfLadder;
double angleInRadians = Math.asin(sine);
double angleInDegrees = angleInRadians / Math.PI * 180;

Categories

Resources