Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
EE2E1 (MS)
Introduction to Java programming
EE2E2 (DP)
Object Oriented Software Design
Assessment
EE2E1 and EE2E2 are assessed jointly
(making up the EE2E module)
EE2E1 is assessed through a classtest and
programming exercises and a major
programming assignment
15% through a 1 hour class test
C.S.Horstmann, G. Cornell
Amazon Link
My web page
http://www.eee.bham.ac.uk/spannm/Courses/ee2e.h
tm
Why should I learn Java?
Main reason, Java is object oriented
What is OOP good for?
Modelling asynchronously interacting objects
GUIs
Event simulation
Ray tracing visualisation
CAD simulation
Real-time control/embedded systems
Robotics
Image/Video processing
Client/Server systems
etc
OK, so whats good about Java?
Java is free to download and is easy to learn
Java has powerful (and free!) development tools
e.g. Eclipse , Netbeans
Excellent documentation support Javadocs
Great community support
Rich collection of open source libraries
Can develop Android apps in Java supported by
Eclipse and Netbeans
Android is a good platform for mobile apps
because of ease of release, wide range of
devices and its an open platform
EE2E1. JAVA Programming
Lecture 1
From C to Java
Contents
A simple Java program
Data types
Variables
Assignment/initialization
Operators
Control Flow
Strings
Arrays
Simple example Java program
public class firstProgram
{
public static void main(String[] args)
{
System.out.println(Java is fun!);
}
}
Main points
Everything in a Java program is a class
Keyword public is an access modifier
Program starts execution from the main method
We will worry about what static void means later
The program prints the string Java is fun!
System.out.println() means call the println()
method of the object System.out (which is part of
the class System)
Data types
Like C, Java is strongly typed
Java has 8 primitive data types
Machine independent storage requirements
Primitive data types
Type Storage requirement Range
int 4 bytes -2,147,483,648 ..
2,147,483,647
short 2 bytes -32768 .. 32767
18
long 8 bytes Approx 9x10
byte 1 byte -128 .. 127
38
float 4 bytes Approx 3.4x10
308
double 8 bytes Approx 1.8x10
char 2 bytes (Unicode)
boolean false, true
The char datatype
char represented by a 2-byte Unicode value
Designed to represent all characters in the written
world
Allows 65536 characters (35000 are in use)
whereas ascii only allows 255
Expressed as hexidecimal \u0000 to \uFFFF
(\u0000 to \u00FF is the ascii set)
\u indicates a Unicode value
control
In Java, keyboard input is not
straightforward
Done by the readDouble() method in
class Console
Strings
Strings are sequences of characters as in C
The standard Java library has a predefined
class String
String name = Mike;
a[][] a[0]
a[1] a[3][0]
a[2] a[3][1]
a[3]
a[4] a[3][2]
a[3][3]
Copying arrays
Copying 1 array variable to another is
equivalent (in C) to copying pointers
2
evenNumbers
4
6
newArray
8
The method System.arraycopy() should
be used to copy the array contents
System.arraycopy(from, fromIndex, to,
toIndex,n)
int[] newArray = {0,0,0,0}
System.arraycopy(evenNumbers,0,newArray,0,4);
2 2
evenNumbers
4 4
6 6
8 8
newArray
Class java.util.Arrays has a number of
convenience utility functions for arrays
Arrays.sort(a) - sorts array a into
ascending order
Arrays.fill(a,val) fills array a with value
val
Arrays.binarySearch(a, key) searches
next semester
Assessed by a formal lab report per lab
group