In: Computer Science
Identify errors and correct them of the following Java program: 1. import java.utility.Random; 2. 3. public class Sequen 4. 5. private int stand 6. private int calc; 7. 8. public Sequen(int numStand) 9. { 10. stand = numStand; 11. skip; 12. } 13. 14. public void skip() 15. { 16. Random sit = new Random(); 17. calc = sit.nextInt(stand) + 1; 18. } 19. 20. public getStand() 21. { 22. return stand; 23. } 24. 25. int getCalc() 26. { 27. return calc; 28. } 29. } One error is already identified for you as shown below: Line Number: 5 Error description: Missing semicolon Statement after correction: private int stand; Identify 5 other errors and give their Line Number, Error description and Statement after correction.
Ans:
1. Line number 3
Error description: '{' expected (open curly bracket missing).
Statement after correction: public class Sequen{
2.Line number 5
Error description : ";" (semicolon) expected.
Statement after correction:private int stand;
3.Line Number 11
Error description : not a statement (skip is a function hence we need to use () while invoking)
Statement after correction: skip();
4.Line Number 20
Error description:invalid method declaration; return type required( we need to specify return type while defining a function)
Statement after correction: public int getStand() ( since function returns value stored in stand which is of type int)
5.Line Number 1
Error description : package java.utility does not exist ( It is actually java.util)
Statement after correction: import java.util.Random;
Approach
Run the java code in any IDE or online java compiler and check for errors, one by one resolve the error until no error is shown (Keep in mind that the error shown at first compiling may not be correct you might get more after resolving them)
code after correcting.
import java.util.Random;
public class Sequen{
private int stand;
private int calc;
public Sequen(int numStand)
{
stand = numStand;
skip();
}
public void skip()
{
Random sit = new Random();
calc = sit.nextInt(stand) + 1;
}
public int getStand()
{
return stand;
}
int getCalc()
{
return calc;
}
}
code screenshot
comment if you have any doubt.