Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Program: KK_FreightTrain.java
- * Author: K. Kowalsky
- * Date: Dec 6, 2012
- * Description: Ask for user input for number of trains, create an array with that many variables
- * Randomize weight on trains in array
- * Send array to method printTrainArray to print the array to screen
- * Send array to method testWeightDistribution to test if any two trains exceed weight distribution
- * Send array to method getTotalWeight to calculate the final weight, send the final weight back to main method for printing to screen
- */
- // Import the Scanner class for obtaining user input
- import java.util.Scanner;
- public class KK_FreightTrain
- {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- //Ask for user input for number of trains, create an array with that many variables
- System.out.println("Freight Train Load Tester \n\n" +
- "This program will analyze the weight distribution of rail cars in a\nsimulated train to determine if it will meet a bridge capacity requirement." +
- "\n\nHow many cars are on the train?");
- int trainNum = input.nextInt();
- while (trainNum < 10 || trainNum > 30)
- {
- System.out.println("That is an invalid number. How many cars would you like on the train?");
- trainNum = input.nextInt();
- }
- double[] weightArray = new double[trainNum + 1];
- weightArray[0] = 70;
- //Randomize weight on trains in array
- for (int i = 1; i < weightArray.length; i++)
- {
- int randomInt = (int)((130-30+1) * Math.random() + 30);
- weightArray[i] = randomInt;
- }
- //Send array to method printTrainArray to print the array to screen
- printTrainArray(weightArray);
- System.out.println("\n");
- //Send array to method testWeightDistribution to test if any two trains exceed weight distribution
- testWeightDistribution(weightArray);
- //Send array to method getTotalWeight to calculate the final weight, send the final weight back to main method for printing to screen
- double finalWeight = getTotalWeight(weightArray, 0);
- System.out.println("\nThe total weight of this train in metric tons is " + finalWeight + ".");
- input.close();
- }
- static void printTrainArray(double [] a)
- {
- int carNum = 1;
- System.out.println("Here are the weights in metric tons of all parts of the simulated train:\n");
- for(int i = 0; i < a.length; i++)
- {
- if (i == 0)
- {
- System.out.println("Engine: " + a[i]);
- }
- else if (i > 0)
- {
- System.out.println("Car " + carNum + ": " + a[i]);
- carNum ++;
- }
- }
- }
- static void testWeightDistribution(double [] b)
- {
- int errors = 0;
- for (int j = 0; j < b.length - 1; j++)
- {
- if ((b[j] + b[j+1]) > 200)
- {
- if (j == 0)
- {
- System.out.println("\nWARNING: The engine and car 1 exceed the two car weight limit.");
- errors ++;
- }
- else
- {
- System.out.println("WARNING: Car " + j + " and car " + (j+1) + " exceed the two car weight limit.");
- errors ++;
- }
- }
- }
- if (errors == 0)
- {
- System.out.println("No weight distribution problems found for this train.");
- }
- }
- static double getTotalWeight(double [] c, double totalWeight)
- {
- for (int i = 0; i < c.length; i++)
- {
- totalWeight = totalWeight + c[i];
- }
- return totalWeight;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement