[Java Index] S. No.

Topic

Page No.

1. 2. 3. 4. 5 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.

Certificates and Acknowledgement Introduction to Java Features of Java Introduction to JVM (Java Virtual Machine) Naming Conventions in Java Programming Introduction to OOPs Features of OOPs Classes and Object in Java Program to show the use of Class and Object Encapsulation Program to show the Encapsulation feature Abstraction Program to show the Abstraction feature Inheritance Program to show the Inheritance feature Polymorphism Program to show the Polymorphism feature Abstract Class and Interface class Program based on Abstract Class Program based on Interface Some Basic Java Program based on Assignment Program to find the greatest number by using ( ?,: ) Operator. Program to find the factorial of any number. Program to find the number of prime integer between given range. Program to find the leap year. Program to print Fibonacci series using recursion. Coding for Package in Java Coding for Exception Handling in Java Coding for Files Creation and Manipulation Coding for Graphical Programming using AWT in Java Coding for Applet in Java

i 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 14 14

21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

15 16 17 18 19 20 21 22 23

For Study Material: http://www.niomsolution2000.blogspot.com

Date

Page |1

Introduction to Java – Sun Microsystems Inc. (US) was conceived a project to develop software for consumer electronic devices that could be controlled by a remote. This project was called Stealth Project but later its name was changed to Green Project. In January of 1991, James Gosling and his colleagues thought C and C++ could be used to develop the project. But the problem they faced with them is that they were system dependent language and hence could not be used on various processors, which the electronic devices might use. So he started developing a new language, which was completely system independent. This language was initially called Oak. Since this name was registered by some other company, later it was changed to Java. By September of 1994, Naughton and Jonathan Payne started writing WebRunner – a Java-based Web browser, which was later renamed as HotJava. By October 1994, HotJava was stable and was demonstrated to Sun executives. HotJava was the first browser, having the capabilities of executing applets, which are programs designed to run dynamically on Internet. This time, Java’s potential in the context of the World Wide Web was recognized.

For Study Material: http://www.niomsolution2000.blogspot.com

Page |2

Features of Java – There are following features of Java which make Java programming much reliable.  Simple: Java is a simple Programming language. Rather than feature simplicity of this programming design is the objective to cover huge number of devices. First of all, the difficult concepts of C and C++ have been omitted in Java.  Object – Oriented: Java is an object-oriented programming language. This means Java programs use objects and classes. An object is anything that really exists in the world and can be distinguished from others. Everything that we see physically will come into this definition, for example, every human being, a book, a tree, and so on.  Distributed: Information is distributed on various computers on a network.  Robust: Robust means strong. Java programs are strong and they don’t crash easily like a C or C++ program.  Secure: Security problems like eavesdropping, tampering, impersonation, and virus threats can be eliminated or minimized by using Java on Internet.  System Independence: Java’s byte code is not machine dependent. It can be run on any machine with any processor and any operating system.  Portability: If a program yields the same result on every machine, then that program is called portable. Java programs are portable. This is the result of Java’s System Independence nature.

For Study Material: http://www.niomsolution2000.blogspot.com

Page |3

Introduction to JVM (Java Virtual Machine) – Java Virtual Machine (JVM) is the heart of entire Java program execution process. It is responsible for taking the .class file and converting each byte code instruction into the machine language instruction that can be executed by the microprocessor. Architecture of Java Virtual Machine.

Class loader sub system

Class File

Method area

Heap

Java Stacks

PC Registers

Native method stacks

Runtime Data Areas

Interpreter

JIT Compiler

Native method interface

Native method libraries

Execution engine Operating System Figure 1: Components in JVM architecture

For Study Material: http://www.niomsolution2000.blogspot.com

Page |4

Naming Conventions in Java:  A package represents a sub directory that contains a group of classes and interfaces. Names of packages in Java are written in small letters as: java.awt java.io javax.swing  A class is a model for creating objects. A class specifies the properties and actions of objects. An interface is also similar to a class. Each word of class names and interface names start with a capital letter as: String DataInputStream ActionListener  A class and an interface contain methods and variables. The first word of a method name is in small letters; then from second word onwards, each new word starts with a capital letter as shown here: println() readLine() getNumberInstance()  The naming convention for variables names is same as that for methods as given here: age empName employee_Net_Sal  Constants represent fixed values that cannot be altered. For example, PI is a constant whose value is 3.14, which is fixed. Such constants should be written by using all capital letters as shown here: PI MAX_VALUE Font.BOLD  All keywords should be written by using all small letters as follows: public void static

For Study Material: http://www.niomsolution2000.blogspot.com

Page |5

Introduction to OOPs – Languages like C++ and Java use classes and objects in their programs and are called Object Oriented Programming languages. A class is a module which itself contains data and methods (functions) to achieve the task. The main task is divided into several modules, and these are represented as classes. Each class can perform some tasks for which several methods are written in a class. This approach is called Object Oriented Approach.

Data methods Class Class with main() method

Class Class

Data methods

Data methods

Figure 2: Object Oriented Approach Features of OOPs –     

Class/Object Encapsulation Abstraction Inheritance Polymorphism

For Study Material: http://www.niomsolution2000.blogspot.com

Page |6

Class/Object:Entire OOP methodology has been derived from a single root concept, called ‘object’. An object is anything that really exists in the world and can be distinguished from others. For example, a table, a car, a dog etc.. everything will come under objects. A class is group name and does not exist physically, but objects exist physically. Note: A class is a model for creating objects and does not exist physically. An object is anything that exists physically. Both the class and objects contain variables and methods.

Creating Class & Objects in Java A class is created by using keyword, class. class Person { //properties of a person - veriables String name; int age; //actions done by a person - methods void talk() {} void eat() {} } This class code is stored in JVM’s method area. When we want to use this class, we should create an object to the class as: Person Raju = new Person(); Here, Raju is an object to Person class. Object represents memory to store the actual data Preceding ‘new’ operator tells the JVM to allot memory. For Study Material: http://www.niomsolution2000.blogspot.com

Page |7

Encapsulation:Encapsulation is a mechanism where the data (variables) and the code (methods) that act on the data will bind together. For example, if we take a class, we write the variables and methods inside the class. Thus, class is binding them together. So called class is an example for encapsulation. Encapsulation isolates the members of a class from the members of another class. Program to show the Encapsulation Feature: class Employee { int id = 1001; String name = "NIOM"; void displayDetails() { System.out.println("Id = " + id); System.out.println("Name = " + name); } class Student { int id = 12; //Encapsulated from Employee String name = "Foundation"; void displayDetails() { System.out.println("Id = " + id); System.out.println("Name = " + name); } }

For Study Material: http://www.niomsolution2000.blogspot.com

Page |8

Abstraction:There may be a lot of data, a class contains and the user does not need the entire data. The user requires only some part of the available data. In this case, we can hide the unnecessary data from the user and expose only that data that is of interest to the user. This is called abstraction. Program to show Abstraction Feature: class Bank{ private int accno; private String name; private float balance; private float profit; private float loan; public void display_to_clerk() { System.out.println("AccNo : " + accno); System.out.println("Name : " + name); System.out.println("Balance : " + balance); } } In the preceding class, in spite of several data items, the display_to_clerk() method is able to access and display only the accno, name and balance values. It cannot access profit and loan of the customer. This means the profit and loan data is hidden from the view of the bank clerk.

For Study Material: http://www.niomsolution2000.blogspot.com

Page |9

Inheritance: Inheritance is defined as Deriving new classes from existing classes such that the new classes acquire all the features of existing classes is called inheritance. When a class is written by a programmer and another programmer wants the same features in his class also, then the other programmer will go for inheritance. This is done by deriving the new class form existing class. Program to show the Inheritance features: //Teacher class class Teacher { //instance variables int id; String name; String sal; //setter method to store id void setId(int id) { void setName(String name) { void setSal(String sal) { //getter method to retrieve id int getId() { int getName() { int getSal() { }

this.id=id; } this.name=name; } this.sal=sal; } return id; } return name;} return sal; }

//Student class class Student extends Teacher { //id, name are available from Teacher class. String marks; //setter method to store id void setMarks(String mark) { this.mark=mark; //getter method to retrieve id int getMarks() { return mark; } }

}

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 10

