You are on page 1of 19

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 java.io.*;
class Student
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name,group,course;
int rno,year,eng,tel,maths,physics,computers,tot;
float avg;
//input the details
System.out.println("Enter Roll no");
rno=Integer.parseInt(br.readLine());
System.out.println("Enter name");
name=br.readLine();
System.out.println("Enter group");
group=br.readLine();
System.out.println("Enter course");
course=br.readLine();
System.out.println("Enter year");
year=Integer.parseInt(br.readLine());
//input the marks
System.out.println("Enter telugu marks");
tel=Integer.parseInt(br.readLine());
System.out.println("Enter English");
eng=Integer.parseInt(br.readLine());
System.out.println("Enter Maths marks");
maths=Integer.parseInt(br.readLine());
System.out.println("Enter Physics");
physics=Integer.parseInt(br.readLine());
System.out.println("Enter computers marks");
computers=Integer.parseInt(br.readLine());
//calculate total and average
tot=eng+tel+maths+physics+computers;
avg=tot/5;
//displaying the details
System.out.println("---Student details are----");
System.out.println("roll no is :"+rno);
System.out.println("name is :"+name);
System.out.println("group is :"+group);
System.out.println("course is :"+course);
System.out.println("year is :"+year);
System.out.println("telugu marks are :"+tel);
System.out.println("english marks are :"+eng);
System.out.println("maths marks are :"+maths);
System.out.println("physics marks are :"+physics);
System.out.println("computers marks are :"+computers);
System.out.println("Total marks are :"+tot);
System.out.println("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;
System.out.println("Before swapping");
System.out.println("a value is "+a+"and"+"b value is "+b);
c=a;
a=b;
b=c;
System.out.println("After swapping");
System.out.println("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;
System.out.println("Before swapping");
System.out.println("a value is "+a+"and"+"b value is "+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After swapping");
System.out.println("a value is "+a+"and"+"b value is "+b);
}
}
3. Operators
import java.io.*;
class Operators
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int choice;
boolean yes=true;
do
{
System.out.println("-------------------------------------------");
System.out.println("1.Arthematic Operators");
System.out.println("2.Relations Operators");
System.out.println("3.Bitwise Operators");
System.out.println("4.Logical operators");
System.out.println("5.Exit");
System.out.println("Enter your choice");
choice = Integer.parseInt(br.readLine());
System.out.println("-------------------------------------------");
switch(choice)
{
case 1:
int a,b,c,d,e,f,g;
System.out.println("Enter any 2 numbers for Addition,Subtraction,Multiplication,Division,Modulos");
System.out.println("Enter First Number");
a=Integer.parseInt(br.readLine());;
System.out.println("Enter Second Number");
b=Integer.parseInt(br.readLine());
System.out.println("Arthematic operarors Results are");
System.out.println("-------------------------------------------");
c=a+b;
System.out.println("Addition of 2 Numbers is : "+c);
d=a-b;
System.out.println("Subtraction of 2 Numbers is : "+d);
e=a*b;
System.out.println("Multiplication of 2 Numbers is : "+e);
f=a/b;
System.out.println("Division of 2 Numbers is : "+f);
g=a%b;
System.out.println("Modulos of 2 Numbers is : "+g);
break;
case 2:
int h,i;
System.out.println("Enter any 2 numbers for
graterthan,lessthan,graterthanequals,lessthansequals,equalsto");
System.out.println("Enter First Number");
h=Integer.parseInt(br.readLine());;
System.out.println("Enter Second Number");
i=Integer.parseInt(br.readLine());
System.out.println("Relational operarors Results are");
System.out.println("-------------------------------------------");
if(h>i)
{
System.out.println(h+"is grater than"+i);
}
else
System.out.println(h+"is not grater than"+i);
if(h<i)
{
System.out.println(h+"is less than"+i);
}
else
System.out.println(h+"is not less than"+i);
if(h>=i)
{
System.out.println(h+"is grater than equals to"+i);
}
else
System.out.println(h+"is not grater than equals to"+i);
if(h<=i)
{
System.out.println(h+"is less than equals"+i);
}
else
System.out.println(h+"is not less than equals"+i);
if(h==i)
{
System.out.println(h+"is equals to "+i);
}
else
System.out.println(h+"is not equals to "+i);
if(h!=i)
{
System.out.println(""+h+"and"+i+"both are not equal");
}
break;
case 3:
int j,k,complement,and,or,exor,lshift,rshift,zrshift;
System.out.println("Enter any 2 numbers for Bitwise Operators");
System.out.println("Enter First Number");
j=Integer.parseInt(br.readLine());;
System.out.println("Enter Second Number");
k=Integer.parseInt(br.readLine());
System.out.println("Bitwise operarors Results are");
System.out.println("-------------------------------------------");
complement=~(j);
System.out.println("complement of "+j+"is :"+complement);
and=(j&k);
System.out.println("Bitwise And of "+j+" & "+k+"is :"+and);
or=(j|k);
System.out.println("Bitwise Or of "+j+"| "+k+"is :"+or);
exor=(j^k);
System.out.println("Bitwise ExclusiveOR of "+j+"^ "+k+"is :"+exor);
lshift=(j<<2);
System.out.println("Bitwise Left Shift of "+j+"<<2 is :"+lshift);
rshift=(k>>2);
System.out.println("Bitwise Right Shift of "+k+">>2 is :"+rshift);
zrshift=(k>>>2);
lshift=(j<<2);
System.out.println("Bitwise ZeroFillRight Shift of "+k+">>>2 is :"+zrshift);
break;
case 4:
int l,m,n,la,lo;
System.out.println("Enter any 3 numbers for Logical Operators");
System.out.println("Enter First Number");
l=Integer.parseInt(br.readLine());;
System.out.println("Enter Second Number");
m=Integer.parseInt(br.readLine());
System.out.println("Enter Third Number");
n=Integer.parseInt(br.readLine());
System.out.println("Logical operarors Results are");
System.out.println("-------------------------------------------");
System.out.println("Logical And Result is");
if((l>m)&&(l>n))
{
la=l;
System.out.println("the Gratest no is :"+la);
}
else if(m>n)
{
la=m;
System.out.println("the Gratest no is :"+la);
}
else
{
la=n;
System.out.println("the Gratest no is :"+la);
}
System.out.println("Logical Or Result is ");
if((l>m)||(l>n))
{
lo=l;
System.out.println("the Gratest no is :"+la);
}
else if(m>n)
{
lo=m;
System.out.println("the Gratest no is :"+la);
}
else
{
lo=n;
System.out.println("the Gratest no is :"+la);
}
System.out.println("Logical Not Result is");
if(!(l==0))
{
System.out.println(l+" is not equals to zero ");
}
break;
case 5:
yes=false;
break;
default :
System.out.println("wrong choice");
}
}while(yes==true);
}
}
4. Prime Number Generation
import java.io.*;
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)
System.out.print(n+"\t");
}
}
}
5. Matrix Multiplication
import java.io.*;
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;
System.out.println("A matrix is:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
System.out.println("B matrix is:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.print("\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];
}
}
}
//System.out.println("matrix multiplication of A and B is:");
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.print("\n");
}
}
}
6. Generating Student Details using Class & Displaying with 3 Different Objects
import java.io.*;
class Student
{
int rno,s1,s2,s3,total;
String name;
float avg;
void Sdetails()
{
System.out.println("rno is:"+rno);
System.out.println("name is :"+name);
System.out.println("sub1 marks are:"+s1);
System.out.println("sub2 marks are:"+s2);
System.out.println("sub3 marks are:"+s3);
total=s1+s2+s3;
System.out.println("total marks are:"+total);
avg=total/3;
System.out.println("avg marks are:"+avg);
}
}
class Stddetails
{
public static void main(String args[])
{
Student m1=new Student();
System.out.println("---First object---");
System.out.println("bhanu details are:");
m1.rno=2;
m1.name="bhanu";
m1.s1=87;
m1.s2=90;
m1.s3=99;
m1.Sdetails();
Student m2=new Student();
System.out.println("---Second object---");
System.out.println("priya details are:");
m2.rno=4;
m2.name="priya";
m2.s1=85;
m2.s2=69;
m2.s3=80;
m2.Sdetails();
Student m3=new Student();
System.out.println("---Third object---");
System.out.println("rajini details are:");
m3.rno=8;
m3.name="rajini";
m3.s1=90;
m3.s2=70;
m3.s3=50;
m3.Sdetails();
}
}
7.Method OverLoading & Method OverRiding

//over loading
class Sample
{
void add(int a,int b)
{
System.out.println("sum of 2 no's:"+(a+b));
}
void add(int a,int b,int c)
{
System.out.println("sum of 3 no's:"+(a+b+c));
}
}
class OverLoad
{
public static void main(String args[])
{
Sample s=new Sample();
s.add(7,8);
s.add(5,6,7);
}
}

//overriding

import java.io.*;
import java.util.*;
class Find
{
void calc(long x)
{
System.out.println("Sqaure value of x is"+(x*x));
}
void calc(int x)
{
System.out.println("Sqaure root of value x is"+Math.sqrt(x));
}
}
class OverRide
{
public static void main(String args[])
{
Find z=new Find();
z.calc(25);
}
}
8.Single Level Inheritance
//inhertiance example
class Person
{
String name;
String permanentAddress;
int age;
void setpermanentDetails(String name,String permanentAddress,int age)
{
this.name=name;
this.permanentAddress=permanentAddress;
this.age=age;
}
void getpermanentDetails()
{
System.out.println("name is"+name);
System.out.println("permanentAddress is"+permanentAddress);
System.out.println("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)
{
this.id=id;
setpermanentDetails(name,permanentAddress,age);
this.companyName=companyName;
this.companyAddress=companyAddress;
}
void getEmployDetails()
{
System.out.println("Employ id is"+id);
getpermanentDetails();
System.out.println("companyName is"+companyName);
System.out.println("companyAddress is"+companyAddress);
}
}
class InherDemo
{
public static void main(String args[])
{
Employ e1=new Employ(104,"gopi","subbaraopeta-kotabommali",30,"srikakulam","adivarampeta-
narsannapeta");
e1.getEmployDetails();
}
}
9.Interface 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()
{
System.out.println("Total property is :" +(Father.PROPERTY+Mother.PROPERTY));
System.out.println ("Child Average height is :" + (Father.HEIGHT + Mother.HEIGHT)/2 );
}
}
class InterfaceDemo
{
public static void main(String args[])
{
Child ob1 =new Child();
ob1.show();
}
}
10.String 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;
System.out.println("Length of str1: " + str1.length());
// display str1, one char at a time.
for(int i=0; i < str1.length(); i++)
System.out.print(str1.charAt(i));
System.out.println();
if(str1.equals(str2))
System.out.println("str1 equals str2");
else
System.out.println("str1 does not equal str2");
if(str1.equals(str3))
System.out.println("str1 equals str3");
else
System.out.println("str1 does not equal str3");
result = str1.compareTo(str3);
if(result == 0)
System.out.println("str1 and str3 are equal");
else if(result < 0)
System.out.println("str1 is less than str3");
else
System.out.println("str1 is greater than str3");
System.out.println(str1.toUpperCase());
System.out.println(str3.toLowerCase());
System.out.println(str3.trim());
}
}
11.Creating Multiple Threads
import java.io.*;
class Theatre extends Thread
{
String str;
Theatre(String str)
{
this.str=str;
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(str+":"+i);
try
{
Thread.sleep(2000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
}
}
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);
t1.start();
t2.start();
}
}
12.Creating Thread Priorities
public class HighLess extends Thread
{
public static void main(String args[])
{
HighLess hl1 = new HighLess();
HighLess hl2 = new HighLess();
// setting priorities
hl1.setPriority(Thread.MAX_PRIORITY-1); // 9
hl2.setPriority(Thread.MIN_PRIORITY+1); // 3
// setting the names
hl1.setName("High"); // for 8 thread
hl2.setName("Less"); // for 3 thread
// to retrieve the priorities
System.out.println("High Priority is " + hl1.getPriority()); // prints 9
System.out.println("Less Priority is " + hl2.getPriority()); // prints 3
hl2.start(); // wantedly hl2 is started first
hl1.start();
}
public void run()
{
for(int i=0; i<10; i++)
{
System.out.println(this.getName() + ": " + 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("");
System.out.println("accno\tname\tbalance");
for(int i=0;i<5;i++)
{
System.out.println(me.accno[i]+"/t"
+me.name[i]+"/t"+me.bal[i]);
if(me.bal[i]<2000)
{
MyException me1=new MyException("Insufficient Balance");
throw me1;
}
}
}
catch(MyException e)
{
e.printStackTrace();
}
}
}
14. Drawing Polygon Lines Using Applet

/*<applet code="DrawingPolygons.class" width="350" height="300">


</applet>*/
import java.awt.*;
import java.applet.*;
public class DrawingPolygons extends Applet
{
public void paint(Graphics g)
{
int x[] = { 70, 150, 190, 80, 100 };
int y[] = { 80, 110, 160, 190, 100 };
g.drawPolygon (x, y, 5);
int x1[] = { 210, 280, 330, 210, 230 };
int y1[] = { 70, 110, 160, 190, 100 };
g.fillPolygon (x1, y1, 5);
}
}
15.Creating 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();
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);
System.out.println(obj.d);
}
}

package same;
import same.A;
class C extends A
{
public static void main(String args[])
{
C obj=new C();
System.out.println(obj.a);
System.out.println(obj.b);
System.out.println(obj.c);
System.out.println(obj.d);
}
}
16.Creating a file And Reading a File Using Byte Stream Classes

//Creating a text file using byte stream classes


import java.io.*;
class Create1
{
public static void main(String args[]) throws IOException
{
//attach keyboard to DataInputStream
DataInputStream dis = new DataInputStream (System.in);
//attach the file to FileOutputStream
FileOutputStream fout = new FileOutputStream ("myfile.txt");
//read data from DataInputStream and write into FileOutputStream
char ch;
System.out.println("Enter @ at end : " ) ;
while( (ch = (char) dis.read() ) != '@' )
fout.write(ch);
fout.close();
}
}

//Reading a text file using byte stream classes


import java.io.*;
class Read1
{
public static void main(String args[]) throws IOException
{
//attach the file to FileInputStream
FileInputStream fin = new FileInputStream("myfile.txt");
//read data from FileInputStream and display it on the monitor
int ch;
while( (ch = fin.read() ) != -1 )
System.out.print((char) ch);
fin.close();
}
}