I have to write a method called CalcTriangleArea | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have to write a method called CalcTriangleArea

It takes two decimals as parameters (double) for the triangel base and height. The me th of should calculate the figure area and return resultat as a double My code in java public double calcTriangleAre(double base,double height){ result= double area; area = (double)base*height /2 Return result; } Error: row 3 double area class expected

10th Jan 2023, 1:59 AM
Melissa 123
Melissa 123 - avatar
3 Answers
+ 5
Your method is already receiving two numbers with "double" type. no need to use typecasting here. To make it simple, you don't even need to declare any local variables, because the calculation is very short. Just return the value of the expression like this. public double calcTriangleAre(double base, double height) { return base * height / 2; } If you need to temporaryly store some parts of a calculation, review how a variable is declared, first specify the type, then the name, then you can assign the value with = operator. Then your function can return this variable. double area = base * height / 2; return area;
10th Jan 2023, 7:28 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Java is statically typed so it requires data type. Row 3 is not a valid syntax. On row 4, you should specify data type for area. Replace Return with return (keyword, case sensitive) Tip: You can delete "result= " from line 3 and simply return area instead of result.
10th Jan 2023, 2:57 AM
Sanjaykumar Rathod
0
Thank you your code was right tibor thanks
12th Jan 2023, 1:10 AM
Melissa 123
Melissa 123 - avatar