java simple programs
1.Demonstration of sub class
// lab exercise 5
// demonstration of sub class
class Room //super class
{
float length;
float width;
Room(float x,float y)
{
length=x;
width=y;
}
float findArea()
{
return (length*width);
}
}
class LivingRoom extends Room // sub class
{
float height;
LivingRoom(float x,float y,float z)
{
super(x,y); //passing to super class constructor
height=10;
}
float Volume()
{
float v;
v=length*width*height;
return v;
}
}
class InherTest1
{
public static void main(String args[])
{
LivingRoom r1=new LivingRoom(14,15,12);
float area=r1.findArea(); //calling base class method
float vol=r1.Volume(); //calling sub class method
System.out.println("Area = " + area);
System.out.println("Volume = " +vol);
}
}
2.nested class
//lab excersise 6
//Nested class
class Outer //outer class
{
int a=10;
class Inner //inner class
{
int b=20;
void display()
{
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}
}
class NestedDemo
{
public static void main(String args[])
{
Outer obj1=new Outer(); //creating object for outer class
Outer.Inner obj2=obj1.new Inner(); //creating object inner class
obj2.display();
}
}
3.method overloading
//lab exercise 3
//method overloadinog
import java.lang.*;
class Overload
{
void display(String s)//method1 defined
{
System.out.println("s = "+s);
}
void display(int a) // methods defined
{
System.out.println("a = "+a);
}
void display(float b,float c)//method 3defined
{
System.out.println("b = "+ b +"\tc = "+c);
}
void display(double d) // method 4 defined
{
System.out.println("d = "+d);
}
}
class OverloadDemo
{
public static void main(String args[])
{
Overload ob=new Overload();
ob.display ("welcome to methods Overloading"); //callingmethod1
ob.display(1234); //calling method2
ob.display(3456.89d); //calling method4
ob.display(24.56f,153.5F); //calling method3
}
}
4.defining a class with two instance method
//2nd java program
//defining a class with two instance metods//
class Rectangle
{
float length;
float width;
void setdata(float x,float y)
{
length=x;
width=y;
}
float findArea()
{
float area;
area= length * width;
return (area);
}
}
class RectangleArea
{
public static void main(String args[])
{
float area1;
Rectangle rect=new Rectangle();
rect.setdata(10,15);
area1=rect.findArea();
System.out.println("Area= "+area1);
}
}
5.defining a class with two instance methods
//2nd java program
//defining a class with two instance methods//
class Rectangle
{
float length;
float width;
void setdata(float x,float y)
{
length=x;
width=y;
}
float findArea()
{
float area;
area= length * width;
return (area);
}
}
class RectangleArea
{
public static void main(String args[])
{
float area1;
Rectangle rect=new Rectangle();
rect.setdata(10,15);
area1=rect.findArea();
System.out.println("Area= "+area1);
}
}
6.Selection Sort
// Selection Sort
import java.lang.*;
class SelectionSort
{
public static void main(String args[])
{
String name[]={"d","e","g"};
int n=name.length;
int i,j,pos;
String temp;
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(name[j].compareTo(name[pos])<0)
pos=j;
}
if(pos!=i)
{
temp=name[pos];
name[pos]=name[i];
name[i]=temp;
}
}
System.out.println("the sorted names are.....\n");
for(i=0;i<n;i++)
{
System.out.println(name[i]);
}
}
}
No comments:
Post a Comment