Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java 2 Programming Lecture 2 Java Syntax, Robocode.

Similar presentations

Presentation on theme: "Introduction to Java 2 Programming Lecture 2 Java Syntax, Robocode."— Presentation transcript:

1 Introduction to Java 2 Programming Lecture 2 Java Syntax, Robocode

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

3 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

4 Java Types Java has two basic types –Primitive 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)

5 TypeDescriptionSize Boolean ( boolean ) True/false value1 bit Byte ( byte ) Byte-length integer1 byte Short ( short ) Short integer2 bytes Integer ( int ) Integer4 bytes Long ( long ) Long Integer8 bytes Float ( float ) Single precision floating point number 4 bytes Double ( double ) Double precision float8 bytes Char ( char ) Single character2 bytes Primitive Types

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

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

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

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

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

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

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

13 Classes One Java class defined in 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.

14 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

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

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

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

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

Download ppt "Introduction to Java 2 Programming Lecture 2 Java Syntax, Robocode."

Similar presentations

Ads by Google