Polymorphism:Polymorphism came from the two Greek words ‘poly’ meaning many and ‘morphos’ meaning forms. The ability to exist in different forms is called ‘polymorphism’. In Java, a variable, an object or a method can exist in different forms, thus performing various tasks depending on the context.

Program to show the Polymorphism feature: class Sample { //method to add two values void add(int a, int b) { System.out.println("Sum of two = " + (a + b)); } //method to add three values void add(int a, int b, int c) { System.out.println("Sum of two = " + (a + b + c)); } } class Polymorhism { public static void main(String args[]) { //create Sample class object Sample s = new Sample(); //call add() and pass two values s.add(10,15); s.add(10,15,20); } } Output

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 11

Abstract Class:An abstract class is a class that contains 0 or more abstract method. Abstract method is a method without method body. An abstract method is written when the same method has to perform different tasks depending on the object calling it.

Program based on Abstract Class: //All the objects need different implementations of the same method abstract class Myclass { //this is abstract method abstract void calculate(double x); } class Sub1 extends Myclass { //Calculate square value void calculate(double x) { System.out.println("Square = " + (x*x)); } } class Sub2 extends Myclass { //calculate square root value void calculate(double x) { System.out.println("Square roots = Math.sqrt(x)); } } class Different { public static void main(String args[]) { //Create sub class objects Sub1 obj1 = new Sub1();

For Study Material: http://www.niomsolution2000.blogspot.com

"

+

P a g e | 12

Sub2 obj2 = new Sub2(); //let the objects call and use calculate() method obj1.calculate(3); obj2.calculate(4); } }

Interfaces: An interface is a specification of method prototypes. All the methods of the interface are public and abstract. An interface will have 0 or more abstract methods which are all public and abstract by default. None of the methods in interface can be private, protected or static.

Program based on Interface: //interface example - connecting to any Database interface MyInter { void connect(); void disconnect(); } class OracleDB implements MyInter { public void connect() { System.out.println("Connecting to DB"); } public void disconnect() {

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 13

System.out.println("Disconnecting from DB"); } } class InterfaceDemo { public static void main(String args[]) throws Exception { Class c = Class.forName(args[0]); MyInter mi = (MyInter)c.newInstance(); mi.connect(); mi.disconnect(); } }

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 14

Some Basic Java Program base on Assignment //Program to find the greatest number by using (?,:) Operator. class MaxNum { public static void main(String args[]) { int a,b,c,d; a = 3; b = 6; c = 8; d = ((a>b)&&(a>c))?a:(b>c)?b:c; System.out.println("The Greatest Number between a=3, b=6 & c=8 : "+ d); } }

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 15

//Program to find the factorial of any number

import java.io.*; class Factorial { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num,fact,i; System.out.println("Enter your Number: "); num=Integer.parseInt(br.readLine()); fact = 1; i = 1; while(i <= num) { fact = fact * i; i++; } System.out.println("The Factorial of " + num + " is : " + fact); } }

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 16

//Program to find the number of prime integer between given range.

