您好我有一个java作业不太会做,能麻烦帮我解答一下么,时间比较急,下午两点前due
这个是作业要求http://pan.baidu.com/s/1c0zVOBm
如果大神可以帮忙,万分感谢 展开
你们老师让你完成他给的代码,做成这样~我试试
需要修改两个文件的代码:第一个:Cache.java
package cs1410;
public class Cache
{
private String code;
private String title;
private String owner;
private double difficulty;
private double terrain;
private String latitude;
private String longitude;
public Cache(String attributes)
{
String[] attrs = attributes.split("\t");
if (attrs.length != 7)
{
throw new IllegalArgumentException();
}
this.code = attrs[0];
this.title = attrs[1];
this.owner = attrs[2];
try
{
this.difficulty = Double.parseDouble(attrs[3]);
this.terrain = Double.parseDouble(attrs[4]);
}
catch (NumberFormatException e)
{
throw new IllegalArgumentException();
}
this.latitude = attrs[5];
this.longitude = attrs[6];
}
public String toString()
{
return getTitle() + " by " + getOwner();
}
public String getOwner()
{
return this.owner;
}
public String getTitle()
{
return this.title;
}
public double getDifficulty()
{
return this.difficulty;
}
public double getTerrain()
{
return this.terrain;
}
public String getGcCode()
{
return this.code;
}
public String getLatitude()
{
return this.latitude;
}
public String getLongitude()
{
return this.longitude;
}
}
第二个CacheList.java:
package cs1410;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Scanner;
public class CacheList
{
private ArrayList<Cache> allCaches;
private double minTerr;
private double maxTerr;
private double minDiff;
private double maxDiff;
private String ownerConstraint;
private String titleConstraint;
public CacheList(Scanner caches)
throws IOException
{
this.allCaches = new ArrayList();
int line = 1;
try
{
while (caches.hasNextLine())
{
this.allCaches.add(new Cache(caches.nextLine()));
line++;
}
}
catch (IllegalArgumentException e)
{
throw new IllegalArgumentException(e);
}
Collections.sort(this.allCaches, new CacheComparator());
this.minTerr = 1.0D;
this.maxTerr = 5.0D;
this.minDiff = 1.0D;
this.maxDiff = 5.0D;
this.ownerConstraint = "";
this.titleConstraint = "";
}
public void setTitleConstraint(String title)
{
this.titleConstraint = title;
}
public void setOwnerConstraint(String owner)
{
this.ownerConstraint = owner;
}
public void setDifficultyConstraints(double min, double max)
{
this.minDiff = min;
this.maxDiff = max;
}
public void setTerrainConstraints(double min, double max)
{
this.minTerr = min;
this.maxTerr = max;
}
public ArrayList<Cache> select()
{
ArrayList caches = new ArrayList();
for (Cache c : this.allCaches)
{
if ((!c.getTitle().contains(this.titleConstraint)) ||
((!this.ownerConstraint.equals("")) &&
(!c.getOwner().equals(
this.ownerConstraint))) || (c.getDifficulty() < this.minDiff) ||
(c.getDifficulty() > this.maxDiff) ||
(c.getTerrain() < this.minTerr) || (c.getTerrain() > this.maxTerr))
continue;
caches.add(c);
}
return caches;
}
public ArrayList<String> getOwners()
{
HashSet ownerSet = new HashSet();
for (Cache c : this.allCaches)
{
ownerSet.add(c.getOwner());
}
ArrayList owners = new ArrayList(ownerSet);
Collections.sort(owners, new StringComparator());
return owners;
}
private class CacheComparator
implements Comparator<Cache>
{
public int compare(Cache c1, Cache c2)
{
return c1.getTitle().compareToIgnoreCase(c2.getTitle());
}
}
private class StringComparator
implements Comparator<String>
{
public int compare(String s1, String s2)
{
return s1.compareToIgnoreCase(s2);
}
}
}
把我给的两个文件代码替换后运行绝对和你老师的一样效果。
这是我本地运行结果: