Wednesday, 19 September 2012

Java Practical - 9



    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
*/


Kindly Bookmark and Share it:

0 comments :

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT