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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Chapter 6 Horstmann Programs that make decisions: the IF command.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introduction to Java Java Translation Program Structure
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Agenda Basic Logic Purpose if statement if / else statement
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
CS 177 Recitation Week 1 – Intro to Java. Questions?
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
JavaScript: Conditionals contd.
Lecture 1b- Introduction
Dept of Computer Science University of Maryland College Park
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Content Programming Overview The JVM A brief look at Structure
CS0007: Introduction to Computer Programming
Introduction to Computer Science / Procedural – 67130
Classes and Objects in Java
IF statements.
Primitive Data, Variables, Loops (Maybe)
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
User input We’ve seen how to use the standard output buffer
CompSci 230 Software Construction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Conditionals & Boolean Expressions
Comp1202: Introduction III
Introduction to Primitive Data types
Introduction to TouchDevelop
How to Run a Java Program
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Classes and Objects in Java
Namespaces, Scopes, Access privileges
Java Review Most of these slides are based on
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
Introduction to Primitives
Introduction to Primitives
LCC 6310 Computation as an Expressive Medium
Classes and Objects in Java
IS 135 Business Programming
Introduction to Primitive Data types
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

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

Lecture 2 Java 2

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

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

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

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

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

Fitting 8

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

Fitting 10

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

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

Structure OO Source file Class Method Statement 13

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

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

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

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

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

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

Before we start to think about coding... 20

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

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

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

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

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

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

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

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

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

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

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

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

First Building Block 33

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

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

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

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

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

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

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

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

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

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

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

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

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; }

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 &&

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 &&

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

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

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

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