Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Assumes that the ball is on the edge closest to table
- import java.util.*;
- public class Cantilever {
- private final double LEN = 0.3; //length of each slat in meters. Also 30 cm
- private final double MASS = 0.036; //mass of each slat in kg. Also 36g
- private final int NUM_SLATS = 20; //number of slats to be used
- private double ball; //mass of extra "ball" on top of cantilever in meters. 0.05, 0.1, or 0.2kg
- private ArrayList<Double> prevDist = new ArrayList<>(); //list of all previous distances from ledge
- private double finalCOM = 0; //final center of mass
- private double[] balls = {0.005, 0.01, 0.02}; //different masses that ball could be
- private double totalSum;
- public Cantilever(){
- for (int h = 0; h < 3; h++){
- ball = balls[h];
- for(int n = 1; n <= NUM_SLATS; n++){//n is the nth slat
- double distSum = 0;
- distSum += ball*(getPrev(n-1) + LEN) + n*MASS*(LEN/2);
- for (int j = 0; j < prevDist.size(); j++){
- distSum += (j + 1)*prevDist.get(j)*MASS;
- }
- distSum /= (n*MASS + ball);
- prevDist.add(LEN - distSum);
- finalCOM = LEN - distSum;
- }
- System.out.println("Mass of ball: " + ball*1000 + " grams.");
- printLedgeDistances();
- System.out.printf("The final center of mass is %.2f centimeters.\n", finalCOM);
- System.out.printf("Score: %.2f\n", ball*1000*(totalSum+LEN));
- System.out.println();
- prevDist.clear();
- }
- }
- //Gets sum of previous distances up to the nth previous distance (inclusive)
- public double getPrev(int n){
- double sum = 0;
- if (n == 0){
- return 0;
- } else {
- for (int i = 0; i < n; i++){
- sum += prevDist.get(i);
- }
- return sum;
- }
- }
- //sums all of the ledge distances to get total distance
- public double sumAll(){
- double totalSum = 0;
- for (Double dist : prevDist){
- totalSum += dist;
- }
- return totalSum;
- }
- public void printLedgeDistances(){
- totalSum = 0;
- for(int j = 0; j < prevDist.size(); j++){
- System.out.printf("Ledge %d: %.2f centimeters\n", (j+1), prevDist.get(j)*100);
- totalSum += prevDist.get(j)*100;
- }
- System.out.printf("Total distance: %.2f centimeters.\n", sumAll()*100);
- }
- public static void main(String[] arguments){
- Cantilever c = new Cantilever();
- }
- }
Add Comment
Please, Sign In to add comment