Introduction to Java 2 Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Java 2 Programming

Description:

Introduction to Java 2 Programming ... Packages Group related classes together Each class in a package must have a unique name Indicate the package a ... Leigh Dodds ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 13
Provided by: Leig122
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java 2 Programming


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

2
Overview
  • Java Syntax
  • Quick Overview
  • 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
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.

5
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

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

7
Examples (if)
  • if (x y)
  • //executes if true
  • if (somethingIsTrue())
  • doSomething()
  • else
  • doSomethingElse()

8
Example (for)
  • int x0
  • for (int i1 ilt10 i)
  • //code to repeat ten times
  • x x i

9
Example (while)
  • int x0
  • while (x lt 10)
  • doSomething()
  • x
  • //loop forever
  • while (true)

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

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

12
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
Write a Comment
User Comments (0)
About PowerShow.com