Download presentation
Presentation is loading. Please wait.
Published byAllison Golden Modified over 6 years ago
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
2
Lecture 2 Java
3
Java OO 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 Principal Java OO
4
From code to program You write code... You compile code
My Code x = 1 ; (raining) if { takeUmbrella() } You write code... You compile code You run the program My Code My Program Compiler This is true for most, but not all languages. It isn’t true for php or other scripts, but it is similar My Program Me The Computer
5
From code to program You write code... You compile code
My Code x = 1 ; (Southampton) if { takeUmbrella() } You write code... You compile code You run the program My Code My Program Javac My Program Me The Computer The JVM
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?
7
Why the JVM is good Lets say Java is this shape:
Windows is this shape: Mac is this shape: Unix is this shape:
8
Fitting
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:
10
Fitting
11
That is all you need to know about the JVM for this course Stop thinking about it
12
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
13
Structure class method statement method statement OO Source file Class
Source file – has .java extention Every object is based on a class. A method goes in a class, between the classes curly braces. A statement goes in a method. method Statement statement
14
What does Source File mean?
OO 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 Source file – has .java extention Every object is based on a class. A method goes in a class, between the classes curly braces. A statement goes in a method.
15
What does Class mean? public class Dog { }
OO What does Class mean? A class is the blueprint for an object public class Dog { The opening brace This is a class Public so everyone can see it We’ll come back to this later in the course The name of the class Source file – has .java extention Every object is based on a class. A method goes in a class, between the classes curly braces. A statement goes in a method. } The closing brace
16
What does Method mean? } Methods do things, they define behavior
OO What does Method mean? Methods do things, they define behavior public void bark () { The opening brace This is the return type. Void means we are returning nothing Public so everyone can see it We’ll come back to this later in the course The brackets, these sometimes contain arguments to the methods Source file – has .java extention Every object is based on a class. A method goes in a class, between the classes curly braces. A statement goes in a method. The name of the method } The closing brace
17
What does Statement mean?
OO What does Statement mean? Statements are the code that do the operations Print “Woof”; Every statement ends with a ; Sometimes you write looooooooooooooooong statements that break over lines. Semicolon tells the compiler where the line ends 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 Source file – has .java extention Every object is based on a class. A method goes in a class, between the classes curly braces. A statement goes in a method.
18
? Hang on a sec.... public void bark(){ Print “Woof”; }
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. public void bark(){ Print “Woof”; }
19
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
20
Before we start to think about coding...
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
22
Magic Incantations For example
We started looking at class structure... 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!
23
Magic Incantations i.e. You can start going nowhere fast
These slides will give you tools to write your first programs Go with the flow You may not understand the ‘incantations’ don’t let it bother you, trust that it works You will understand later
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?
25
Magic Incantation #1 Where does a program start? In a special method?
public static void main(String[] args){ }
26
A post it note? Make a dog object Tell it to bark
public static void main(String[] args){ } is a post-it note With instructions for how to set your program up Make a dog object Tell it to bark
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)
28
Magic Incantation #2 How do I output things to the screen?
System.out.println(“Your text here”);
29
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
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
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
32
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
33
First Building Block If Statements
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 Else Allow the withdrawal For this we need to cover variables in more depth
35
Variables -Just like algebra?
Storing values In variables Eg Dog int age; age = 3; Name of variable – think about ‘x’ in algebra Data type: Can be class or primitive Actual value to put into variable More on variables next lecture
36
? What kind of decisions Conditionals are based on TRUE or FALSE x=4;
a = false; If(x==4){ } ?
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)
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”;
39
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
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 }
41
? Withdrawal if(!active) } 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) } ?
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)
43
Withdrawal if(!active){
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”); }
44
Withdrawal if(!active){
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”); }
45
Withdrawal if(!active){
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; }
46
The flow of if active = false The program will only flow into one ‘slot’. Then it goes to the end 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 active = TRUE && amount > balance
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; }
48
The flow of if active = TRUE && public void withdrawal{ if(!active){
amount <= balance 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; }
49
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
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()
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
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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.