简体   繁体   中英

Adding one class' info to an array list in another class

I'm making a project in Java that's a basic GPS process. It has a point class that is the current location, shown below;

public class Point {
// Constants useful for bounds checking, etc

private static final double MIN_LONGITUDE = -180.0;
private static final double MAX_LONGITUDE = 180.0;
private static final double MIN_LATITUDE = -90.0;
private static final double MAX_LATITUDE = 90.0;
private static final double MEAN_EARTH_RADIUS = 6.371009e+6;

// TODO: Define fields for time, longitude, latitude and elevation

public static ZonedDateTime time = ZonedDateTime.now();
public static double longitude;
public static double latitude;
public static double elevation;

// TODO: Define a constructor

public Point(ZonedDateTime timestamp, double longitude, double latitude, double elevation) {


    this.longitude = longitude;
    this.latitude = latitude;
    this.elevation = elevation;

}

Point point1 = new Point ( time, -1.54853, 53.80462, 72.5);

// TODO: Define getters for the fields


public static ZonedDateTime getTime() {
    return time;
}

public static double getLongitude() {
    return longitude;
}

public static double getLatitude() {
    return latitude;
}

public static double getElevation() {
    return elevation;
}

// TODO: Define a toString() method that meets requirements
public String toString() {

    return "( " + longitude + ", " + latitude + " ),  " + elevation;
}

Then another class, track, that is supposed to be a collection of points showing the journey. However, I am struggling with how to go about this, I have created a linked list but I'm unsure where to go from here. I've been messing around with extending the class but having identifier expected errors when using Point in the add method.

import java.time.ZonedDateTime; import java.util.ArrayList;

class Track{

//public LinkedList<Point> Track = new LinkedList<>();


public Track() {

    ArrayList<Point> Track = new ArrayList<>();

}

public static void add(Object Point){

    Track.add((Point));
}

} But I am having trouble referencing the new array created by track() to add the next point.

Ok so you are trying to do a number of different things here - the way I am going to place it is not the only way, but something to consider...I have put everything into a package so you will need to restructure in your eclipse setup accordingly...

Made some modification here because you are referring vars and methods in a static way, not that you can't do that, but for the purpose of what you are trying to achieve this is probably a better learning experience. I also removed code that was dead...The timestamp portion may not be what you are looking for - but it seemed like a better fit here.

package com.gps;
import java.time.ZonedDateTime;

public class Point {

    // TODO: Define fields for time, longitude, latitude and elevation

    public ZonedDateTime time;
    public double longitude;
    public double latitude;
    public double elevation;

    public Point(ZonedDateTime timeStamp, double longitude, double latitude, double elevation) {
        this.time = timeStamp;
        this.longitude = longitude;
        this.latitude = latitude;
        this.elevation = elevation;

    }

    // TODO: Define getters for the fields

    public ZonedDateTime getTime() {
        return time;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getElevation() {
        return elevation;
    }

    @Override
    public String toString() {
        return "Point [time=" + time + ", longitude=" + longitude + ", latitude=" + latitude + ", elevation="
                + elevation + "]";
    }



}

Here I added a toString() so you can see it when you print from the Engine class, I have renamed your variables and methods as I mentioned in my first post so you can keep track of whats what - it will matter make sure you do it...

package com.gps;

import java.util.ArrayList;

public class Track {

     ArrayList<Point> track = new ArrayList<>();


    public void add(Point point){

        track.add(point);
    }


    @Override
    public String toString() {
        return "Track [track=" + track + "]";
    }


}

Since the only data I have to work with is the one that you supplied that is what I am returning - the date might not be right, but it worked well from Mykong's examples :)

package com.gps;

import java.time.ZonedDateTime;

public class Engine {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Track track = new Track();
        Point point = new Point (ZonedDateTime.now(), -1.54853, 53.80462, 72.5);
        System.out.println(point.getTime());
        track.add(point);

        System.out.println(track);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM