Content Programming Overview The JVM A brief look at Structure

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)
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.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Classes and Objects in Java
Introduction to Java Java Translation Program Structure
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.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
CS 177 Recitation Week 1 – Intro to Java. Questions?
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
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.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Introduction to java (class and object). Programming languages: –Easier to understand than CPU instructions –Needs to be translated for the CPU to understand.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
JavaScript: Conditionals contd.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 1b- Introduction
Dept of Computer Science University of Maryland College Park
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CS0007: Introduction to Computer Programming
Introduction to Computer Science / Procedural – 67130
Classes and Objects in Java
Debugging and Random Numbers
IF statements.
(A crash-course overview for humanities folk) Part I
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,
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.
Introduction to CS Your First C Programs
Number and String Operations
Comp1202: Introduction III
Introduction to Primitive Data types
Introduction to TouchDevelop
Java Programming Control Structures Part 1
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Classes and Objects in Java
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

Lecture 2 Java

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

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

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

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?

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

Fitting

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

Fitting

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

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

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

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.

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

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

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.

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

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

Before we start to think about coding...

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

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!

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

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?

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

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

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)

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

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

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

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

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

First Building Block If Statements

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

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

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

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)

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

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

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 }

? 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) } ?

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)

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

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

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

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

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

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

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

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()

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

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!