fundamentals of programming - · pdf file03-02-2015 · java •is a programming...

24
Fundamentals of Programming By Budditha Hettige

Upload: vuongtram

Post on 25-Mar-2018

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

Fundamentals of Programming

By Budditha Hettige

Page 2: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 3: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 4: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 5: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 6: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 7: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 8: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 9: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 10: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 11: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 12: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 13: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 14: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

Java Keywords

14 Budditha Hettige ([email protected])

Page 15: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 16: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 17: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

A class without a main Method public class FirstProgram { }

Operating System Can not Execute the program

17

Operating System

FirstProgram

Budditha Hettige ([email protected])

Page 18: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 19: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 20: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 21: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 22: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 23: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])

Page 24: Fundamentals of Programming - · PDF file03-02-2015 · JAVA •Is a programming language created by James Gosling from Sun Microsystems in 1991 •Is a general-purpose, class-based,

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])