Friday 5 October 2012

Java Practical - 25





Program Definition 25 ::



Make the class CartesianPoint, belong to a package called edu. gtu. geometry, the classes Polygon, Triangle and Rectangle belong to the package edu. gtu. geometry. shapes and the classes TestTriangle belong to the package edu. gtu.test. Use appropriate access specifiers for the classes and the members of the classes defined in the earlier exercises. Now onwards all the classes must be defined in a package.





// save this code in edu/gtu/geometry folder
package edu.gtu.geometry;
//program 25

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

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

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



// save this code in edu/gtu/geometry/shapes folder
package edu.gtu.geometry.shapes;
import edu.gtu.geometry.*;
public class Triangle
{
CartesianPoint cp[];
public Triangle(CartesianPoint c[])
{
cp = new CartesianPoint[ c.length ];
for(int i=0;i<c.length;i++)
{
cp[i]=c[i];
}
}

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();
}
}
}


// save this code in edu/gtu/test folder

package edu.gtu.test;
import edu.gtu.geometry.*;
import edu.gtu.geometry.shapes.*;

class TestTriangle
{
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 edu/gtu/test/*.java

D:\JAVA >java edu.gtu.test.TestTriangle
(5, 5)
(10, 5)
(10, 10)
Area :: 12.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