This bit of my program is supposed to calculate bottomAngle using cosine rule.
public double bottomAngle() {
topAngleinRadians = Math.toRadians(topAngle) ;
return (Math.cos(topAngleinRadians)(bottomAngle() = ladderLength^2 + floorLength^2 - verticalHeight^2) / 2 * ladderLength * floorLength) ;
}
Errors produced:
Here is my list of errors and I can't figure it out what's wrong with my formula. All the methods such verticalHeight , ladderLength works perfectly fine in other methods. There is something wrong with the way I put this formula. Can you please help me out?
Without seeing your list of errors, you do have syntax errors:
return (Math.cos(topAngleinRadians)(bottomAngle() = ladderLength^2 + floorLength^2 - verticalHeight^2) / 2 * ladderLength * floorLength);
You have no operator between your call to Math.cos() and the next part of your expression.
You're also appear to be assigning values to a function call, which doesn't make sense.
The ^ operator is also not the exponential operator, but a bitwise exclusive OR operator. You're probably looking for Math.pow().
Those are just what I'm seeing right off the top. It might be helpful to read up about the Java operators and how they are evaluated.
There's a few issues here.
Multiplication - unlike in regular algebra, you have to explicitly define that you want multiplication between two expressions Math.cos(topAngleinRadians)*...
Assignment - you appear to be trying to assign something to a method call (bottomAngle() = ...). This is not really something you can do, and I'm not really sure what you are trying to achieve by it.
Squaring - 10^2 does not square 10 into 100 in java, but is rather the XOR (exclusive OR) operator. You probably want to use Math.pow(ladderLength, 2) or simply ladderLength * ladderLength
Unlike algebraic notation, Java parentheses do not implicitly multiply.
You need to insert a * between )(.
Can't really understand the purpose of your return statement, but I would rather break the statement into 2-3 lines to make it more readable: -
public double bottomAngle() {
topAngleinRadians = Math.toRadians(topAngle) ;
double bottomAngle = Math.pow(ladderLength, 2) + Math.pow(floorLength, 2) -
Math.pow(verticalHeight, 2);
double denom = 2 * ladderLength * floorLength;
double numerator = bottomAngle * Math.cos(topAngleinRadians);
return numerator / denom ;
}
Note that, 3 ^ 2 does mean 3 squared in Java. You would need Math.pow method for that.
Also, you need to check why you were having bottomAngle() method call on LHS. I have assumed it to be a temp variable here.
As you can see, your code looks much more readable. And it becomes easy to find out compiler errors.
Your sytnax is incorrect. Parenthesis do not mean mutliplication, you need explicit * (multiplication operator). Also, you have some other mistakes:
Math.cos(topAngleinRadians)(bottomAngle() = ladderLength^2 + floorLength^2...
this looks like a method call i.e. bottomAngle() being set equal to some other expression, this is invalid also..
Related
Working on a Java class, its making me crazy because this expression is evaluating to zero, I need it to evaluate to a double, then round it down to the nearest int. So what Im trying to get is for days to be a whole number of days, yet when I run it through java it evaluates to 0. When I run it through my calculator it evaluates to the correct value. I would love a fix and an explanation to why this what I already have isn't working.
public int getEventDays(){
//variables
double daysCalc;
int days;
//logic
if (getStatus().equals("filling")){
//this is indented less to fit everything on one line, its not this way in
//the fractions are for unit conversion
daysCalc= Math.floor(((capacity-storage)/(inflow-outflow))*(43560)*(1/3600)*(1/24));
days = (int)daysCalc;
}
else if (getStatus().equals("emptying")){
//this is indented less to fit everything
//the fractions are for unit conversion
daysCalc=Math.floor(((storage-0)/(outflow-inflow))*(43560)*(1/3600)*(1/24));
days = (int)daysCalc;
}
else{
days = -1;
}
return days;
}
Change your code to this :
daysCalc = Math.floor(((storage-0)/(outflow-inflow))*(43560)*(1.0/3600)*(1.0/24));
Explanation:
The right hand expression is returning an integer value. In your case, 1/3600 is rounded to 0, similar to the case of 1/24.
Now by using 1.0 instead of 1, it is giving the unrounded float value of 1/3600.
Your problem is connected with the order of operations within your expression. The parentheses around 1/3600 and 1/24 cause these expressions to be evaluated first - and since each of these divisions has an expression of integer type on either side of the division, it's treated as an integer division. In other words, 1/3600 and 1/24 are both evaluated as integers, to give a result of zero. This means that your arithmetic includes a couple of multiplications by zero, which is why your result is zero.
The simplest fix is to understand that multiplying by the reciprocal of some number is the same as dividing by that number. In other words, you could simplify the calculation to
daysCalc = Math.floor( storage / ( outflow - inflow ) * 43560 / 3600 / 24 );
which will give the correct result, provided storage, outflow and inflow are not all integers.
On the other hand, if storage, outflow and inflow are all integers, then you'll need to make sure that the first division is also not treated as an integer division. You could do this by writing
daysCalc = Math.floor((double) storage / ( outflow - inflow ) * 43560 / 3600 / 24 );
which forces the division to be done with floating point arithmetic; and thereafter, each one of the divisions is done in floating point.
Sorry this might be a stupid question but Im trying to learn java and im currently really confused. Im currently learning the math.pow(); method and Im really confused to as what is going on. If i declare an integer called power which is equal to 2 and then use Math.pow(); and raise 2, to the power of 2 and print power i get 2. Am I printing Math.pow incorrectly? Thanks!
int power = 2;
Math.pow(2, power);
System.out.println(power);
You have to assign the result of the pow method to the power variable if you want it to contain that result:
power = Math.pow(2, power);
Otherwise the power variable remains unchanged.
You can store returned result (from Math.pow() method) in a variable and then print it.
int power = 2;
int result = Math.pow(2, power);
System.out.println(result);
Math.pow is a function that returns a value. You are discarding that. Rework to
int result = Math.pow(2, power);
Functions that do not modify their input parameters tend to lead to improved program stability. Java is very good at that.
Or if you don't want to store the result, you can use:
System.out.println(Math.pow(2, power));
It works, because the Math.pow() function returns a double value, and the println function gets it and writes it to your console
I've been reviewing my Java for a class I've taken for the whole year and haven't been doing well on. They have a separate review book called "Be Prepared." I want to see if I get the math behind this line.
result = (1 / 2) * n * (n + 1); // result is 0.0
The thing is, this is basic basic basic. I need to know if I'm actually getting this. It looks like my like (1/2) is 0.5. That cast to an int is 0. That's why the whole thing is 0.0.
Am I right?
This book is how you get ready for the AP test. Anyone done eimacs who can help?
(1 / 2) will return zero. both are integer and as per integer calculation it will return zero.
Try
result = (1.0 / 2) * n * (n + 1);
Please have a look at :
Division of integers in Java
How do math equations work in Java?
In Java the result of each operation is decided by the higher type involved in calculation. It doesn't matter in what type are you assigning the result.
for e.g
double d = 10/3;
the value of d will be 3.0 only not 3.33.
Yes, and note that 999 / 1000 will also return 0. It's truncation, not rounding down.
Suppose, I need to calculate 3 * (2 / 3) = 2 but 3 * (2 / 3) = 0 in integer arithmetic. So, it looks like I should use floating-point arithmetic 3 * (2.0 / 3) = 2.0 and cast the floating point result to int.
Does it make sense? How would you avoid casting here?
In this particular example, you may be better off rearranging the expression as 3 * 2 / 3, or as 3 / 3 * 2. This way you'll get the exact result using integer math alone.
Using floating-point math is the more general solution, but you need to be aware of rounding issues. In general, you shouldn't expect that x * (y / x) would give you exactly y when x and y are floating-point numbers.
If you know that the result is an integer, or simply want to round it to the nearest integer, you could use Math.round():
Math.round(3 * (2f / 3))
Simply casting the result to int is not a good idea since floating-point numbers are inexact, and such a cast would simply truncate the number.
You can't avoid casting. You can either use integer division or you can use full division but then arguments and result will be in floating point.
You can try moving around the expression parts to bring out the integer division to the top level. In your case you could transform 3 * (2 / 3) to (3 * 2) / 3 which will result in 2 without any floating point operations.
* and / have the same precedence, so omitting the parentheses will provide you with a correct result of the example.
Casting (or truncating) is unavoidable otherwise.
Personally I would re-arrange the equation to be:
3 / 3 * 2 = 2
But not sure if thats what you need. You could try doing the calculation on float's and then use round() to round it up or down to the nearest integar.
This is because of the integer division. In these kinds of expressions you are better off using float, yes.
You can avoid the explicit casting only if you are using the compound operator. The compound operator does an automatic cast for you.
int a = 3;
a *= (2.0/3);
System.out.println(a);
What exactly is it you want to achieve? Using integer arithmetic the result depends on where you set you parentheses. (a/b) * (c/d) * (e/f) will, as you noticed yourself, in general result in something different than (a*c*d) / (b*d*f), even though mathematically both expressions are equal. Nontheless both expressions may be correct depending on the task you want to solve. I do miss a clear statement of this.
If you are looking for the integer closest to the mathematically correct value just calculate nominator and denominator seperately and then perform the division. This way intermediate errors do not occur. If not you need to reformulate your question.
I'm porting some code over from Processing to Java, and one issue I've come across is that processing's precompiler turns any doubles to floats. In Eclipse however, I've had to explicitly cast my values as float. Still though, I get errors that I don't understand. For instance, shouldn't putting an f at the end of this statement fix the type mismatch (Type mismatch: cannot convert from double to
float)?
springing[n] = .05*(.17*(n+1))f;
And even on simpler statements like this I get a type mismatch. What am I doing wrong?
float theta = .01f;
Well, your second statement is correct by Java's standards, but in your first example Java is probably trying to prevent you from converting doubles to floats due to a loss of precision that must be explicitly asked for by the programmer, as so:
double a = //some double;
float b = (float) a; //b will lose some of a's precision
According the Java grammar, the f suffix is only applicable to float literals. Your second statement should work. The first however is an expression and therefore requires a cast:
springing[n] = (float)(.05*(.17*(n+1)));
In your first example, the f suffix is only valid directly on literals, not after a whole expression. So write it in either of those ways (assuming springing is a float[]):
springing[n] = .05f*(.17f*(n+1));
springing[n] = (float)( .05*(.17*(n+1)));
The first does the whole calculation (apart from the n+1 part) in float, the second one calculates in double and then converts only the result to float.
(And in both cases, the parenthesis between .05 and .17 (and the matching one) is usually superfluous, since multiplication is associative. It might do some difference for really big values of n, but in these cases you would usually want the other way to avoid overflow.)