Presentation is loading. Please wait.

Presentation is loading. Please wait.

Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.

Similar presentations


Presentation on theme: "Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming."— Presentation transcript:

1 Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming Principle(1) – If and Boolean operations – Coding a Bank Account Quick look at ToolBox 1

2 Lecture 2 Java 2

3 This is a more practical lecture than last time This course exists for you to learn programming principles, not programming Java. On the following slides, these symbols mean: Principal OO PrincipalJava OO 3

4 From code to program You write code... You compile code You run the program My Code My Program Compiler My Program Me The Computer 4

5 From code to program You write code... You compile code You run the program My Code My Program Javac My Program Me The Computer The JVM 5

6 Eeeeek! What is a JVM? The JVM Java Virtual Machine Each operating system is different on a microcode level: – Different way of putting things on the screen – Different way of making sound – Different way of taking input from keyboard – Etc So how can java work on all these platforms? 6

7 Why the JVM is good Lets say Java is this shape: Windows is this shape: Mac is this shape: Unix is this shape: 7

8 Fitting 8

9 What shape is the JVM? There is a version of the JVM for each of these operating systems So the JVM is this shape: 9

10 Fitting 10

11 That is all you need to know about the JVM for this course Stop thinking about it 11

12 Content Programming Overview The JVM A brief look at Structure –C–Class –M–Method –S–Statement Magic incantations –m–main() –o–output Coding a Dog Programming Principle(1) –I–If and Boolean operations –C–Coding a Bank Account Quick look at ToolBox 12

13 Structure OO Source file Class Method Statement 13

14 What does Source File mean? Source file: – With text, ends in.txt – With Java, ends in.java The source file is simply the file that contains the source(code) for your class OO 14

15 What does Class mean? A class is the blueprint for an object OO Public so everyone can see it We’ll come back to this later in the course This is a class....... The name of the class The opening brace The closing brace 15

16 What does Method mean? Methods do things, they define behavior OO Public so everyone can see it We’ll come back to this later in the course This is the return type. Void means we are returning nothing The name of the method The opening brace The closing brace The brackets, these sometimes contain arguments to the methods 16

17 What does Statement mean? Statements are the code that do the operations OO This statement starts with a command, Print. Print isn’t a valid command in Java, this is an pseudo code (pretend code) example This is the string we are outputting. Woof should appear on screen when we run this line in the program Every statement ends with a ; Sometimes you write loooooooooooooo ooong statements that break over lines. Semicolon tells the compiler where the line ends 17

18 Hang on a sec.... The method says: The “return type” is void, but it’s still printing something out, that can’t be right, can it? It is right. Printing something to the screen is very different from returning something from a method. Good question though. 18

19 Content Programming Overview The JVM A brief look at Structure –C–Class –M–Method –S–Statement Magic incantations –m–main() –o–output Coding a Dog Programming Principle(1) –I–If and Boolean operations –C–Coding a Bank Account Quick look at ToolBox 19

20 Before we start to think about coding... 20

21 Magic Incantations Programming principles is a complex study You start learning something... and find out you need to know something else to understand it and something else to understand that and something else to understand that 21

22 Magic Incantations We started looking at class structure... For example And then mentioned visibility modifiers (the ‘public’ word in the class and method) And then mentioned arguments (The brackets at the end of the method) And then touched on return types And we still haven’t coded anything! 22

23 Magic Incantations These slides will give you tools to write your first programs You may not understand the ‘incantations’ don’t let it bother you, trust that it works You will understand later i.e. You can start going nowhere fast Go with the flow 23

24 Magic Incantation #1 Where does a program start? At the top of the class? – What about if you have more than one class, a cat class, a dog class and a parrot class? At the top of a specific class, the main class? – Still not specific enough, what if it gets accidently changed, or someone writes a method on top of your top method in the class? 24

25 Magic Incantation #1 Where does a program start? –I–In a special method? public static void main(String[] args){ } 25

26 A post it note? public static void main(String[] args){ } is a post-it note With instructions for how to set your program up 26

27 Rules – It doesn’t matter in which class you put the main method in – You can put it in its own one if you like – There must be 1 and only 1 in the whole program (program is a set of classes that work together) 27

28 Magic Incantation #2 How do I output things to the screen? System.out.println(“Your text here”); 28

29 Content Programming Overview The JVM A brief look at Structure –C–Class –M–Method –S–Statement Magic incantations –m–main() –o–output Coding a Dog Programming Principle(1) –I–If and Boolean operations –C–Coding a Bank Account Quick look at ToolBox 29

30 Write a Dog Using last lecture’s ideas – Let’s write a dog – Source file – Class – Method – Main Method Declare a dog Make a dog Call the method 30

