Games Course, summer Introduction to Java. Frédéric Haziza

Similar documents
3. Java - Language Constructs I

Index COPYRIGHTED MATERIAL

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Pace University. Fundamental Concepts of CS121 1

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to Programming Using Java (98-388)

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

COMP 202 Java in one week

CSC 1214: Object-Oriented Programming

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Computer Components. Software{ User Programs. Operating System. Hardware

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

CS 231 Data Structures and Algorithms, Fall 2016

Java Bytecode (binary file)

Building Java Programs. Introduction to Programming and Simple Java Programs

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

3. Java - Language Constructs I

DM550 Introduction to Programming part 2. Jan Baumbach.

STRUCTURING OF PROGRAM

An overview of Java, Data types and variables

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

Lecture 1: Basic Java Syntax

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

2 rd class Department of Programming. OOP with Java Programming

DM503 Programming B. Peter Schneider-Kamp.

Basics of Java Programming

1 Shyam sir JAVA Notes

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Program Fundamentals

CompSci 125 Lecture 02

PROGRAMMING FUNDAMENTALS

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Entering the world of Javatar

CS11 Java. Fall Lecture 1

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Java Programming. Atul Prakash

Certified Core Java Developer VS-1036

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Array. Prepared By - Rifat Shahriyar

Character Stream : It provides a convenient means for handling input and output of characters.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Language Fundamentals Summary

CSCI 2101 Java Style Guide

Static Methods & Decomposition

Java: introduction to object-oriented features

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

ECE 122 Engineering Problem Solving with Java

The Java Language The Java Language Reference (2 nd ed.) is the defining document for the Java language. Most beginning programming students expect

Ticket Machine Project(s)

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Full file at

Java: framework overview and in-the-small features

B.V. Patel Institute of BMC & IT, UTU 2014

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Course Outline. Introduction to java

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Introduction to Java

Basic Types, Variables, Literals, Constants

CS212:Data Structure

Fall 2017 CISC124 9/16/2017

Programming in C++ 4. The lexical basis of C++

Java Overview An introduction to the Java Programming Language

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Syntax and Variables

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Class, Variable, Constructor, Object, Method Questions

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Getting started with Java

Chapter 2. Elementary Programming

Key Differences Between Python and Java

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Creating a C++ Program

About this exam review

Introduction to. Android Saturday. Yanqiao ZHU Google Camp School of Software Engineering, Tongji University. In courtesy of The Java Tutorials

Accelerating Information Technology Innovation

9 Working with the Java Class Library

Basics of Java Programming. Hendrik Speleers

Lecture 1: Overview of Java

CHAPTER 7 OBJECTS AND CLASSES

Transcription:

Games Course, summer 2005 Introduction to Java Frédéric Haziza (daz@it.uu.se) Summer 2005 1

Outline Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 2

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 3

Language Evolution Your friend: http://java.sun.com J2SE : Standard Edition Suitable for 'usual' Desktops J2EE : Enterprise Edition Suitable for Servers J2ME : Minimal Edition Suitable for Mobile 4

Java 2 SDK Standard Development Kit Latest stable version : 1.4.2 and 5.0 http://java.sun.com/j2se/index.jsp JRE : Java Runtime Environment (java command) Able to run Java programs only Compilation (javac command) Comes with the SDK, able to build and run Java programs 5

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 6

The Java Programming Language Object-Oriented See : http://java.sun.com/docs/books/tutorial/java/concepts/index.html Compilation is necessary Compilation = Translation of human readable code into machine code 7

Java Virtual Machine JVM : Java Virtual Machine Bytecode Written once, Runnable everywhere 8

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 9

Cooking an egg 1. Name of the recipe : Cooking egg 2. The materials : A pot (No holes and empty) Water (sufficiantly, no nitrates) A stove (Heat power mini 1000 Watts) an egg (Not roten) 3. Actions : Put water in the pot Boil the water Put the egg in the water Wait 3 minutes Take out the egg from the water 10

Algorithm Algorithm : 1. Name 2. Data, which are used in the algorithm 3. Behaviour, description of the chained actions The person knows what actions are possible with the specified objects Put the pot in the egg, boil the egg, cut the pot in small piece. The pot can be in different states : empty, full of water or milk, to a specific temperature All pots have properties that distinguish them from oven. ==> The item pot is of type "pot", or belongs to the class "pot" 11

Type In algorithmics, all items have a type. This type combines: a set of values/attributes/states that this item can have/take a set of actions, which can be applied to those item of that type, allowing to get/set/modify the values. Because the operation "cut in small pieces" hasn't been defined for the type "pot", it is impossible, and even forbidden, to cut a pot in small pieces. 12

Predefined types int, long, short float, double boolean char byte void Self contained type Determines the set of value it can have 13

Primitive Type Example For byte, from -128 to 127, inclusive (8bits) For short, from -32768 to 32767, inclusive (16 bits) For int, from -2147483648 to 2147483647, inclusive (32 bits) For long, from -9223372036854775808 to 9223372036854775807, inclusive (64 bits) For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535 (16 bits -- unsigned) Literal 178 8864L 37.266 37.266D 87.363F Data Type int long double double float Literal 26.77e3 'c' true false Data Type double char boolean boolean 14

Strings Character chains Special class in Java, just for our convenience :) String str1 = Salut ; String str2 = comment ça va? ; String str3 = str1 +, + str2; str3 ----> Salut, comment ça va? 15

Type Conversion Type conversion = change from one type to another byte i; (max of 127) int j = 468; i= j; Value of i? 468 in binary is 111010100 Depending on the system: 111010100-106 -84 16

