0% found this document useful (0 votes)
159 views8 pages

Anna University (Syllabus) V Semester (EEE) Cs-1261 Object Oriented Programming Lab Programs Java

The document contains examples of Java programs covering basic concepts like: - Finding the largest of two numbers and addition - Arithmetic operations by taking input from the user - Checking if a number is even or odd - Calculating the factorial of a number - Handling exceptions in a program It also includes examples of: - Implementing interfaces to compute areas of shapes - Using multiple inheritance with interfaces - Developing packages and using them in a main program
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views8 pages

Anna University (Syllabus) V Semester (EEE) Cs-1261 Object Oriented Programming Lab Programs Java

The document contains examples of Java programs covering basic concepts like: - Finding the largest of two numbers and addition - Arithmetic operations by taking input from the user - Checking if a number is even or odd - Calculating the factorial of a number - Handling exceptions in a program It also includes examples of: - Implementing interfaces to compute areas of shapes - Using multiple inheritance with interfaces - Developing packages and using them in a main program
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Anna University (Syllabus)

V Semester (EEE)
Cs-1261 Object oriented programming
Lab programs
JAVA
EX.NO.1
Simple java programs
A) Largest of two numbers
import java.io.*;
class greatest
{
public static void main(String args[])
{
int a=10,b=20;
if(a>b)
{
System.out.println("A="+a"is greater");
else
System.out.println("B="+b"is greater");
}
}
}

B) Addition of two numbers

import java.io.*;
class add
{
public static void main(String args[])
{
int a=2,b=3,c;
c=a+b;
System.out.println("Addition="+c);
}
}

C) Arithmetic operations

import java.io.*;
class arithmetic
{
public static void main(String args[]) throws IOException
{
int a,b;
DataInputStream=new DataInputStream(System.in);
System.out.println("Enter a and b value:");
a=Integer.parseInt(din.readLine());
b=Integer.parseInt(din.readLine());
System.out.println("The addition="+(a+b));
System.out.println("The subtraction="+(a-b));
System.out.println("The multiplication="+(a*b));
System.out.println("The division="+(a/b));
}
}

D) Even or odd

import java.io.*;
class even
{
public static void main(String args[]) throes IOException
{
int a,b;
DataInputStream=new DataInputStream(System.in);
System.out.println("Enter a number:");
a=Integer.parseInt(din.readLine());
b=a%2;
if(b==0)
{
System.out.println(a+"is even");
}
else
{
System.out.println(a+"is odd");
}
}
}

E) Factorial of a number

import java.io.*;
class factorial
{
public static void main(String args[]) throws IOException
{
int n,i,fact=1;
DataInputStream=new DataStreamInput(System.in);
System.out.println("Enter a number:");
n=Integer.parseInt(din.readLine());
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial="+fact);
}
}

F) Exception handling

import java.io.*;
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)
{
System.out.println("Division by zero");
}
y=a/(b+c);
System.out.println("y="+y);
}
}

EX.NO.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;
System.out.println("Area of rectangle="+a.compute(10,20));
a=c;
System.out.println("Area of circle="+a.compute(10,0));
}
}

B) Multiple inheritance using interface

class student
{
int rollno;
void getno(int n)
{
rollno=n;
}
void putno()
{
System.out.println("Rollno="+rollno);
}
}
class test extends student
{
float m1,m2;
void getmark(float x, float y)
{
m1=x;
m2=y;
}
void putmark()
{
System.out.println("Mark1="+m1);
System.out.println("Mark2="+m2);
}
}
interface sports
{
float score=6.5f;
void putscore();
}
class result extends test implements sports
{
float total;
public void putscore()
{
System.out.println("Sports score="+score);
}
void display()
{
total=m1+m2+score;
putno();
putmark();
putscore();
System.out.println("Total score="+total);
}
}
class hybrid
{
public static void main(String args[])
{
result r=new result();
r.getno(26);
r.getmark(90.9f,99.4f);
r.display();
}
}

EX.NO.3

Developing packages

/*package 1*/
package pack;
import java.io.*;
public class student
{
public static void mark() throws IOException
{
string name;
int rollno,m1,m2;
DataInputStream din=new DataInputStream(System.in);
System.out.println("Enter the name, rollno and 2submarks:");
name=din.readLine();
rollno=Integer.parseInt(din.readLine());
m1=Integer.parseInt(din.readLine());
m2=Integer.parseInt(din.readLine());
System.out.println("Name="+name);
System.out.println("Rollno="+rollno);
System.out.println("Mark1="+m1);
System.out.println("Mark2="+m2);
}
}

/*package2*/
package pack1;
import java.io.*;
public class sports
{
public static void mark() throws IOException
{
string name;
int score;
DataInputStream din=new DataInputStream(System.in);
System.out.println("Enter the game name and score:");
name=din.readLine();
score=Integer.parseInt(din.readLine());
System.out.println("Sports name="+name);
System.out.println("Score="+score);
}
}

//Main program
import java.io.*;
import pack.student;
import pack1.sports;
class packmain()
{
public static void main(String args[]) throws IOException
{
student s=new student();
sports t=new sports();
s.mark();
t.display();
}
}

You might also like