Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.

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

CS0007: Introduction to Computer Programming
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Classes and Objects in Java
Chapter 2: Java Fundamentals Java Program Structure.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
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.
Introduction to Java.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Java Language and SW Dev’t
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
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.
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Classes and Objects in Java
Introduction to Java Java Translation Program Structure
Review, Pseudocode, Flow Charting, and Storyboarding.
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.
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
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.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
31/01/ Selection If selection construct.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
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?
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS 201 Lecture 1 (b) Using an IDE Tarik Booker CS 201: Introduction to Programming California State University, Los Angeles.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Foundations of Programming: Java
Content Programming Overview The JVM A brief look at Structure
CS0007: Introduction to Computer Programming
Classes and Objects in Java
IF statements.
Introduction to javadoc
An Introduction to Java – Part I, language basics
Comp1202: Introduction III
Introduction to javadoc
Overview of Java 6-Apr-19.
Chapter 2: Java Fundamentals
LCC 6310 Computation as an Expressive Medium
Classes and Objects in Java
Methods/Functions.
Presentation transcript:

Comp1004: Introduction III Java

Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations – The main() method A First Example – Defining an Account class – If and Boolean operations Introducing Toolbox

How Java Works

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

From code to program You write code... You compile code You run the program My Code.java My Program.class javac My Program.class Me The Computer The JVM

What is a JVM? The JVM Java Virtual Machine Each operating system / machine is different at a very low 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?

The JVM So the JVM is software that allows your Java program to run on any platform where there is a JVM What is a JVM? Your Program The Machine

To compile and run a class Save with same name as class – E.g. Dog.java – (Rule: Must do this) Command line to folder Compile (create the.class file) – javac Dog.java Run (execute the.class file) – java Dog 8

Writing a Class in Java

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); }

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } Source file A Source file is a basic text file. Normally these end.txt, but for java we always save them as.java

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } Source file Class Remember. A class is the blueprint for an object

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } Source file Class Member Variable Member variables are how Java handles properties, they store data about the class It means that all objects (of this class) will contain their own copy of the variable

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } Source file Class Member Variable Method Methods do things, they define behaviour.

Dog.java Structure public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } Source file Class Member Variable Method Statement Statements are the lines that actually do the operations. Each statement ends in a ;

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

Dog.java Magic Incantations public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); }

Magic Incantations We will explain all these things You may not understand the ‘incantations’ But don’t let it bother you, trust that they work You will understand later You can start going nowhere fast if you try and understand everything at once! Go with the flow

Magic Incantation #1 Where does a program start? The program is made up of lots of classes, and those classes have methods, but which method is called first? In a special method called ‘main’ public static void main(String[] args){ }

The main method Creates the objects you need and tells them what to do, a bit like a conductor in an orchestra It doesn’t matter which class you put the main method in. You can put it in its own one if you like – But there must be 1 and only 1 in the whole program (program is a set of classes that work together) Dog.java public class Dog { public String noise = “Woof”; public void bark() { System.out.println(noise); } public static void main(String[] args){ Dog d = new Dog(); d.bark(); }

A First Example

Banking Example public class Account{ int balance = 10;//the bank balance boolean active = false;//true if the account is active public void withdrawFiver(){ balance = balance – 5; } Note that we can use // to show that everything that follows on that line is a comment and should be ignored by Java int and boolean are variable types, they tell Java what sort of thing is stored in the variables

Making decisions Often we want to make the result of a program conditional on something If the bank account is not active 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 a control structure called if/else

Withdrawal public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver(){ if(active != true) System.out.println(“Your account isn’t active”); } a != b is a conditional operator, it performs a logical test on a and b and returns true if they are not equal. Other conditionals include and == The one statement immediately following the if will only be run if the condition between the () brackets is true

Withdrawal public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver(){ if(active != true) { System.out.println(“Your account isn’t active”); System.out.println(“No withdrawal allowed”); } We can use { } to group several statements together so that the if applies to all of them together

Withdrawal public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver(){ if(!active){ System.out.println(“Your account isn’t active”); System.out.println(“No withdrawal allowed”); } We can simplify ! Is a logical operator, it reverses a logical value (so !a returns true if a is false) Other logical operators are OR (a || b), AND (a && b)

Withdrawal public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver{ if(!active){ System.out.println(“Your account isn’t active”); System.out.println(“No withdrawal allowed”); }else { balance = balance - 5; } Optionally we can add an else clause.

Withdrawal public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver{ if(!active){ System.out.println(“Your account isn’t active”); System.out.println(“No withdrawal allowed”); }else { if(balance < 5) { System.out.println(“Not enough money!”); } else { balance = balance - 5; } A whole if/else block is actually a single statement, so we can chain them together like this

Withdrawal public class Account{ int balance;//the bank balance boolean active;// true if the account is active active = true;//set active to true //some code omitted public void withdrawFiver{ if(!active){ System.out.println(“Your account isn’t active”); System.out.println(“No withdrawal allowed”); }else if(balance < 5) { System.out.println(“Not enough money!”); } else { balance = balance - 5; } We can simplify Because an if/else block is actually considered a single statement we can get rid of some of these brackets and tidy up

The Lab

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.

Summary How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations – The main() method A First Example – Defining an Account class – If and Boolean operations Introducing Toolbox Good luck for the Lab!