Monday, August 20, 2012

Programs

Data Structure Programs in java

Hi Here the Links to download various Data Structure Programs in java 

DoubleLinkedlist
Bubblesort java 
Binarysearch java
Linearsearch java 
Mergesort java 
Linkedlist java 
Stack java



Java Constructors Overloading

This Simple Java Program is for performing Constructor Overloading in Java. This is a part of Mumbai University MCA Colleges Java Practicals.

Constructor is required when we declare a class object. Java Class Constructors initializes the variables of the object. Constructor Overloading is done so that the objects of different forms can be initialized at the declaration time.

The Below program uses a class named Box which finds the volume of the Box object. This class has 2 constructors one for initializing a Cube Box and other for Initializing a Rectangular Box.
As those are constructors and return nothing, they dont have a return type.


class Box
{
int l,b,h;
Box(int x)             // Constructor for Cube
{
 l=x;
 b=x;
 h=x;
}
Box(int x,int y,int z)            //Constructor for Rectangle
{
 l=x;
 b=y;
 h=z;
}
void volume()
{
          System.out.println("Volume of the box is "+(l*b*h));
}
public static void main(String args[])
{
Box cube= new Box(10);          //Calling cube constructor
Box b=new Box(10,4,7);          //Calling box constructor
cube.volume();
b.volume();
}
}

Hope this Java Program is useful to you in some sense or other. Happy Programming and Studying.




In the Below program Abstract Class named Interest is used by 2 classes named SimpleInterest and CompoundInterest. These 2 classes uses the variables, Constructor and the display method of the abstract class. Both the classes use different definitions for the abstract method calc() to calculate the interest.

abstract class Interest
{
double p,amt,n;
Interest(double a,double y)
{
p=a;
n=y;
}
void display()
{
System.out.println("\nAMOUNT = "+p+"\nNOS OF YRS ="+n);
}
abstract void calc();
}

class SimpleInterest extends Interest
{
SimpleInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= (p*n*0.0925);
System.out.println("SIMPLE INTEREST="+amt);
}
}

class CompoundInterest extends Interest
{
CompoundInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= p*Math.pow((1+(8.5/100)),n);
System.out.println("COMPOUND INTEREST="+amt);
}
}

class Bank
{
public static void main(String str[])
{
Interest i;
SimpleInterest s1=new SimpleInterest(25000,5);
CompoundInterest c1=new CompoundInterest(25000,5);
i=s1;
i.display();
i.calc();
i=c1;
i.display();
i.calc();
}
}






The Below java program calculates the areas of Circle, Triangle and Rectangle with same name function but different signatures.


class Area
{
void area(int l,int b)
{
          System.out.println("AREA OF RECTANGLE ="+(l*b));
}
void area(double b,double h)
{
System.out.println("AREA OF TRIANGLE ="+(0.5*b*h));
}
void area(double r)
{
System.out.println("AREA OF CIRCLE ="+(3.14*r*r));
}
public static void main(String args[])
{
Area a=new Area();
a.area(10.5,20.4);
a.area(2,6);
a.area(15.3);
}
}



Interface just carry the signature of the function to be used. The class which implements a interface has to define the function which its gonna implement.

.

interface Exam
{
boolean pass(int marks);
}

interface Classify
{
String division(int avg);
}

class Result implements Exam,Classify
{
public boolean pass(int marks)
{
System.out.println("Marks obtained are "+ marks);
if(marks>=50)
return true;
else
return false;
}
public String division(int avg)
{
if(avg>=60)
return("First Division");
else if(avg>50)
return("Second Division");
else
return("Third Division");
}
public static void main(String ar[])
{
Result r =new Result();
boolean flg;
flg=r.pass(60);
if(flg==true)
System.out.println("Marks>=50");
else
System.out.println("Marks<50");
System.out.println(r.division(60)); 
}
}





No comments:

Post a Comment