public static String toString(Point [] data ) -- returns the data in String form
ID: 3536384 • Letter: P
Question
public static String toString(Point[] data) -- returns the data in String form with the following format: {(x1, y1), (x2, y2), (x3, y3),...} (in other words, the Points in typically point notation, separated by commas and the entire set surrounded by curly braces.) IMPORTANT: You may not use the 'toString()' method in class Arrays; you must solve the problem by using a loop to access the array elements directly. This format choice is deliberate because it makes you write a carefully designed loop with initialization and finalization. You may use the toString() method of the Point class.
Explanation / Answer
public class Point
{
int x;
int y;
public Point()
{
x=0;
y=0;
}
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return x+","+y;
}
public static String toString(Point[] data)
{
String s="{";
for(int i=0;i<data.length;i++)
s=s+"("+data[i].toString()+")";
s=s+"}";
return s;
}
public static void main(String argv[])
{
Point[] data=new Point[5];
data[0]=new Point(2,3);
data[1]=new Point(4,7);
data[2]=new Point(1,9);
data[3]=new Point(8,8);
data[4]=new Point(4,1);
System.out.println(Point.toString(data));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.