31 Run a Dog Save with same name as class (Rule: Must do this) Command line to folder Compile - javac Dog.java Run – java Dog 31

32 Content Programming Overview The JVM A brief look at Structure –C–Class –M–Method –S–Statement Magic incantations –m–main() –o–output Coding a Dog Programming Principle(1) –I–If and Boolean operations –C–Coding a Bank Account Quick look at ToolBox 32

33 First Building Block 33

34 Making decisions Often we want to make the result of a program conditional on something If the bank account doesn’t exist Don’t allow a withdrawal – Else, if the account doesn’t have enough money Don’t allow a withdrawal – Else Allow the withdrawal For this we need to cover variables in more depth 34

35 Variables -Just like algebra? Storing values – In variables – Eg Dog int age; age = 3; Data type: Can be class or primitive Name of variable – think about ‘x’ in algebra Actual value to put into variable 35 More on variables next lecture

36 What kind of decisions Conditionals are based on TRUE or FALSE x=4; a = false; If(x==4){ } 36

37 Comparisons #1 < less than > greater than == equal to – Yes, that is two equals signs x = 4 means ‘assign the value 4 to x’ x == 4 means ‘does x equal 4 ? Yes (true) or No (false) 37

38 What kind of decisions Conditionals are based on TRUE or FALSE x=4; a = false; If(x==4){ Print “Elephant!”; } If(a){ Print “Pink Elephant”; } 38

39 Content Programming Overview The JVM A brief look at Structure –C–Class –M–Method –S–Statement Magic incantations –m–main() –o–output Coding a Dog Programming Principle(1) –I–If and Boolean operations –C–Coding a Bank Account Quick look at ToolBox 39

40 Back to Banking public class Account{ int balance;//the bank balance int amount;//the amount to withdraw boolean active;// true if the account is active //some code omitted } 40

41 Withdrawal public class Account{ int balance;//the bank balance int amount;//the amount to withdraw boolean active;// true if the account is active //some code omitted public void withdrawal(){ if(!active) } } 41

42 Comparisons #2 ! means NOT or negate – (x!=4) means ‘TRUE if x is not equal to 4’ && means AND – (x==4 && y==2) means: – TRUE if x equals 4 AND y equals 2 || means OR – (x==4 || y==2) – TRUE if either x equals 4 OR y equals 2 (or both) 42

43 Withdrawal public class Account{ int balance;//the bank balance int amount;//the amount to withdraw boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawal{ if(!active){ System.out.println(“Your account isn’t active”); } 43

44 Withdrawal public class Account{ int balance;//the bank balance int amount;//the amount to withdraw boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawal{ if(!active){ System.out.println(“Your account isn’t active”); }else if(amount>balance){ System.out.println(“Not enough money”); } 44

45 Withdrawal public class Account{ int balance;//the bank balance int amount;//the amount to withdraw boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawal{ if(!active){ System.out.println(“Your account isn’t active”); }else if(amount>balance){ System.out.println(“Not enough money”); }else{ balance = balance – amount; } 45

46 The flow of if The program will only flow into one ‘slot’. Then it goes to the end 46 public void withdrawal{ if(!active){ System.out.println(“Your account isn’t active”); }else if(amount>balance){ System.out.println(“Not enough money”); }else{ balance = balance – amount; }

47 The flow of if 47 public void withdrawal{ if(!active){ System.out.println(“Your account isn’t active”); }else if(amount>balance){ System.out.println(“Not enough money”); }else{ balance = balance – amount; } active = TRUE &&

48 The flow of if 48 public void withdrawal{ if(!active){ System.out.println(“Your account isn’t active”); }else if(amount>balance){ System.out.println(“Not enough money”); }else{ balance = balance – amount; } active = TRUE &&

49 Content Programming Overview The JVM A brief look at Structure –C–Class –M–Method –S–Statement Magic incantations –m–main() –o–output Coding a Dog Programming Principle(1) –I–If and Boolean operations –C–Coding a Bank Account Quick look at ToolBox 49

50 A quick word about toolbox Getting input and output can be a pain in java. So can some other things ECS have provided a ‘toolbox’ for you to use. This toolbox is a class of useful methods Like readStringfromCmd() 50

51 Getting input String theword; //or whatever name you choose theword = Toolbox.readStringFromCmd(); Entering this in you code means when the program gets to that line – The command prompt will ask you for a string – You enter the string and press enter – The program keeps going, with the string you typed stored in the variable 51

52 Toolbox Has some other useful functions, we’ll introduce them as we need them But you don’t have to use them if you know what to do already. Have fun in the lab! 52


Download ppt "Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming."

Similar presentations


Ads by Google