Sunday 30 September 2012

Java Practical - 20

0 comments



Program Definition 20 ::


Define one class E in package epack.  In class E, three variables are defined of access modifiers protected, private & public. Define class F in package fpack which extends E and write display() method which access variables of class E.  Define class G in package gpack which has one method display() in that create one object of class E and display its variables.  Define class ProtectedDemo in package hpack in which write main() method.  Create objects of class F and G and class display method for both these objects.
 



interface I1
{
}

interface I2
{
}

interface I3 extends I1,I2
{
}

interface I4
{
}

class X implements I3
{
}

class W extends X implements I4
{
}

class Prog20
{
public static void main( String args[] )
{
W wObj = new W();

if ( wObj instanceof I1 )
System.out.println(" W implements I1 ");
if ( wObj instanceof I2 )
System.out.println(" W implements I2 ");
if ( wObj instanceof I3 )
System.out.println(" W implements I3 ");
if ( wObj instanceof I4 )
System.out.println(" W implements I4 ");
if ( wObj instanceof X )
System.out.println(" W extends X ");
}
}

/*

D:\JAVA >javac Prog20.java

D:\JAVA >java Prog20
 W implements I1
 W implements I2
 W implements I3
 W implements I4
 W extends X
*/

Saturday 29 September 2012

Java Practical - 19

0 comments


    Program Definition 19 ::
 
    The abstract class Fish declares one abstract method named display().   It also has two abstract subclasses named FreshWaterFish and SaltWaterFish.  Define three concrete subclasses named Trout, Flounder and Tuna.  Trout extends FreshWaterFish and Flounder and Tuna both extends SaltWaterFish.  Write a main() method which creates an array of type Fish.  Instantiate and assign different types of Fish to the elements of the array.  Display the name of fish which is of type SaltWaterFish using display() method.




abstract class Fish
{
String name;
abstract public void display();
}

abstract class SaltWaterFish extends Fish
{
}

abstract class FreshWaterFish extends Fish
{
}

class Trout extends FreshWaterFish
{
Trout( String n )
{
this.name = n;
}
public void display()
{
System.out.println( name );
}
}

class Flounder extends SaltWaterFish
{
Flounder( String n )
{
this.name = n ;
}
public void display()
{
System.out.println( name );
}
}

class Tuna extends SaltWaterFish
{
Tuna( String n )
{
this.name = n ;
}
public void display()
{
System.out.println( name );
}
}


class Prog19
{
public static void main( String args[] )
{
Fish fish[] = new Fish[3] ;

fish [ 0 ] = new Trout("Trout");
fish [ 1 ] = new Flounder("Flounder");
fish [ 2 ] = new Tuna("Tuna");

for ( Fish f : fish)
{
//finds fish that belongs to SaltWater using instanceOf operator
if ( f instanceof SaltWaterFish)
f.display();
}
}
}

/*

D:\JAVA >javac Prog19.java

D:\JAVA >java Prog19
Flounder
Tuna
*/

Friday 28 September 2012

Java Practical - 18

0 comments


    Program Definition 18 ::
 
     Interface LuminousObject declares lightOff() and lightOn() methods.  Class SolidObject is extended by Cone and Cube.  Class LuminousCone extends Cone and implements LuminousObject.  Class LuminousCube extends Cube and implements LuminousObject.  Instantiate the LuminousCone and LuminousCube classes.  Use interface reference to refer to those objects.  Invoke the methods of the LuminousObject interface via the interface reference.




interface LuminousObject
{
public void lightOn();
public void lightOff();
}

class SolidObject
{
}

class Cube extends SolidObject
{
}

class Cone extends SolidObject
{
}

class LuminousCone extends Cone implements LuminousObject
{
public void lightOn()
{
System.out.println( " lightOn() from LuminousCone class " );
}
public void lightOff()
{
System.out.println( " lightOff() from LuminousCone class " );
}
}

class LuminousCube extends Cube implements LuminousObject
{
public void lightOn()
{
System.out.println( " lightOn() from LuminousCube class " );
}
public void lightOff()
{
System.out.println( " lightOff() from LuminousCone class " );
}
}


class Prog18
{
public static void main( String args[] )
{
LuminousObject lObj[] = new  LuminousObject[2];

lObj[ 0 ] = new LuminousCone();
lObj[ 1 ] = new LuminousCube();

for ( LuminousObject l : lObj )
{
l.lightOn();
}

for ( LuminousObject l : lObj )
{
l.lightOff();
}
}
}

