Software and Programming 1
Lab 1: Introduction to Java: HelloWorld and InterestCalculator
1
SP1-Lab1-2019-20.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 16 January 2020
Software and Programming 1 Lab 1: Introduction to Java: HelloWorld - - PowerPoint PPT Presentation
Software and Programming 1 Lab 1: Introduction to Java: HelloWorld and InterestCalculator SP1-Lab1-2019-20.pptx 1 16 January 2020 Tobi Brodie (tobi@dcs.bbk.ac.uk) Module Information Lectures: Afternoon 2pm (surnames A-H), 3.30pm (surnames
1
SP1-Lab1-2019-20.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 16 January 2020
2
3
4
(see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html)
– ( e.g. int speed = 70; not int s = 70;)
5
6
7
8
9
/* * Here is a block comment. */ if (a == 2) { return 25; /* special case */ }
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions- 141999.html
10
if (number >= 0) { // code for natural numbers ... } else { return 0; // number will not be in range }
11
12
13
14
15
16
17
Note the indentations of the lines of code which make the code easier to read.
18
19
20
21
22
23
24
(Based on JFE, Section 1.7)
25
26
27
28
public class InterestCalculator { public static void main(String[] args) { // declare variable initialBalance System.out.println("The initial balance is £" + initialBalance); // declare variable currentBalance currentBalance = withInterestOn (initialBalance); // print new balance /* repeat previous 2 lines (use currentBalance instead of initialBalance as argument for withInterestOn call) to calculate balance for 2 more years(see previous slide) */ } public static double withInterestOn(double balance) { double interest = balance * 0.05; return balance + interest; } }
29
30
31
32