42
App Design for Business Topic: Coding Principles Topic Number: 2

Lecture 2 coding_principles

Embed Size (px)

DESCRIPTION

L2

Citation preview

Page 1: Lecture 2 coding_principles

App Design for Business

Topic: Coding Principles

Topic Number: 2

Page 2: Lecture 2 coding_principles

2

Key topics / learning outcomes of this lecture

• introducing bits & bytes;• introducing Java data types;• introducing Java decision making;• understanding variables;• learn to run Java code in IntelliJ;• introduction to xml;

Page 3: Lecture 2 coding_principles

B4004A L1 3

Computers work with bits and bytes comprising of 1’s and 0’s.

Page 4: Lecture 2 coding_principles

B4004A L1 4

bits and bytes …

Page 5: Lecture 2 coding_principles

B4004A L1 5

Example of bits in RGB colour model

Page 6: Lecture 2 coding_principles

B4004A L1 6

RGB Colour Model - some colour values

red green blue

255 0 0 red

0 255 0 green

0 0 255 blue

0 0 0 black

100 100 100 dark gray

255 255 255 white

255 255 0 yellow

255 0 255 magenta

160 82 45 sienna

Page 7: Lecture 2 coding_principles

B4004A L1 7

Bits and bytes are sometimes called machine code

Page 8: Lecture 2 coding_principles

B4004A L1 8

End of introduction to bits and bytes

Page 9: Lecture 2 coding_principles

B4004A L1 9

Introduction to Data Types

Here you can see data types and how many bits are needed for each data type:

Page 10: Lecture 2 coding_principles

B4004A L1 10

Data Types

• byte - int 127• integer – int 2^31• short – int 32767 • long – int 2^63-1 • float – 1.754 • double – 3.1415926 (to 754 decimal

places)• boolean – True or False, 1 or 0• char – a (16 bit Unicode character)• String – String s = “This is a string”

Page 11: Lecture 2 coding_principles

B4004A L1 11

End of Data Types

Page 12: Lecture 2 coding_principles

B4004A L1 12

Introduction to Variables

Page 13: Lecture 2 coding_principles

B4004A L1 13

How variables are used in programs …

int a = 1; int b = 2; int c; c=(a+b);

System.out.print(c);This will print out : 3

Page 14: Lecture 2 coding_principles

B4004A L1 14

Assign different values to those variables …

int a = 2; int b = 4; int c; c=(a+b);

System.out.print(c);This will print out : 6

Page 15: Lecture 2 coding_principles

B4004A L1 15

The same code in IntelliJ

Page 16: Lecture 2 coding_principles

B4004A L1 16

Change of variable values …

Page 17: Lecture 2 coding_principles

B4004A L1 17

Introducing loops to use with the variables …

Page 18: Lecture 2 coding_principles

B4004A L1 18

Introducing loops – the for loop

Page 19: Lecture 2 coding_principles

B4004A L1 19

Print a new line within the for loop

Page 20: Lecture 2 coding_principles

B4004A L1 20

Increment variable a within the loop

Page 21: Lecture 2 coding_principles

B4004A L1 21

Change variable values and multiply instead, achieve 75 times table …

Page 22: Lecture 2 coding_principles

B4004A L1 22

How to print out in a coherent style (note error in terminal) …

Page 23: Lecture 2 coding_principles

B4004A L1 23

Fix error, place the iterator below the System.out …

Page 24: Lecture 2 coding_principles

B4004A L1 24

Loops – while loop

Page 25: Lecture 2 coding_principles

B4004A L1 25

Loops - introducing the do…..while loop, compare the two …

Page 26: Lecture 2 coding_principles

B4004A L1 26

End of loops

Page 27: Lecture 2 coding_principles

B4004A L1 27

Strings

String greeting = “Hello World!”int len = greeting.length();

char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };String helloString = new String(helloArray);System.out.println(helloString);

Page 28: Lecture 2 coding_principles

B4004A L1 28

Arraylist (slide 1 of 2)

ArrayList al = new ArrayList(); System.out.println("Initial size of al: " + al.size());

// add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); al.add(1, "A2"); System.out.println("Size of al after additions: " + al.size());

Page 29: Lecture 2 coding_principles

B4004A L1 29

Arraylist (slide 2 of 2) // display the array list System.out.println("Contents of al: " + al); // Remove elements from the array list al.remove("F"); al.remove(2); System.out.println("Size of al after deletions: " + al.size()); System.out.println("Contents of al: " + al); }}This would produce the following result:

Initial size of al: 0Size of al after additions: 7Contents of al: [C, A2, A, E, B, D, F]Size of al after deletions: 5Contents of al: [C, A2, E, B, D]

Page 30: Lecture 2 coding_principles

B4004A L1 30

Arrays

// initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // and so forth anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600;

Page 31: Lecture 2 coding_principles

B4004A L1 31

Arrays of different Data Types

• byte[ ] anArrayOfBytes; • short[ ] anArrayOfShorts; • long[ ] anArrayOfLongs; • float[ ] anArrayOfFloats; • double[ ] anArrayOfDoubles; • boolean[ ] anArrayOfBooleans; • char[ ] anArrayOfChars; • String[ ] anArrayOfStrings;

Page 32: Lecture 2 coding_principles

B4004A L1 32

Java Decision Structures

if …

if ….. else if …… else

switch

Page 33: Lecture 2 coding_principles

B4004A L1 33

If ….. else if ….. elsepublic class Test {

public static void main(String args[]){ int x = 30;

if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is else statement"); } }}

Page 34: Lecture 2 coding_principles

B4004A L1 34

Use if………

Page 35: Lecture 2 coding_principles

B4004A L1 35

Introducing break; within if

Page 36: Lecture 2 coding_principles

B4004A L1 36

Introducing continue; within if

Page 37: Lecture 2 coding_principles

B4004A L1 37

Introducing switch statementpublic class SwitchDemo { public static void main(String[] args) {

int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break;

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Page 38: Lecture 2 coding_principles

B4004A L1 38

… the end of the Introduction to Java and Coding Principles

… next a brief look at xml

Page 39: Lecture 2 coding_principles

B4004A L1 39

xml

source www.w3schools.com/xml

Page 40: Lecture 2 coding_principles

B4004A L1 40

xml is based on a particular xml Schema

source www.w3schools.com/schema

Page 41: Lecture 2 coding_principles

41

Essential work for next week

• Please consult the OLE for details of:– Essential readings*– Seminar/workshop preparation work*– Recommended further readings– Any additional learning

* Essential readings and preparation work must always be completed in time for the next session

Page 42: Lecture 2 coding_principles

End of presentation

© Pearson College 2013