java chapter 1 - psis

32
Programming Language (JAVA) Unit 6.1 – Basics of Java Presentation 1

Upload: faez-dane

Post on 18-Apr-2015

50 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Chapter 1 - PSIS

Programming Language (JAVA)

Unit 6.1 – Basics of Java

Presentation 1

Page 2: Java Chapter 1 - PSIS

Objectives

At the end of this presentation, you will be able to: Describe the evolution and architecture of Java Describe the basic concepts of object oriented

programming Explain the features of Java List the differences between C++ and Java Describe the types of Java programs Describe the tools available in Java Developer

Kit Write a simple Java program

Page 3: Java Chapter 1 - PSIS

Introduction to Java

Is a object oriented programming (OOP) language.

Is simple and easy to understand.

Runs on a wide variety of operating environments.

Is a powerful and popular language to develop Internet applications.

Page 4: Java Chapter 1 - PSIS

History of Java

Year History

1990 C++ was found not fit to control electronic devices. So research for a new programming language started.

1991-92 A new programming language OAK was found. Later renamed as JAVA.

1993-94 Java became a perfect language to develop Internet-based applications

1995 Sun Microsystems introduced Java-enabled Web browser “Hot Java”.

Page 5: Java Chapter 1 - PSIS

Java Architecture

Java is platform-independent as it can run on a wide variety of computers using different OS.

There are four important components in Java architecture:

Java Source Code Java Compiler Java Byte code (Object code) Java Virtual Machine (JVM)

Page 6: Java Chapter 1 - PSIS

Java Architecture - Components The components can be explained as follows:

Java Source Code - Program written in the form of text using Java.

Java Compiler - Used to convert source code into binary program that consists of byte code. It creates .class file.

Java Byte Code (Object code) - Byte code is a set of instructions that are machine-independent. Executed by JVM.

Java Virtual Machine (JVM) - Is a Java runtime system. Converts the bytecode in .class file to machine language.

Page 7: Java Chapter 1 - PSIS

Object-Oriented Approach

Java supports object-oriented approach.

This approach helps to organize complex programs easily.

Java implements the real life aspects in programming.

Page 8: Java Chapter 1 - PSIS

Classes and Objects

Classes and objects are building blocks of OOP approach.

Class - A class is a template or blueprint to create an object.

Object - Is an instance of a class.

Characteristics of real world object are variables or data members in a class.

Behaviour of objects are called as methods or member functions of a class.

Page 9: Java Chapter 1 - PSIS

Features of OOP

Data Abstraction - Refers to the concept of representing only the essential features of a data without including the non-essential details.

Encapsulation - Refers to the mechanism of wrapping up of data and methods (that operate on the data) into a single unit (class).

Inheritance - Refers to the concept by which one class derives the properties of another class.

Polymorphism - Refers to the response (output) of each object differently for the same input.

Page 10: Java Chapter 1 - PSIS

Features in Java

Page 11: Java Chapter 1 - PSIS

Features of Java (Contd…)

Simple - Java is easy to learn and use.

Object-Oriented - This approach to design programs very close to the real world.

Platform-Independent - Java programs written in one environment can run on all other environments.

Portable - Feature of bytecode and basic data types in Java makes it compatible with all systems.

Page 12: Java Chapter 1 - PSIS

Features of Java (Contd…)

Distributed - Java is powerful language to share and access data across the net.

Robust - Java programs are reliable. Early checking and dynamic checking during runtime, mostly eliminates situations that cause errors.

Secure - Java is a highly secure programming language.

Multi-threaded - This concept enables Java programs to handle many tasks simultaneously.

Page 13: Java Chapter 1 - PSIS

C++ and Java ComparisonCompilation model of C++ and Java.

Page 14: Java Chapter 1 - PSIS

C++ and Java Comparison (Contd..)

C++ Java

Supports ASCII char set. Supports Unicode char set.

Supports pointers, structures, complex functions concepts.

Does not support pointers, structures and complex functions concepts.

Supports multiple inheritance. Does not support multiple inheritance. It can be supported through interface feature.

Page 15: Java Chapter 1 - PSIS

Types of Java Program

The two types of Java programs are: Application Programs

Application programs are the stand-alone

programs, which can execute from the

command prompt.

Applet Programs

Applet programs are the Internet based

programs, which can run in a Web-browser.

Page 16: Java Chapter 1 - PSIS

Java Tools

The tools available in Java Development Kit (JDK) are:

Compiler - javac is the Java compiler. It converts

the source code to .class file with bytecode.

