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
*/
1 comments :
Free Baccarat for Beginners | FEBCASINO
› 2021/08/24 › Free 더킹카지노 › 2021/08/24 › Free Feb 24, 2021 — Feb 24, 2021 샌즈 Free Baccarat is the popular 강원 랜드 바카라 game of the day in which players take a break from If you're 슬롯 a beginner, you'll want to practice it 클레오 카지노 as well.