Mock test review Revision of Activity Diagrams for Loops, and Dry Run Example with “for” Loop
First – revision of loops Activity Diagrams need to include a diamond shape for questions if statements, or conditions controlling a loop [loop condition FALSE] [loop condition TRUE] [loop condition FALSE] [loop condition TRUE]
Activity Diagram for “do…while” The test is made after the loop body not before as in the while loop. This means the loop body is certain to be performed at least once. Loop Body [loop condition TRUE] [loop condition FALSE] Statement After Loop
Statement(s) After Loop Activity Diagram for “while” The test is made before the loop body not after as in the do … while loop [loop condition FALSE] This means the loop body may not be performed at all (if the condition is false the first time through he loop. [loop condition TRUE] Loop Body Statement(s) After Loop
Before next week, you need to complete the debugging and dry run on WOLF and then answer the questions in preparation for your test For the test, fill out the paper copy first and then answer the electronic test on Wolf If you have any difficulty, or don’t get the answers you expect, please ask for help next week during the tutorial or workshop.
Test 1 format Test 1 which is NOT open book consists of: - A dry run exercise which is worth 30 marks of your overall module assessment result. Twenty Jafa type questions worth a total of 10 marks. Test 1 has a maximum of 40 marks. Test 1 will be held on Thursday November 27th for most people. Anyone entitled to extra time for any reason will have their test on Tuesday morning starting at 10:00 am in MI214. Part-time students will have their test on Monday 24th starting at 10:00 am in MA112.
How the portfolio grade is calculated You must pass the on-line Jafa by answering at least 60% of the questions correctly. This does not affect your overall portfolio result, but if you don’t pass Jafa you cannot pass the module. Test 1 is worth 40% of the marks in total. Test 2 which is held in assessment week (programming exercises) is worth 60% of the marks in total.
Why test dry runs? By now, we expect you to be able to understand code, even if you can’t always make it compile and don’t always understand the errors you see. By asking you to debug, trace through code and explain code, we can check your overall understanding. The final test is a ‘live’ test of your programming skills; you will be required to design, code, and run programs under test conditions.
The test questions are shuffled and randomly allocated, so we don’t know in advance which student will get which questions. Today, we’re going to look at some of the questions involving loops to give you a bit of extra practice.
A simple for loop public class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ // for integer i equals one // while i is less than eleven // at each repeat add one to i System.out.println("Count is: " + i); } Straightforward, but it might be worth writing it up – ouputting 1 then separate line 2, etc, showing how it returns to the beginning of the loop each time. The first value for i is 1. The first output is: Count is: 1 The last output is Count is: 10 Why is the last output not Count is: 11 ? What value does i begin with? What is the first output? What is the last output?
Example dry run of nested for loops 1.public class Exercise1 { 2.public static void main(String [] args) { 3. int numRows = 4; 4. int numCols = 3; 5. for (int row = 1; row <= numRows; row++){ 6. for (int col = 1; col <= numCols; col++){ 7. System.out.print("*"); 8. } // end for 9. System.out.println(); 10. } // end for 11. } // end main program 12.} // end class Talk them through this, maybe sketching where row and col are at each run of the loop. (row 1, cols 1-6, row 2, etc) The class is called Exercise1. The main program is set up. Variable numRows is declared and given the value 4. Variable numCols is declared and given the value 3. First for loop uses control variable row. Second for loop uses control variable col. Now we will work out what will be printed out by the end.
How can we represent this as a dry run? First, set up the dry run
What happens after line 7?
We go back to the beginning of the nested loop. The conditions of the nested loop are still valid so we re-enter the loop. What happens when col = 3?
Have a look carefully at the code. The condition col < numCols is false so we leave the inner nested loop and go down to the next instruction on line 9 which is to output a new line. This reaches the end of the outer loop so we now return to line 5 where we add 1 to row and then continue ...
Complete Dry Run
Final Output ** (newline)
How to make sure you will pass test 1 Make sure that you have done all the exercises in all the workbooks to date. If you’re falling behind, you still have time before the test to catch up. Ensure that you are up to date with jafa. If you are having difficulty with dry runs, ask your tutor in the workshop or tutorial. We are here to help!