Scientific Programming II

ModSim

Unit 1 - Assignment


Problem 1

Write a program to simulate the vertical free fall of an object. Start by writing a class called Particle to represent the falling object. The properties of the Particle class will defined as below:

Properties
Methods
Necessary Equations

In addition, write a test program to perform the following functions:

Test Program
import java.util.Scanner;
import java.text.DecimalFormat;
 
public class VerticalFreeFallSimulation {
    public static void main (String args[]) {
        System.out.println("Modeling a Vertical Free Fall");
        Scanner scanner = new Scanner (System.in);
        System.out.println("Total Time of Free Fall In Seconds?");
        double total_time = scanner.nextDouble();
        System.out.println("Length of Each Time Interval In Seconds?");
        double time_interval = scanner.nextDouble();
        Particle object = new Particle();
        DecimalFormat decimalformat = new DecimalFormat("0.00");
 
        System.out.println("TimeInterval" + "\t\t" + "Height" +  "\t\t\t" + "Velocity");
        System.out.println("#" + "\t\t\t" + "(m)" +  "\t\t\t" + "(m/s)" );
        for (double i = 0; i <= total_time; i += time_interval) {
            object.setTime(i);
 
            try {
                Thread.sleep((int) (time_interval * 1000));
            }
            catch (InterruptedException e) {}
                                        
            System.out.println(i + "\t\t\t" + 
                        decimalformat.format(object.verticalHeight()) + "\t\t\t" + 
                        decimalformat.format(object.verticalVelocity()));
        }
    }
}
Output
Modeling a Vertical Free Fall
Total Time of Free Fall In Seconds?
> 10
Length of Each Time Interval In Seconds?
> 0.5
Time Interval   Height      Velocity
#               (m)         (m/s)
0               0.00        0.00
1               4.90        9.80
2               19.60       19.60
3               44.10       29.40
4               78.40       39.20
5               122.50      49.00
6               176.40      58.80
7               240.10      68.60
8               313.60      78.40
9               396.90      88.20
10              490.00      98.00
11              592.90      107.80
12              705.60      117.60
13              828.10      127.40
14              960.40      137.20
15              1102.50     147.00
16              1254.40     156.80
17              1416.10     166.60
18              1587.60     176.40
19              1768.90     186.20
20              1960.00     196.00

Problem 2

Re-write Problem 1 such that the data table (as seen above) is written to an Excel file. Once the data is written to an Excel file, plot the data and observe how velocity and distance increase with time. The following program creates an Excel file called File.xls and copies all the data values to it.

Note: The Excel file will be in the same folder as your BlueJ project.

Test Program
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.FileWriter;
import java.io.IOException;
 
public class VerticalFreeFallWriteToFile {
    public static void main(String args[]) {
        System.out.println("Modeling a Vertical Free Fall");
        Scanner scanner = new Scanner (System.in);
        System.out.println("Total Time of Free Fall In Seconds?");
        double total_time = scanner.nextDouble();
        System.out.println("Length of Each Time Interval In Seconds?");
        double time_interval = scanner.nextDouble();
        Particle object = new Particle();
        DecimalFormat decimalformat = new DecimalFormat ("0.00");
 
        try {
            FileWriter filewriter = new FileWriter("File.xls"); 
            filewriter.write("Interval" + "\t\t" + "Height" +  "\t\t" + "Velocity\n");
            filewriter.write("#" + "\t\t" + "(m)" +  "\t\t" + "(m/s)" + "\n");
               
            for(double i = 0; i <= total_time; i += time_interval) {
                object.setTime(i);
                filewriter.write(i + "\t\t" + 
                            decimalformat.format(object.verticalHeight()) + "\t\t" + 
                            decimalformat.format(object.verticalVelocity())+ "\n");
            }     
            filewriter.close();
            System.out.println("End of Data Collection");
        }
        catch (IOException e) {}
    }
}

Problem 3

Re-write Problem 2 such that each of the intermediary height and velocity values are added to a LinkedList. Finally, print both lists.

Java Generics will help you here. Find a tutorial here