Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

St Brendan’s AS Computer Science Summer task Summer 2016

St Brendan’s
Computer
Science
Summer task
2016

jaz last edit: 2016 6 20 page 1 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Summer Task – is a course requirement


In addition to the other entry requirements you need to complete this task to be allowed onto the
A’ level computer Science course. You should aim to achieve at least 80%. Below you will be
guided to download and install the free programming software used in the course and complete
some exercises. First some background information.

A’ level Computer Science at St Brendan’s


The computer science course at St Brendan’s follows the AQA syllabus and consists of both
theory and practical work. The theory work material is largely covered by a textbook. Practical
work uses the Java programming language. For more details on the whole course see: Welcome
to Computer Science 2016.ppt

Introduction to Java
Java Basics – do not worry if you do not understand
this section fully (you can do all the work below without it)
Programming languages are typically written using text,
called source code, using a text editor and translated, using
a compiler or interpreter, into executable machine language
object code which runs on specific platforms (machine
types). Unlike many other programming languages Java
source code is first translated (compiled) into byte code
which is the same for different platforms. The Java Virtual
Machine (JVM) is software that translates (interprets) and
executes byte code on a particular platform. Byte code can
be thought of as machine language for the JVM software machine. The JVM allows Java source
code to be translated to byte code that is the same for different machines (platform independent).

Java source code is held in .java files and compiled into byte code in .class files for the JVM to
execute.
[see http://chortle.ccsu.edu/java5/Notes/chap05/ch05_1.html to ch05_5.html for more detail]

Java Programming
In order to run a Java program the Java Runtime Environment (JRE) is required.
In order to write a Java program a Java Development Kit (JDK) is required.

An Integrated Development Environment (IDE) is


useful software to help develop Java programs. Popular
IDEs are Eclipse, Netbeans, IntelliJ and BlueJ. In the
first year at St Brendan’s we use BlueJ mostly.

Software installation
Use the guidance in the next few pages to download and install the latest Java SE JDK 7 or Java
and BlueJ IDE. You can now get both from the BlueJ site

St Brendan’s website or Canvas – summer task materials


Summer task materials are all available at http://www.stbrn.ac.uk/find/courses/computer-science-2/ or
canvas.instructure.com/enroll/3TKL4C . For the canvas pages you will need to register but will enable
you to access additional materials.

jaz last edit: 2016 6 20 page 2 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

1 Download and install BlueJ this automatically installs Java (JDK ) also.
From http://www.bluej.org/ download BlueJ 3 latest version (currently 3.1.7)
For windows operating system your progress should be as follows:

2 Simple program using BlueJ


We shall now use the BlueJ IDE to develop a simple Java program to
say ‘Hello’.

In BlueJ select: Project – New Project. Create a project ‘Summer


project’ in a suitable location - Create – New Class – type HelloWorld
for class name - Ok.
NB: Java is case sensitive

Double click
to type code

Double click the HelloWorld class to open the code edit window (or right click – Open Editor).

jaz last edit: 2016 6 20 page 3 of 15


St Brendan’s AS Computer Science Summer task Summer 2016
Delete all the example code and type in the HelloWorld code as shown below, but with your own
name after author (and not anything in the dashed rectangles which are notes to help you).

Class name must be exactly the same as file name

Java multi line comment

Don’t worry
about this line –
Displays Hello World just that it is
message needed for now
Java single line comments start with //

Note:
 Java comments do not affect how a program runs but are used to help a programmer
 Indent code to help readability (does not affect compilation / execution): use Edit - Auto-layout
 Semi colon ; curly brackets {, } round brackets (, ) and square brackets [, ] must be used
correctly and are NOT interchangeable.
 To see line numbers (in the code editor): Options – preferences – display line numbers.
 BlueJ reference site: http://www.bluej.org/
Click compile (you should get ‘Class compiled - no syntax
errors’ - otherwise check lines 5 to 11 are correctly typed – Java
is case sensitive, and remember curly brackets and semicolon –
Java is fussy!
Close the editor.
Right click the HelloWorld class
Select void main(String[] args)
Select Ok
The terminal window should contain
“Hello World!” as output as shown:

{Note:
It is possible to write Java using a normal
text editor, and compile and run java programs using the Java SDK without need for an IDE such as BlueJ by using
the command line interface. You are not required to do so, but if you are interested to do this, see Chortle chapter 7
or YouTube tutorial 2 running Java: http://www.youtube.com/watch?v=5u8rFbpdvds&feature=fvwrel.}

youTube reference links for installing BlueJ and a HelloWorld program, zaychenok Java Lesson 1&2:
http://www.youtube.com/watch?v=aqQefkprf3s,
http://www.youtube.com/watch?v=l9nHHCgXBMk&feature=related

Now you are ready to start work. Allow at least 12 hours to complete the work that follows.

jaz last edit: 2016 6 20 page 4 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

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: 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 a output
if a MOD 2 is 1 5 5
output a
else Trace table for input value 8
output a – 1
end if 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.

jaz last edit: 2016 6 20 page 5 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Trace 2
set sum to 0
Consider the pseudocode in the box alongside: set v1 and v2 to 1
output v1
Note: output v2
The pseudocode contains a loop starting at line 6: read in the value of n
‘for i is 3 to n’ and ending at the line 11: ‘end for i is 3 to n
for loop’. This means lines 7 to 10 are repeated (in sum ← v1 + v2 Lines 7 to 10
the order: 7, 8, 9, 10, 7, 8, 9, 10 . . . etc.) v1 ← v2 repeated for
v2 ← sum values of i
This loop is controlled by stepper variable, i, which output sum from 3 to n
starts with a value of 3 and at the end of each end for loop
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.

jaz last edit: 2016 6 20 page 6 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

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).

Useful links to go with chortle reading above and help with exercises to follow below:
Basic BlueJ program www.youtube.com/watch?v=-Pt2cl3lfmI
Use of Scanner www.youtube.com/watch?v=cEwVhv_JW1A
Bucky tutorials (use Eclipse IDE instead of BlueJ but otherwise are relevant)
Java Programming Tutorial - 6 - Getting User Input
http://www.youtube.com/watch?v=5DdacOkrTgo&feature=relmfu
(OR on the new boston: http://thenewboston.org/watch.php?cat=31&number=6 etc)
Java Programming Tutorial - 7 - Building a Basic Calculator
http://www.youtube.com/watch?v=2MjKuUTXa4o

StBrendan (use BlueJ Scanner with integer division and remainder useful for que 2)
https://www.youtube.com/watch?v=pcyIU7RRSmA
Code academy – learn Java (units 1 and 2 – free stuff only – also try code in BlueJ)
www.codecademy.com www.codecademy.com/learn/learn-java
Type your answers to the exercises below in your Electronic Answer Document.
You must save this document at regular intervals.

Question 1
Create a class Exercise1 with the code below (you can type in or copy and paste):
public class Exercise1
{
public static void main (String[] args)
{
double km = 30, miles
miles = km * 5 / 8;
System.println(km + " kilometres is " + miles + " miles");
}//end main
}//end class

(a) The program has two compilation errors. Correct these.


What you need to do
Modify the program so that it so the program compiles and runs
Evidence that you need to provide
Include the following in your Electronic Answer Document.

01 Your corrected PROGRAM SOURCE CODE. (2 marks)

02 SCREEN CAPTURE of program output in terminal window (1 mark)

(tip: see EAD front sheet for help on pasting screenshots into EAD)

(b) (i) replace miles = km * 5 / 8; by miles = 5.0 / 8 * km; compile and run
(ii) replace miles = 5.0 / 8 * km; by miles = 5 / 8 * km; compile and run

03 Explain fully the results obtained in running versions b(i) and (ii). To gain full marks
you will need to note the relevance of data types (Chortle chp 8, 9B) (2 marks)
jaz last edit: 2016 6 20 page 7 of 15
St Brendan’s AS Computer Science Summer task Summer 2016
Question 2
Create a class Exercise2 with the code below:

import java.util.Scanner; //package needed to read from keyboard


public class Exercise2
{
public static void main (String[] args)
{
//declare Scanner and integer variables
Scanner sc = new Scanner(System.in);
int allMonths, years, partMonths;

//ask for age in months


System.out.print("Age in months: ");

//set allMonths to value typed in


//code here

//calculate years from allMonths


//code here
//calculate partMonths from allMonths
//code here

//display message about calculated years and months


//code here
}//end main
}//end class

Currently the program prompts the user for a number of months but does not yet calculate
and output how many years and months this is equivalent to. For example when the user
types an input of 40 the program output should be:
Age in months: 40
40 months = 3 years and 4 months

What you need to do


Modify the program so that for an input number of months the whole years and months are
calculated and displayed

Test your program with age values in months as follows.


Test 1: 9
Test 2: 24
Test 3: 47

Evidence that you need to provide


Include the following in your Electronic Answer Document.

04 Your completed PROGRAM SOURCE CODE. (6 marks)

05 SCREEN CAPTURE for Test 1 (1 mark)

06 SCREEN CAPTURE for Test 2 (1 mark)

07 SCREEN CAPTURE for Test 3 (1 mark)

jaz last edit: 2016 6 20 page 8 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Question 3
Create a class PizzaCost with the code below:

Currently the program prompts the user for how many share a pizza but the pizza price is set
at £10.95 and does not allow a user to input a pizza price or the number of pizzas.

What you need to do


Modify the program so that the user is
prompted for the number of pizzas and
the price of each pizza as well as how
many people are sharing and outputs
the amount each person has to pay for
an equal share. Do not worry about the
number of decimal places in your
answer – this will be dealt with later.

Test your program as follows.


Test: 4 pizzas, £8.50 for each pizza and 8 people sharing

Evidence that you need to provide


Include the following in your Electronic Answer Document.

08 Your completed PROGRAM SOURCE CODE. (5 marks)

09 SCREEN CAPTURE for the test (2 marks)

jaz last edit: 2016 6 20 page 9 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Question 4
Create a class BillChange class with the code below:

This program contains 3 errors: 2 syntax errors and 1 semantic error.


Note: the decimal formatting used will round the decimal number to
two decimal places

What you need to do


TASK A: Modify the program to calculate the change for a payment
of a random bill (up to £100). An example is shown here.

Test: Test your program with an appropriate payment for your


random bill.

Evidence that you need to provide


Include the following in your Electronic Answer Document.

10 Your completed PROGRAM SOURCE CODE. (3 marks)

11 SCREEN CAPTURE for Test (1 mark)

jaz last edit: 2016 6 20 page 10 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Question 5a
Create a class SwapUsingTeacup class with the code below:

What you need to do


Modify the above program to swap the values
held in variables x and y by making use of the
variable temp (do not just print other way round).
The variable temp used in the swap is
sometimes called a teacup. An example output is
shown here.

Test: Test your program with the initial values


3 and 7 in x and y

Evidence that you need to provide


Include the following in your Electronic Answer Document.

12 Your completed PROGRAM SOURCE CODE. (2 marks)

13 SCREEN CAPTURE for Test (1 mark)

jaz last edit: 2016 6 20 page 11 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Question 5b
Create a class SwapWithoutTeacup class with the code below:

What you need to do


Modify the program to swap the values held in
variables x and y by only making use of variables
x and y (and not creating/using any other
variables). An example output is shown here.

Test: Test your program with the initial values 11


and -4 in x and y

Evidence that you need to provide


Include the following in your Electronic Answer Document.

14 Your completed PROGRAM SOURCE CODE. (2 marks)

15 SCREEN CAPTURE for Test (1 mark)

jaz last edit: 2016 6 20 page 12 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Question 6 a (review traces on page 5 and 6 of this booklet before trying this question)
Trace the pseudocode of an algorithm.
Consider the pseudocode shown in the box alongside:. input a and b
if a > b then
What you need to do output a – b
Complete the trace below with input values: a is 2 and b is 7. else
Copy your answers to the Electronic Answer Document (EAD) output b – a
end if
16 a b output
2 7
(1 mark)

Fill in the trace below with a input as 8 and b as 3 and copy to your EAD
a b output
17 8 3 (1 mark)

18 State the purpose of this algorithm? (1 mark)

Question 6 b
Trace the pseudocode of an algorithm. input n
Consider the pseudocode shown in the box alongside:. set s to 0
for x is 1 to n
What you need to do if (x MOD 3 ≠ 0)
Complete the trace below with input value: n is 4. s←s+x
Copy your answers to the Electronic Answer Document (EAD) end if
n x s output end for loop
19 4 0 output s
1

(2 marks)

Fill in the trace below with a input value of n as 8 and copy to your EAD
n x s output
20 8 0
1

(2 marks)

21 State the purpose of this algorithm? (1 mark)

Note: MOD calculates the remainder (what is left) from integer division. For example
17 MOD 5 is 2
You may want to code these to check your answers (but you are not required to do so).

jaz last edit: 2016 6 20 page 13 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Theory work
Set aside at least five hours to view the lecture and work through the tutorial chapters below.

Listen to MIT introductory lecture, Lec 1 | MIT 6.00 Introduction to Computer Science and
Programming, Fall 2008: http://www.youtube.com/watch?v=k6U-i4gXkLM&feature=related (54
mins, but skip minutes: 0 – 4.35 and 7.15 – 16.15). Do not worry if you find it difficult to follow in
places and note the programming language introduced briefly is Python not Java but it is still a good
introduction to what computing is about and relevant to our course. Then answer question 7 below:

Read through http://chortle.ccsu.edu/java5/index.html chapters 1 to 4. Try the quizzes and


flashcards. Then work through the rest of the questions below.
Note Chortle does NOT use binary prefix so you need a little extra research for question 10.

Type your answers to the exercises below in your Electronic Answer Document.
You must save this document at regular intervals.

Question 7

22 James knows he needs to find the shortest route from Bath to Milton Keynes. Angela
knows Dijkstra’s algorithm/method for finding the shortest path between any two
locations. State and explain which person has imperative knowledge and which
person has declarative knowledge in this situation. (1 mark)

23 Two students want to sum up the integer variables x and y using Java.
Freda writes int z = x + y and forgets to place a semicolon at the end of the statement
George writes int z = x – y; and has wrongly put a minus instead of a plus. Both have made
errors. State and explain who has made a syntax error and who has made a semantic error
(1 mark)

24 How many primitives did Turing use to program anything that can be programmed?
(1 mark)

Question 8 (based on Chortle Chapter 1)

25 Explain what software refers to for a computer system (1 mark)

26 Explain what bus refers to for a computer system (1 mark)

27 Main Memory (RAM) is volatile but Secondary Memory (also called secondary
storage or auxiliary storage) is not. Explain what volatile means. (1 mark)

28 Where are programs and data kept for long-term storage (when computer is
switched off)? (1 mark)

29 Where is a program and its data kept whilst the program runs (from where
small chunks are then fetched into the processor to be executed)? (1 mark)

jaz last edit: 2016 6 20 page 14 of 15


St Brendan’s AS Computer Science Summer task Summer 2016

Question 9 (based on Chortle Chapter 2)

30 Explain what bit stands for and what it means. (1 mark)

31 What does one Hertz mean? (1 mark)

Question 10 (based on Chortle Chapter 3 – and research on binary prefix)


For this question also investigate binary prefix as defined by the US National Institute of
Standards and Technology and in particular the meaning of kibibyte, mebibyte and
gibibyte . NOTE: Chortle does NOT cover this – so look up each binary prefix in
Wikipedia (for example https://en.wikipedia.org/wiki/Kibibyte).

32 How many bits are there in one byte? (1 mark)

33 What are 220 bytes and 230 bytes (using binary prefix) called respectively? (1 mark)

34 A particular digital image is 1024 rows by 1024 columns, and each location (pixel) is
represented by 24 bits. How many mebibytes are needed to store this image? (1 mark)

35 How many bytes can a 64 bit processor read from (or write to) memory at one time? (1 mark)

Question 11 (based on Chortle Chapter 4)

36 Is Java a high or low level programming language? (1 mark)

37 Software X is used to translate the whole source code of a high level language program and
saves it to a binary (machine code) file that can then be run. Which type of translator is
software X - a compiler or an interpreter? (1 mark)

End of questions

Please complete the feedback survey at the end of the EAD

Remember to submit the completed EAD – see EAD for details

Optional extras - the future?


 Ubiquitous computing: Youtube (BBC4) Michio Kaku - The Intelligence Revolution
 CS50 introduction Harvard http://cs50.tv/2013/fall/lectures/0/w/
 Jobs: TED talk: Andrew Mcafee - future jobs

jaz last edit: 2016 6 20 page 15 of 15

You might also like