Programming Assignment 1

Verified

Added on  2019/09/20

|9
|750
|196
Practical Assignment
AI Summary
This practical assignment focuses on implementing a FizzBuzz program in Java. It involves choosing between for and while loops, handling potential errors from invalid user input (e.g., strings instead of numbers), and designing a main screen using event-driven programming. The assignment includes testing the program with various inputs (including valid and invalid ones), comparing expected and actual results, and documenting discrepancies. The provided solution demonstrates the implementation of the FizzBuzz logic using a for loop, explains the choice of a for loop over a while loop, provides a method for input validation, and outlines the design of the main screen with menu options, a logo, a list box, and a combo box. The solution also includes test cases with inputs 18 and -1, along with an analysis of the results and a table comparing expected and actual outputs for various inputs.
Document Page
Task 2
2.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 repetition
structure.
Even though a check has been performed to make sure that the variable HowFar is greater
than 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 than
MyLoop variable value the loop will continue to execute. Whereas while loop is used where we
do not know exactly how many times loop will execute.
b) Provide an example of an input that might cause the program to terminate and describe
a method that could be used to prevent this.
Ans: If user input a string value instead of number then it will cause program to abruptly
terminate 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;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}catch(NumberFormatException ex){
return false;
}
7. }
Task 3
Design your application main screen using an event driven programming language. Main
screen should have following features:
a) Menu options such as Start new game, save output, Advance game, Clear, Help, Quit
Game 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 in
ListBox Object) should move from ListBox to ComboBox.
Document Page
Document Page
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Document Page
Document Page
Task4
Write a program that implements the pseudo-code as shown in Figure 1.
1. import java.util.Scanner;
2.
3.
4. public class Test {
5.
6. public static void main(String args[]) {
7.
8. Scanner sc = new Scanner(System.in);
9. System.out.println("How far to count?");
10. double HowFar = sc.nextDouble();
11. while (HowFar < 1) {
12. System.out.println("Not a valid number, please try again.");
13. HowFar = sc.nextDouble();
14. }
15. for (double MyLoop = 1; MyLoop < HowFar; MyLoop++) {
16. if (MyLoop % 3 == 0 && MyLoop % 5 == 0) {
17. System.out.println("FizzBuzz");
18. } else if (MyLoop % 3 == 0) {
19. System.out.println("Fizz");
20. } else if (MyLoop
21.
22. % 5 == 0) {
23. System.out.println("Buzz");
24. } else {
25. System.out.println(MyLoop);
26. }// end of else
27.
28. } // end of for loop
29. }// end of main
30.
31. }
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Task 5
5.1 a) Test the program by showing the result of entering a value of 18 when prompted by
the program.
Ans:
b) Test the program by showing the result of entering a value of -1 when prompted by the
program.
Ans:
Document Page
5.2 Analyze actual test results against expected results to identify discrepancies, take screen
shots where necessary and explain them. You can draw a table of two columns and in the
first Colum you can write expected results and on in the second Colum write the actual
results.
Input Expected output Actual input
“str” “number format
exception”
Same as expected
-2 Invalid input Same as expected
10 10
1.0
2.0
Fizz
4.0
Buzz
Fizz
7.0
8.0
Fizz
Same as expected
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]