import java.io.*; class PrimeNumber { public static void main(String args[])throws IOException { int i=0; int num=0; int userNumber; String primeNumber=""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter range to which prime number should print: "); userNumber=Integer.parseInt(br.readLine()); for(i=1;i<=userNumber;i++) { int counter=0; for(num=i; num>=1;num--) { if(i%num==0) { counter = counter + 1; } } if(counter == 2) { primeNumber=primeNumber + i + " "; } } System.out.println("Prime Number from 1 to 100 are : "); System.out.println(primeNumber); } }

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 17

Program to find the leap year.

import java.io.*; class LeapYear { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your Year: "); int year = Integer.parseInt(br.readLine()); boolean isLeapYear = false; if(year % 400 == 0) { isLeapYear=true; } else if(year % 100 == 0) { isLeapYear = false; } else if(year % 4 == 0) { isLeapYear = true; } else { isLeapYear = false; } if(isLeapYear)

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 18

{

} else {

}

Year"); } }

System.out.println("Year" + year+" is a Leap Year"); System.out.println("Year "+year+" is not a Leap

Program to Print Fibonacci Series in given range

import java.util.Scanner; class Fibonacci { public static void main(String args[]) { System.out.print("Enter number upto which Fibonacci series to print: "); int number = new Scanner(System.in).nextInt(); System.out.println("Fibonacci series upto " + number + " numbers: "); for(int i=1; i<=number; i++){ System.out.print(fibonacci2(i) + " "); } } public static int fibonacci(int number){ For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 19

if(number ==1 || number ==2){ return 1; } return fibonacci(number-1) + fibonacci(number-2);

} public static int fibonacci2(int number){ if((number == 1) || (number ==2)){ return 1; }

}

}

int fibo1 = 1, fibo2= 1, fibonacci=1; for(int i =3; i<= number; i++){ fibonacci = fibo1 + fibo2; fibo1 = fibo2; fibo2 = fibonacci; } return fibonacci;

Program for Package in Java

//STEP 1 : Creating a package pack with Addition class package pack; public class Addition { //instance vars private double d1, d2; public Addition(double a, double b) { d1 = a; d2 = b; }

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 20

}

//method to find sum of two numbers public void sum() { System.out.println("Sum = " + (d1+d2)); }

//Step 2: Using the package pack import pack.Addition; class Use { public static void main(String args[]) { //create Addition class object Addition obj = new Addition(10, 15.5); //Call the sum() method obj.sum(); } }

Program for Exception Handling in Java

//Exception Handling class Ex { public static void main(String args[]) { try { //Open the files System.out.println("Open Files");

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 21

//do some proccessing int n= args.length; System.out.println("n = " + n); int a = 45/n; System.out.println("a = " + a);

}

} catch(ArithmeticException ae) { //Display the exception detials System.out.println(ae); //display any message to the user System.out.println("Please pass data while running this program"); } finally { //Close the file System.out.println("Close files"); } }

Coding for Files Creation and Manipulation

//Creating a text file using FileWriter import java.io.*; class CreateFile { public static void main(String args[]) throws IOException { //take a string For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 22

String str = "This is a book on Java. " + "\nI am a learner of java."; //attach file to FileWriter FileWriter fw = new FileWriter("text");

//read character wise from string and write into FileWriter for(int i=0; i
}

}

//close the file fw.close();

Coding for Graphical Programming using AWT in Java //Creating a frame import java.awt.*; class MyFrame { public static void main(String args[]) { //create a frame

For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 23

}

}

Frame f = new Frame("My AWT Frame"); //set the size of the frame f.setSize(300,250); //display the frame f.setVisible(true);

Coding for Applet in Java

//A simple Applet import java.awt.*; import java.applet.*; public class MyApp extends Applet { //set a background color for the frame public void init() For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 24

{

}

setBackground(Color.yellow);

} //display message in applet window public void paint(Graphics g) { g.drawString("Hello Applets!", 50, 100); }

Now, MyApp.class is created. This byte code should be embedded into a HTML page using tag,

***THE END*** For Study Material: http://www.niomsolution2000.blogspot.com

P a g e | 25

For Study Material: http://www.niomsolution2000.blogspot.com

Coding for Applet in Java

BOLD. ➢ All keywords should be written by using all small letters as follows: public ..... <html>. . applet>.

904KB Sizes 5 Downloads 166 Views

Recommend Documents

applet example in java pdf
Page 1 of 1. applet example in java pdf. Click here if your download doesn't start automatically. Page 1 of 1. applet example in java pdf. applet example in java ...

Java Card & STK Applet Development Guidelines - IS MU
Defining an Access Domain for an applet instance is peculiar to this applet instance. This implies: ...... Below are examples of GOOD recommended practice.

Java Card & STK Applet Development Guidelines - IS MU
Level 3 – Use of transaction services by applications . ..... Creating an applet instance by the way of the Install APDU command in Install mode. ..... Once the applet is triggered, the SIM toolkit framework calls the applet's processToolkit().

Scratch Coding Cards: Creative Coding Activities for Kids
With the Scratch Coding Cards, kids learn to code as they create interactive games, stories, music, and animations. The short-and-simple activities provide an ...

Coding for Posterity - ATS @ UCLA
production system. Alternatively, a program may be used ... every program with the assumption that it may be used again later. ... be found by reading the code and any separate data flow diagrams ... language can see the significance of RUN statement

the cert oracle secure coding standard for java pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. the cert oracle ...