In: Computer Science
The following program supposes to calculate the area of a rectangle and square. Then, produce the following output.
Area of Rectangle:
50
Unfortunately, the program has compile-time and run-time errors that prevent the program from running and producing the correct result. Using table 3.1 below, allocate the error(s) on each program line.
1 public class RectangleArea {
2 Public static void main() {
3 int width == 10;
4 int height = 5;
5 int recArea = width + height;
6 System.Out.print(Area of
Rectangle:);
7 System.out.println(“recArea”)
8
9
Note:
Some of the answers are given to you as an example.
Table 3.1: compile-time / run-time error
Line No |
compile-time / run-time error |
1 |
No error |
2 |
Error.
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
Here is the correct program that will print the output to the screen as defined in the question.
public class RectangleArea {
public static void main(String args[]) {
int width = 10;
int height = 5;
int recArea = width * height;
System.out.println("Area of Rectangle:");
System.out.println(recArea);
}
}
SCREENSHOT OF THE OUTPUT AND THE CODE :
ERROR IN YOUR PROGRAM WITH EXPLIANATION:
public class RectangleArea {
Public static void main() { //This line encounters an error as the
keyword public "P" should be small and the //String args[ ]
statement is missing here.
int width == 10; //here we need to assign but not to the compare
but we are using comparision operators.So we // need to use
assigment operator "=".
int height = 5; //here no error
int recArea = width + height; //here we need to use the
formula(l*b) but (l+b) will not result ot the area.
System.Out.print(Area of Rectangle:); //here the syntax is not
correct and the correct syntax is //"System.out.println" and also
there is no variable Area of Rectangle //and we need to write it in
" " as it will print to the output.
System.out.println(“recArea”) //here we show not use " " as we need
to print the value of the //variable. and termination "semicolon "
is missing here.
//and the cosing braces need to be here there should be 2 closing
braces.