The assignment is to create a code that demonstrates a copy(), equals(), and toS
ID: 3631527 • Letter: T
Question
The assignment is to create a code that demonstrates a copy(), equals(), and toString() in the main method.
Here's what is says specifically:
1. A Point2d has two instance variables- x and y - both doubles
2. Point2d should have a two-argument contructor
3. Point2d should have an equals method.
4. Point2d should have a copy method
5. Point2d should have a copy constructor
6. Point2d should have a toString method.
7. Write a main method that test your code.
I have created all the methods and attempted to show the use of the methods in the main method, but I get the two following errors:
Hw12.java:10: package p1 does not exist
Point2d p2 = new p1.copy();
^
Hw12.java:14: non-static method toString() cannot be referenced from a static context
System.out.println(toString());
^
2 errors
Can someone please take a look at my code and tell me what I have done wrong?
//------------------------------code-----------------------------
///////////////////////////////////////////////
class Hw12
{
//----------------------------------------------------------------
public static void main(String [] args)
{
Point2d p1 = new Point2d(5,6);
Point2d p2 = new p1.copy();
Point2d p3 = new Point2d(p1);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println(toString());
}
//----------------------------------------------------------------
}// end Class Hw12
//////////////////////////////////////////////
class Point2d
{
private double x;
private double y;
//---------------------------------------------------------------
public Point2d(double x, double y)
{
this.x = x;
this.y = y;
}
//---------------------------------------------------------------
public Point2d( Point2d point)
{
this.x = point.x;
this.y = point.y;
}
//---------------------------------------------------------------
public Point2d copy()
{
return new Point2d (this.x, this.y);
}
//---------------------------------------------------------------
public boolean equals (Point2d that)
{
return this.x == that.x && this.x == that.y;
//---------------------------------------------------------------
public String toString()
{
String str = "X: " + x +
" Y: " + y;
}
//---------------------------------------------------------------
}//end class Point2d
//////////////////////////////////////////////
Explanation / Answer
Thanks for your comment, I got that and came here. You can do that again in the future to get my attention also. So okay let me start.
1. To use copy method, just do "Point2D p2= p1.copy();"
2. You cannot print out the method toString() like that. To envoke toString() method, print out an object name. I'll copy my code so you may use as reference.
// Written by Cal Poly Pomona student
//
// Solves CS 141, Fall 2011, Homework 12.
//
import java.lang.Math;
/////////////////////////////////////////////////////////////////////////
public class Hw12
{
//-----------------------------------------------------------------------
public static void main(String[] args)
{
Point2D p1= new Point2D(5,10);
System.out.println("p1 "+p1);
Point2D p2= p1.copy();
System.out.println("p2 "+p2);
Point2D p3=new Point2D(p2);
System.out.println("p3 "+p3);
Point2D p4 = new Point2D(9,18);
System.out.println("p4 "+p4);
System.out.println();
System.out.println("p1==p2 "+(p1==p2));
System.out.println("p1.equals(p2) "+p1.equals(p2));
System.out.println("p2.equals(p3) "+p2.equals(p3));
System.out.println("p3.equals(p4) "+p3.equals(p4));
}
//-----------------------------------------------------------------------
}//End of class Hw12
/////////////////////////////////////////////////////////////////////////
class Point2D
{
private double x,y;
//-----------------------------------------------------------------------
public Point2D(double x, double y)
{
this.x=x;
this.y=y;
}
//-----------------------------------------------------------------------
public Point2D(Point2D pt)
{
this.x=pt.x;
this.y=pt.y;
}
//-----------------------------------------------------------------------
public boolean equals(Point2D pt)
{
return (Math.abs(this.x-pt.x)<.00001&&Math.abs(this.y-pt.y)<.00001);
}
//-----------------------------------------------------------------------
public Point2D copy()
{
return new Point2D(this.x,this.y);
}
//-----------------------------------------------------------------------
public String toString()
{
return("has coordinates: X="+x+" Y="+y);
}
//-----------------------------------------------------------------------
}//End of class Point2D
/////////////////////////////////////////////////////////////////////////
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.