/*

D:\MCA\JAVA >javac Prog18.java

D:\MCA\JAVA >java Prog18
 lightOn() from LuminousCone class
 lightOn() from LuminousCube class
 lightOff() from LuminousCone class
 lightOff() from LuminousCone class
*/

Thursday 27 September 2012

Java Practical - 17

0 comments


    Program Definition 17 ::
 
     Interface A declares a display() method.  Classes C1, C2 and C3 implement that method.  The main() method declares variable a of type A.  Therefore, it can hold a reference to any object of class C1, C2 and C3 and invoking the display() method relative to the interface reference.




interface A
{
public void display();
}

class C1 implements A
{
public void display()
{
System.out.println("C1 display called");
}
}
class C2 implements A
{
public void display()
{
System.out.println("C2 display called");
}

}
class C3 implements A
{
public void display()
{
System.out.println("C3 display called");
}

}

class Prog17
{
public static void main( String args[] )
{
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();

c1.display();
c2.display();
c3.display();
}
}

/*
D:\JAVA >javac Prog17.java

D:\JAVA >java Prog17
C1 display called
C2 display called
C3 display called
*/

Tuesday 25 September 2012

Java Practical - 15

0 comments


    Program Definition 15 ::
 
     Define one abstract class named Mammal.  Bear, Elephant, Horse and Lion are concrete classes which extends Mammal.  Define interface Vehicle which declares drive() method and this interface is implemented by the Elephant and Horse classes.  Write main() method which creates and initializes an array of four Mammal objects.  Call drive() method for each object which has implemented Vehicle interface.




interface Vhicle
{

public void drive();
}

class Mammel
{

}

class Bear extends Mammel
{


}

class Elephant extends Mammel implements Vhicle
{
public void drive()
{
System.out.println("Elephant Called");
}

}

class Horse extends Mammel implements Vhicle
{
public void drive()
{
System.out.println("Horses Called");
}
}

class Lion extends Mammel
{

}

class TestMammel
{
public static void main( String args[] )
{

Horse h = new Horse();
Elephant e = new Elephant();

h.drive();
e.drive();

}
}


/*

D:\JAVA >javac TestMammel.java

D:\JAVA >java TestMammel
Horse Called
Elephant Called
*/

Monday 24 September 2012

Java Practical - 14

0 comments


    Program Definition 14::

     The abstract Fruit class has four subclasses named Apple, Banana, Orange and Strawberry.  Write an application that demonstrates how to establish this class hierarchy.  Declare one instance variable of type string that indicates the color of a fruit.  Create and display instances of these object. Override the toString() method of Object to return a string with the name of the fruit and its color.




abstract class Fruit
{
String color;

Fruit( String c )
{
color = c ;
}
abstract public String toString();

}

class Apple extends Fruit
{
Apple( String c )
{
super(c);
}

public String toString()
{
return "Apple color :: " + color ;
}

}

class Banana extends Fruit
{
Banana( String c )
{
super(c);
}
public String toString()
{
return "Banana color :: " + color ;
}
}

class Orange extends Fruit
{
Orange( String c )
{
super(c);
}
public String toString()
{
return "Orange color :: " + color ;
}

}

class Stawberry extends Fruit
{
Stawberry( String c )
{
super(c);
}
public String toString()
{
return "Stawberry color :: " + color ;
}

}

class TestFruit
{
public static void main( String args[] )
{

Fruit f[] = new Fruit[4];

f[ 0 ] = new Apple("Red");
f[ 1 ] = new Banana("Yellow");
f[ 2 ] = new Orange("Orange");
f[ 3 ] = new Stawberry("Red");


for( Fruit f1 : f )
{
System.out.println(f1.toString());
}

}
}


/*
D:\JAVA >javac TestFruit.java

D:\JAVA >java TestFruit
Apple color :: Red
Banana color :: Yellow
Orange color :: Orange
Stawberry color :: Red
*/

Sunday 23 September 2012

Java Practical - 13

0 comments


    Program Definition 13 ::

     The abstract Widget class has four concrete subclasses named Widget A, WidgetB, WidgetC and WidgetD.  Each of these four classes has a different mass in kilograms.  The mass of any WidgetA object is 4 kilograms.  The masses for the WidgetB, WidgetC and WidgetD classes are 1, 5 and 17 kilograms, respectively.  Each widget object has a string that identifies its color.  Create six different Widgets and store them in a one-dimension array.  Display the entries in the array and their total mass.




