EE936 Computer Technology and Modern Programming Concepts
Dr B. Stephen
Institute for Energy & Environment,University of Strathclyde
Glasgow UK
EE936 Lecture 1
Introducing Java
Why Program?
Put your ideas into practice without the constraints of high level and/or third party tools:
File formats
Processing limitations
Output visualisation
Do you need any other reason?
An entirely hypothetical project scenario…
Bunch of
data
VisualiseCalculate statistics
Pre-process
Take this data (yes, all 500m records)
and…
…throw away values
that are more than 3
days old and…
…calculate higher order (not mean and variance) moments and cumulants
(what?!!?) then…
…do a time series 3D scatter plot with vector overlays. Do it again tomorrow.
An entirely hypothetical project scenario (made real)…
‘Bunch of data’ could be:
Windspeed and air temperature/pressure for every weather station in the UK at 10 second intervals every day this century
‘Pre-Process’ could be:
Downsample the data to an hourly average
‘Calculate statistics’ could be:
Extract max/min temp and wind speed variance
‘Visualise’ could be:Vector diagram overlaid on contour plot
Another entirely hypothetical project scenario…
Extractfeatures
Pre-processBunch
of documents
Bunch of
data
Take these document
s and…
…ensure they’re in the right format…
…take only parameters A, B and C
and…
…put it into a relational database.
Do it again in 5 minutes.
Another entirely hypothetical project scenario (made real again)…
‘Bunch of documents’ could be:
Satellite Images
Fault Recorder Data
Substation Maintenance Reports
‘Pre-Process’ could be:
Get rid of the ones with clouds
Average current levels
Validate report fields
‘Extract features’ could be:
Retain areas over land only (cut out areas of water)
Calculate current variance
Put reports into bins according to keywords used
Try doing that by hand – digital acquisition of data has lead to enormous data archives that need automated (and usually specialised) processing
Project Work Pitfalls
Could do this manually in Excel but:
Time consuming and labour intensive
Scope for human error
Can Excel do everything you need it to?
Limit on the number of rows…
Could possibly do this in Matlab
Automation still tricky
Do you have a Matlab license? ($$$’s…)
Can you deploy your solution everywhere?
Why Program in Java?
More functional than C and FORTRAN
Easier than C++
Not tied to a specific platform unlike C#
More widely available than Smalltalk, Ruby, Eiffel and Dylan
Still in widespread use 12 years after its first appearance
What this course will do for you:
Prepare you for project work
Show you how to build useful applications that may support future coursework
Introduce the basic concepts of Object Oriented Programming
Allow you to put your existing knowledge (as an Engineer) into practice
What this course won’t do for you:
Teach you how to manage software development projects
Teach you how to write Java Applets, Servlets or Spotlets
Although you will get the foundation knowledge to go ahead and pursue this yourself
Teach you ‘how to make Web pages’
What’s Special About Java?
Platform Independent
Automatic Garbage Collection
Object Oriented
Fully Featured
Objects and Syntax based around good programming practices
Developing Java Applications
You’ll need a text editor such as:
Forte
JBuilder
MS Wordpad (which we’ll look at)
JEdit (which we’ll not use)
Eclipse (which you can use if you want)
And a copy of the Java Development Kit (try to use the latest version wherever possible)
The Lab PCs have all we need to get started
Using the Command Line
Command Line Commands: DIR
DIR - lists the contents of the current directory and emits its path
DIR /P - as with DIR but only a page at a time
DIR *.exe - only lists exe files in the current directory
Command Line Commands: CD
CD - this tells you what the current directory is
CD <path> - this changes directory to the named directory
CD .. - this changes directory to the directory immediately above
The Development Cycle
SourceFile
Build OK?
Write/Edit Code
Run in JVM
Compile Using JAVAC
Great.
Do What you Expected?NO
NO YES
YES
// ----------------------------------------------------------- // ----------- EE936 Computer Technology & Modern ------------ // ------------------ Programming Concepts ------------------- // ----------------------------------------------------------- // Lecture 1, Example 1: The simplest program you can possibly // write in Java. // Version 1: 26th September 2005 // Version 2: 28th September 2006 // Version 3: 17th September 2007 // Author: Bruce Stephen // ----------------------------------------------------------- // -----------------------------------------------------------
public class Lecture1Example1 { public static void main(String[] args)
{ System.out.println("Hello Mum!"); }
}
Anatomy of a Java Program
The bodies of all classes
are encapsulated in
curly brackets
This is a comment. It does nothing
in any program.
Every time a statement is made
it must be terminated by a semi-colon
This is the declarationof your one and only public class
This is the method name
This is the method
argument
This is a method declaration
The bodies of all methods are enclosed
in curly brackets
Anatomy of a Java Program
Must have ONE public class per file
Must have the same filename as the public class name
Only ONE public class must have a method that is called ‘main’
‘main’ must be declared as shown
Method and class bodies must follow declarations and be enclosed in ‘{‘ & ‘}’
What is a Method?
A method is some code that carries out a task whenever it is called
This way you can repeat that task just by calling the method
Methods take inputs known as
arguments (more about this later)
public static void main(String[] args) { System.out.println("Hello
Mum!"); }
Your First Java Application
H e l l o M u m !
public class Lecture1Example1 { public static void main(String[] args)
{ System.out.println("Hello Mum!"); }
}
What does Example 1 do?
The program has a public class called Lecture1Example1
The main method of the program writes an inane message to the command line, takes a new line then exits
That’s all it does.
Example 2
H e l l o M u m !
public class Lecture1Example2 { public static void main(String[] args)
{ System.out.print("Hello "); System.out.println("Mum!"); }
}
What does Example 2 do?
Same inane message - different way of delivering it
This time we use the method print to output some text
We then output some more text and take a new line as before using println
Both print and println belong to an object already provided called System.out
Example 3
H e l l o M u m !
public class Lecture1Example3 { public static void main(String[] args)
{ System.out.print("Hello "); System.out.print("Mum!"); System.out.println(); }
}
What does Example 3 do?
Same as the last one! But done differently again…
Again we use the print and println methods in System.out but we gave them different arguments
Example 4
H e l l o
M u m !
public class Lecture1Example4 { public static void main(String[] args)
{ System.out.println("Hello "); System.out.print("Mum!"); }
}
What does Example 4 do?
Ha! Not exactly the same this time - each word is printed on its own line
The choice of method, arguments and the order the method is called dictates what the program does
Example 5
H e l l o M u m !
public class Lecture1Example5 { public static void main(String[] args)
{ System.out.print("H"); System.out.print("e"); System.out.print("l"); System.out.print("l"); System.out.print("o"); System.out.print(" "); System.out.print("Mum"); System.out.println("!"); }
}
What does Example 5 do?
Back to the same thing: inane message on one line but notice the labour intensive way we have achieved it this time
Again this serves to show how what method you call and when you call it affects your programs behaviour
What you should now know…
How to use the windows command line
How to navigate the Windows file system using cd and dir
The anatomy of a simple Java program
What to call your source files
What method you should always have in at least one of your classes
What your source files should always contain
How to build Java programs using javac.exe
How to run Java programs using java.exe
How to write to the command line window in a Java application
Questions?
Next 2 hours are Lab Time(work through the Eclipse tutorial and do the
exercises provided)