oppgave: train

This commit is contained in:
2026-03-23 18:36:37 +01:00
parent fd661adfc5
commit 3f7ea46a2f
8 changed files with 56 additions and 39 deletions
+9 -10
View File
@@ -9,7 +9,7 @@ package oving7.train;
*/
public class PassengerCar extends TrainCar {
// TODO: Add fields here
private int passengerCount;
/**
* Constructor for the passenger car.
@@ -21,7 +21,7 @@ public class PassengerCar extends TrainCar {
* @see PassengerCarTest#testWeight()
*/
public PassengerCar(int deadWeight, int passengerCount) {
// TODO: Implement this constructor
this.passengerCount = passengerCount;
super(deadWeight);
}
@@ -31,8 +31,7 @@ public class PassengerCar extends TrainCar {
* @see PassengerCarTest#testWeight()
*/
public int getPassengerCount() {
// TODO: Implement this method
return 0;
return passengerCount;
}
/**
@@ -42,22 +41,22 @@ public class PassengerCar extends TrainCar {
* @see PassengerCarTest#testWeight()
*/
public void setPassengerCount(int passengerCount) {
// TODO: Implement this method
if (passengerCount < 0) {
throw new IllegalArgumentException("passenger count cannot be less than zero");
}
this.passengerCount = passengerCount;
}
@Override
public int getTotalWeight() {
// To calculate the total weight of the passenger car, you can assume that an average
// passenger weighs 80 kg
// TODO: Implement this method
return 0;
return super.getDeadWeight() + passengerCount * 80;
}
@Override
public String toString() {
// TODO: Implement this method
return null;
return "PassengerCar";
}
public static void main(String[] args) {