JAVA
CONTENTS
Ø INTRODUCTION
Ø BASIC OOPS CONCEPTS
Ø DATA TYPES AND VARIABLES
Ø DECISION MAKING AND BRANCHING
Ø DECISION MAKING AND LOOPING
Ø ARRAYS
Ø INHERITANCE
Ø APPLETS
JAVA
INTRODUCTION
Java History:
Java is a general-purpose, Object-oriented Programming Language developed by sun Microsystems of USA in 1991. Originally called Oak by James Gosling one of the inventors of the Language, Java was designed for the development of Software for consumer electronic devices like TVs, VGRs, toasters and such other electronic machines.
Java features:
The inventors of java wanted to design a language which could offer solutions to some of the problems encountered in modern programming. They wanted the language to be not only reliable, portable and distributed but also simple, compact and interactive. Sun Microsystems officially describes Java with, the following attributes.
- Compiled and Interpreted
- platform – Independent and Portable
- Object – Oriented
- Robust and Secure
- Distributed
- Familiar, Simple and Small
- Multi threaded and Interactive
- High Performance
- Dynamic and Extensible
WEB BROWSERS:
• Hot Java
• Netscape navigator
• Interner explorer
JAVA ENVIRONMENT
Java environment includes a large number of development tools and hundreds of and methods. The development tools are part of the system as known as Java development kit(jdk), and the classes and methods are the part of the Java standard library (jsl), also known as application programming interface (API).
JAVA DEVELOPMENT KIT:
.
· APPLET VIEWER
Enables to run Java applets without using a Java compatible browser.
· JAVAC
Java Complier Which Translate Java Source Code To Byte Code Files That Interpreter Can Understand.
· JAVA
Java Interpreter Which Runs Applet And Applications By Reading Interpreting Byte code Files .
JAVA P ((JAVA DISASSEMBLER)
JAVA H (HEADER FILES)
JAVA DOC (CREATING HTML DOCUMENTS)
JDB (JAVA DEBUGGER)
JAVA VIRTUAL MACHINE:Process of compilation:
SOURCE CODE ------> BYTE CODE (using compiler)
BYTE CODE -------> LOW LEVEL LANGUAGE OR MACHINE LANGUAGE(using interpreter)
BYTE CODE -------> LOW LEVEL LANGUAGE OR MACHINE LANGUAGE(using interpreter)
VIRTUAL MACHINE REAL MACHINE
BASIC OOPS CONCEPTS:
1.Objects and classes:
objects are the basic run time entities in an object oriented
system. Like place, person, bank account,etc.Classes are user defined data types and behave like the built in types of a programming language
Format: Classname Objectname
EX: FRUIT apple
2.DATA ABSTRACTION AND ENCAPULATION:
Wrapping up of data and methods into a single unit
is known as encapsulation. Abstraction refers to the act of representing essential features without including the background details or explanations.
3. INHERITENCE:
The process by which object of one class acquire the properties of objects of another class.
4.POLYMORPHISM:
Polymorphism means ability to take more than one form.
5.DYNAMIC BINDING:
Dynamic binding means that thr code associated with a given procedure call is not known untill the time of the call at runtime.
6.MESSAGE COMMUNICATION:
1. creating classes that define objects and their behaviors
2. Creating objects from class definitions.
3. Establishing communication amount objects.
III. DATA TYPES AND VARIABLES:
Every variable in Java has a data type. Data types specify the size and type of values that can be stored. The variety of data types shown in below diagram.
Variables:
In Java variables are the names of storage locations.
DECLARING VARIABLES:
General format:
Type variable 1, variable2,... ,variableN;
EX:-
int a,b,c;
float a,b;
char a='y'; variables are separated by commas.
1. Using assignment statement.
1. Using read statement.
Giving values to the variable:
General format:
Variable = value;
EX:
A=10;
Simple Java Program: -
class sample
{
public static void main(String args[])
{
System.out.println("ICE TECH");
}
}
Output:
ICE TECH
public: an access specifier making it accessible to all other class.
static : the main must always be declared as static since the interpreter uses this method before any objects are created.
void: type modifier. It dose not return any value.
Program:
Largest of two nosJ
import java.io.*;
class Aa
{
public static void main(String args[])throws IOException
{
DataInputStream d1=new DataInputStream(System.in);
int a, b;
a=Integer.parseInt(d1.readLine());
b=Integer.parseInt(d1.readLine());
System.out.println ("Enter the value of a");
System.out.println("Enter the value of b");
if(a>b)
System.out.println("A is largest value");
else
System.out.println ("B is largest value");
}
}
Sum of two nos
import java.10.*;
class Test
{
public Static void main(string args[ ])
{
int a=30;b=20
c=a+b
System.out.println("sum of two number="+c);
}
}
Subtraction of two nos
import java.10.*;
class Test
{
public Static void main(string args[ ])
{
int a=30;b=20
c=a-b
System.out.println("sub of two number="+c);
}
}
Multiplication of two nos
import java.10.*;
class Test
{
public Static void main(string args[ ])
{
int a=30;b=20
c=a*b
System.out.println("multi of two number="+c);
}
}
Division of two nos
import java.10.*;
class Test
{
public Static void main(string args[ ])
{
int a=30;b=20
c=a+b
System.out.printin("sum of two number="+c);
}
}
Area of the triangle
import java.io.*;
class Area1
{
public static void main(String args[])
{
int b=2,h=4;
float d;
d=(b*h)/2;
System.out.println("Area of the triangle:"+d);
}
Distance between two points
import java.lang.Math;
class Distanc
{
public static void main(String args[])
{
int x1=2;
int x2=4,y1=3,y2=9,x,y,s;
double d;
x=(x2-x1);
y=(y2-y1);
s=(x*x)+(y*y);
d=Math.sqrt(s);
System.out.println("The distance between the points:"+d);
}
}
Calculation
import java.io.*;
class Calculate
{
public static void main(String args[])
{
int a=5;
int b=a+2;
int c=a*b;
int d=c-b;
int e=c/d;
System.out.println("value of b="+b);
System.out.println("value of c="+c);
System.out.println("value of d="+d);
System.out.println("value of e="+e);
}
}
Conversion from celcius to farenheit
import java.io.*;
class Conv1
{
int c;
void getc(int x)
{
c=x;
}
}
class Abc
{
public static void main(String args[])
{
int f;
int cc=17;
Conv1 obj=new Conv1();
obj.getc(cc);
System.out.println("CELCIUS TO FARENHEIT CONVERSION");
System.out.println("The celsius value is:"+cc+"C");
f=(((9*obj.c)/5)+32);
System.out.println("the farenheit value is:"+f+"F");
}
}
Decision making and branching
Java language allows such decision making capabilities and supports the following statements.
1. if statement
2. Switch statement
3. Conditional operator statement
If Statement
In Java, if statement is used to control the flow of execution of statements. It is two way decision statements. Format:
if (test expression) Ex:
if(a=10)
System.out.println("a is equal to 10");
The if statement may be implemented in different forms.
· Simple if statement
· If else statement
· Nested if else statement
The general format of if statement is
if (test exp )
{
statement block;
}
Ex:
if(a=10)
{
System.out.println("a is equal to 10");
}
Program:
import Java . io.* ;
Class test 2
{
public static void main (String args [ ])throws IOException
{
DataInputStream d1 = new DataInputStream(System. in);
Int a, b, c;
System.out.println(“Enter the value of a”);
a = Integer.parseInt(d1. readLine ( ));
System. out.println (“enter the value of b “);
b = Float. parseFloat (d1. readLine ( ));
if (a>b)
System. out. println (“A is Largest value “);
else
System. out. println (“B is Largest value”);
}
}
if., else statement
The general form of if else statement:
{
True block statement
}
else
{
False block statement
}
Nesting if., else statement
The general form of nested if else statement is
if(test exp)
{
statement- 1;
}
else
{
statement-2;
}
Program:
import Java . io.* ;
class test 2
{
public static void main (String args [ ]) throws IOExcption
{
DataInputStream d1 = new DataInputStream (System. in);
int a, b, c, d;
System. out. println (“enter the value of a, b, c, d”) ;
a = Integer.parseInt (d1. readLine ());
b = Integer.parseInt (d1. readLine ());
c = Integer.parseInt (d1. readLine ());
d = Integer.parseInt (d1. readLine ());
if (a>b && a>c && a>d)
System. out. println (” is Largest value “);
Else if (b>c&& b>d)
System. out. println (“b is largest value”);
Else if (c>d)
System. out.println (“c is largest value”) ;
Else
System. out.println (“d is largest value”) ;
}
Sum and avg of two nos:
import java.io.*;
class test2
{
public static void main (String args [ ]) throws IOException
{
DataInputStream d1 = new DataInputSream (System. in);
int t, e, m, tot ;
float avg ;
System. out.println (“Enter the value of t, e, m,”);
t =Integer.parseInt (d1. readLine ());
e = Integer.parseInt (d1. readLine ());
m=Integer. parseInt (d1. readLine ());
tot = t + e + m;
avg = tot / 3 ;
System. out. println (“Total = “+ tot);
System. out.println (“avg = “+avg);
if (t>=40&& e>=40 && m>=40)
{
System. out. println (“pass”);
if (avg>=80)
System. out. println (“First class”);
else if (avg > = 60)
System. out. println (“Second class”) ;
else
System. out. print (“Third class”);
}
else
{
System. out. Println (“Fail”);
System. out. Println (“No class”);
}
}
}
Decision making and looping
The process of repeatedly executing a block of statements is known as looping. In looping, sequences of statements are executed until some conditions for the termination of the loop are satisfied. A program loop consists of two segments, one known as the body of loop and the other known as the control statement. The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop.
A looping process includes following four steps:
- Setting and initization of a counter
- Execution of the statements in the loop.
- Test for a specified condition for execution of the loop.
- Incrementing the counter
Types of loop operations:
- The while statement
- The do while statement
- For statement
The while statement
The simplest of all looping structures in Java is the while statement.
While(test condition)
{
body of the loop
}
Program:
import java . io .* ;
Class clv1
{
Public Static Void main (string args [] )
{
Int i=1;
Int sum=0;
while(i=10)
{
Sum=sum i;
I=i+1;
}
System . out. Println ( “the sum of first ten natural numbers:” + Sum );
}
}
Do while statement
on some occasions it might be necessary to execute the body of the loop before the test is performed. Such situations can be handeled with the help of the do statement.
Format:
Initialization;
Do
{
body of the loop
}while(condition);
Program:
Import java.io.*;
Class dowhile
{
Public static void main(String args[])
{
Int sum=0;
Int n=10;
Int inc=2;
Do
{
Sum=sum+inc;
Inc=inc+2;
}
While(inc<=10);
System.out.println(“the sum of even numbers till 10:”+sum);
For statement
The for loop is another entry-controlled loop that provides a more concise loop control structure .the general form of the for loop is
For(initialization;condition;iteration)
{
Body of the loop
}
Program:
import java.io.*;
class jaya
{
public static void main(String args[])
{
int i,s=0;
for(i=5;i<=50;i+=5)
{
s=s+i;
}
System.out.println("Sum="+s)
}
}
Arrays:
An array is a group of contiguous or related data items that share a common name.
For example:
A[10];
S[20];
One dimensional array:
A list of items can be given one variable name using only one subscript and such a variable is called a one dimensional array.
Creating array
Declare the array
Create memory locations
Put values into the memory locations
Declaration of arrays
Form l:
Type array name [];
Form 2:
Type [] array name;
Ex:
Int num[];
Float [] count;
Remember, we do not enter the size of the arrays in the declaration.
Creation of arrays
Array name = new type[size];
Ex: '
Count = new int[5];
Initialization of arrays
The final step is to put the values into the array created. This process is known as initialization.
Format:
Arrayname[subscript] = value;
Ex:
Count[0]=l;
Count[l]=22;
We can also initialize an arrays like this
Type arrayname[] = (list of values};
Ex:
inta[]={ 10,2,30};
Sample program:
Class sample
{
public static void main(String args[])
{
inta[]={10,20,30,40,50,60};
for(int i=0;i<=5;i++)
System.out.println(a[i]);
}
}
}
}
}
Inheritance:
Defining a new class based on an existing class is called derivation.the new class or derived class is called as the direct sub class of the class from which it is being derived.the base class can also be called as super class.
The general declaration for a class that inherits a superclass is
Class subclass_name extends superclass_name
{
Body of the class
}
Java doesn’t support multiple inheritance. it only supports multi level inheritance. With multi level inheritance, we can create a hierarchy of inheritance in which a subclass becomes a super class of another sub class.
Program:
Import java.io.*;
Class shape
{
Int d1;
Int d2;
Public void setvalues(int dimen1,int dimen2)
{
D1=dimen1;
D2=dimen2;
}
}
Class triangle extends shape
{
Int ar;
Public void area(0
{
Ar=(d1*d2)/2;
System.out.println(“area of triangle:”+ar);
}
}class rectangle extends shape
{
Int ar;
Public void area(0
{
Ar=d1*d2;
System.out.println(“area of triangle:”+ar);
}
}
Public class inher
{
Public static void main(String args[])
{
Triangle t=new Triangle();
t.setvalues(10,10);
Rectangle r=new rectangle(0;
r.setvalues(5,10);
t.area(0;
r.area(0;
}
}
Applets
Applets are small ,special kind of java programs that run within a web document.they are accessed an internet server and transported over the network.’applet’ basically isa a java class defined in the java.applet package of jdk.thus,all applets must import java.applet and java.awt.packages.
To create user defined applet:
Public class example extends applet
{
…….
…….
}
Example:
Import java.awt.8;
Import java.applet.*;
Public class my applet extends applet
{
Public void paint(Graphics g)
{
g.drawstring (“my first java applet”,50,50);
}
}
HTML CODE
<HTML>
<head>
<title> first applet</title>
</head>
<APPLET CODE=myapplet.class WIDTH=300 HEIGHT=300>
</APPLET>
</body>
</HTML>
Multithreading
import java.io.*;
class A extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("\t from thread A:i="+i);
}
System.out.println("exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=0;j<5;j++)
{
System.out.println("\t from thread B:j="+j);
}
System.out.println("exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=0;k<5;k++)
{
System.out.println("\t from thread C:k="+k);
}
System.out.println("exit from C");
}
}
class Sample
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}