Advertisement
Guest User

Untitled

a guest
May 27th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3.  
  4. /**
  5.  * @author Manuel Komann & Matthes Elstermann
  6.  * @version 23-Feb-2018
  7.  *
  8.  * Template for exercise trajectory with methods
  9.  *
  10.  */
  11. public class JavaToMaple{
  12.  
  13.     //global variables to save the trajectory data
  14.     public static double t []  = new double[150]; //time
  15.     public static double x []  = new double[150]; //x-coordinate
  16.     public static double y []  = new double[150]; //y-coordintate
  17.  
  18.     //... space for other global variables
  19.  
  20.  
  21.  
  22.  
  23.  
  24.     /**
  25.      *  Method for printing the global t, x and y- coordinate pairs
  26.      *  to the console
  27.      */
  28.     public static void printToConsole(){
  29.         // print t, x and y coordinate pairs
  30.         // from arrays to shell area
  31.  
  32.         //... to be implemented
  33.  
  34.         for (int i = 0; i < t.length; i++) {
  35.             System.out.println(t[i] + ", " + x[i] + ", " + y[i]);
  36.         }
  37.     }
  38.  
  39.  
  40.     /**
  41.      *  Method for calculating the trajectory
  42.      */
  43.     public static void calc(double v0, double t0, double alpha){
  44.         for (int i = 0; i < t.length; i++) {
  45.             t[i] = t0 + i;
  46.             x[i] = v0 * t[i] * Math.cos(alpha);
  47.             y[i] = v0 * t[i] * Math.sin(alpha) - 0.5 * 9.81 * t[i] * t[i];
  48.         }
  49.     }
  50.  
  51.     public static void main(String[] args){
  52.         if (args.length >= 3) {
  53.             System.out.println(args[0]);
  54.             System.out.println(args[1]);
  55.             System.out.println(args[2]);
  56.         }
  57.  
  58.         // example for parsing the first input parameter
  59.         // Warning: program will crash if no input parameter is given
  60.  
  61.         double V0 = Double.parseDouble(args[0]);
  62.         double T0 = Double.parseDouble(args[1]);
  63.         double a = Double.parseDouble(args[2]);
  64.  
  65.         //...implement further parsing and evaluation of args
  66.  
  67.  
  68.  
  69.  
  70.  
  71.         //calling mehtods
  72.         calc(V0, T0, a);
  73.         printToConsole();
  74.  
  75.         //saving the elements to the maple worksheet
  76.         //uncomment to save to the maple sheet
  77.         //for Praktomat-turn-in this must be commented out
  78.         // (writing files is not allowed in test envoironment)
  79.         // saveMaple("throw");
  80.     }
  81.  
  82.     /**
  83.      * Method that creates a new Maple-work sheet. (.mw file)
  84.      * This method is complete! The details are not mandatory to be understood.
  85.      *
  86.      * @param title  - Name that the new Maple File will get
  87.      */
  88.     public static void saveMaple(String title){
  89.  
  90.         String plot = "restart; with(plots):A:=<<";
  91.  
  92.         //try-catch block: not relevant for exam,
  93.         //but the preferred way to handle possible exceptions in JAVA
  94.         //in this case if the computer fails to write a file for some reason
  95.         try{
  96.             //a class of the package IO for writing to text files
  97.             FileWriter w = new FileWriter(title + ".mw");
  98.  
  99.             int i = 0;
  100.             for(i = 0; i < t.length-1; i++){ plot = plot +  x[i] + ",";}
  101.             plot = plot + x[i]+">|<";
  102.  
  103.             for(i = 0; i < t.length-1; i++){ plot = plot + y[i] + ",";}
  104.  
  105.             plot = plot + y[i]+">>:";
  106.             plot = plot + "pointplot(A,color=blue,symbol=cross,labels=[distance,hight]);";
  107.  
  108.             w.append(plot);
  109.             w.flush();
  110.             w.close();
  111.  
  112.             System.out.println("\nFile saved as " + title);
  113.  
  114.         }catch(IOException e){e.printStackTrace();}
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement