hello guys i am a fifty years old man and i am doing IT degree. i am having a exam in next Tuesday, but there are still some question in sample exam paper that i could figure out. hope some one can help me out.
i) Define the term "logic error". Provide an example of a logic error and explain how a logic error can be found. [2 marks]
ii) The following segment of code is supposed to validate that a user inputs either “y” or “n” when prompted to continue the program. The program does not contain any syntax errors. However, when the program is tested, the loop does not terminate. In your answer booklet, state the line(s) in which the error(s) occur(s), copy that (those) line(s) and circle the error(s). Write the correct code for only the line(s) with error(s).
1. System.out.print("Do you wish to continue (y/n)? ");
2. String answer = in.next();
4. while(!answer.equalsIgnoreCase("y") && answer.equalsIgnoreCase("n"));
6. System.out.print("ERROR! Answer must be either y or n: ");
8. answer = in.next();
The requirement is to check that the user typed a y or n. But the existing loop does not let the user type a new value after typing a bad one. So the existing line 8, which prompts for a new value, needs to be moved back inside the loop.
Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.