abstract class Widget
{
int weight;
String color;

Widget( int w , String c )
{
weight = w ; color = c ;
}
abstract public void display();
}

class WidgetA extends Widget
{

WidgetA( String c )
{
super( 4 , c);
}

public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}
}

class WidgetB extends Widget
{
WidgetB( String c )
{
super( 1 , c );
}
public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}

}

class WidgetC extends Widget
{
WidgetC( String c )
{
super( 5 , c );
}
public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}

}

class WidgetD extends Widget
{
WidgetD( String c )
{
super( 17 , c );
}
public void display()
{
System.out.println( "Weight = " + weight + "\n color = " + color );
}

}


class TestWidget
{
public static void main( String args[] )
{
Widget w[] = new Widget[6];

w[ 0 ] = new WidgetA("yellow");
w[ 1 ] = new WidgetA("black");
w[ 2 ] = new WidgetB("violet");
w[ 3 ] = new WidgetC("red");
w[ 4 ] = new WidgetC("white");
w[ 5 ] = new WidgetD("blue");


int totalMass = 0;
for( Widget w1 : w)
{
w1.display();

totalMass = totalMass + w1.weight ;
}

System.out.println("\nTotal Mass is :: " + totalMass);
}
}


/*
D:\JAVA >java TestWidget
Weight = 4
 color = yellow
Weight = 4
 color = black
Weight = 1
 color = violet
Weight = 5
 color = red
Weight = 5
 color = white
Weight = 17
 color = blue
Total Mass is :: 36
*/

Saturday 22 September 2012

Java Practical - 12

0 comments


    Program Definition 12 ::
 
     The abstract Monster class has three concrete subclasses named Vampire, Werewolf and Zombie.  Create six different monsters of various types and store them in a one-dimensional array.  Create a loop that displays the type of each monster.




abstract class Monster
{
String name;

Monster(String n)
{
name = n;
}

abstract public void display();
}

class Vampire extends Monster
{

Vampire(String n)
{
super(n);
}


public void display()
{
System.out.println("This is Vampire :: " + name);
}
}

class Werewolf extends Monster
{

Werewolf(String n)
{
super(n);
}
public void display()
{
System.out.println("This is Werewolf :: " + name);
}
}

class Zombie extends Monster
{
Zombie(String n)
{
super(n);
}

public void display()
{
System.out.println("This is Zombie :: " + name);
}
}

class TestMonster
{
public static void main( String[] args)
{
Monster[] m = new Monster[6];

m[0] = new Vampire("vam1");
m[1] = new Vampire("vam1");
m[2] = new Zombie("zom1");
m[3] = new Zombie("zom1");
m[4] = new Werewolf("wolf1");
m[5] = new Werewolf("wolf1");


for ( Monster m1 : m)
{
m1.display();
}

}
}


/*
D:\JAVA >javac TestMonster.java

D:\JAVA >java TestMonster
This is Vampire :: vam1
This is Vampire :: vam1
This is Zombie :: zom1
This is Zombie :: zom1
This is Werewolf :: wolf1
This is Werewolf :: wolf1
*/

Friday 21 September 2012

Java Practical - 11

0 comments


    Program Definition 11 ::
 
     The abstract Airplane class has three subclasses named B747, B757 and B767.  Each airplane type can transport a different number of passengers.  Each airplane object has a unique serial number.  Write an application that declares class hierarchy.  Instantiate several types of airplanes and display them.  Override the toString() method of Object to return a string with the type, serial number and capacity..





abstract class Airplane
{
protected int serialNumber;
protected int passengerCapacity;
protected String name;
Airplane(int sn, String nm ,int pc)
{
serialNumber = sn ;
name = nm ;
passengerCapacity = pc ;
}

public String toString()
{
return "serial number : " + serialNumber + "  \n passenger capacity : " +

passengerCapacity + " \n  name : " + name;

}
}


class B747 extends Airplane
{

B747(int sn , String nm, int pc)
{
super(sn , nm,  pc);
}
}

class B757 extends Airplane
{
B757(int sn , String nm, int pc)
{
super(sn , nm,  pc);
}
}

class B767 extends Airplane
{
B767(int sn , String nm , int pc)
{
super(sn ,nm,  pc);
}
}

class TestAirplane
{
public static void main(String args[])
{
System.out.println(" Program 11");

B747 b1 = new B747(001,"Boying", 450);
B757 b2 = new B757(101,"Airbus", 750);

System.out.println(b1.toString());
System.out.println(b2.toString());

}
}

/*
D:\JAVA >javac Prog11.java

D:\JAVA >java TestAirplane
 Program 11
serial number : 1
 passenger capacity : 450
  name : Boying

serial number : 101
 passenger capacity : 750
  name : Airbus
*/




Thursday 20 September 2012

Java Practical - 10

0 comments


    Program Definition 10 ::
 
     Write a program that illustrates method overriding.  Class Bond is extended by ConvertibleBond.  Each of these classes defines a display() method that outputs the string “Bond” or  “ConvertibleBond”, respectively.  Declare an array to hold six Bond objects.  Initialize the elements of the array with a mix of Bond and ConvertibleBond objects.  Execute a program loop to invoke the display() method of each object.




class Bond
{
public void display()
{
System.out.println("Bond");
}
}
class ConvertibleBond extends Bond
{
public void display()
{
System.out.println("ConvertibleBond");
}
}

class Prog10
{
public static void main(String args[])
{
Bond[] b = new Bond[ 6 ];

b[ 0 ] = new Bond();
b[ 1 ] = new ConvertibleBond();
b[ 2 ] = new Bond();
b[ 3 ] = new ConvertibleBond();
b[ 4 ] = new Bond();
b[ 5 ] = new ConvertibleBond();

for ( Bond bi : b )
{
bi.display();
}
}
}

/*

D:\JAVA>javac Prog10.java

D:\JAVA>java Prog10
Bond
ConvertibleBond
Bond
ConvertibleBond
Bond
ConvertibleBond
*/

Wednesday 19 September 2012

Java Practical - 9

0 comments


    Program Definition 9 ::

     Write an application that defines a Sphere class with three constructors.  The first form accepts no arguments.  It assumes the sphere is centered at the origin and has a radius of one unit.  The second form accepts one double value that represents the radius of the sphere.  It assumes the sphere is centered at the origin.  The third form accepts four double arguments.  These specify the coordinates of the center and the radius.  Provide two instance methods to this class.  The first named move(), which takes three double parameters that are new values for the co-ordinates of the center.  The second is named scale(), which takes one double parameter that is used to scale the radius.  Demonstrate these methods




class Sphere
{
double x;
double y;
double z;
double radius;

Sphere()
{
radius = 1 ;
}
Sphere( double r )
{
radius = r ;
}
Sphere( double x,double y,double z,double r)
{
this.z = z;
this.y = y;
this.x = x;
this.radius = r ;
}

public void move( double x,double y,double z )
{
this.z = z;
this.y = y;
this.x = x;
}
public void scale( double r )
{
radius *= r ;
}
public void display()
{
System.out.println("\n x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
System.out.println("radius = " + radius );

}
}



class Prog9
{
public static void main(String args[])
{
Sphere s = new Sphere();
s.display();

s = new Sphere(10);
s.display();

s = new Sphere(1,-2,4,9);
s.display();

s.move( 2 , -3 , 4);
s.display();

s.scale(2);
s.display();
}
}

/*

D:JAVA>javac Prog9.java

D:\JAVA>java Prog9

 x = 0.0
y = 0.0
z = 0.0
radius = 1.0

 x = 0.0
y = 0.0
z = 0.0
radius = 10.0

 x = 1.0
y = -2.0
z = 4.0
radius = 9.0

 x = 2.0
y = -3.0
z = 4.0
radius = 9.0

 x = 2.0
y = -3.0
z = 4.0
radius = 18.0
*/

Tuesday 18 September 2012

Java Practical - 8

0 comments


    Program Definition 8 ::

     Define an abstract class called Polygon. Provide a constructor which takes an array of Cartesian Point as parameter. Also provide method called perimeter, which calculates and returns the perimeter of the Polygon. Declare abstract method area for this class. Also define a method called move, which takes two parameters x and y to specify the destination for the first point of the Polygon, and overload to make it work for Cartesian Point as a parameter. Now update the classes Triangle and Rectangle in the exercise 8 above, to be a subclass of the Polygon class. Write appropriate class with main method to test the polymorphism in the area method.




class CartesianPoint
{
int x, y;
CartesianPoint(int a)
{
x = y = a;
}
CartesianPoint(int a , int b)
{
x = a; y = b;
}

protected int getX()
{
return x;
}
protected int getY()
{
return y;
}
protected void move(int a)
{
x = y = a;
}
protected void move(int a, int b)
{
x = a; y = b;
}

public void display()
{
System.out.println("("+ x +", " + y + ")");
}
}

abstract class Polygon
{
CartesianPoint cp[];
Polygon(CartesianPoint c[])
{
cp = new CartesianPoint[ c.length ];
for(int i=0;i<c.length;i++)
{
cp[i]=c[i];
}
}
double perimeter()
{
double peri = 0,a[];
a = new double[cp.length];

for(int i = 0 ; i < cp.length ; i++)
{
if((i+1) < cp.length)
a[i]=Math.sqrt(Math.pow((cp[i].getX()-cp[i+1].getX()),2)+Math.pow((cp[i].getY()-cp[i+1].getY()),2));
else
a[i]=Math.sqrt(Math.pow((cp[i].getX()-cp[0].getX()),2)+Math.pow((cp[i].getY()-cp[0].getY()),2));
peri=peri+a[i];
}
return peri;
}

abstract public double area();

public void move(int x, int y)
{
cp[ 0 ].move( x, y);
}
public void move(CartesianPoint c)
{
cp[ 0 ].x = c.getX();
cp[ 0 ].y = c.getY();
}

}

class Triangle extends Polygon
{
Triangle( CartesianPoint c[] )
{
super(c);
}
public double area()
{
return ((Math.abs(cp[0].getX()*(cp[1].getY()-cp[2].getY()) + cp[1].getX()*(cp[2].getY()-cp[0].getY()) + cp[2].getX()*(cp[0].getY()-cp[1].getY()))/2));
}
public void display()
{
for ( int i = 0 ; i < 3 ; i++ )
{
cp[i].display();
}
}
}
class Prog8
{
public static void main(String args[])
{
CartesianPoint c[] = new CartesianPoint[ 3 ];
c[0] = new CartesianPoint(5,5);
c[1] = new CartesianPoint(10,5);
c[2] = new CartesianPoint(10,10);

Triangle T1 = new Triangle(c);

T1.display();

System.out.println("Area :: " + T1.area() );

}
}

/*

D:\JAVA >javac Prog8.java

D:\JAVA >java Prog8
(5, 5)
(10, 5)
(10, 10)
Area :: 12.0
*/

Monday 17 September 2012

Java Practical - 7

0 comments


    Program Definition 7 ::
 
     Override the toString, equals and the hash Code methods of the classes Triangle defined in program 5 and 6,  in appropriate manner, and also redefine the display methods to use the toString method.




class CartesianPoint
{
int x, y;
CartesianPoint(int a)
{
x = y = a;
}
CartesianPoint(int a , int b)
{
x = a; y = b;
}
protected int getX()
{
return x;
}
protected int getY()
{
return y;
}
protected void move(int a)
{
x = y = a;
}
protected void move(int a, int b)
{
x = a; y = b;
}

public String toString()
{
return " point(x, y) = (" + x +", " + y + ")";
}
}

class Triangle
{
private CartesianPoint A, B, C;
Triangle ( CartesianPoint p1, CartesianPoint p2, CartesianPoint p3 )
{
A = p1 ; B = p2 ; C = p3;
}
public String toString()
{
return "A(x,y):B(x,y):C(x,y) ==>> " + "(" +A.getX() + ", " + A.getY() + "):" +"(" +B.getX() + ", " + B.getY() + "):" + "(" +C.getX() + ", " + C.getY() + ")";
}
}

class Prog7
{
public static void main(String args[])
{
CartesianPoint A = new CartesianPoint(15,15);
CartesianPoint B = new CartesianPoint(7,7);
CartesianPoint C = new CartesianPoint(25,25);

Triangle T1 = new Triangle(A,B,C);
System.out.println("A.toString() :: " + A.toString());
System.out.println("T1.toString()" + T1.toString());
System.out.println("\n\n");
System.out.println("Cartesian A HashCode is :: " + A.hashCode());
System.out.println("Cartesian B HashCode is :: " + B.hashCode());
System.out.println("Triangle T1 HashCode is :: " + B.hashCode());
System.out.println("\n\n");

if(A.equals(A))
{
System.out.println( "Same object" );
}
else
{
System.out.println( "different object" );
}

if(A.equals(B))
{
System.out.println( "Same object" );
}
else
{
System.out.println( "different object" );
}
}
}

/*
D:\JAVA >JAVAC Prog7.java

D:\JAVA >JAVA Prog7
A.toString() ::  point(x, y) = (15, 15)
T1.toString()A(x,y):B(x,y):C(x,y) ==>> (15, 15):(7, 7):(25, 25)

Cartesian A HashCode is :: 7254922
Cartesian B HashCode is :: 30223967
Triangle T1 HashCode is :: 30223967

Same object
different object

*/

Sunday 16 September 2012

Java Practical - 6

0 comments


    Program Definition 6 ::
 
     Define a class called Triangle, which has constructor with three parameters, which are of type Cartesian Point, defined in the exercise 6. Provide methods to find the area and the perimeter of the Triangle, a method display() to display the three Cartesian Points separated by ':' character, a method move() to move the first Cartesian Point to the specified x, y location, the move should take care of relatively moving the other points as well, a method called rotate, which takes two arguments, one is the Cartesian Point and other is the angle in clockwise direction. Overload the move method to work with Cartesian Point as a parameter. Now define a class called TestTriangle to test the various methods defined in the Triangle class.




class CartesianPoint
{
int x, y;
CartesianPoint(int a)
{
x = y = a;
}
CartesianPoint(int a , int b)
{
x = a; y = b;
}

protected int getX()
{
return x;
}
protected int getY()
{
return y;
}
protected void move(int a)
{
x = y = a;
}
protected void move(int a, int b)
{
x = a; y = b;
}

public void display()
{
System.out.println(" point(x, y) = (" + x +", " + y + ")");
}
}

class Triangle
{
private CartesianPoint A, B, C;
Triangle ( CartesianPoint p1, CartesianPoint p2, CartesianPoint p3 )
{
A = p1 ; B = p2 ; C = p3;
}
public void display()
{
System.out.println("A(x,y):B(x,y):C(x,y) ==>> " + "(" +A.getX() + ", " + A.getY() + "):" +"(" +B.getX() + ", " + B.getY() + "):" + "(" +C.getX() + ", " + C.getY() + ")");
}

public void move( int a, int b )
{
int d1 = a - A.getX();
int d2 = b - A.getY();
A = new CartesianPoint(a,b);
B = new CartesianPoint( B.getX() + d1 , B.getY() + d2);
C = new CartesianPoint( C.getX() + d1 , C.getY() + d2);
}
public double getArea()
{
return ((Math.abs(A.getX()*(B.getY()-C.getY()) + B.getX()*(C.getY()-A.getY()) + C.getX()*(A.getY()-B.getY()))/2));
}
public double perimeter()
{
double d1,d2,d3;

d1 = Math.sqrt((B.getX()-A.getX())*(B.getX()-A.getX())+(B.getY()-A.getY())*(B.getY()-A.getY()));
d2 = Math.sqrt((C.getX()-B.getX())*(C.getX()-B.getX())+(C.getY()-B.getY())*(C.getY()-B.getY()));
d3 = Math.sqrt((A.getX()-C.getX())*(A.getX()-C.getX())+(A.getY()-C.getY())*(A.getY()-C.getY()));

return (d1+d2+d3);
}

public void rotate(double angle, CartesianPoint D)
{
int x, y;

x = (int)(D.getX()*Math.cos(angle) - D.getY()*Math.sin(angle));
y = (int)(D.getX()*Math.sin(angle) + D.getY()*Math.cos(angle));

A = new CartesianPoint( x , y );
}
}

class TestTriangle
{
public static void main(String args[])
{
CartesianPoint A = new CartesianPoint(15,15);
CartesianPoint B = new CartesianPoint(23,30);
CartesianPoint C = new CartesianPoint(50,25);

Triangle T1 = new Triangle(A,B,C);

T1.display();
T1.move(16,16); // moves only one point

System.out.println("\n After move(16,16) ::>>\n");

T1.display();
System.out.println("Area of Triangle is :: " + T1.getArea());
System.out.println("Perimeter of Triangle is :: " + T1.perimeter());

T1.rotate(90, A);

System.out.println("\n\nAfter 30 degree Rotation at (16,16), New Cordinates are ::\n");
T1.display();
}
}

/*
D:\JAVA >JAVAC Prog6.java

D:\JAVA >JAVa TestTriangle
A(x,y):B(x,y):C(x,y) ==>> (15, 15):(23, 30):(50, 25)

 After move(16,16) ::>>

A(x,y):B(x,y):C(x,y) ==>> (16, 16):(24, 31):(51, 26)
Area of Triangle is :: 222.0
Perimeter of Triangle is :: 80.85960988189456


After 30 degree Rotation at (16,16), New Cordinates are ::

A(x,y):B(x,y):C(x,y) ==>> (-20, 6):(24, 31):(51, 26)

*/

Java Practical - 16

0 comments


