St Brendan’s as computer Science Summer task Summer 2016 St Brendan’s Computer Science Summer task


User input using Scanner with BlueJ



Download 1.82 Mb.
Page8/10
Date28.05.2018
Size1.82 Mb.
#51383
1   2   3   4   5   6   7   8   9   10
3 User input using Scanner with BlueJ

Type this code carefully into BlueJ.



Now run as a program in BlueJ and test with data.


NOTE:

/ gives integer division eg 15 / 6 = 2

% gives the remainder eg 15 % 6 = 3

Note: it can be helpful in debugging a program to add a breakpoint and examine how your program behaves from that breakpoint line by line. For an example on adding a breakpoint see video: https://www.youtube.com/watch?v=Q625RZTc1Ho


4 Pseudocode trace

Pseudocode is a way of describing an algorithm without using a specific programming language. A trace is a way of checking an algorithm by working through line by line of an algorithm and keeping track of each relevant value. In the A level course you will be expected to use both.

Trace 1

The pseudocode below is traced for input value 5 and then for input value 8


Pseudocode Trace table for input value 5


input a

if a MOD 2 is 1

output a

else


output a – 1

end if





a

output

5

5

Trace table for input value 8




a

output

8

7

Where MOD means remainder. For example 13 MOD 5 is 3 because when 13 is divided by 5 the remainder is 3.
Youtube recording for this example: www.youtube.com/watch?v=PSpm9d2kq_o
The purpose of this pseudocode is: to output an odd number that is the same as the input number, if the input is odd, or one lower than the input number if the input is even.

Trace 2



Consider the pseudocode in the box alongside:


Note:

The pseudocode contains a loop starting at line 6:

‘for i is 3 to n’ and ending at the line 11: ‘end

for loop’. This means lines 7 to 10 are repeated (in the order: 7, 8, 9, 10, 7, 8, 9, 10 . . . etc.)


This loop is controlled by stepper variable, i, which starts with a value of 3 and at the end of each iteration is increased by one until in the final iteration it reaches a value of n.
Suppose the algorithm is traced with variable n having a value read in of 10. We produce a trace by considering and tracking the values of each variable: n, i, sum, v1, v2 and what is output as we work through the pseudocode one line at a time (repeating lines 7 to 10 for each value of i).


n

i

sum

v1

v2

output







0

1

1

1
















1

10

3

2

1

2

2




4

3

2

3

3




5

5

3

5

5




5

8

5

8

8




6

13

8

13

13




7

21

13

21

21




8

34

21

34

34




9

55

34

55

55

Notice:


  • the table shows n stays as 10 all the way through

  • i has values 1,2, . . . , 10

  • v1 and v2 start with value 1

  • when i is 3, the value of sum is set to 2, v1 changes to 2 and 2 is printed out etc

HELP:


See video on how to fill in this trace table: www.youtube.com/watch?v=ycdcDYl7yd4
When tracing an algorithm make sure you work through carefully one line at a time and do not worry about the purpose of the algorithm until you have finished.
The purpose of this algorithm is to print out the first n values of the Fibonacci sequence. Research the Fibonacci sequence and check if the algorithm works for other values of n.
I think this algorithm should work fine for most whole number values of n. But what about values less than 2 (n= 1 or 0 or a negative). We can rewrite the algorithm so that it will better cope with n being 1 or less by printing out the correct number or giving an error message.
Java Work – do this before attempting Java exercises (or after you get stuck!)

Make sure you have completed sections 1-4 of this booklet.

Go to chortle site http://chortle.ccsu.edu/CS151/cs151java.html or use mirror site http://programmedlessons.org/java5/index.html. Go through chapters 8 – 11 and do as many exercises and quizzes as you need. Set aside at least six hours to work through these chapters and more if you do more than a few of the exercises. After chapters 8 – 9c attempt question 1 below (filling in answers 01 to 03). After chapters 10 – 11 attempt question 2, 3 and 4 below (filling in answers 04 to 11).



Download 1.82 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   10




The database is protected by copyright ©ininet.org 2024
send message

    Main page