Welcome to Java Examples

Take a cup of tea and Let's Start programming

Declare a class called coordinate to represent 3 dimensional Cartesian coordinates( x, y and z). Define following methods:
- constructor
- display, to print values of members
- add_coordinates, to add three such coordinate objects to produce a resultant coordinate object. Generate and handle exception if x, y and z coordinates of the result are zero.
- main, to show use of above methods.



import java.lang.Exception;

class Cartesian extends Exception
{
double result;
double x,y,z;
Cartesian()
{

}
Cartesian(double x,double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}

void display()
{
System.out.println("value of x = "+x);
System.out.println("value of y = "+y);
System.out.println("value of z = "+z);
}

void add_coordinates() throws Cartesian
{
result=x+y+z;
System.out.println("the Addition of three coordinates is"+result);
if(result == 0)
{
throw new Cartesian();
}



}

public static void main(String [] args)
{
Cartesian obj=new Cartesian(5,5,-10);
obj.display();


try
{
obj.add_coordinates();
}
catch(Cartesian j)
{
System.out.println("Exception Generated \n try different coordiante values");
}

}

}

output



0 comments :

Post a Comment