Download File - LSESU Applicable Maths Society

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JAVA Workshop!
Applicable Mathematics Society!
Why Java?!
• 
• 
• 
• 
Run once deploy everywhere (runs on ‘virtual
machine’ JVM)!
Most used programming language in the world!
Very similar to C++, C#!
Most Android applications are programmed in Java!
2!
Workshop contents!
• 
• 
• 
• 
• 
• 
• 
Introduction to course and first program!
Variables and Operators!
Console input / if - else statements!
Loops (while, do while, for statements)!
Classes and Objects!
Arrays!
Exception Handling Basics!
3!
Recommended Text Books!
• 
• 
• 
Absolute Java (Walter Savitch)!
Java - A beginner’s guide (Herbert Shildt)!
Many other books are available!
4!
How the computer works!
Memory!
Central Processing Unit!
(CPU)!
Input/Output !
(IO) Devices!
Example of CPU instructions!
• 
z=x+y!
Read location x!
Read location y!
Execute addition!
Write to location z!
Installing Java on your
laptop !
If you have a windows laptop you have to download
the java development kit (JDK)
•  Download the appropriate development kit from
here: !
http://www.oracle.com/technetwork/java/javase/
downloads/index.html !
•  You need to set-up the path variables:
http://docs.oracle.com/javase/tutorial/essential/
environment/paths.html
• 
7!
Writing a java program!
1st step: Creating a class!
!
public class welcome!
!
{!
!
!
!
!
!
!
!
!
!
}!
!
!
8!
Writing a java program!
2nd step: Adding the main method!
public class welcome!
!
{!
!
public static void main(String
[] arguments)!
!
!
{!
!
!
!
!
!
!
!
!
!
! !
}!
!
}!
9!
First Java Program!
public class welcome !
{!
public static void main(String[] arguments)!
{!
//Program execution begins here
System.out.println(“Hello World”);!
!
} //end method
} //end class
Output:!
Hello World!
10!
Program Structure!
public class CLASSNAME
{!
public static void main(String [] arguments)!
{!
!
//Commands
}!
}!
11!
Output!
System.out.println(String)!
•  Outputs a word or a number to the console.!
•  Output has to be between speech marks.!
•  i.e “Output123_*&!”!
12!
Example 2!
public class Second{!
public static void main (String[] arguments){!
System.out.print(“Hello”);!
System.out.println(“Goodbye”);!
}!
}!
Output:!
HelloGoodbye!
13!
How do we compile and run
our program!
1.  We need to save our class in a file with a .java
extension. The name of the class has to be the same
as the name of the file.
2.  Using the terminal (or command for Windows
users) window, we have to direct the computer to the
directory (folder) the file is in, using the cd (change
directory) command. If you want to “move up” a
folder you use, cd NameOfFolder, if you want to go
back, “move down”, you use cd .. .
3.  To compile we type javac welcome.java!
4.  To run we type java welcome
14!
Now you try it!!
public class welcome {!
public static void main(String[] arguments){!
//Program execution begins here
System.out.println(“Hello World”);!
!
} //end method
1.  Open Terminal window!
} //end class
2.  cd NameOfNextFolder!
3.  javac NameOfClass.java!
4.  java NameOfClass!
15!
Write a java program that
outputs the following:!
*!
**!
*****!
**!
*!