Java Programming Course
Lecture1
Organization of the course
Java basics
Prof. Mohammed A.M. Ibrahim
Java Programming
1
Course objectives
•Knowledge and understanding of the principles
of object oriented programming,
•Knowledge of and experience with (the syntax
and semantics of) Java, including important parts
of the Java API,
•Knowledge of and experience with solving simple
problems by the design and implementation
object oriented programs.
Prof. Mohammed A.M. Ibrahim
Java Programming 2
Contents of the lectures (provisional)
1. Java basics
2. Selection, iteration
3. Methods, specification
4. Array’s, algorithms
5. Application of classes, UML class diagrams
6. Implementation of classes
7. Inheritance
8. AWT, Swing, Java Interfaces, Events
9. Exceptions, File IOJava
10. Database Connectivity and Networking Classes
11. Utility classes
12. Written examination
Prof. Mohammed A.M. Ibrahim
Java Programming 3
What is the purpose of a program? How does it operate?
Manipulating data: (storing data, retrieving data,
combining
data, transforming data, …).
Data is stored in memory, it is manipulated by a
cpu (central
processing unit)
12
13
14
15
16
17
Memory
+
CPU
1
2
Prof. Mohammed A.M. Ibrahim
Java Programming
4
Low level language, high level language
Copy the contents of MEM13 to CPU1
Copy the contents of MEM15 to CPU2
Add the contents of CPU1 to the contents of CPU2
Copy the contents of CPU2 to MEM12
Low level statements:
MOV M13 C1
MOV M15 C2
ADD C1 C2
MOV C2 M12
High level statement:
som = getal1 + getal2;
Prof. Mohammed A.M. Ibrahim
Java Programming 5
Compilation of Pascal, C, C++, … programs.
Prof. Mohammed A.M. Ibrahim
Java Programming 6
Compilation and execution of a Java program
Prof. Mohammed A.M. Ibrahim
Java Programming 7
Tools for writing, compiling and executing Java programs
Texteditor => putting together the program text
Compiler => translation of program text to bytecode
Virtual machine => execution of bytecode
javac Hello.java //call of the compiler
java Hello //call of the virtual machine
Prof. Mohammed A.M. Ibrahim
Java Programming 8
Example program: class Hello
Text editor:
public class Hello{
public static void main(String[]
args){
System.out.println(“Hello”);
}
}
The file MUST be saved as: Hello.java
Prof. Mohammed A.M. Ibrahim
Java Programming 9
Result of the compilation and execution
Prof. Mohammed A.M. Ibrahim
Java Programming 10
Context, a Java Development Environment (IDE)
Prof. Mohammed A.M. Ibrahim
Java Programming 11
Class Hello
//example of a class structure
public class Hello{
//main method, driver of the program
public static void main(String[]
args){
//example of a (print)
statement
System.out.println(“Hello”);
}
}
Prof. Mohammed A.M. Ibrahim
Java Programming 12
Basic ingredients of Java
Data types: collection of data elements + operators
Integer numbers: int byte, short, long
Floating numbers: double float
Logic values: boolean
Letters, digits, writing & reading signs: char
Text: String
Prof. Mohammed A.M. Ibrahim
Java Programming 13
Datatype int
Data elements: -2147483648 t/m
+2147483647
operators: +, -, *, / and %
3 / 4 -> 0 3 % 4 -> 3
5 / 4 -> 1 5 % 4 -> 1
Integer division Modulo division
All elements of the interval are available.
Prof. Mohammed A.M. Ibrahim
Java Programming 14
Storing data
During the execution of a program data is stored in
memory.
This data is retrieved and restored many times.
How much room is needed to store data?
At which location in the memory is the data stored?
How is data stored?
How is data retrieved?
Prof. Mohammed A.M. Ibrahim
Java Programming 16
Declaration of an int-variabele
int score;
A memory cell is allocated to store 1 int value.
The location of the memory cell is not available (hidden).
The memory cell is identified by the name of the variable
score
Prof. Mohammed A.M. Ibrahim
Java Programming 17
Assignment operator =
public static void main(String[] args){
int x;
x = 10;
double d;
d = 34.67;
String s;
s = “abcd”;
}
Prof. Mohammed A.M. Ibrahim
Java Programming 18
Application of memory locations
public static void main(String[] args){
int count; //creation of the
variable
count = 23; //storing data
System.out.print(count);
//retrieving, printing
count = count + 1; //retrieving,
storing
System.out.println(count); //
retrieving, storing
}
Prof. Mohammed A.M. Ibrahim
Java Programming 19
Application of memory locations (continued)
public static void main(String[] args){
//creation, storing
double average = 4.5;
//retrieving, storing
average = average + 3.4;
//creation, storing
String name = “Ali”;
//retrieving, storing
name = name + “alHassany”;
}
Prof. Mohammed A.M. Ibrahim
Java Programming 20
Writing data: print() and println()
System.out.print(3);
System.out.println(13.5);
System.out.print(“The count is: “);
System.out.println(count);
System.out.println(3 * 4.5);
System.out.println(“Name:” + “Ali”);
System.out.println(“Count: “ + count);
System.out.println(“My name is: ” +
name);
Prof. Mohammed A.M. Ibrahim
Java Programming 21
Importing data: class Scanner
import java.util.*;
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
double average = sc.nextDouble();
String name = sc.next();
Prof. Mohammed A.M. Ibrahim
Java Programming 22
Example program
import java.util.*;
public class Example{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“Type an int-number:
“);
int number = sc.nextInt();
System.out.println(“The number is “ +
number);
}
}
Prof. Mohammed A.M. Ibrahim
Java Programming 23