我是Java的新手,正在使用BlueJ。
该计划的背景概述。
我正在尝试创建一个俱乐部数据库(arraylist,而不是实际的数据库),用户可以在其中添加一个新登山者(姓名,年龄,性别)以及他们攀登的山峰(名称,高度)到arraylist 。
我创造了Climber课程和山地课程。我还为登山者创建了一个ArrayList,可以添加到这个阵列。我的问题是..我怎样才能将登山者添加到登山者ArrayList中,并且能够同时添加他们攀爬过的山峰及其高度?
添加登山者的方法需要访问Climber和Mountain类吗?我是否需要将Mountain类中的字段传递给Climber?
虽然我们非常感谢能够解决这个问题的代码,但如果我朝着正确的方向展示,那将会很有帮助,所以我可以理解它更多!
感谢。
import java.util.ArrayList;
import java.util.Scanner;
/**
* Write a description of class ClubStats here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ClubStats
{
// An ArrayList for storing climber details.
private ArrayList<Climber> climbers;
/**
* Constructor for objects of class ClubStats
*/
public ClubStats()
{
// Initialise instance variables.
climbers = new ArrayList<Climber>();
}
public void addClimber(Climber newName)
{
climbers.add(newName);
}
public Climber getClimber(String name)
{
Climber foundClimber = null;
int index = 0;
boolean searching = true;
while(searching && index < climbers.size()) {
Climber climber = climbers.get(index);
if(climber.getName().equals(name)) {
searching = false;
foundClimber = climber;
}
else {
System.out.println(name + " not found");
index++;
}
}
return foundClimber;
}
public void displayList()
{
for (int item = 0; item<climbers.size();
item++) {
Climber climber = climbers.get(item);
System.out.println(climber.getName() + (" ") + climber.getAge() + (" ")
+ climber.getGender());
}
}
}
public class Climber
{
// Instance variables.
// The climber name.
private String name;
// The climber age
private int age;
// The climber gender.
private String gender;
/**
* Constructor for objects of class Climber
*/
public Climber (String newName, int newAge, String newGender)
{
// Initialise instance variables.
name = newName;
age = newAge;
gender = newGender;
}
/**
* Accessor method for climber's name.
*/
public String getName()
{
return name;
}
/**
* Set the climber's name.
*/
public void setName(String newName)
{
name = newName;
}
/**
* Accessor method for climber's age.
*/
public int getAge()
{
return age;
}
/**
* Set the climber's age.
*/
public void setAge(int newAge)
{
age = newAge;
}
/**
* Set the climer's gender.
*/
public String getGender()
{
return gender;
}
/**
* Accessor method for climber's gender.
*/
public void getGender(String newGender)
{
gender = newGender;
}
}
public class Mountain
{
// Instance variables.
private double height;
private String name;
/**
* Constructor for objects of class Mountain
*/
public Mountain(String mName, double mHeight)
{
// Initialise instance variables
name = mName;
height = mHeight;
}
/**
* Accessor method for mountain name.
*/
public String getName()
{
return name;
}
/**
* Set the mountain name.
*/
public void setName(String newName)
{
name = newName;
}
/**
* Accessor method for mountain height.
*/
public double getHeight()
{
// put your code here
return height;
}
/**
* Set the mountain height.
*/
public void setHeight(double newHeight)
{
height = newHeight;
}
}
答案 0 :(得分:3)
Climber
可以包含此List<Mountain>
攀升的Mountain
个Climber
。
您可以向addMountain(Mountain mountain)
课程添加Climber
方法,该方法会向Mountain
添加List
。
您可以添加将返回List<Mountain> getMountainList()
。
List
这样,对于每个Climber
,您都可以访问此Mountain
已登上的Climber
。
请注意,如果多个Climber
已攀升相同的Mountain
,则他们都可以在Mountain
中包含对同一List<Mountain>
个实例的引用(即无需为每个Mountain
创建Climber
个实例的副本。