    Program Definition 16 ::
 
     Interface Material defines a set of String constants for various materials.  Abstract class MaterialObject has one instance variable named material of type String.  These records the material used to construct the object.  Classes Ball, Coin and Ring extend MaterialObject .  The constructor initializes the material variable.  Class MaterialObjects instantiates these three classes.  A different material is passed to each constructor.  Display material of each object.




interface Material
{
String gold = "Gold";
String silver = "Silver";
String rubber = "Rubber";
}

abstract class MaterialObject implements Material
{
String material;
MaterialObject( String m )
{
material = m ;
}
abstract public void display();
}
class Ball extends MaterialObject
{
Ball( String m )
{
super( m );
}
public void display()
{
System.out.println( "Ball Material :: " + material );
}
}

class Coin extends MaterialObject
{
Coin( String m )
{
super(m);
}
public void display()
{
System.out.println( "Coin Material :: " + material );
}
}

class Ring extends MaterialObject
{
Ring( String m )
{
super( m );
}
public void display()
{
System.out.println( "Ring Material :: " + material );
}
}

class TestMaterial
{
public static void main( String args[])
{
MaterialObject[] m = new MaterialObject[3];

m[ 0 ] = new Ring(Material.gold);
m[ 1 ] = new Ball(Material.rubber);
m[ 2 ] = new Coin(Material.silver);

for ( MaterialObject m1 : m )
{
m1.display();
}
}
}





/*
D:\MCA\JAVA>javac TestMaterial.java

D:\MCA\JAVA>java TestMaterial
Ring Material :: Gold
Ball Material :: Rubber
Coin Material :: Silver
*/

Saturday 15 September 2012

Java Practical - 5

0 comments

    Program Definition 5 ::

     Define a class called CartesianPoint, which has two instance variables, x and y. Provide the methods getX() and getY() to return the values of the x and y values respectively, a method called move() which would take two integers as parameters and change the values of x and y respectively, a method called display() which would display the current values of x and y. Now overload the method move() to work with single parameter, which would set both x and y to the same values, Provide constructors with two parameters and overload to work with one parameter as well. Now define a class called TestCartesianPoint, with the main method to test the various methods in the CartesianPoint class.




class CartesianPoint
{
int x, y;
CartesianPoint(int a)
{
x = y = a;
}
CartesianPoint(int a , int b)
{
x = a; y = b;
}

protected int getX()
{
return x;
}
protected int getY()
{
return y;
}
protected void move(int a)
{
x = y = a;
}
protected void move(int a, int b)
{
x = a; y = b;
}

public void display()
{
System.out.println(" point(x, y) = (" + x +", " + y + ")");
}
}

class TestCartesian
{
public static void main(String args[])
{
CartesianPoint cp1 = new CartesianPoint(5,10);
CartesianPoint cp2 = new CartesianPoint(5);

System.out.println(" point1 = >> " );
cp1.display();
System.out.println(" point2 = >> " );
cp2.display();

cp1.move(5);
cp2.move(6,10);

System.out.print(" After cp1.move(5); point 1 =>>  ");
cp1.display();
System.out.print(" After cp2.move(6,10); point 2 =>>  ");
cp2.display();
}
}

/*
D:\JAVA >javac Prog5.java

D:\JAVA >java TestCartesian
 point1 = >>
 point(x, y) = (5, 10)
 point2 = >>
 point(x, y) = (5, 5)
 After cp1.move(5); point 1 =>>   point(x, y) = (5, 5)
 After cp2.move(6,10); point 2 =>>   point(x, y) = (6, 10)
*/

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT