Courses‎ > ‎AP Computer Science 2‎ > ‎Konstantinovich‎ > ‎

2017-02-28

posted Feb 28, 2017, 5:56 AM by Samuel Konstantinovich   [ updated Feb 28, 2017, 6:01 AM ]
Here are the test methods for HW01 and HW02. Please test your assignments and fix them if needed.
Y
Passing the test without extra output is required! Only the main should print things, unless otherwise required.

public class Test01{
  public static boolean closeEnough(double a, double b){
    if(a==0.0 && b==0.0)return true;
    if(a==0.0)return b < 0.00000000001;
    if(b==0.0)return a < 0.00000000001;
    return Math.abs(a-b)/a < 0.0001;//very generous %error accepted
  }
  public static void main(String[]args){
    double[] input = {1.0,2.0,4.0,7.0,10.0,100.0,1024.0,-1.0,0.0};
    int score = 0;
    for(double in : input){
      try{
        if(closeEnough(Recursion.sqrt(in),Math.sqrt(in))){
          score++;
          //System.out.println("PASS test "+in+" "+Recursion.sqrt(in));
        }
        else{
          System.out.println("Fail test "+in+" "+Recursion.sqrt(in)+" vs "+Math.sqrt(in));
        }
      }catch(IllegalArgumentException n){
        if(in == -1){
          score++;
        }
      }catch(Exception e){
        System.out.println("Some exception in test case:"+in);
      }
    }
    System.out.println(score+";out of: "+input.length+";"+Recursion.name());
  }
}

public class Test02{
 public static void main(String[]args){
  QueenBoard b;
  int[]tests =   {2,4, 5,6, 7, 8,  9,10,11};
  int[]answers = {0,2,10,4,40,92,352,724,2680};
  int score = 0;
  for(int i = 0; i < tests.length; i++){
    int size = tests[i];
    int ans  = answers[i];
    b = new QueenBoard(size);
    b.countSolutions();
    if(b.getSolutionCount()==ans){
      score++;
    }else{
      System.out.println("Failed board size: "+size );
    }
  }
  b = new QueenBoard(5);
  if(b.getSolutionCount() == -1){
    score++;
  }else{
    System.out.println("Failed when solution not yet run");
  }
  System.out.println("Score: "+score+" / "+(tests.length+1));
 }
}
Comments