| |||||||||||
Urgent problem with Inheritance and java arrays. Posted by Yvessa (Yvessa), 13 May 2005 Hi, I need to create a program which can store an array of information relating to the latitudes and longitudes of a set of 12 cities and airports. I've done this, but now I need to access that first array and use a method that is defined in another class to work out the distance between the locations and then display in a table form (so essentially a two dimensional array).(edited: replaced my tutors code with my own, due to uncertain copyright) Here is the code for the files: /** * The <code>city</code> class represents a city. Information about the * city's population is stored. The <code>getPopulation</code> method * returns the population. * * It is a subclass of Location. * * @author Anulipi Dasgupta * @version 1.0 * */ public class City extends Location { //class variables private int population; /** * * Constructor for City Class * Calls parent constructor. * @param n city name * @param x x coordinate - miles * @param y y coordinate - miles * @param pop population - peoples * * */ public City(String n, double x, double y, int pop) { super(n,x,y); population=pop; } /** * * City's population * * @return population (people) * */ public int getPopulation(){ return population; } } /** * The <code>location</code> class represents a location. Information about the * locations name and coordinates are stored. The <code>getName</code> method * returns the name of the location while the <code>getDistance</code> method * returns the distance between two locations. * * @author Anulipi Dasgupta * @version 1.0 * */ public class Location { //class variables private double xCoord; private double yCoord; private double distance; private String name; /** * * Constructor for Location Class * @param n location name * @param x x coordinate - miles/70 * @param y y coordinate - miles/70 * * */ public Location(String n, double x, double y) { xCoord = x; yCoord = y; name=n; } /** * * Location coordinates * * @return xCoord (miles/70) * */ public double getXCoord(){ return xCoord; } /** * * Location name * * @return name * */ public String getName(){ return name; } /** * * Location coodinates * * @return yCoord (miles/70) * */ public double getYCoord(){ return yCoord; } /** * * Mutator for Location class * @param a new location * * */ public void calcDistance(Location a){ distance = 70*(Math.sqrt(((xCoord-a.getXCoord())*(xCoord-a.getXCoord())) + ((yCoord-a.getYCoord())*(yCoord-a.getYCoord())))); } /** * * Distance between two locations * * @return distance (miles) * */ public double getDistance(){ return distance; } } /** * The <code>airport</code> class represents a airport. Information about the * airport's code is stored. The <code>getCode</code> method * returns the code. * It is a subclass of Location * * @author Anulipi Dasgupta * @version 1.0 * */ public class Airport extends Location { //class variables private String code; /** * * Constructor for Airport Class * Calls parent constructor. * @param n airport name * @param x x coordinate - miles * @param y y coordinate - miles * @param c code * * */ public Airport(String n, double x, double y, String c) { super(n,x,y); code=c; } /** * * Airport code * * @return code * */ public String getCode(){ return code; } } And finally the problem class: //Create instances of all the cities and airports. In an array //calculate the distance between all the cities. public class Route{ public static void main(String[] args) { //create locations City sheffield = new City("Sheffield", 53.38, 1.5, 513000); City cambridge = new City("Cambridge", 52.22, -0.13, 109000); City edinburgh = new City("edinburgh", 55.95, 3.22, 449000); City inverness = new City("Inverness", 57.45, 4.25, 51000); City ipswitch = new City("Ipswitch", 52.01, -1.17, 117000); City leeds = new City("Leeds", 53.83, 1.58, 715000); Airport manchester = new Airport("Manchester", 53.35, 2.27, "MAN"); Airport glasgow = new Airport("Glasgow", 55.87, 4.43, "GLA"); Airport heathrow = new Airport("Heathrow", 51.47, 0.46, "LHR"); Airport gatwick = new Airport("Gatwick", 51.14, 0.19, "LGW"); Airport belfast = new Airport("Belfast", 54.65, 6.88, "BFS"); Airport cardiff = new Airport("Cardiff", 51.39, 3.34, "CWL"); Location[] locs = new Location[12]; locs[0] = sheffield; locs[1] = cambridge; locs[2] = edinburgh; locs[3] = inverness; locs[4] = ipswitch; locs[5] = leeds; locs[6] = manchester; locs[7] = glasgow; locs[8] = heathrow; locs[9] = gatwick; locs[10]= belfast; locs[11]= cardiff; double dis[][] = new double [12][11]; Location loc; double dist; for(int i=0;i<12; i++){ for(int j=i+1; j<12; j++) { loc=locs; dist=loc[i].getDistance(loc[j]); dis[i][j]=dist; // System.out.println("The distance between" + loc[i].getName() + "and" +loc[j].getName()+ "is" + dis[i]); }//for }//for } } [i]Edit by moderator - I have removed the email address of the author of the sample classes from this post as a temporary measure Posted by admin (Graham Ellis), 13 May 2005 OK ... I understand the application you're attempting - do you have a specific question or are you looking for suggestions or a complete answer (if we do the latter for you, you won't learn very much and unless you're really Prince Harry it might be frowned on by your college ).By the way - see our tips about posting and answers - I try to make comment on everything within 24 hours but don't "DO" urgent in this free service! Posted by Yvessa (Yvessa), 13 May 2005 Hi, I really just want to know how to overcome the problem with the error; I would prefer to find the final solution myself, but I've been hitting my head on a proverbial brick wall for the past two days. The error I get is for this linedist=loc[i].getDistance(loc[j]); And it says Array expected, Location found or similar. Now as far as I understand it this is because getDistance is defined in Location.class and not Route.class and so the for loop simply cannot access it. I can't seem to find any other way of accessing it (i've been trawling forums for a day) Thanks in advance, and thanks for the quick response. Yve Posted by Custard (Custard), 13 May 2005 Hi,In this bit of code: Code:
You probably don't want the 'Location loc' and 'loc=locs' lines. This is assigning an array to a single value variable (which I would have thought was enough to grok the compiler). And change the 'loc' in the System.out line accordingly. Was there a reason you wanted to copy the array? B Posted by Yvessa (Yvessa), 13 May 2005 Thanks it now compiles! But I've run into another problem; I can't figure out how to test the output. Any ideas? (I'm just looking for suggestions, I'll figure the rest out). (i hope)Posted by Custard (Custard), 13 May 2005 Does your System.out.println not do the trick?It looks like if it were uncommented it would produce a list of airports and distances. Try inserting more 'debug' lines like these anywhere you feel there may be a problem. For larger apps, Log4j is a great debug/logging system. For your example, debug lines should be enough. HTH B Posted by Custard (Custard), 13 May 2005 Also, as an OO hint or at least suggestion..Maybe Route should have 2 Airports as arguments to it's constructor, and the getDistance method from Location be moved to Route. Then you can say something like (pseudo code) Code:
Which may make your arrays neater. HTH B This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |