Anna University (Syllabus)
V Semester (EEE)
Cs-1261 Object oriented programming
Lab programs
JAVA
[Link].1
Simple java programs
A) Largest of two numbers
import [Link].*;
class greatest
{
public static void main(String args[])
{
int a=10,b=20;
if(a>b)
{
[Link]("A="+a"is greater");
else
[Link]("B="+b"is greater");
}
}
}
B) Addition of two numbers
import [Link].*;
class add
{
public static void main(String args[])
{
int a=2,b=3,c;
c=a+b;
[Link]("Addition="+c);
}
}
C) Arithmetic operations
import [Link].*;
class arithmetic
{
public static void main(String args[]) throws IOException
{
int a,b;
DataInputStream=new DataInputStream([Link]);
[Link]("Enter a and b value:");
a=[Link]([Link]());
b=[Link]([Link]());
[Link]("The addition="+(a+b));
[Link]("The subtraction="+(a-b));
[Link]("The multiplication="+(a*b));
[Link]("The division="+(a/b));
}
}
D) Even or odd
import [Link].*;
class even
{
public static void main(String args[]) throes IOException
{
int a,b;
DataInputStream=new DataInputStream([Link]);
[Link]("Enter a number:");
a=[Link]([Link]());
b=a%2;
if(b==0)
{
[Link](a+"is even");
}
else
{
[Link](a+"is odd");
}
}
}
E) Factorial of a number
import [Link].*;
class factorial
{
public static void main(String args[]) throws IOException
{
int n,i,fact=1;
DataInputStream=new DataStreamInput([Link]);
[Link]("Enter a number:");
n=[Link]([Link]());
for(i=1;i<=n;i++)
{
fact=fact*i;
}
[Link]("Factorial="+fact);
}
}
F) Exception handling
import [Link].*;
class errorprg
{
public static void main(String args[])
{
int a=10,b=5,c=5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
[Link]("Division by zero");
}
y=a/(b+c);
[Link]("y="+y);
}
}
[Link].2
Interface implementing
A) To compute area
interface area
{
final static float pi=3.14f;
float compute(float x, float y);
}
class rectangle implements area
{
public float compute(float x, float y)
{
return (x*y);
}
}
class circle implements area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class interfacetest
{
public static void main(String args[])
{
rectangle r=new rectangle();
circle c=new circle();
area a;
a=r;
[Link]("Area of rectangle="+[Link](10,20));
a=c;
[Link]("Area of circle="+[Link](10,0));
}
}
B) Multiple inheritance using interface
class student
{
int rollno;
void getno(int n)
{
rollno=n;
}
void putno()
{
[Link]("Rollno="+rollno);
}
}
class test extends student
{
float m1,m2;
void getmark(float x, float y)
{
m1=x;
m2=y;
}
void putmark()
{
[Link]("Mark1="+m1);
[Link]("Mark2="+m2);
}
}
interface sports
{
float score=6.5f;
void putscore();
}
class result extends test implements sports
{
float total;
public void putscore()
{
[Link]("Sports score="+score);
}
void display()
{
total=m1+m2+score;
putno();
putmark();
putscore();
[Link]("Total score="+total);
}
}
class hybrid
{
public static void main(String args[])
{
result r=new result();
[Link](26);
[Link](90.9f,99.4f);
[Link]();
}
}
[Link].3
Developing packages
/*package 1*/
package pack;
import [Link].*;
public class student
{
public static void mark() throws IOException
{
string name;
int rollno,m1,m2;
DataInputStream din=new DataInputStream([Link]);
[Link]("Enter the name, rollno and 2submarks:");
name=[Link]();
rollno=[Link]([Link]());
m1=[Link]([Link]());
m2=[Link]([Link]());
[Link]("Name="+name);
[Link]("Rollno="+rollno);
[Link]("Mark1="+m1);
[Link]("Mark2="+m2);
}
}
/*package2*/
package pack1;
import [Link].*;
public class sports
{
public static void mark() throws IOException
{
string name;
int score;
DataInputStream din=new DataInputStream([Link]);
[Link]("Enter the game name and score:");
name=[Link]();
score=[Link]([Link]());
[Link]("Sports name="+name);
[Link]("Score="+score);
}
}
//Main program
import [Link].*;
import [Link];
import [Link];
class packmain()
{
public static void main(String args[]) throws IOException
{
student s=new student();
sports t=new sports();
[Link]();
[Link]();
}
}