Need help with my java work

Xylitol

Diamond Member
Aug 28, 2005
6,617
0
76
This is a lab that I have to complete. Right now, I have to do this and a different lab which I am working on right now. I was wondering if AT could help me with this one so that it makes everything go faster

I will edit this every so often for this lab when I come along a new problem

Current problem:
the compareTo method
I dont understand what it's supposed to return/how to return something that has 2 things in it (X,Y - unless I'm misunderstanding)

Instructions:
Imagine, instead of latitude and longitude, we divide the world up into a rectangular grid similar to the world used by Karel the Robot. Then, a location would be uniquely specified by stating the row number and the column number of the grid. Cities are often organized with streets running east and west, and avenues running north and south so that a position inside the city can be specified by the street, avenue pair.

Let?s devise a Java class that will encapsulate a location on this imaginary grid.

Our class Location will encapsulate the row and column number location for any object that ?has-a? Location. When a location object is constructed, the row and column coordinates are specified in the constructor. Accessors are provided for getting the row and column information from a Location object. Locations can be ordered, so the Location class implements the Comparable interface. An equals method is provided as well as a toString method. An outline of the class Locatable looks like this:
Main:

/**
* Write a description of class Main here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Main
{ public static void main ()
{
Location testing = new Location (3,6);
Location atesting = new Location (4,7);
System.out.println ("Rows: " + testing.row());
System.out.println ("Columns: " + testing.col());
System.out.println ("Index of Compare to A" + testing.compareTo("a"));
if (testing.equals(atesting)==true)
{
System.out.println ("They are equal");
}
else
{
System.out.println ("They are not equal");
}
System.out.println (testing.toString());
}
}

Location:
public class Location implements Comparable
{
private int qrow;
private int qcol;
// constructors
public Location(int row, int col)
{
qrow=row;
qcol=col;
}
// accessors
public int row()
{
return qrow;
// return the row value for this Location
}
public int col()
{
return qcol;
// return the col value for this Location
}



// compareTo and equals
public int compareTo(Object other)
{
/*
if ((Location)(other).row()==qrow && (Location)(other).col()==qcol)
{
return 0;
}
else if ((Location)other.row()!=qrow || (Location)other.col()!=qcol)
{
return -1;
}
*/
}
public boolean equals(Object other)
{
if (qrow==(Location)(other).row() && qcol==(Location)(other).col())
{
return true;
}
else
{
return false;
}
}
// toString method returns a String representation
// of this Location
public String toString()
{
String remember="[" + qrow + " ," + qcol + "]";
return remember;
}
}

Locatable (Interface):

/**
* Write a description of interface Locatable here.
*
* @author (your name)
* @version (a version number or a date)
*/

public interface Locatable
{
Location location();
}
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
You're asking about the compareTo() method? It compares the calling object to the one specified in the argument. By compare I mean it checks if both objects have the same row coordinate and the same column coordinate.

If both row and column coordinates for the two objects are the same, they are equal and it returns 0. If either the row and/or column coordinates are different between the two, they are not equal and return -1. Sort of strange, why not use true/false, but anyways that's what it does.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Originally posted by: clamum
You're asking about the compareTo() method? It compares the calling object to the one specified in the argument. By compare I mean it checks if both objects have the same row coordinate and the same column coordinate.

If both row and column coordinates for the two objects are the same, they are equal and it returns 0. If either the row and/or column coordinates are different between the two, they are not equal and return -1. Sort of strange, why not use true/false, but anyways that's what it does.

I think the reason (or one implication) compareTo() has an int return value is to support a sorted collection. If you implement the compareTo( Object ) method correctly, your type can be part of a Collection which will automatically sort itself.

Edit: For this particular assignment, you need to decide whether you want the ordering as row major or column major.