mardi 28 avril 2015

gestellter Code fehlerhaft?

Ich habe als Hausaufgabe folgenden Code bekommen:

Java Code:

  1. import java.math.BigInteger;
  2. import java.util.Random;
  3.  
  4. public class Gcd {
  5. public static BigInteger gcdNaive(BigInteger a, BigInteger b) {
  6. // bitte implementieren Sie diese Methode
  7. return BigInteger.ZERO;
  8. }
  9. public static BigInteger gcdEuclid(BigInteger a, BigInteger b) {
  10. // bitte implementieren Sie diese Methode
  11. return BigInteger.ZERO;
  12. }
  13.  
  14. public static boolean testNaive(int sample_size, int num_bits, Random random) {
  15. for(int i = 0; i < sample_size; i++) {
  16. BigInteger a = new BigInteger(num_bits, random).add(BigInteger.ONE);
  17. BigInteger b = new BigInteger(num_bits, random).add(BigInteger.ONE);
  18. BigInteger gcd = gcdNaive(a, b);
  19.  
  20. if(!gcd.equals(a.gcd(b))) {
  21. System.out.println("Naive algorithm: Wrong solution!");
  22. System.out.println(" for gcd(" + a + ", " + b + "):"
  23. + " Expected " + a.gcd(b) + " but got " + gcd);
  24. return false;
  25. }
  26. }
  27.  
  28. return true;
  29. }
  30.  
  31. public static boolean testEuclid(int sample_size, int num_bits, Random random) {
  32. for(int i = 0; i < sample_size; i++) {
  33. BigInteger a = new BigInteger(num_bits, random).add(BigInteger.ONE);
  34. BigInteger b = new BigInteger(num_bits, random).add(BigInteger.ONE);
  35. BigInteger gcd = gcdEuclid(a, b);
  36.  
  37. if(!gcd.equals(a.gcd(b))) {
  38. System.out.println("Euclidean algorithm: Wrong solution!");
  39. System.out.println(" for gcd(" + a + ", " + b + "):"
  40. + " Expected " + a.gcd(b) + " but got " + gcd);
  41. return false;
  42. }
  43. }
  44.  
  45. return true;
  46. }
  47.  
  48. public static long benchmarkNaive(int sample_size, int num_bits, Random random) {
  49. long time_sum = 0;
  50.  
  51. for(int i = 0; i < sample_size; i++) {
  52. BigInteger a = new BigInteger(num_bits, random).add(BigInteger.ONE);
  53. BigInteger b = new BigInteger(num_bits, random).add(BigInteger.ONE);
  54.  
  55. long start = System.nanoTime();
  56. BigInteger gcd = gcdNaive(a, b);
  57. time_sum += System.nanoTime() - start;
  58.  
  59. if(!gcd.equals(a.gcd(b)))
  60. System.out.println("Naive algorithm: Wrong solution!");
  61. }
  62.  
  63. return time_sum / sample_size;
  64. }
  65.  
  66. public static long benchmarkEuclid(int sample_size, int num_bits, Random random) {
  67. long time_sum = 0;
  68.  
  69. for(int i = 0; i < sample_size; i++) {
  70. BigInteger a = new BigInteger(num_bits, random).add(BigInteger.ONE);
  71. BigInteger b = new BigInteger(num_bits, random).add(BigInteger.ONE);
  72.  
  73. long start = System.nanoTime();
  74. BigInteger gcd = gcdEuclid(a, b);
  75. time_sum += System.nanoTime() - start;
  76.  
  77. if(!gcd.equals(a.gcd(b)))
  78. System.out.println("Euclidean algorithm: Wrong solution!");
  79. }
  80.  
  81. return time_sum / sample_size;
  82. }
  83.  
  84. public static void main(String[] args) {
  85. Random random = new Random();
  86.  
  87. boolean ok = true;
  88. if(!testNaive(100, 16, random)) {
  89. ok = false;
  90. }else{
  91. System.out.println("Naive algorithm seems to be correct!");
  92. }
  93. if(!testEuclid(100, 16, random)) {
  94. ok = false;
  95. }else{
  96. System.out.println("Euclidean algorithm seems to be correct!");
  97. }
  98. if(!ok)
  99. return;
  100.  
  101. int sample_size = 10;
  102.  
  103. System.out.println("Benchmark:");
  104. System.out.println(String.format("%12s %24s %24s",
  105. "#bits", "runtime (naive)", "runtime (Euclidean)"));
  106.  
  107. int num_bits = 4;
  108. while(true) {
  109. long naive_time = benchmarkNaive(sample_size, num_bits, random);
  110. long euclid_time = benchmarkEuclid(sample_size, num_bits, random);
  111.  
  112. System.out.println(String.format("%12s %24s %24s", num_bits,
  113. ((naive_time / 1000000L) + "." + String.format("%06d", naive_time % 1000000L)),
  114. ((euclid_time / 1000000L) + "." + String.format("%06d", euclid_time % 1000000L))));
  115.  
  116. if(naive_time * sample_size > 5L * 1000000000L
  117. || euclid_time * sample_size > 5L * 1000000000L)
  118. break;
  119.  
  120. num_bits = num_bits << 1;
  121. }
  122. System.out.println("All timings are averages over " + sample_size
  123. + " runs measured in ms");
  124. }
  125. };


Er soll ein Programm darstellen, welches mit anzeigt, ob die von mir (noch nicht) implementierten Methoden, die den größten gemeinsamen Teiler (= greates commen dividend = gcd) auf euklidischer oder naive Art errechnen sollen, korrekt sind.

Wenn ich das Programm starte, bekomme ich folgende Fehlermeldung:
Zitat:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method format(Locale, String, Object[]) in the type String is not applicable for the arguments (String, String, String, String)
The method format(String, Object[]) in the type String is not applicable for the arguments (String, long)
The method format(String, Object[]) in the type String is not applicable for the arguments (String, long)

at gcdP.Gcd.main(Gcd.java:123)
Wenn ich die Methoden implementiere, bekomme ich natürlich immer noch die gleiche Fehlermeldung. Wir sollen nicht (wir dürfen sogar nicht) am sonstigen Code basteln. Ist der Code fehlerhaft?


gestellter Code fehlerhaft?

0 commentaires:

Enregistrer un commentaire