fork(1) download
  1. import java.io.*;
  2. abstract class ScalarProduct<A> {
  3. public abstract Integer scalarProduct(A second);
  4. }
  5. class Nil extends ScalarProduct<Nil>{
  6. Nil(){}
  7. public Integer scalarProduct(Nil second) {
  8. return 0;
  9. }
  10. }
  11. class Cons<A extends ScalarProduct<A>> extends ScalarProduct<Cons<A>>{
  12. public Integer value;
  13. public A tail;
  14. Cons(Integer _value, A _tail) {
  15. value = _value;
  16. tail = _tail;
  17. }
  18. public Integer scalarProduct(Cons<A> second){
  19. return value * second.value + tail.scalarProduct(second.tail);
  20. }
  21. }
  22. class _Test{
  23. public static Integer main(Integer n){
  24. return _main(n, 0, new Nil(), new Nil());
  25. }
  26. public static <A extends ScalarProduct<A>> Integer _main(Integer n, Integer i, A first, A second){
  27. if (n == 0) {
  28. return first.scalarProduct(second);
  29. } else {
  30. return _main(n-1, i+1, new Cons<A>(i, new Cons<A>(2*i+1,first)), new Cons<Cons<A>>(i, new Cons<A>(i*i, second)));
  31. //return _main(n-1, i+1, new Cons<A>(2*i+1,first), new Cons<A>(i*i, second));
  32. //return _main(n-1, i+1, first, new Cons<A>(i*i, second));
  33. }
  34. }
  35. }
  36. class Test{
  37. public static void main(String [] args){
  38. System.out.print("Enter a number: ");
  39. try {
  40. String line = is.readLine();
  41. Integer val = Integer.parseInt(line);
  42. System.out.println(_Test.main(val));
  43. } catch (NumberFormatException ex) {
  44. System.err.println("Not a valid number");
  45. } catch (IOException e) {
  46. System.err.println("Unexpected IO ERROR");
  47. }
  48. }
  49. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
3
compilation info
Main.java:30: error: constructor Cons in class Cons<A#2> cannot be applied to given types;
            return _main(n-1, i+1, new Cons<A>(i, new Cons<A>(2*i+1,first)), new Cons<Cons<A>>(i, new Cons<A>(i*i, second)));
                                   ^
  required: Integer,A#1
  found: Integer,Cons<A#1>
  reason: actual argument Cons<A#1> cannot be converted to A#1 by method invocation conversion
  where A#1,A#2 are type-variables:
    A#1 extends ScalarProduct<A#1> declared in method <A#1>_main(Integer,Integer,A#1,A#1)
    A#2 extends ScalarProduct<A#2> declared in class Cons
1 error
stdout
Standard output is empty