Advertisement
K_Wall_24

Final

Dec 6th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /**
  2. * Program: KK_FreightTrain.java
  3. * Author: K. Kowalsky
  4. * Date: Dec 6, 2012
  5. * Description: Ask for user input for number of trains, create an array with that many variables
  6. * Randomize weight on trains in array
  7. * Send array to method printTrainArray to print the array to screen
  8. * Send array to method testWeightDistribution to test if any two trains exceed weight distribution
  9. * Send array to method getTotalWeight to calculate the final weight, send the final weight back to main method for printing to screen
  10. */
  11.  
  12. // Import the Scanner class for obtaining user input
  13. import java.util.Scanner;
  14.  
  15. public class KK_FreightTrain
  16. {
  17. public static void main(String[] args)
  18. {
  19. Scanner input = new Scanner(System.in);
  20.  
  21. //Ask for user input for number of trains, create an array with that many variables
  22. System.out.println("Freight Train Load Tester \n\n" +
  23. "This program will analyze the weight distribution of rail cars in a\nsimulated train to determine if it will meet a bridge capacity requirement." +
  24. "\n\nHow many cars are on the train?");
  25. int trainNum = input.nextInt();
  26. while (trainNum < 10 || trainNum > 30)
  27. {
  28. System.out.println("That is an invalid number. How many cars would you like on the train?");
  29. trainNum = input.nextInt();
  30. }
  31.  
  32. double[] weightArray = new double[trainNum + 1];
  33. weightArray[0] = 70;
  34.  
  35. //Randomize weight on trains in array
  36.  
  37. for (int i = 1; i < weightArray.length; i++)
  38. {
  39. int randomInt = (int)((130-30+1) * Math.random() + 30);
  40. weightArray[i] = randomInt;
  41. }
  42.  
  43. //Send array to method printTrainArray to print the array to screen
  44. printTrainArray(weightArray);
  45. System.out.println("\n");
  46. //Send array to method testWeightDistribution to test if any two trains exceed weight distribution
  47. testWeightDistribution(weightArray);
  48.  
  49. //Send array to method getTotalWeight to calculate the final weight, send the final weight back to main method for printing to screen
  50. double finalWeight = getTotalWeight(weightArray, 0);
  51.  
  52. System.out.println("\nThe total weight of this train in metric tons is " + finalWeight + ".");
  53.  
  54. input.close();
  55. }
  56.  
  57. static void printTrainArray(double [] a)
  58. {
  59. int carNum = 1;
  60. System.out.println("Here are the weights in metric tons of all parts of the simulated train:\n");
  61. for(int i = 0; i < a.length; i++)
  62. {
  63. if (i == 0)
  64. {
  65. System.out.println("Engine: " + a[i]);
  66. }
  67. else if (i > 0)
  68. {
  69. System.out.println("Car " + carNum + ": " + a[i]);
  70. carNum ++;
  71. }
  72. }
  73. }
  74. static void testWeightDistribution(double [] b)
  75. {
  76. int errors = 0;
  77. for (int j = 0; j < b.length - 1; j++)
  78. {
  79.  
  80. if ((b[j] + b[j+1]) > 200)
  81. {
  82.  
  83. if (j == 0)
  84. {
  85. System.out.println("\nWARNING: The engine and car 1 exceed the two car weight limit.");
  86. errors ++;
  87. }
  88. else
  89. {
  90. System.out.println("WARNING: Car " + j + " and car " + (j+1) + " exceed the two car weight limit.");
  91. errors ++;
  92. }
  93.  
  94. }
  95. }
  96. if (errors == 0)
  97. {
  98. System.out.println("No weight distribution problems found for this train.");
  99. }
  100.  
  101. }
  102. static double getTotalWeight(double [] c, double totalWeight)
  103. {
  104. for (int i = 0; i < c.length; i++)
  105. {
  106. totalWeight = totalWeight + c[i];
  107. }
  108. return totalWeight;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement