• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help on JAVA

Xylitol

Diamond Member
Other parts of the project, I've already done; however, I'm stuck on 2 parts (Making the interface and creating a Location

Picture of the structure
http://img47.imageshack.us/img47/946/untitledgi7.jpg

Instructions:
Locatable Objects

Create a class that represents a Locatable object. Use your imagination ? it can be anything that is Locatable, which is to say it must implement the Locatable interface. As an added requirement, your class must also implement Comparable and provide a toString method.

Your compareTo method should order instances of your class according to their location.

Show me your Locatable and Comparable class before proceeding.

The Locatable Interface

Any object that ?Has-A? Location should be able to report its Location on demand, and all objects that have a Location should report their location in the same way. We enforce this rule by introducing an interface that specifies how an object should report its location, and by insisting that all objects that have a Location implement this interface. We call objects with a Location ?Locatable?.

The Locatable interface specifies only one method: location. The method returns a Location object which encapsulates the Locatable object?s current location. The interface looks like this:

Public interface Locatable
{
Location location();
}

Code the Locatable interface and add it to your project.

PROGRAM

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

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
{
int difference;

if (((Location)other).row()==qrow)
{
int diff=qcol-(((Location)other).col());
return diff;
}
else
{
difference=qrow-(((Location)other).row());
return difference;
}
}
}
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 x = qrow + "this is the row";
String y = qcol + "this is the col";
return x + y;
// String remember=qrow + " ," + qcol;
// return remember;
}
}

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

public interface Locatable
{
Location location();
}

/**
* Write a description of class School here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class School extends Location
{
private int qasdf;
private int qfdsa;
public School(int asdf, int fdsa)
{
qasdf=asdf;
qfdsa=fdsa;
}
}

The School class has an error of a wrong constructor for location
What's the issue?

Thanks for any help
 
Back
Top