Upload
moduledesign
View
47
Download
0
Embed Size (px)
Citation preview
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 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 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 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 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 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 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 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