Presentation is loading. Please wait.

Presentation is loading. Please wait.

F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.

Similar presentations


Presentation on theme: "F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson."— Presentation transcript:

1 F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson

2 Arithmetic operators expression ::= expression 1 operator expression 2 + == add 1+2  3 - == subtract 5-7  -2 * == multiply 3*4  12 / == divide & lose remainder 31 div 3  10 % == remainder from division - modulo 31 % 3  1

3 Arithmetic operators if both operands are int, use integer arithmetic if either operand is float / double use floating point arithmetic 56+10.2  66.2

4 Negation expression ::= operator expression unary operator - expression expression is a numeric type evaluate expression to value  minus value e.g. -42 -12.34

5 Brackets, order & precedence expression ::= ( expression 1 )  value from expression 1 evaluate expression from left to right precedence == order of applying operators (...) before - before * or / or % before + or –

6 Brackets, order & precedence 10+4*5  10+20  30 (10+4)*5  14*5  70 11-7-2  4-2  2 11-(7-2)  11-5  6 -6-7  -13 -(6-7)  -(-1)  1

7 Constants if a value is to be used in many different places then: – declare it once as static final – i.e. only one instance which can’t be modified – identifier should be CAPITAL LETTERS Java convention – only ever use the identifier to change the value everywhere just change the declaration once

8 Numeric input for types other than String, must convert from character sequences Scanner methods int nextInt() return next int from Scanner object float nextFloat() return next float from Scanner object double nextDouble() return next double from Scanner object

9 Programming principles always tell user what’s happening – user is not programmer – programmer knows what program does, but user doesn’t – so program must always tell user what’s going on e.g. input – print to display to say what’s expected before input from keyboard e.g. output – print brief heading information before numbers – never print raw numbers!

10 Comments /* text */ – use text to remind you and other programmers what program sections do – compiler/computer ignores text – text can run over several lines always start program with header: /* your name & date course/laboratory/exercise details what program does */

