Introduction to Java 2 Programming

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Introduction to Java 2 Programming Lecture 2 Java Syntax, Robocode.
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Java Syntax Primitive data types Operators Control statements.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Primitive Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Primitive Variables.
A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
LCC 6310 Computation as an Expressive Medium Lecture 2.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
1.2 Primitive Data Types and Variables
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
MIT AITI Lecture 2 The of Java In the following lectures you will learn the basic structures that make up the Java programming language: »Variables.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Fundamentals 2.
Topic 2 Elementary Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Elementary Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Variables and Primative Types
Data types and variables
Java Programming: From Problem Analysis to Program Design, 4e
Computers & Programming Languages
IDENTIFIERS CSC 111.
Principles of Computer Programming (using Java) Chapter 2, Part 1
Unit-2 Objects and Classes
Computers & Programming Languages
An Introduction to Java – Part I, language basics
Chapter 2 Variables.
Object-Oriented Programming
Chapter 2: Java Fundamentals
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
Chapter 2: Java Fundamentals
Java Programming Review 1
Chapter 3 Introduction to Classes, Objects Methods and Strings
IAT 800 Foundations of Computational Art and Design
LCC 6310 Computation as an Expressive Medium
Variables and Constants
Presentation transcript:

Introduction to Java 2 Programming Lecture 2 Java Syntax, Robocode

Overview Java Syntax Robocode Practical Exercises The Basics Classes, Packages Robocode How it works Writing Robots Anatomy of a Robot Practical Exercises

Naming All Java syntax is case sensitive Valid Java names Consist of letters, numbers, underscore, and dollar Names can only start with letter or underscore E.g. firstAttribute but not 1stAttribute “Camel case” convention Java encourages long, explanatory names Start with a lower case letter, with words capitalised E.g. thisIsCamelCase, andSoIsThisAsWell Meaningful names are an important way to make your code understandable. While the later examples don’t always follow that recommendation, that’s just to save space on the slides. Camel case is also recommended as it makes long identifier names much more readable. It’s called camel case because there are humps in the words! Note that there are naming conventions for method names and variables also (see later slides)

Java Types Java has two basic types Reference types Primitive types integers, floating point numbers, characters, etc Refer to actual values Reference types Arrays, Classes, Objects, etc Refer to memory locations (by name, not location) Primitive types are basic values. References types are structures (arrays, classes, objects, etc). A reference is as close as Java gets to a C pointer, but references are accessed by name and not by location. A Java programmer has no control over memory allocations, locations, etc. Ideally in an OO language everything would be an object of some form, and Java does have objects that represent the kinds of values that can be stored in primitive types. However reference types take up more memory so for efficiency, Java allows values to be stored in a primitive form.

Primitive Types Type Description Size Boolean (boolean) True/false value 1 bit Byte (byte) Byte-length integer 1 byte Short (short) Short integer 2 bytes Integer (int) Integer 4 bytes Long (long) Long Integer 8 bytes Float (float) Single precision floating point number Double (double) Double precision float Char (char) Single character

Syntax Examples (Variables) int anInteger; Boolean isSwitchOn; Variables can be initialised when they are declared Int anInteger = 10; Boolean isSwitchOn = true;

Syntax Examples (if) if (x == y) { //executes if true } if (somethingIsTrue()) doSomething(); else doSomethingElse();

Example (for) int x=0; for (int i=1; i<=10; i++) { //code to repeat ten times x = x + i; }

Example (while) int x=0; while (x < 10) { doSomething(); x++; } //loop forever while (true)

Methods Define some behaviour of a class Method declarations have four basic sections, and a method body: Visibility modifier (who can call the method) Return type (what does it return) Method name Parameter list (what parameters does it accept) Methods can also indicate what kinds of errors (exceptions) might arise when they’re called. We’ll cover these in a later section. Methods defined in interfaces don’t have a method body, again we’ll cover these later. There are also some additional types of modifiers (e.g. abstract, final) which we’ll also cover later!

Syntax Examples (Methods) public void calculatePayroll() { //code goes here } private int addNumbers(int x, int y)

Method Naming Conventions If a method sets some value on an object public void setVatLevel(float vat); If a method retrieves some value from the object public float getTotalPayroll(); If a method returns a boolean value public boolean isSwitchOn();

Classes One Java class defined in each .java file File name must match the name of the class Otherwise there will be compilation errors Class names start with an upper case letter Compiler will generate a .class file with same name Contains the bytecode Classes defined using the class keyword. There’s no equivalent of the C/C++ header file: just the .java file. One class per file (some exceptions, but outside the scope of this course). File name matching the class name is important. File names are case sensitive, just as class names are.

Packages Group related classes together Each class in a package must have a unique name Indicate the package a class belongs to with the package keyword Recommended each class is put in a package Gain access to public classes in other packages using the import keyword The JVM needs to know where the classes are defined before you can use them Classes declare that they belong to a package. There’s no way for a package to be defined separately. Packages are a way of grouping relating classes together, to build a modular unit. The basic Java API has a Large number of pages, e.g. java.lang for the basic, java.util for utility classes, java.net for networking, etc. If a class wishes to refer to a class defined in another package, it must declare that it needs access to that package. This is done using the import keyword. It’s the responsibility of the JVM to find the relevant package, and classes on disk.

Anatomy of a Java Source File package intro2java; import java.util.*; /** * A description of the class */ public class MyFirstClass { //attributes private boolean isSomething; private int aCounter; //methods public void doSomething() { //code goes here }

Common Sources of Error Mistakes in naming Wrong case, illegal names class name and source file name not the same Missing semi-colon Missing curly brackets Incorrect Loop checks Loop is too large/small, or occurs forever Testing equality Assignment with = (single equals sign) Comparison with == (two equals signs)

Overview Java Syntax Robocode Practical Exercises Quick Overview How it works Writing Robots Anatomy of a Robot Practical Exercises

Anatomy of a Robot package yourname; import robocode.*; /** * Description of Robot */ public class MyRobot extends Robot { * Description of run method public void run() { //the robots behaviour }