mardi 22 août 2017

Java (Eclipse) Class return statements not working

I am writing a class that among other things creates accessors and mutators. However when I run my program to make use of the class nothing is being returned, or well my program does not output anything.

In test program (below) I create class variables and send them to the class where I input and then returned the variable.

Here is my class code:

package practiceProblems;

import stdlib.StdOut;

public class GPSPosition implements Comparable <GPSPosition>{
   // Private global variables
   private Double Latitude;
   private Double Longitude;
   private Double Altitude;

// Constructor 1
public <Item extends Comparable<? super Item>> GPSPosition() { Latitude = 0.0; Longitude = 0.0; }

// Constructor 2
public <Item extends Comparable<? super Item>> GPSPosition(Double lat, Double lon, Double alt) {
    this.Altitude = alt;
    this.Latitude = lat;
    this.Longitude = lon;

    if (lat < -90 || lat > 90) {
        throw new IllegalArgumentException("NOPE!");
    }
    if (lon < -180 || lon > 180) {
        throw new IllegalArgumentException("NOPE!!");
    }
    if (alt < 0) {
        throw new IllegalArgumentException("NOPE!!!");
    }

}

// Latitude Accessor
public Double getLatitude() { return this.Latitude; }
// Longitude Accessor
public Double getLongitute() { return this.Longitude; }
// Altitude Accessor
public Double getAltutude() { return this.Altitude; }

// Mutator for Latitude
public void setLatitude(Double Latitude) { this.Latitude = Latitude;    }
// Mutator for Longitude
public void setLongitude(Double Longitude) { this.Longitude = Longitude;    }
// Mutator for Altitude
public void setAltitude(Double Altitude) { this.Altitude = Altitude;    }

// Compare to method
public int compareTo(GPSPosition that){

    if (this.Latitude.compareTo(that.Latitude) > 0) return 1;
    if (this.Latitude.compareTo(that.Latitude) < 0) return -1;
    return that.Latitude.compareTo(this.Latitude);

}
// toString method
public String toString() {
    String latOutput = "";
    String longOutput = "";

    if (this.Latitude < 0) { latOutput = "S"; }
    else {latOutput = "N";}

    if (this.Longitude < 0) { latOutput = "W"; }
    else {latOutput = "E";}
    return (this.Latitude + latOutput + " " + this.Longitude + longOutput + " " + this.Altitude + "m");
}

public double distance(GPSPosition that) {

    return (Math.sqrt((this.Latitude - this.Longitude) + (that.Longitude - that.Latitude)));

}   

}

Here is my test program:

package practiceProblems;
import practiceProblems.GPSPosition;

public class TestGPS {

public static void main(String[] args) {
    // Positions
    GPSPosition position = new GPSPosition(-37.2, 87.2, 200.0); 
    GPSPosition position2 = new GPSPosition(37.2, 7.2, 100.0);  

    // Set
    position.setAltitude(200.0); position.setLatitude(-37.2);
    position.setLongitude(87.2);

    // Get
    position.getAltutude(); position.getLatitude(); position.getLongitute();

    // Compare to
    position.compareTo(position2);

    // To String call
    position.toString();

    // Distance between two positions
    position.distance(position2);   

}

}

Aucun commentaire:

Enregistrer un commentaire