11 Layout use space on page to indicate how sections of program are related GJM likes indentation – line up starts of lines between { and } – opening { on new line – after opening {, 2 spaces, then 3 spaces at start of subsequent lines until closing } – line up closing } under matching opening { matter of personal taste develop own style but be consistent!

12 Example: Pie shop $java Pie pie: £2.39 how many pies? 2 2 pie @ £2.39 = £4.78

13 Example: Pie Shop /* Greg Michaelson 27/9/10 F27SA1 Lecture 3 Pie Shop */ import java.util.*; class Pie { static final double PIE_PRICE = 2.39; public static void main(String [] argv) { Scanner input = new Scanner(System.in);

14 Example: Pie Shop System.out.println("pie: £"+PIE_PRICE); System.out.println("How many pies?"); int pieNo = input.nextInt(); double pieTotal = pieNo*PIE_PRICE; System.out.println (pieNo+" pie @ "+PIE_PRICE+" = "+pieTotal); }

15 Example: Take Away $java TakeAway samosa: £1.55 how many samosas? 3 3 samosa @ £1.55 = £4.65 pie: £2.39 how many pies? 2 2 pie @ £2.39 = £4.78 total = £9.43

16 Example: Take Away /* Greg Michaelson 27/9/10 F27SA1 Lecture 3 Take away */ import java.util.*; class TakeAway { static final double SAMOSA_PRICE = 1.55; static final double PIE_PRICE = 2.39; public static void main(String [] argv) { Scanner input = new Scanner(System.in);

17 Example: Take Away System.out.println("samosa: £"+SAMOSA_PRICE); System.out.println("How many samosa?"); int samosaNo = input.nextInt(); double samosaTotal = samosaNo*SAMOSA_PRICE; System.out.println (samosaNo+" samosa @ "+SAMOSA_PRICE+" = “+samosaTotal); System.out.println("pie: £"+PIE_PRICE); System.out.println("How many pies?"); int pieNo = input.nextInt(); double pieTotal = pieNo*PIE_PRICE; System.out.println (pieNo+" pie @ "+PIE_PRICE+" = "+pieTotal);

18 Example: Take Away System.out.println ("total = "+(samosaTotal+pieTotal)); } dreadful programming style! big block of code repeated code: – same actions for samosas and pies

19 Example: Take Away identify general Food class String name double price double total void findTotal(Scanner input) – prompt for & get number of food items from keyboard calculate & display total double getTotal() – return value of total

20 Example: Take Away make samosas & pies instances of Food need constructor to create instance Food(String name,double price) now need to initialise instance fields separately from declaring them

21 Assignment assignment changes value of declared variable statement ::=... | assignment assignment ::= identifier = expression; – variable must be declared – value must be of same type as for variable – evaluate expression to get a new value – set variable for identifier to new value use to: – reuse variable – set value of object field in constructor

22 Assignment int age = 56; age = age + 1; changes age to 57 int age = 56; int age = age + 1; declares two variables called age sets first to 56 and second to 57 allocates space for two variables called age ! can’t access first age as identifier always refers to second one!

23 Assignment evaluated from right to left! identifier 1 = identifier 2 ; sets identifier 1 to value of identifier 2 int twin1Age = 25; int twin2Age; twin2Age = twin1Age; – sets twin2Age to 25

24 Food object class Food { String name; double price; double total; Food(String name,double price) { this.name = name; this.price = price; } this == current object instance use to distinguish field identifier from parameter identifier double getTotal() { return total; }

25 Food object void findTotal(Scanner input) { System.out.println(name+": £"+price); System.out.println("How many "+name+" ?"); int no = input.nextInt(); total = price * no; System.out.println (no+" "+name+" @ £"+price+" = £"+total); }

26 Example: Take Away 2 class TakeAway2 { public static void main(String [] argv) { Scanner input = new Scanner(System.in); Food samosa = new Food("samosa",1.55); Food pie = new Food("pie",2.39); samosa.findTotal(input); pie.findTotal(input); System.out.println ("Total £"+(samosa.getTotal()+pie.getTotal())); } no code repetition can easily add new items of food

27 Example: Take Away 2 void findTotal(Scanner input) {... int no = input.nextInt();... }... public static void main (String [] argv) { Scanner input = new Scanner(System.in);... samosa.findTotal(input); pie.findTotal(input);... } findTotal has a Scanner argument main declares Scanner input passed input to both calls to findTotal – so both calls use same input stream samosa.findTotal(input) called before pie.findTotal(input) – so number of samosas is input before number of pies

28 Programming principles if similar code sequences used in different places then generalise as class – identify common: variables  fields statements  methods keep object classes separate from class for program ( main ) that manipulates objects

29 Boolean type boolean type values true and false use to: – indicate whether or not some property holds e.g. likes pies: yes/no – represent two-value state e.g. switch: on/off

30 Example: Switch class Switch { boolean on; Switch(boolean on) { this.on = on;} void setOn(){ on = true; } void setOff(){ on = false; } boolean isOn{ return on; }

31 Logical operators: negation ! expression – unary expression must be boolean truth table – shows values of boolean expression for different values of operands expression ! expression falsetrue false

32 Example: Switch... void changeSwitch() { on = !on; }... Switch s1 = new Switch(true);  s1.on == true... 1.s1.changeSwitch();  s1.on == false 2.s1.changeSwitch();  s1.on == true 3.s1.changeSwitch();  s1.on == false

33 Logical operators: and expression 1 && expression 2 binary conjunction expression 1 & expression 2 must be boolean true if both operands are true otherwise false expression 1 expression 2 exp 1 && exp 2 false truefalse truefalse true

34 Logical operators: or expression 1 || expression 2 binary disjunction expression 1 & expression 2 must be boolean true if either operand is true otherwise false expression 1 expression 2 exp 1 || exp 2 false true falsetrue

35 Example: Switch Switch hallLight = new Switch(true); Switch stairLight = new Switch(false);......hallLight.isOn() || stairLight.isOn()  true || false  true... hallLight.changeSwitch(); stairLight.changeSwitch();......!hallLight.isOn() && stairLight.isOn()  not false && true  true && true  true


Download ppt "F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson."

Similar presentations


Ads by Google