JAVA
SNO PROGRAM NAME PAGE NO REMARKS
1 Student Result Details
2 Swapping Of 2 Nos with AND without 3rd variable
3 Different Types Of Operators using Switch Case
4 Generating Prime Nos upto 100
5 Matrix Multiplication of 2 Matrices
6 Generating Student Details using Class with
Constructor & Displaying with 3 Different Objects
7 Method Overloading & Method Overriding
8 Single Level Inheritance
9 Interface Implementation
10 String Operations
11 Creating Multiple Threads
12 Priority Setting & Priority Getting on Threads
13 User defined Exception Handling
14 Drawing Polygon Lines Using Applet
15 Creating a Different Package Access Specifier
16 Creating a file and Reading a file using byte stream
classes
1. Student Details
import [Link].*;
class Student
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
String name,group,course;
int rno,year,eng,tel,maths,physics,computers,tot;
float avg;
//input the details
[Link]("Enter Roll no");
rno=[Link]([Link]());
[Link]("Enter name");
name=[Link]();
[Link]("Enter group");
group=[Link]();
[Link]("Enter course");
course=[Link]();
[Link]("Enter year");
year=[Link]([Link]());
//input the marks
[Link]("Enter telugu marks");
tel=[Link]([Link]());
[Link]("Enter English");
eng=[Link]([Link]());
[Link]("Enter Maths marks");
maths=[Link]([Link]());
[Link]("Enter Physics");
physics=[Link]([Link]());
[Link]("Enter computers marks");
computers=[Link]([Link]());
//calculate total and average
tot=eng+tel+maths+physics+computers;
avg=tot/5;
//displaying the details
[Link]("---Student details are----");
[Link]("roll no is :"+rno);
[Link]("name is :"+name);
[Link]("group is :"+group);
[Link]("course is :"+course);
[Link]("year is :"+year);
[Link]("telugu marks are :"+tel);
[Link]("english marks are :"+eng);
[Link]("maths marks are :"+maths);
[Link]("physics marks are :"+physics);
[Link]("computers marks are :"+computers);
[Link]("Total marks are :"+tot);
[Link]("Avereage marks are :"+avg);
}
}
2. Swapping 2 nos
//Swapping 2 nos with 3rd variable
class Swap3
{
public static void main(String args[])
{
int a,b,c;
a=33;
b=99;
[Link]("Before swapping");
[Link]("a value is "+a+"and"+"b value is "+b);
c=a;
a=b;
b=c;
[Link]("After swapping");
[Link]("a value is "+a+"and"+"b value is "+b);
}
}
//Swapping 2 nos without 3rd variable
class Swap2
{
public static void main(String args[])
{
int a,b;
a=33;
b=99;
[Link]("Before swapping");
[Link]("a value is "+a+"and"+"b value is "+b);
a=a+b;
b=a-b;
a=a-b;
[Link]("After swapping");
[Link]("a value is "+a+"and"+"b value is "+b);
}
}
3. Operators
import [Link].*;
class Operators
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
int choice;
boolean yes=true;
do
{
[Link]("-------------------------------------------");
[Link]("[Link] Operators");
[Link]("[Link] Operators");
[Link]("[Link] Operators");
[Link]("[Link] operators");
[Link]("[Link]");
[Link]("Enter your choice");
choice = [Link]([Link]());
[Link]("-------------------------------------------");
switch(choice)
{
case 1:
int a,b,c,d,e,f,g;
[Link]("Enter any 2 numbers for Addition,Subtraction,Multiplication,Division,Modulos");
[Link]("Enter First Number");
a=[Link]([Link]());;
[Link]("Enter Second Number");
b=[Link]([Link]());
[Link]("Arthematic operarors Results are");
[Link]("-------------------------------------------");
c=a+b;
[Link]("Addition of 2 Numbers is : "+c);
d=a-b;
[Link]("Subtraction of 2 Numbers is : "+d);
e=a*b;
[Link]("Multiplication of 2 Numbers is : "+e);
f=a/b;
[Link]("Division of 2 Numbers is : "+f);
g=a%b;
[Link]("Modulos of 2 Numbers is : "+g);
break;
case 2:
int h,i;
[Link]("Enter any 2 numbers for
graterthan,lessthan,graterthanequals,lessthansequals,equalsto");
[Link]("Enter First Number");
h=[Link]([Link]());;
[Link]("Enter Second Number");
i=[Link]([Link]());
[Link]("Relational operarors Results are");
[Link]("-------------------------------------------");
if(h>i)
{
[Link](h+"is grater than"+i);
}
else
[Link](h+"is not grater than"+i);
if(h<i)
{
[Link](h+"is less than"+i);
}
else
[Link](h+"is not less than"+i);
if(h>=i)
{
[Link](h+"is grater than equals to"+i);
}
else
[Link](h+"is not grater than equals to"+i);
if(h<=i)
{
[Link](h+"is less than equals"+i);
}
else
[Link](h+"is not less than equals"+i);
if(h==i)
{
[Link](h+"is equals to "+i);
}
else
[Link](h+"is not equals to "+i);
if(h!=i)
{
[Link](""+h+"and"+i+"both are not equal");
}
break;
case 3:
int j,k,complement,and,or,exor,lshift,rshift,zrshift;
[Link]("Enter any 2 numbers for Bitwise Operators");
[Link]("Enter First Number");
j=[Link]([Link]());;
[Link]("Enter Second Number");
k=[Link]([Link]());
[Link]("Bitwise operarors Results are");
[Link]("-------------------------------------------");
complement=~(j);
[Link]("complement of "+j+"is :"+complement);
and=(j&k);
[Link]("Bitwise And of "+j+" & "+k+"is :"+and);
or=(j|k);
[Link]("Bitwise Or of "+j+"| "+k+"is :"+or);
exor=(j^k);
[Link]("Bitwise ExclusiveOR of "+j+"^ "+k+"is :"+exor);
lshift=(j<<2);
[Link]("Bitwise Left Shift of "+j+"<<2 is :"+lshift);
rshift=(k>>2);
[Link]("Bitwise Right Shift of "+k+">>2 is :"+rshift);
zrshift=(k>>>2);
lshift=(j<<2);
[Link]("Bitwise ZeroFillRight Shift of "+k+">>>2 is :"+zrshift);
break;
case 4:
int l,m,n,la,lo;
[Link]("Enter any 3 numbers for Logical Operators");
[Link]("Enter First Number");
l=[Link]([Link]());;
[Link]("Enter Second Number");
m=[Link]([Link]());
[Link]("Enter Third Number");
n=[Link]([Link]());
[Link]("Logical operarors Results are");
[Link]("-------------------------------------------");
[Link]("Logical And Result is");
if((l>m)&&(l>n))
{
la=l;
[Link]("the Gratest no is :"+la);
}
else if(m>n)
{
la=m;
[Link]("the Gratest no is :"+la);
}
else
{
la=n;
[Link]("the Gratest no is :"+la);
}
[Link]("Logical Or Result is ");
if((l>m)||(l>n))
{
lo=l;
[Link]("the Gratest no is :"+la);
}
else if(m>n)
{
lo=m;
[Link]("the Gratest no is :"+la);
}
else
{
lo=n;
[Link]("the Gratest no is :"+la);
}
[Link]("Logical Not Result is");
if(!(l==0))
{
[Link](l+" is not equals to zero ");
}
break;
case 5:
yes=false;
break;
default :
[Link]("wrong choice");
}
}while(yes==true);
}
}
4. Prime Number Generation
import [Link].*;
class Primenumber
{
public static void main(String args[])
{
int num=100,n,div,p=0;
for(n=2;n<=num;n++)
{
for(div=2;div<n;div++)
{
if(n%div==0)
{
p=0;
break;
}
p=1;
}
if(p==1)
[Link](n+"\t");
}
}
}
5. Matrix Multiplication
import [Link].*;
class Matrixmultiplication
{
public static void main(String args[])
{
int a[][]={{3,4},{5,6}},b[][]={{7,8},{9,2}},c[][]={{0,0},{0,0}},i,j,k;
[Link]("A matrix is:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
[Link](a[i][j]+"\t");
}
[Link]("\n");
}
[Link]("B matrix is:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
[Link](b[i][j]+"\t");
}
[Link]("\n");
}
//logic for Matrix multiplicatoin
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
//[Link]("matrix multiplication of A and B is:");
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
{
[Link](c[i][j]+"\t");
}
[Link]("\n");
}
}
}
6. Generating Student Details using Class & Displaying with 3 Different Objects
import [Link].*;
class Student
{
int rno,s1,s2,s3,total;
String name;
float avg;
void Sdetails()
{
[Link]("rno is:"+rno);
[Link]("name is :"+name);
[Link]("sub1 marks are:"+s1);
[Link]("sub2 marks are:"+s2);
[Link]("sub3 marks are:"+s3);
total=s1+s2+s3;
[Link]("total marks are:"+total);
avg=total/3;
[Link]("avg marks are:"+avg);
}
}
class Stddetails
{
public static void main(String args[])
{
Student m1=new Student();
[Link]("---First object---");
[Link]("bhanu details are:");
[Link]=2;
[Link]="bhanu";
m1.s1=87;
m1.s2=90;
m1.s3=99;
[Link]();
Student m2=new Student();
[Link]("---Second object---");
[Link]("priya details are:");
[Link]=4;
[Link]="priya";
m2.s1=85;
m2.s2=69;
m2.s3=80;
[Link]();
Student m3=new Student();
[Link]("---Third object---");
[Link]("rajini details are:");
[Link]=8;
[Link]="rajini";
m3.s1=90;
m3.s2=70;
m3.s3=50;
[Link]();
}
}
[Link] OverLoading & Method OverRiding
//over loading
class Sample
{
void add(int a,int b)
{
[Link]("sum of 2 no's:"+(a+b));
}
void add(int a,int b,int c)
{
[Link]("sum of 3 no's:"+(a+b+c));
}
}
class OverLoad
{
public static void main(String args[])
{
Sample s=new Sample();
[Link](7,8);
[Link](5,6,7);
}
}
//overriding
import [Link].*;
import [Link].*;
class Find
{
void calc(long x)
{
[Link]("Sqaure value of x is"+(x*x));
}
void calc(int x)
{
[Link]("Sqaure root of value x is"+[Link](x));
}
}
class OverRide
{
public static void main(String args[])
{
Find z=new Find();
[Link](25);
}
}
[Link] Level Inheritance
//inhertiance example
class Person
{
String name;
String permanentAddress;
int age;
void setpermanentDetails(String name,String permanentAddress,int age)
{
[Link]=name;
[Link]=permanentAddress;
[Link]=age;
}
void getpermanentDetails()
{
[Link]("name is"+name);
[Link]("permanentAddress is"+permanentAddress);
[Link]("age is"+age);
}
}
class Employ extends Person
{
int id;
String companyName;
String companyAddress;
Employ(int id,String name,String permanentAddress,int age,String companyName,String companyAddress)
{
[Link]=id;
setpermanentDetails(name,permanentAddress,age);
[Link]=companyName;
[Link]=companyAddress;
}
void getEmployDetails()
{
[Link]("Employ id is"+id);
getpermanentDetails();
[Link]("companyName is"+companyName);
[Link]("companyAddress is"+companyAddress);
}
}
class InherDemo
{
public static void main(String args[])
{
Employ e1=new Employ(104,"gopi","subbaraopeta-kotabommali",30,"srikakulam","adivarampeta-
narsannapeta");
[Link]();
}
}
[Link] Implementation
interface Father
{
double PROPERTY = 10000;
double HEIGHT = 5.6;
}
interface Mother
{
double PROPERTY = 30000;
double HEIGHT = 5.4;
}
class Child implements Father, Mother
{
void show()
{
[Link]("Total property is :" +([Link]+[Link]));
[Link] ("Child Average height is :" + ([Link] + [Link])/2 );
}
}
class InterfaceDemo
{
public static void main(String args[])
{
Child ob1 =new Child();
[Link]();
}
}
[Link] Operations
class StrOps
{
public static void main(String args[])
{
String str1 = "Object oriented programming";
String str2 = new String (str1);
String str3 = "JAVA STRINGS ARE POWERFUL";
int result;
char ch;
[Link]("Length of str1: " + [Link]());
// display str1, one char at a time.
for(int i=0; i < [Link](); i++)
[Link]([Link](i));
[Link]();
if([Link](str2))
[Link]("str1 equals str2");
else
[Link]("str1 does not equal str2");
if([Link](str3))
[Link]("str1 equals str3");
else
[Link]("str1 does not equal str3");
result = [Link](str3);
if(result == 0)
[Link]("str1 and str3 are equal");
else if(result < 0)
[Link]("str1 is less than str3");
else
[Link]("str1 is greater than str3");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}
[Link] Multiple Threads
import [Link].*;
class Theatre extends Thread
{
String str;
Theatre(String str)
{
[Link]=str;
}
public void run()
{
for(int i=1;i<=10;i++)
{
[Link](str+":"+i);
try
{
[Link](2000);
}
catch(InterruptedException ie)
{
[Link]();
}
}
}
}
class TDemo
{
public static void main(String args[])
{
Theatre obj1=new Theatre("cut ticket");
Theatre obj2=new Theatre("show chair");
Thread t1=new Thread(obj1);
Thread t2=new Thread(obj2);
[Link]();
[Link]();
}
}
[Link] Thread Priorities
public class HighLess extends Thread
{
public static void main(String args[])
{
HighLess hl1 = new HighLess();
HighLess hl2 = new HighLess();
// setting priorities
[Link](Thread.MAX_PRIORITY-1); // 9
[Link](Thread.MIN_PRIORITY+1); // 3
// setting the names
[Link]("High"); // for 8 thread
[Link]("Less"); // for 3 thread
// to retrieve the priorities
[Link]("High Priority is " + [Link]()); // prints 9
[Link]("Less Priority is " + [Link]()); // prints 3
[Link](); // wantedly hl2 is started first
[Link]();
}
public void run()
{
for(int i=0; i<10; i++)
{
[Link]([Link]() + ": " + i);
}
}
}
13. Userdefined Exceptions
//user defind Exception
class MyException extends Exception
{
int accno[]={1011,1012,1013,1014,1015};
String name[]={"gopi","babji","bhasha","jagadeesh","venky"};
double bal[]={2500,3500,1500,1000,4000};
MyException()
{
}
MyException(String str)
{
super(str);
}
public static void main(String args[])
{
try
{
MyException me=new MyException("");
[Link]("accno\tname\tbalance");
for(int i=0;i<5;i++)
{
[Link]([Link][i]+"/t"
+[Link][i]+"/t"+[Link][i]);
if([Link][i]<2000)
{
MyException me1=new MyException("Insufficient Balance");
throw me1;
}
}
}
catch(MyException e)
{
[Link]();
}
}
}
14. Drawing Polygon Lines Using Applet
/*<applet code="[Link]" width="350" height="300">
</applet>*/
import [Link].*;
import [Link].*;
public class DrawingPolygons extends Applet
{
public void paint(Graphics g)
{
int x[] = { 70, 150, 190, 80, 100 };
int y[] = { 80, 110, 160, 190, 100 };
[Link] (x, y, 5);
int x1[] = { 210, 280, 330, 210, 230 };
int y1[] = { 70, 110, 160, 190, 100 };
[Link] (x1, y1, 5);
}
}
[Link] Packages Using Access Specifiers
//create A package same
package same;
public class A
{
private int a=1;
public int b=2;
protected int c=3;
int d=4;
}
//class B of same package
package same;
import same.A;
public class B
{
public static void main(String args[])
{
A obj=new A();
[Link](obj.a);
[Link](obj.b);
[Link](obj.c);
[Link](obj.d);
}
}
package same;
import same.A;
class C extends A
{
public static void main(String args[])
{
C obj=new C();
[Link](obj.a);
[Link](obj.b);
[Link](obj.c);
[Link](obj.d);
}
}
[Link] a file And Reading a File Using Byte Stream Classes
//Creating a text file using byte stream classes
import [Link].*;
class Create1
{
public static void main(String args[]) throws IOException
{
//attach keyboard to DataInputStream
DataInputStream dis = new DataInputStream ([Link]);
//attach the file to FileOutputStream
FileOutputStream fout = new FileOutputStream ("[Link]");
//read data from DataInputStream and write into FileOutputStream
char ch;
[Link]("Enter @ at end : " ) ;
while( (ch = (char) [Link]() ) != '@' )
[Link](ch);
[Link]();
}
}
//Reading a text file using byte stream classes
import [Link].*;
class Read1
{
public static void main(String args[]) throws IOException
{
//attach the file to FileInputStream
FileInputStream fin = new FileInputStream("[Link]");
//read data from FileInputStream and display it on the monitor
int ch;
while( (ch = [Link]() ) != -1 )
[Link]((char) ch);
[Link]();
}
}