Q3: Below fragments of code, using your knowledge to figure the following: [4 Po
ID: 3670897 • Letter: Q
Question
Q3: Below fragments of code, using your knowledge to figure the following: [4 Points]
Find the error and specify the type. (you can use different color or highlight the errors)
What the cause of this error?
Correct errors.
---------------------------------------------------------
/**
* this program will read input from user
* then, it'll print the number if it is
* greater than 10, else it'll
* print a message says your entry
* is less 10
*/
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Please insert"+
" a number:");
double x = in.nextDouble();
if(x>10)
System.out.println("you'r number"+
" is greater than 10");
System.out.println("you'r number is"+
" less than or equals 10");
}
}
-------------------------------------------------------------------------
/**
* This program declare and initialize two variable
* Then print the sum
*/
public class MyClass {
public static void main(String args[]) {
int x = 4;
int y = 5;
int x = 10;
System.out.println(" x+y = "+(x+Y));
}
}
---------------------------------------------------------
/**
* This program declare and initialize two variable
* Then print the sum
*/
public class MyClass {
public final int x = 3;
public static void main(String args[]) {
int y = 5;
System.out.println(" x+y = "+(x+Y));
}
}
Explanation / Answer
Below are the corrected codes :
---------------------------------------------------------
/**
* this program will read input from user
* then, it'll print the number if it is
* greater than 10, else it'll
* print a message says your entry
* is less 10
*/
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Please insert"+
" a number:");
double x = in.nextDouble();
if(x>10)
System.out.println("you'r number"+
" is greater than 10");
else // else case is missing
System.out.println("you'r number is"+
" less than or equals 10");
}
}
-------------------------------------------------------------------------
/**
* This program declare and initialize two variable
* Then print the sum
*/
public class MyClass {
public static void main(String args[]) {
int x = 4;
int y = 5;
// int x = 10; x is initialized 2 times
System.out.println(" x+y = "+(x+Y)); // y must be small
}
}
---------------------------------------------------------
/**
* This program declare and initialize two variable
* Then print the sum
*/
public class MyClass {
public final int x = 3;
public static void main(String args[]) {
int y = 5;
System.out.println(" x+y = "+(x+Y)); // x is not initialized and y must be small
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.