fundamentals of programming - · pdf file03-02-2015 · java •is a programming...
TRANSCRIPT
Fundamentals of Programming
By Budditha Hettige
Overview
• Exercises (Previous Lesson)
• The JAVA Programming Languages
• Java Virtual Machine
• Characteristics
• What is a class?
• JAVA Standards
• JAVA Keywords
• How Install JAVA
• Sample Java Program
2 Budditha Hettige ([email protected])
Exercises
Draw a flowchart for the following
1. Enter two number from key bard and print average
2. Enter three number and find the maximum number
3. Process of the ATM machine
3 Budditha Hettige ([email protected])
JAVA
• Is a programming language created by James
Gosling from Sun Microsystems in 1991
• Is a general-purpose, class-based, object-
oriented Programming language
• is intended to let application developers
“write once, run anywhere.”
• Current version (JDK) 7u21
• URL : http://www.oracle.com/
4 Budditha Hettige ([email protected])
Java cont…
• Java programming language consists – Java compiler:
Java compiler translates Java coding into
byte-code
– Java virtual machine(JVM)
Java virtual machine interprets this byte-code and runs the program
– Java class libraries
Java Class Library is a set of dynamically loadable libraries that Java applications can call at run time
5 Budditha Hettige ([email protected])
Java Virtual Machine (JVM)
Architecture
JAVA Source File (.java)
JAVA Bite code (.class)
Java Compiler
Java Virtual Machine
Memory Manager
Byte code Verifier
Interpreter
Java API
6 Budditha Hettige ([email protected])
JAVA Vs C++
7
JAVA Source File (.java)
JAVA Bite code (.class)
Java Compiler
Java VM
C++ Source File (.cpp)
Executable program (.exe)
C++ Compiler
Operating system can
directly execute
JAVA VM can directly execute
Budditha Hettige ([email protected])
Characteristics
Java has the following properties
• Platform independent
• Object-orientated programming
language
• Strongly-typed programming language
• Interpreted and compiled language
• Automatic memory management
8 Budditha Hettige ([email protected])
Platform independent
• Can Run on any Platform
– Windows
– Linux
– MaC OS
9
JAVA Source File (.java)
JAVA Bite code (.class)
Java Compiler
Java VM
Budditha Hettige ([email protected])
Object-orientated programming
language
• Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (State) and methods (Behavior)
• What is an Objects? – Object is a software bundle of related state
and behavior
– Characteristics: state and behavior
– Example (Person) • State (Name, NIC, height)
• Behavior (Speech, Sleep, eat)
– Object is an instance of a class (instance is a specific realization of any object)
10 Budditha Hettige ([email protected])
What is a class?
• A Java class is a group of Java
methods and variables
• Example (Person)
– State (Name, NIC, height)
– Behavior (Speech, Sleep, eat)
11
class Person
{
}
Budditha Hettige ([email protected])
Requirements for Class name
• Class name must begin with letter of
the alphabet
• Contains only letters, digits,
underscores or dollar sign
• Cannot be a language reserved
keywords (public, class etc)
• Name cannot be following values (true,
false or null)
12 Budditha Hettige ([email protected])
Java Class name standard
• Begin with uppercase letter
• No spaces
• Emphasizes new word with an initial uppercase letter
Example
• EmployeRecords
• Student
• FirstExample
• SampleProgram
13 Budditha Hettige ([email protected])
Java Keywords
14 Budditha Hettige ([email protected])
Install JAVA
• Download Java Development Kit (JDK)
– http://www.oracle.com
• RUN Installation setup
• SET PATH for the JAVA
• Test JAVA is working
• Video Link
– http://www.dscs.sjp.ac.lk/~budditha/java/vd/ins/install.html
15 Budditha Hettige ([email protected])
Simple Java Program
• A class with a main Method public class FirstProgram { public static void main(String[] args) { System.out.println("Hello"); } }
16
Operating System
FirstProgram
Main Method Output
Budditha Hettige ([email protected])
A class without a main Method public class FirstProgram { }
Operating System Can not Execute the program
17
Operating System
FirstProgram
Budditha Hettige ([email protected])
Your first Java program
• Open a text editor (text pad, Notepad etc.)
• Type the following sample
• Save program as “FirstProgram.java”
public class FirstProgram
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
18 Budditha Hettige ([email protected])
Compile and RUN
• Using command prompt go to the place
where in your java file
• To Compile: type javac <space><fileName>
Javac FirstProgram.java
• To Run: type java<space><fileName>
Java FirstProgram
• Video Link
– http://www.dscs.sjp.ac.lk/~budditha/java/vd/s1/02.html
19 Budditha Hettige ([email protected])
First Java program
20
public class FirstProgram
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Flow chart
Start
Display “Hello World”
End Budditha Hettige ([email protected])
Print Output on Command
window
• Function
– System.out.println(“Some Text”);
– System.out.print(“Some Text”);
• Example
public class FirstProgram { public static void main(String[] args) {
System.out.println("Hello World"); } }
21 Budditha Hettige ([email protected])
Escape Sequences
• A character preceded by a backslash (\) is an
escape sequence
• has special meaning to the compiler
Escape Sequence Description
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.
22 Budditha Hettige ([email protected])
ASCII art with JAVA • ASCII art is a graphic design technique that uses computers for
presentation and consists of pictures pieced together from the
95 printable (from a total of 128) characters defined by the
ASCII Standard from
23
System.out.println(" @ @ @ ");
System.out.println(" @ @ @ @ ");
System.out.println(" @ @ @ ");
System.out.println(" \\|/ ");
System.out.println(" | | | ");
System.out.println(" | | | ");
System.out.println(" | | | ");
System.out.println(" | | | ");
System.out.println(" ( ) ");
System.out.println(" ^^^^^ "); Budditha Hettige ([email protected])
Exercise 1. Why JAVA is Platform independent?
2. What class ?
3. What is JAVA Runtime Environment (JRE) ?
4. What is JAVA Class file?
5. Write a JAVA Program to Display the Following
output
6. Try to run the above program on the Linux machine
24
-------------------------------
HSIT 2130
Fundamentals of Programming
-------------------------------
Budditha Hettige ([email protected])