horselurrver

Cantilever

Nov 26th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. //Assumes that the ball is on the edge closest to table
  2. import java.util.*;
  3. public class Cantilever {
  4. private final double LEN = 0.3; //length of each slat in meters. Also 30 cm
  5. private final double MASS = 0.036; //mass of each slat in kg. Also 36g
  6. private final int NUM_SLATS = 20; //number of slats to be used
  7. private double ball; //mass of extra "ball" on top of cantilever in meters. 0.05, 0.1, or 0.2kg
  8. private ArrayList<Double> prevDist = new ArrayList<>(); //list of all previous distances from ledge
  9. private double finalCOM = 0; //final center of mass
  10. private double[] balls = {0.005, 0.01, 0.02}; //different masses that ball could be
  11. private double totalSum;
  12. public Cantilever(){
  13. for (int h = 0; h < 3; h++){
  14. ball = balls[h];
  15. for(int n = 1; n <= NUM_SLATS; n++){//n is the nth slat
  16. double distSum = 0;
  17. distSum += ball*(getPrev(n-1) + LEN) + n*MASS*(LEN/2);
  18. for (int j = 0; j < prevDist.size(); j++){
  19. distSum += (j + 1)*prevDist.get(j)*MASS;
  20. }
  21. distSum /= (n*MASS + ball);
  22. prevDist.add(LEN - distSum);
  23. finalCOM = LEN - distSum;
  24. }
  25. System.out.println("Mass of ball: " + ball*1000 + " grams.");
  26. printLedgeDistances();
  27. System.out.printf("The final center of mass is %.2f centimeters.\n", finalCOM);
  28. System.out.printf("Score: %.2f\n", ball*1000*(totalSum+LEN));
  29. System.out.println();
  30. prevDist.clear();
  31. }
  32. }
  33.  
  34. //Gets sum of previous distances up to the nth previous distance (inclusive)
  35. public double getPrev(int n){
  36. double sum = 0;
  37. if (n == 0){
  38. return 0;
  39. } else {
  40. for (int i = 0; i < n; i++){
  41. sum += prevDist.get(i);
  42. }
  43. return sum;
  44. }
  45. }
  46.  
  47. //sums all of the ledge distances to get total distance
  48. public double sumAll(){
  49. double totalSum = 0;
  50. for (Double dist : prevDist){
  51. totalSum += dist;
  52. }
  53. return totalSum;
  54. }
  55.  
  56. public void printLedgeDistances(){
  57. totalSum = 0;
  58. for(int j = 0; j < prevDist.size(); j++){
  59. System.out.printf("Ledge %d: %.2f centimeters\n", (j+1), prevDist.get(j)*100);
  60. totalSum += prevDist.get(j)*100;
  61. }
  62. System.out.printf("Total distance: %.2f centimeters.\n", sumAll()*100);
  63. }
  64.  
  65. public static void main(String[] arguments){
  66. Cantilever c = new Cantilever();
  67. }
  68. }
Add Comment
Please, Sign In to add comment