The following program contains 9 mistakes What are they Copy

The following program contains 9 mistakes! What are they? Copy and paste the following Jgrasp/Eclipse and correct the various mistakes. public class Oops {public static void main(String[] args) {int x; System.out.println(x is\" x); int x= 15.2;//set x to 15.2 System.out.println(x is now + x\"); int y;//set y to 1 more than x y = int x +1; System.out.println(x and y are \" + x + and + y);}}

Solution

public class Oops {
   public static void main (String[] args) {
   int x;
   System.out.println(\"x is x\");   
   int x= 15.2;
   System.out.println(\"x is now + x\");
   int y;
   y = int x+1;
   System.out.println(\"x and y are \" + x + and +y);
   }
}

Errors:

In the line 5 , the value 15.2 is type casted into integer type as x= (int) 15.2;

In the line 6 , System.out.println(\"x is now + x\");

The Print statement is given wrong, to print the value of x it must be written as ,

System.out.println(\"x is now\" + x);

In the line 8 , \'x\' is already declared as of int type in program , hence it should be written as, y = x+1;

In the line 9 , the print statement must be written as System.out.println(\"x and y are \" + x + \" and \" +y); , to print the values of x and y .

The corrected program is given below with output :

public class Oops {
   public static void main (String[] args) {
   int x;
   System.out.println(\"x is x\");

x= (int) 15.2;
  
   System.out.println(\"x is now \" + x);
   int y;
   y = x+1;
   System.out.println(\"x and y are \" + x + \" and \" +y);
   }
}

Output :

x is x
x is now 15
x and y are 15 and 16

 The following program contains 9 mistakes! What are they? Copy and paste the following Jgrasp/Eclipse and correct the various mistakes. public class Oops {publ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site