Need help on java


P

paul.paik

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

quote:

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.



quote:

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

quote:

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



quote:


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



quote:


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

public interface Locatable
{
Location location();
}



quote:


/**
* 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
 
Ad

Advertisements

A

Andrew Thompson

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

D:\projects\junk\numbered\237Main\Main.java:1: class, interface, or enum
expected
Public interface Locatable
^

You should be getting the above qoted compilation
error on the very first line of code posted.

(big snip)
The School class has an error of a wrong constructor for location

Huhh? I have never seen the error 'wrong constructor
for location' which makes me suspect that you are
paraphrasing the error the compiler reports.

Please don't do that. Instead, it is always best to
copy/paste compilation and runtime errors, as I did
above.
<http://www.physci.org/codes/javafaq.html#exact>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top