Type Cast Type cast = identify as belonging to a certain type int i; short j; float k; i = 468; j= (short)i; (No problem, 468 spans within the range) k = 3.2; i = (int)k; NB: i is 3 only! String str = 3 ; i = str3; ==> Cast error Idem for classes (cf. later) Conclusion: Not always possible, could lead to mishandlings Permits to view temporarily a typed variable as another type 17

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 18

Hello World! example class FirstProgram{ public static void main (String[] args) { System.out.println( Hello World! ); } } 19

Hello World! revisited class SecondProgram { } public static void main(string[] args) { if(args.length>0) { System.out.println( Welcome to the real world, + args[0]); } else { System.out.println( No arguments, welcome anyway! ); } } 20

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 21

Java Syntax Statements are followed by a semi-colon System.out.println( Hello World! ); Blocks are embraced in curly braces {} Parameters are supplied within brackets () List items are accessed through [] List[3] is the 4 th item in the list, if it exists... 22

Identifiers Examples: String i3 MAX_VALUE isletterordigit No start with a digit No start with?:';^(\) <>$& -+/*%! Keywords abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized 23

Comments There are two kinds of comments: /* text */ all the text in between is ignored // text all the text to the end of the line is ignored Comments do not nest Special comments: Javadoc comments /** javadoc comments */ 24

Operators = > <! ~? : == <= >=!= && ++ -- + - * / & ^ % << >> >>> += -= *= /= &= = ^= %= <<= >>= >>>= 25

Coding Conventions Class names start with a capital letter Method and variable names start with a small letter If the name is composed of several words (to give some sort of sens to it), each word begins with a capital letter, no space, no underscore extractsomepetrolfromtheground(technic t){} BankAccount familyaccount; Indentation : SimpleScreenManager.java 26

Visibility 4 access modifiers 'private', 'protected', 'public' or nothing (you can see 'friendly' sometimes) Public: all class can access the attribute / call the method Private: only the current class can Protected : children can Public Protected Private 27

Method declaration Signature = Combination of return value, name, parameters list and access modifiers public static void main(string[] arg){...} private static void main(string arg){...} ==> <AccessModifiers...> <Return> <Name>(<Typed Param...>){...} 28

Variable declaration int i; // Declaration i=17; // Affectation int i = 17; // Both int i = 12; // Illegal, i is already defined int[] myarraylist = new int[3]; int[][] mymatrix = new int[2][3]; BankAccount[] myarraylistofaccount = new BankAccount[5]; 29

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance API 30

Global or local? Scope of variable declaration : global or local Example of code if(...) { int i= 17; } System.out.println("Here is i : "+i); // Error 31

The class Person Implement an entity that represents a Person Properties : Name, P-nummer Address, City Phone number Requirements : Can have several phone numbers Can change address, city, phone numbers Implement getters and setters Return the age of the person 32

The class Person Person Properties : Name, P-nummer Address, City Phone number Requirements : Phone numbers Change address, city, phone numbers getters/setters age of the person public class Person { } private String name; private String address; private String city; private String pnum; private int[] phonenumbers=new int[5]; public Person(String aname, String anaddress, String acity, String apnum) { name = aname; address = anaddress; city = acity; pnum = apnum; } public String getcity() { return city; } Does it fullfil the requirements? public void setcity(string acity) { city=acity; } No comments! 33

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath File Naming Object - Instance API 34

Where from? Call the BankAccount class : where does it the class definition come from? The JVM detects a BankAccount type, so looks for a file named BankAccount.class under the classpath. If your classpath doesn't point to the BankAccount class definition, the class you're currently writting won't compile. The JVM doesn't go down the folder tree, you must simply add folders to the classpath. ==>prompt>classpath=folder1:folder2:folder3 You can specify the '.' folder into your classpath the JVM will examine the current folder. It will parse the classpath in order. 35

Inheritance Profession Lawyer Driver Secretary TaxiDriver BusDriver Driver Note : No multi-inheritance... 36

Inheritance (cont'd) TaxiDriver inherites from the Driver class. A TaxiDriver is a Driver too. class Driver {...} class TaxiDriver extends Driver{...} TaxiDriver specializes the Driver class. 37

Extending the Person class Recall the Person class Extend it with some extra fields and methods ==> ExtendedPerson.java specializes Person.java See the implementation and code choices 38

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath File Naming Object - Instance API 39

Object - Instance A class is a module that regroups several actions and encapsulates data Also a user-defined type Once the class is declared, we can create instances of it, the same way we declare a variable of a primitive type int i; int j,k; BankAccount myaccount, youraccount; 40

Reference - Instanciation myaccount and youraccount are not objects but references to objects. Those objects are not created yet. They point to nothing, they have the value "null" By using the operator 'new', we create an instance (and allocate the necessary memory space). The result of that operation can be appointed to the myaccount reference. ==> myaccount = new BankAccount(); This instance can be designated by other references: ==> youraccount = myaccount; 41

Null - Affectation See blackboard Once the instance is created we can pass messages to it, meaning we can call the methods that it embodies. It is important that this reference designates an instance before we pass messages onto it. (it will result a NullPointerException which stops the JVM) 42

Scope (cont'd) Recall that a variable can be global (to the class) or local (to the block, typically the method) An object will stay alive as long as a reference points to it. (No need to clean up though: Garbage Collection) Important: BankAccount a = new BankAccount(); BankAccount b = a; changesomethinginbankaccount(a); ==> b has also changed! b was just another reference to the same object For more information: See cloning and deep-cloning 43

Where to get Java Compilation Notions of Type First Program Java Syntax Scope Class example Classpath Object - Instance Import - API 44

Import and API API = Application Programmable Interface If you want to reuse code from other class, the user must supply an additional line of code import java.util.vector; import java.util.linkedlist; import java.util.calendar;... http://java.sun.com/j2se/1.4.2/docs/api/index.html 45