Java error: illegal start of expression in my code [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 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

Related

Why does result of Math.pow() and * differ while calculating aswers in java ? and how do java calculate math in run-time memory?

I was trying to execute simple java program to calculate result with expression as: v^2 - u^2 / 2as
that is v*v - u*u / 2*a*s
code is in java 11
int v=16;
int u =5;
int a = 7;
int s = 9;
int res1 = v*v;
int res2 = u*u;
double FunRes1 = Math.pow(v, 2);
double FunRes2 = Math.pow(u, 2);
int part1 = res1 - res2;
int part2 = 2 *a*s;
int result = part1/part2; // = All 4
int AllResult = (v*v-u*u)/2*a*s; // == results
double doubleResult = FunRes1-FunRes2 / 2*a*s; // === have different
double doubleResult2 = (FunRes1-FunRes2) / 2*a*s; // ==== answers (see ss above)
the asnwers of all 4 variable ( result , AllResult , doubleResult1 , doubleResult2 ) are different .
Anyone explain why this happen ?
and What is the correct answers mathematically ?
This is because of operator precedence. If I write something like 2 * 8 / 8 - 6, without further context, it is ambiguous how it should be evaluated. This can lead to different results. For example (2 * 8) / (8 - 6) == 8 but ((2 * 8) / 8) - 6 == -4. To disambiguate, Java uses a list of precedence rules that are common throughout most languages. You can find the complete list here.
For your case the important part is that multiplication and division are applied before addition and subtraction.
Also of note is what happens in the case of operators having equal precedence, which also appears in your example.
When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
Going back to our example of 2 * 8 / 8 - 6. We can see (from here) that java will evaluate multiplication and division before subtraction. * and / have the same precedence and are both binary operators so we evaluate them left to right. This leaves us with ((2 * 8) / 8) - 6 which is why java evaluates this as -4
System.out.println(2 * 8 / 8 - 6);
-4

Java Triangle Class [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 5 years ago.
Improve this question
I am new to object and classes. I am creating this test program to get the area of the triangle. I kept getting 0 as the area. I have no idea where did I get it wrong.
public class Project1 {
public static void main(String args[]) {
Triangle triangle1 = new Triangle();
System.out.println("The area of the triangle with base "
+ triangle1.base + "and with width "
+ triangle1.width + " is " + triangle1.getArea());
}
}
class Triangle {
double base = 1.0;
double width = 1.0;
double getArea() {
return 1 / 2 * base * width;
}
}
Change the following
double getArea() {
return 1/2 * base * width;
}
To
double getArea() {
return 0.5 * base * width;
}
Due to integer division 1/2 yields 0.
try to use double numbers at getArea() method something like this:
double getArea() {
return 1.0 / 2.0 * base * width;
}
Explanation
You are computing 1 / 2.
This is integer division which does not support decimal values. It always rounds towards zero, the result is thus
1 / 2 = 0
Because of that the computation gets 0 too.
Solution
You can fix it by dividing decimal values instead of integers:
return 1.0 / 2.0 * base * width;
There is no integer division as soon as one of the operands is a decimal value like a double or float. You indicate double by adding the dot like 2.0.
Alternatively you could use the decimal value 0.5 right from the start:
return 0.5 * base * width;
Note that both versions are equally fast since the compiler will pre-compute such constant computations at compile-time. The bytecode will thus have 0.5 for both versions.
It is very common mistake for Java beginners, and I admit I sometimes still make this mistake. When divide one integer by another integer in Java, the result will be an integer as well. So while you expect 1 / 2 will be 0.5, the result will be 0 (i.e. it is truncated). You could cast the number first to force the division to use float or double like:
1 / (float) 2
1 / (double) 2
Or use the shorthand:
1 / 2f
1 / 2d
where f cast the number before it to float, and d to double.

Java/Pawn Maths formula returns 0

could someone explain me what am I doing wrong here, and how can I make this formula to be working? right now it returns 0 in Java and Pawn. But it works in PHP, which isnt making much sense for me.
int test = (65 / 65 / (1 + 25) * 10000);
In Java, integer division will truncate the results. For example, 5/2 will truncate 2.5 to 2.
To ensure you're using float with numeric constants, add a .0 on the end, as in:
int test = (65.0 / 65.0 / (1.0 + 25.0) * 10000.0);
Java operations for multiplication and division is left to right, so your expression is really:
(((65 / 65) / 26) * 1000) // Clarifying parenthesis
((1 / 26) * 1000)
(0 * 1000) // integer division!
0
To avoid this, you just need to ensure the operations are casted to a double.
The simple way would be just changing the first value to a double, either by:
int test = (65D / 65 / (1 + 25) * 10000);
Or
int test = (65.0 / 65 / (1 + 25) * 10000);
However, if you are refactoring your code later, you might want to change more than just the first value.

Which is wrong? Java, Maths or Me...?? Confused...!

I've the below SOP:
System.out.println(9 - ((9 / 2) * 2));
From the maths that I learnt in school, it should evaluate to 0.
But I'm getting the output as 1...!!!
I've tried evaluating the same expression in Google the output is 0.
Java evaluates to 1 while according to Maths, it is evaluated to 0. Any explanation?
You're victim of integer division !
Dividing integers in a computer program requires special care. Some
programming languages, treat integer division (i.e by giving the
integer quotient as the answer). So the answer is an integer.
Some logic :
In Java :
9/2 = 4
4*2 = 8
9-8 = 1
In real life :
9/2 = 4.5
4.5*2 = 9
9-9 = 0
To avoid that you can cast one of the argument to double in your division :
System.out.println(9 - (((double)9 / 2) * 2));
Integer division is the culprit here. (9/2) gives 4. And therefore, (9 - (4 * 2)) would evaluate to 1.
Step-by-step:
(9 - ((9 / 2) * 2));
(9 - ((4) * 2)); // integer division
(9 - (8));
1 // final result
9 / 2 is integer division. It evaluates to 4.
You are facing common issue of Integer division.
9 - ((9 / 2) * 2)// here first 9/2=4 (int division)
Now 9-(4*2)=1
That's why you are getting 1
It is done in integer mathematics (see ch. 5.6.2 in Java Language Specification) so:
9 / 2 is = 4
then 4 * 2 is = 8
then 9 - 8 = 1
If you want 0 then do
public class Dec
{
public static void main(String[] args)
{
System.out.println(9.0 - ((9.0 / 2.0) * 2.0));
}
}
There is a short chapter for understanding of "Binary Numeric conversion" located in Java Language specification.

Using Math.pow() function

I'm trying to make a program in Java to calculate the formula for the Ricker wavelet:
But the results are not matching to the real ones.
This is what I'm using:
private static double rickerWavelet(double t, double f){
double p = (Math.pow(Math.PI, 2))*(Math.pow(f, 2))*(Math.pow(t, 2));
double lnRicker = 1 - (2 * p) * Math.exp(-p);
return lnRicker;
}
Am I using the Math functions wrongly?
To match the formula,
double lnRicker = 1 - (2 * p) * Math.exp(-p);
needs to be
double lnRicker = (1 - (2 * p)) * Math.exp(-p);
Since * has higher operator precedence than -, in your expression the multiplication of (2 * p) with Math.exp(-p) will be done first, which is not what you want.
I'd just like to add that Math.pow(x, 2) can be written more simply (and possibly more accurately and more efficiently) as x * x ... for any variable or constant x.
Look at your executing equation if you know about BODMAS method:
You should do: (1-(2*p))* Math.exp(-p);
I just changed your equation by inserting round brackets around 1-2*p..

Categories

Resources