Task 2. 2.2 The main part of the program can use a FOR

Added on - 20 Sep 2019

  • 9

    pages

  • 750

    Words

  • 79

    Views

  • 0

    Downloads

Showing pages 1 to 3 of 9 pages
Task 22.2 The main part of the program can use a FOR repetition structure.a) Explain why a FOR repetition structure was chosen instead of a WHILE repetitionstructure.Even though a check has been performed to make sure that the variable HowFar is greaterthan 1 there could be inputs that might cause the program to terminate unexpectedly(crash).Ans: For loop is used because we know the exactly how many times for loop have to execute.This is decided by HowFar value. Until the HowFar variable value will remain the greater thanMyLoop variable value the loop will continue to execute. Whereas while loop is used where wedo not know exactly how many times loop will execute.b) Provide an example of an input that might cause the program to terminate and describea method that could be used to prevent this.Ans:If user input a string value instead of number then it will cause program to abruptlyterminate execution. This problem could resolve by the one method that is validateInput()method. First of all we read user input in string format and pass it to validateInput() method.Here is the code describes the all functioning to validate the user input.1.String inputVar = null;2.do{a.System.out.println(“Enter number: “);intputVar = sc.next();b.If(validateInput( String inputVar)){break;}else{System.out.println(“Invalid input”);c.}3.}while(true);4.double HorFar = double.parseDouble(inputVar);5.public Boolean validateInput(String inputVar){6.try{ double no = double.parseDouble(inputVar);if(no < 1){ retrun false; }i.return true;
}catch(NumberFormatException ex){return false;}7.}Task 3Design your application main screen using an event driven programming language. Mainscreen should have following features:a) Menu options such as Start new game, save output, Advance game, Clear, Help, QuitGame etc.b) A suitable logo or background of the main screen.c) A List box to show all the outputs.d) When ‘Start new game’ menu option is clicked all the output of your game (displayed inListBox Object) should move from ListBox to ComboBox.
desklib-logo
You’re reading a preview
card-image

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Download This Document