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
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
JAVA Is a programming language created by James Gosling from Sun Microsystems in 1991 Is a general-purpose, class-based, objectoriented Programming language is intended to let application developers write once, run anywhere. Current version (JDK) 7u21 URL : http://www.oracle.com/ 4
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
Java Virtual Machine (JVM) Architecture JAVA Source File (.java) Java Virtual Machine Java Compiler JAVA Bite code (.class) Memory Manager Java API Byte code Verifier Interpreter 6
JAVA Vs C++ JAVA Source File (.java) C++ Source File (.cpp) Java Compiler C++ Compiler JAVA Bite code (.class) Executable program (.exe) Java VM JAVA VM can directly execute Operating system can directly execute 7
Characteristics Java has the following properties Platform independent Object-orientated programming language Strongly-typed programming language Interpreted and compiled language Automatic memory management 8
Platform independent Can Run on any Platform Windows Linux MaC OS JAVA Source File (.java) Java Compiler JAVA Bite code (.class) Java VM 9
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
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) class Person { } 11
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
Java Class name standard Begin with uppercase letter No spaces Emphasizes new word with an initial uppercase letter Example EmployeRecords Student FirstExample SampleProgram 13
Java Keywords 14
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
Simple Java Program A class with a main Method public class FirstProgram { public static void main(string[] args) { System.out.println("Hello"); } } FirstProgram Operating System Output Main Method 16
A class without a main Method public class FirstProgram { } Operating System Can not Execute the program FirstProgram Operating System 17
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
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
First Java program public class FirstProgram { public static void main(string[] args) { System.out.println("Hello World"); } } Flow chart Start Display Hello World End 20
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
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
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 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(" ^^^^^ "); 23
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 ------------------------------- HSIT 2130 Fundamentals of Programming ------------------------------- 6. Try to run the above program on the Linux machine 24