Question:
help help help~?
♪£yricảl♪
2008-06-09 00:08:49 UTC
Identify 4 syntax errors in the following program. Write down the line number of the error and either describe or correct the error.

public class Compute BMI {
public static void main (String [ ] args) {
double weight, height;
float bmi;

weight = 55.0;
Height = 1.7;

bmi = weight / (height * height);

System.out.println ("BMI = " + bmi);

if bmi <= 19.5
System.out.println ("Underweight");
else if (bmi > 26.0)
System.out.println ("Overweight");
}
}

My answers:

1st error: the Height at line 9 should be small letter (that is, height because it has been declared (is it declare?) that it is height, not Height.)

2nd error: The System.out.println at line 13 should be placed at the end of the if and else if statement..(i don't know how to explain why that is so though..help? thanks)

What are the other 2 errors? I can't seem to spot them..o.o?

And, the first 2 errors that I have pointed out, are them correct? If they are not errors, do tell me..thanks
Six answers:
Mani
2008-06-09 00:19:18 UTC
Exceptions:

line no 1: (Class Name)The public type Compute must be defined in its own file

line no 7: Height cannot be resolved (Exactly what ever you mentioned above is correct)

line no 9: Type mismatch: cannot convert from double to float

line no 13: Syntax error on token "if", ( expected after this token.. (Your are right)



Corrected Program:

public class ComputeBMI {

public static void main(String[] args) {

double weight, height;

float bmi;



weight = 55.0;

height = 1.7;



bmi = (float) (weight / (height * height));



System.out.println("BMI = " + bmi);



if (bmi <= 19.5)

System.out.println("Underweight");

else if (bmi > 26.0)

System.out.println("Overweight");

}

}



Output must be:

BMI = 19.031141

Underweight
big_ombladon
2008-06-09 01:22:28 UTC
1st error it's : "public class Compute BMI {"

the correct class name it's ComputeBMI or Compute_BMI it's a syntax error class names must be typed in 1 word tough Compute_BMI it's considerate 1 word because of "_"

2and error it's:"bmi = weight / (height * height);"

you have bmi declared as float and weight,height as double

the float variables have 4 bytes and double variables have 8 bytes, you can't convert a double variable to a float one so either chance bmi to double or weight, height to float.

but it's possible to convert a float variable to a double one.

3th error and 4th errors are on that multiple if.

if syntax :

it(condition) instruction

else instruction

or it could be multiple instructions :

if(condition) {instructions}

else {instructions}

so your if should like this wey :

if( bmi <= 19.5)

System.out.println ("Underweight");

else {if (bmi > 26.0)

System.out.println ("Overweight");}
AnalProgrammer
2008-06-09 00:44:05 UTC
1. public class Compute BMI {

Line 1 should be public class ComputeBMI {

So the space in the name is wrong.



2. public static void main (String [ ] args) {

Line 2 should be public static void main (String args[]) {

I think the space in the [ ] may be wrong.



3. Height = 1.7;

Line 7 should be a lower case h.



4. if bmi <= 19.5

Line 13 Should be if (bmi <= 19.5)
Biggles
2008-06-09 00:22:23 UTC
i haven't done java in a very long time, but let me try.



Try NOT leaving a space between System.out.println and ("BMI = " + bmi);



And also change the declaration of bmi to double.

double bmi;



You don't need float.



Lastly, this one im not so confident about is the

public class Compute BMI



Shouldn't it be something else ? I can''t remember.
TooManySpammersHere
2008-06-09 00:40:22 UTC
As for the print at the end of the line - that is not necessary.

For a single statement after an IF, you can have the statement on the same line or any number of lines after since whitespace is ignored.



if (...) statement;



is the same as



if (...)

statement;



If you have more than one statement to execute when the if statement is true, you need brackets



if (...) {

statement1;

statement2;

}



and although yahoo cannot show it here, you would in all cases normally indent the statement 2-4 spaces under the if like



if (...)

..statement;
v b
2008-06-09 00:17:10 UTC
ohh god


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...