Guest User

Untitled

a guest
May 28th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import java.util.Random;
  2. import java.lang.Math;
  3.  
  4. public class Aufgabe4 {
  5.  
  6. static class CalcThread extends Thread {
  7.  
  8. private static Random rand = new Random();
  9. private static double sum = 0;
  10. private static int count = 0;
  11.  
  12. private int n;
  13.  
  14. public CalcThread(int n) {
  15. this.n = n;
  16. start(); //starte Thread-Ausfuehrung sofort...
  17. }
  18.  
  19. public void run() {
  20. //hier kommt Ihr Programmcode...
  21. //...
  22.  
  23. double random[]= new double[n];
  24. for (int i=0; i<n;i++){
  25. random[i]=Math.sin(rand.nextDouble());
  26. sum+=Math.pow(random[i],2);
  27. count++;
  28. }
  29. }
  30.  
  31. public static int getCount() {
  32. return count;
  33. }
  34.  
  35. public static double getSum() {
  36. return sum;
  37. }
  38. }
  39.  
  40. public static void main(String[] args) {
  41.  
  42. //Kommandozeilenparameter auslesen
  43. int n = -1;
  44. if(args.length>=1) {
  45. n = Integer.valueOf(args[0]);
  46. } else {
  47. System.out.println("Erster Parameter fehlt!");
  48. System.exit(1);
  49. return;
  50. }
  51.  
  52. //hier kommt Ihr Programmcode...
  53. //...i have no idea what i am doin
  54. CalcThread[] threads= new CalcThread[n];
  55. for(int i=0; i<n;i++){
  56. //threads[i] new CalcThreads.threads(n);
  57. threads[i].run();
  58. //threads[i].join();
  59. }
  60.  
  61. double korrekterDurchschnitt = (0.5 - (0.25 * Math.sin(2)));//...
  62. double meinDurchschnitt = (CalcThread.getSum()/n);//...
  63. double abweichung = (100*(meinDurchschnitt - korrekterDurchschnitt)/korrekterDurchschnitt);//...
  64.  
  65. System.out.println("Ergebnis: " + meinDurchschnitt + ";" + CalcThread.getCount() + ";" + korrekterDurchschnitt + ";" + abweichung);
  66.  
  67. }
  68.  
  69. }
Add Comment
Please, Sign In to add comment