[英]How do I merge two objects in Java?
对于我的家庭作业,我需要在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中检查它。
我认为当你将一列火车合并到另一列火车时,你将机车从火车2添加到火车1,然后你将货车从火车2的阵列加到火车1的阵列。 后一步将涉及复制数组。 我建议您查看Java方法System.arraycopy()
来执行此操作。
要执行这两个操作,您必须使用传递给merge方法的Train实例。
我还希望train2中的机车和发动机归零。 可以使用新的空数组来替换旧数组。 但这取决于你的教练希望你如何表明这一点。
数组中的值是实现细节。 让我们看一下每列火车的状态。
例如:
@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中使用这些方法。
可能有更有效的方法来实现阵列副本,而不是一次只做一辆车。 如果性能成为问题,您应该能够添加批量添加和删除方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.