[text] yessir

Viewer

  1. import java.util.Arrays;
  2.  
  3. public class Train {
  4.         private String name;
  5.         private int locomotive;
  6.         private int[] freightCars = { };
  7.        
  8.         public Train(String name, int locomotive) {
  9.                 if (name == null) {
  10.                         throw new NullPointerException("Name can't be null");
  11.                 }
  12.                 else {
  13.                         this.name = name;
  14.                 }
  15.                
  16.                 if (locomotive < 0) {
  17.                         throw new IllegalArgumentException("locomotive can't be negative");
  18.                 }
  19.                 else {
  20.                         this.locomotive = locomotive;
  21.                 }
  22.  
  23.         }
  24.        
  25.         public int[] getFreightCars() {
  26.                 return Arrays.copyOf(freightCars, freightCars.length);
  27.         }
  28.        
  29.         public String getName() {
  30.                 return name;
  31.         }
  32.        
  33.         public void setName(String n) {
  34.                 if (n == null) {
  35.                         throw new NullPointerException("Name can't be null");
  36.                 }
  37.                 else {
  38.                         this.name = n;
  39.                 }
  40.         }
  41.        
  42.         public int getLocomotive() {
  43.                 return locomotive;
  44.         }
  45.        
  46.         public void setLocomotive(int p) {
  47.                 if (p < 0) {
  48.                         throw new IllegalArgumentException("locomotive can't be negative");
  49.                 }
  50.                 else {
  51.                         this.locomotive = p;
  52.                
  53.                 }
  54.         }
  55.        
  56.        
  57.         public int getTotalWeightOfCars() {
  58.                 int total = 0;
  59.                 for (int n : this.freightCars) {
  60.                         total += n;
  61.                 }
  62.                 return total;
  63.         }
  64.        
  65.         public int getNumbersOfCars() {
  66.                 return this.freightCars.length;
  67.         }
  68.        
  69.         public int maxSpeed() {
  70.                 int speed = this.locomotive - this.getTotalWeightOfCars();
  71.                 if (speed >= 150) {
  72.                         return 150;
  73.                 }
  74.                 else if (speed < 0) {
  75.                         return 0;
  76.                 }
  77.                 else {
  78.                         return speed;
  79.                 }
  80.         }
  81.        
  82.         public void removeAllCars() {
  83.                 this.freightCars = new int[] { };
  84.         }
  85.        
  86.         public void addCars(int... weights) {
  87.                
  88.                 if (weights == null) {
  89.                         throw new NullPointerException(null);
  90.                 }
  91.                
  92.                         int [] temp = new int[this.freightCars.length + weights.length];
  93.                         for (int i = 0; i < this.freightCars.length; i++) {
  94.                                 temp[i] = this.freightCars[i];
  95.                         }
  96.                        
  97.                         for (int i = 0; i < weights.length; i++) {
  98.                                 temp[this.getNumbersOfCars() + i] = weights[i];
  99.                         }
  100.                        
  101.                         this.freightCars = temp;
  102.         }
  103.        
  104.         public void mergeCars(Train other) {
  105.                 this.locomotive = this.locomotive + other.locomotive;
  106.                 other.locomotive = 0;
  107.                 this.addCars(other.freightCars);
  108.                 other.removeAllCars();
  109.         }
  110.  
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  

Editor

You can edit this paste and save as new: