对于家庭作业,我需要合并Java中的两个对象。我必须创建一个方法
void mergeTrains(Train other)
,会将其他火车上的所有机车和汽车添加到当前火车上。因此,在调用train1.mergeTrains(train2)
之后,完成后参数火车将没有车厢也没有机车。它们都将被添加到train1。
然后,我们必须使用JUnit来检查数组和机车是否正确合并。 这是我到目前为止的内容:
public class Train {
// data members
private String trainName;
private int numOfLocomotives;
private int[] freightCars;
// constructor
public Train (String trainName, int numOfLocomotives) {
this.trainName = trainName;
this.numOfLocomotives = numOfLocomotives;
}
public int getNumOfLocomotives() {
return numOfLocomotives;
}
public void setNumOfLocomotives(int numOfLocomotives) {
if (numOfLocomotives < 0) {
System.out.println("Error. Locomotives can't be set to less than 0.");
return;
} else {
this.numOfLocomotives = numOfLocomotives;
}
}
public int[] getFreightCars(int... freightCars) {
if (freightCars == null) {
System.out.println("There are no freight cars in the list.");
return freightCars;
} else {
return freightCars;
}
}
public int[] removeAllCars(int...freightCars) {
freightCars = null;
return freightCars;
}
public int[] addCars(int...weights) {
int count = 0;
int[] freightCars = {10, 20, 30}; // used to check if method functions correctly
int[] newTrain = new int[freightCars.length + weights.length];
for (int i = 0; i < freightCars.length; i++) {
newTrain[i] = freightCars[i];
count++;
}
for (int j = 0; j < weights.length; j++) {
newTrain[count++] = weights[j];
}
for (int i = 0; i < newTrain.length; i++) {
System.out.print(newTrain[i] + " ");
}
return newTrain;
}
public void mergeTrains(Train other) {
this.numOfLocomotives = this.numOfLocomotives + other.numOfLocomotives;
other.numOfLocomotives = 0;
}
这是我的JUnit测试类:
class TestTrain {
int[] train1Cars = {10, 20, 30};
int[] train2Cars = {4, 11, 15};
int[] mergedTrain = {10, 20, 30, 4, 11, 15};
@Test
void testRemoveCars() {
Train t1 = new Train("Thomas", 2);
assertArrayEquals(t1.removeAllCars(train1Cars), null);
}
@Test
void testAddCars() {
Train t1 = new Train ("Thomas", 2);
assertArrayEquals(t1.addCars(train2Cars), mergedTrain);
}
@Test
void testMergeTrains() {
Train t1 = new Train ("Thomas", 1);
Train other = new Train ("Rob", 4);
}
}
基本上,我需要弄清楚我应该如何使用Train other
参数。然后如何使用JUnit进行测试。我认为在mergeTrains方法中更改机车是正确的,但我也不知道如何在JUnit中进行检查。
答案 0 :(得分:0)
我认为,当您将一列火车合并到另一列火车时,您将火车2的机车添加到火车1,然后将火车2中的货运车厢添加到火车1中的火车。后面的步骤将涉及复制阵列。我建议您查看Java方法System.arraycopy()
来完成此操作。
要执行这两项操作,您必须使用传递给merge方法的Train实例。
我还希望train2中的机车和引擎被清零。一个新的空数组可以用来替换旧数组。但这取决于您的老师希望您如何指出。
答案 1 :(得分:0)
数组中的值是实现细节。让我们看看每列火车的状态。
例如:
@Test
void can_add_a_car() {
Train t1 = new Train("Thomas", 2);
t1.addCars(1, 1, 1);
assertEquals(t1.numberOfCars(), 3);
}
所以类开始看起来像
public class Train {
private ...instance variables ...
public addCars(int... weights) { ... }
}
也能拆车真好
@Test
void can_remove_a_car() {
Train t1 = new Train("Thomas", 2);
t1.addCars(1, 2, 3);
t1.removeCar(2);
assertEquals(t1.numberOfCars(), 2);
assertEquals(t1.weight(), 3);
}
这可能表明
之类的方法public int removeCar(int position) {
// TODO: move all the cars up by one
return freightCars[position];
}
public int weight() {
// sum the weights of the cars
}
合并汽车是这样的:
@Test
void can_merge_trains() {
Train t1 = new Train("Thomas", 2);
Train t2 = new Train("Rob", 2);
t1.addCars(1, 1, 1)
t2.addCars(1, 1, 1)
t1.merge(t2);
assertEquals(t1.numberOfCars(), 6);
assertEquals(t2.numberOfCars(), 0);
}
我们可以像这样实现此方法:
public void mergeCars(Train other) {
for(int k = other.numberOfCars() - 1; k >= 0; k--) {
addCar(other.removeCar());
}
}
您还可以添加方法来添加和删除机车,并在mergeCars中使用它们。
可能有更有效的方法来实现阵列副本,而不是一次完成一辆汽车。如果性能出现问题,您应该能够添加批量添加和删除方法。