Object oriented programming with java

advertisement
5TH PIT AND PIET
LAB MANUAL
OBJECT ORIENTED
PROGRAMMING
WITH JAVA
Practical 1:WAP to find out the area of circle
package area;
class AreaDemo
{
void find()
{
int r = 10;
double area = r * r * 3.14;
System.out.println("Area = "+area);
}
}
public class Area {
public static void main(String[] args) {
// TODO code application logic here
AreaDemo a = new AreaDemo();
a.find();
}
}
Output : Area = 314.00
Practical 2 WAP to display a factorial of a given number.
package fact;
class FactDemo
{
void find(int n)
{
int fact = 1;
for(int i=1;i<=n;i++)
{
fact = fact * i;
}
System.out.println("Factorial is "+fact);
}
}
public class Fact {
public static void main(String[] args) {
// TODO code application logic here
FactDemo f = new FactDemo();
f.find(5);
}
}
Output : Factorial is 120
Practical 3: WAP to print 1 to 25 prime numbers.
package prime;
class PrimeDemo
{
void find()
{
int flag =0;
for(int i=1;i<=25;i++)
{
for(int j=2; j<i;j++)
{
if(i%j==0)
{
flag = 1;
break;
}
}
if(flag ==0)
{
System.out.println(i);
}
flag = 0;
}
}
}
public class Prime {
public static void main(String[] args) {
// TODO code application logic here
PrimeDemo p = new PrimeDemo();
p.find();
}
}
Output : 2,3,5,7,11,13,17,19,23
Practical 4: WAP to display 1+1/2+1/3+ … upto n.
package series;
class SeriesDemo
{
void find()
{
double sum=0;
int n=10;
for(double i=1;i<=n;i++)
{
sum = sum + (1/i);
}
System.out.println("Value = "+sum);
}
}
public class Series {
public static void main(String[] args) {
// TODO code application logic here
SeriesDemo s = new SeriesDemo();
s.find();
}
}
Output : 2.98768657
Practical 5: WAP to display average value.
package average;
class AvgDemo
{
void find()
{
int a [] = new int[5];
int sum = 0;
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;
int avg;
for(int i=0;i<a.length;i++)
{
sum = sum + a[i];
}
avg = sum / a.length;
System.out.println("Average = "+avg);
}
}
public class Average {
public static void main(String[] args) {
AvgDemo a = new AvgDemo();
a.find();
// TODO code application logic here
}
}
Output : Average = 30
Practical 6:WAP that accepts integer value as a command line argument.
package cmd;
public class Cmd {
public static void main(String[] args) {
// TODO code application logic here
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
}
}
Output : 10 20 30
Practical 7: WAP to sort an array.
package sort;
class s
{
void srt()
{
int a[] = new int[5];
int temp;
a[1] = 20;
a[0] = 21;
a[3] = 5;
a[2] = 989;
a[4] = 12;
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("Sorted Array.....");
for(int i =0;i<5;i++ )
{
System.out.println(a[i]);
}
}
}
public class Sort {
public static void main(String[] args) {
// TODO code application logic here
s o = new s();
o.srt();
}
}
Output : 5 12 20 21 989
Practical 8:WAP to perform method overloading.
package method;
class m
{
void find(int length , int breadth)
{
int area = length*breadth;
System.out.println("Area = "+area);
}
void find()
{
int length =10, breadth = 20;
int area = length*breadth;
System.out.println("Area = "+area);
}
}
public class Method {
public static void main(String[] args) {
// TODO code application logic here
m o = new m();
o.find();
o.find(20, 30);
}
}
Output : Area = 200
Area = 600
Practical-9 Write a program to create an interface.
public class Main {
public static void main(String[] args) {
shape circleshape=new circle();
circleshape.Draw();
}
}
interface shape
{
public String baseclass="shape";
public void Draw();
}
class circle implements shape
{
public void Draw() {
System.out.println("Drawing Circle here");
}
}
Result : Drawing Circle here
Practical -10 Write a Program to create an abstract class
abstract class Shape
{
abstract void display();
}
class Circle extends Shape
{
void display()
{
System.out.println("You are using circle class");
}
}
class Rectangle extends Shape
{
void display()
{
System.out.println("You are using rectangle class");
}
}
class Triangle extends Shape
{
void display()
{
System.out.println("You are using triangle class");
}
}
class AbstractClassDemo
{
public static void main(String args[])
{
Shape sObj = new Circle();
sobj.display();
sObj = new Rectangle();
sobj.display();
sObj = new Triangle();
sobj.display();
}
}
Output
You are using circle class
You are using rectangle class
You are using triangle class
Practical -11 Write a Program to create thread and life cycle
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
}
}
Result:
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Practical-12 Write a Program to check wheather a string is palindrome or
not
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
Practical- 13 Write a program for custom exception in java
public class MyOwnException {
public static void main(String[] a){
try{
MyOwnException.myTest(null);
} catch(MyAppException mae){
System.out.println("Inside catch block: "+mae.getMessage());
}
}
static void myTest(String str) throws MyAppException{
if(str == null){
throw new MyAppException("String val is null");
}
}
}
class MyAppException extends Exception {
private String message = null;
public MyAppException() {
super();
}
public MyAppException(String message) {
super(message);
this.message = message;
}
public MyAppException(Throwable cause) {
super(cause);
}
@Override
public String toString() {
return message;
}
@Override
public String getMessage() {
return message;
}
}
Practical -14 write a program to draw Triangle in java using applet
import java.applet.Applet;
import java.awt.Graphics;
public class drawtriangle extends Applet {
public void paint(Graphics g)
{
g.drawLine(50, 150, 150, 150);
g.drawLine(50, 50, 150, 150);
g.drawLine(50, 50, 50, 150);
}
}
Practical -15 Write a program to draw String in java
import java.awt.*;
import java.applet.*;
public class WelcomeApplet2 extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString( "Welcome to", 25, 25 );
g.drawString( "Java Programming!", 25, 40 );
}
}
Practical -16 Write a program to display digital clock in applet.
import java.awt.Graphics;
import java.awt.Font;
import java.util.Date;
public class DigitalClock extends java.applet.Applet implements Runnable{
Date d;
Font f = new Font("TimesRoman",Font.BOLD,25);
Thread t;
@Override
public void start()
{
if(t==null)
{
t = new Thread(this);
t.start();
}
}
//@Override
public void run()
{
while(true){
//d = new Date();
repaint();
try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
public void stop()
{
if(t!=null)
{
//t.stop();
t=null;
}
}
public void paint(Graphics g)
{
d = new Date();
g.setFont(f);
g.drawString(d.toString(), 50, 100);
}
private void initComponents() {
setLayout(new java.awt.BorderLayout());
}
}
Download