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

1 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

*/

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT