【发布时间】:2017-08-22 17:58:43
【问题描述】:
我正在编写一个创建访问器和修改器的类。但是,当我运行我的程序以使用该类时,没有返回任何内容,或者我的程序没有输出任何内容。
在测试程序(如下)中,我创建类变量并将它们发送到我输入的类,然后返回变量。
我的理想输出是函数调用返回变量。 例如(getLatitude 将输出纬度变量)
这是我的课程代码:
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)));
}
}
这是我的测试程序:
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);
}
}
【问题讨论】:
-
你期待什么输出?
-
所有的return语句?
-
但是你必须为此声明变量,比如
String positionStr = position.toString(); -
return语句不会自行将内容打印到 StdOut。 -
返回调用不打印任何内容,它们被用作函数的“结果”。
toString()方法也不打印任何内容,它只是返回一个字符串。如果你想打印一些东西到 Eclipse 的控制台,你必须使用System.out.println()。你可以做类似System.out.println(position.toString())的事情。如果你想让getLatitude()和其他方法也打印它们的返回值,你可以在return 语句之前调用System.out.println(return 语句之后的所有内容都不会执行)。
标签: java eclipse class testing return