St Brendan’s AS Computer Science Summer taskSummer 2016
St Brendan’s Computer Science Summer task
2016
jazlast edit: 2016 6 20page 1 of 15
St Brendan’s AS Computer Science Summer taskSummer 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 or canvas.instructure.com/enroll/3TKL4C . For the canvas pages you will need to register but will enable you to access additional materials.
1 Download and install BlueJ this automatically installs Java (JDK ) also.
From 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.
Double click the HelloWorld class to open the code edit window (or right click – Open Editor).
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).
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:
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:
youTube reference links for installing BlueJ and a HelloWorld program, zaychenok Java Lesson 1&2:
Now you are ready to start work. Allow at least 12 hours to complete the work that follows.
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:
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
PseudocodeTrace table for input value 5
a / output5 / 5
Trace table for input value 8
a / output8 / 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:
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 / output0 / 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:
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.
jazlast edit: 2016 6 20page 1 of 15
St Brendan’s AS Computer Science Summer taskSummer 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 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
Use of Scanner
Bucky tutorials (use Eclipse IDE instead of BlueJ but otherwise are relevant)
Java Programming Tutorial - 6 - Getting User Input
(OR on the new boston: etc)
Java Programming Tutorial - 7 - Building a Basic Calculator
StBrendan (use BlueJ Scanner with integer division and remainder useful for que 2)
Code academy – learn Java (units 1 and 2 – free stuff only – also try code in BlueJ)
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
(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
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)
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
jazlast edit: 2016 6 20page 1 of 15
St Brendan’s AS Computer Science Summer taskSummer 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
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.
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
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
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:.
What you need to do
Complete the trace below with input values: a is 2 and b is 7.
Copy your answers to the Electronic Answer Document (EAD)
a / b / output2 / 7
(1 mark)
Fill in the trace below with a input as 8 and b as 3 and copy to your EAD
a / b / output8 / 3
(1 mark)
State the purpose of this algorithm?(1 mark)
Question 6 b
Trace the pseudocode of an algorithm.
Consider the pseudocode shown in the box alongside:.
What you need to do
Complete the trace below with input values: n is 4.
Copy your answers to the Electronic Answer Document (EAD)
n / x / s / output4 / 0
1
(2 marks)
Fill in the trace below with a input as 8 and b as 6 and copy to your EAD
n / x / s / output8 / 0
1
(2 marks)
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).
jazlast edit: 2016 6 20page 1 of 15
St Brendan’s AS Computer Science Summer taskSummer 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: (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
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)
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)
How many primitives did Turing use to program anything that can be programmed? (1 mark)
Question 8 (based on Chortle Chapter 1)
Explain what software refers to for a computer system(1 mark)
Explain what bus refers to for a computer system(1 mark)
Main Memory (RAM) is volatile but Secondary Memory (also called secondary
storage or auxiliary storage) is not. Explain what volatile means.(1 mark)
Where are programs and data kept for long-term storage (when computer is
switched off)?(1 mark)
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)
Question 9 (based on Chortle Chapter 2)
Explain what bit stands for and what it means.(1 mark)
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
How many bits are there in one byte?(1 mark)