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