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