lecture 2 coding_principles
DESCRIPTION
L2TRANSCRIPT
App Design for Business
Topic: Coding Principles
Topic Number: 2
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;
B4004A L1 3
Computers work with bits and bytes comprising of 1’s and 0’s.
B4004A L1 4
bits and bytes …
B4004A L1 5
Example of bits in RGB colour model
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
B4004A L1 7
Bits and bytes are sometimes called machine code
B4004A L1 8
End of introduction to bits and bytes
B4004A L1 9
Introduction to Data Types
Here you can see data types and how many bits are needed for each data type:
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”
B4004A L1 11
End of Data Types
B4004A L1 12
Introduction to Variables
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
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
B4004A L1 15
The same code in IntelliJ
B4004A L1 16
Change of variable values …
B4004A L1 17
Introducing loops to use with the variables …
B4004A L1 18
Introducing loops – the for loop
B4004A L1 19
Print a new line within the for loop
B4004A L1 20
Increment variable a within the loop
B4004A L1 21
Change variable values and multiply instead, achieve 75 times table …
B4004A L1 22
How to print out in a coherent style (note error in terminal) …
B4004A L1 23
Fix error, place the iterator below the System.out …
B4004A L1 24
Loops – while loop
B4004A L1 25
Loops - introducing the do…..while loop, compare the two …
B4004A L1 26
End of loops
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);
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());
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]
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;
B4004A L1 31
Arrays of different Data Types
• byte[ ] anArrayOfBytes; • short[ ] anArrayOfShorts; • long[ ] anArrayOfLongs; • float[ ] anArrayOfFloats; • double[ ] anArrayOfDoubles; • boolean[ ] anArrayOfBooleans; • char[ ] anArrayOfChars; • String[ ] anArrayOfStrings;
B4004A L1 32
Java Decision Structures
if …
if ….. else if …… else
switch
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"); } }}
B4004A L1 34
Use if………
B4004A L1 35
Introducing break; within if
B4004A L1 36
Introducing continue; within if
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
B4004A L1 38
… the end of the Introduction to Java and Coding Principles
… next a brief look at xml
B4004A L1 40
xml is based on a particular xml Schema
source www.w3schools.com/schema
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
End of presentation
© Pearson College 2013