Interpreter - Java interpreter java is used to

translate the bytecode to machine language code.

Applet Viewer - It is a tool used to view the applet

programs created in Java.

Page 17: Java Chapter 1 - PSIS

Hands On!This program enables you to understand how to writesimple Java programs.

Step 1: Open Notepad and type the following code:/*A simple program to display welcome*/class Greeting

{ public static void main(String args[]) { System.out.println("welcome"); }  }

Page 18: Java Chapter 1 - PSIS

Hands On! (Contd…)

Step 2:Choose File Save in Notepad. The Save As dialog box appears on the screen. In the File name text box, give the filename as Greeting.java within double quotes.

Step 3:Compile the Greeting.java file using javac. Type javac Greeting.java at the command prompt.

Step 4:Run the program by typing java Greeting at the command prompt.

Page 19: Java Chapter 1 - PSIS

Structure of Simple Java Program

• A Java program consists of two main parts: a class definition and the main method.

Consider the following sample Java program/* comment entry*/class <class_name> { public static void main (String args[ ]) {

statements; }}

Page 20: Java Chapter 1 - PSIS

Activity 6.1.1 (a)

Step 1:Open sample1.java data file.

Step 2: Run and observe the output.

Step3: Add a print statement System.out.println(“Hello friends”); in line 6.

Step 4: Save the file.

Step 5: Compile and execute it.

Page 21: Java Chapter 1 - PSIS

Activity 6.1.1 (b)

Step 1: Open sample2.java data file.

Step2: Edit code line 5 as

System.out.println("welcome");.

Step 3: Save the program.

Step 4: Run and observe the output.

Page 22: Java Chapter 1 - PSIS

Activity 6.1.1 (c)

Step 1:Open sample3.java data file.

Step 2:Identify the appropriate bracket to be used and fill the code lines 2, 4, 6 and 7.

Step 3:Save the program.

Step 4: Run and observe the output.

Page 23: Java Chapter 1 - PSIS

Activity 6.1.1 (d)

Step 1:Open sample4.java data file.

Step 2: Identify the error in code line 3 and modify the program.

Step 3: Save the program.

Step 4: Run and observe the output.

Page 24: Java Chapter 1 - PSIS

Activity 6.1.1 (e)

Step1: Open sample5.java data file.

Step 2: Identify the missing statement in code line 3 and modify the program.

Step 3: Save the program.

Step 4: Run and observe the output.

Page 25: Java Chapter 1 - PSIS

Lab Exercise

1. Write a Java program to display the following information:

My Name: <Your name>

My Favorite Subject: <Favorite subject>

My Friend’s name: <Friend’s name>

Do the following activities:• Compile the program (using javac command)• Run the program (using java command)

Page 26: Java Chapter 1 - PSIS

Lab Exercise

2. Display the following car details using a Java program:

Car Number Car Name

1 Proton ISWARA

2 Perodua KEMBARA

Page 27: Java Chapter 1 - PSIS

Lab Exercise

3. Create a Java program to display the No, Name, Age and Weight of students

No. Name Age Gender Weight

1 Rubia 27 Female 40

2 Jonnie 26 Male 55

Page 28: Java Chapter 1 - PSIS

Lab Exercise

4. Write a Java program to display the list of drinks.

Energy Drink

Cola

Fresh Orange juice

Barley Water

Page 29: Java Chapter 1 - PSIS

Summary

In this presentation, you learnt the following: Java is a object oriented programming

(OOP) language. Java is found to be powerful for Internet

applications. Java is platform-independent as it can run

on wide variety of computers using different OS.

Java compiler, javac converts source code of Java to the .class file with byte code.

Page 30: Java Chapter 1 - PSIS

Summary

In this presentation, you learnt the following: Java Virtual Machine (JVM) is Java runtime

environment that converts bytecode to machine language.

Classes and objects are building blocks of OOP approach. A class is a template or blueprint to create

an object. An object Is an instance of a class.

Page 31: Java Chapter 1 - PSIS

Summary

In this presentation, you learnt the following: A Java program consists of two main parts; a

class definition and the main method. The two types of Java program are Application

and Applets. The JDK tools are Compiler, Interpreter and

Applet Viewer. A Java program consists of two main parts; a

class definition and the main method.

Page 32: Java Chapter 1 - PSIS

Assignment

1. What is bytecode?

2. What is Java Virtual Machine?

3. What are the tools available in Java Developer’s Kit.

4. List the two main parts of Java program.

5. Write a simple Java program to display the message Java is a OOP language and execute it.