-2

/* This is a question for an online test where the student writes a function whose answer is validated by my 'correctfunction' which is hidden to the student. I want to compare the results of the two functions in main method. */

import java.util.Arrays;

    class SortArr
  {
   static int[] arr = new int[10];

 public int[] sortin(int[] ans)
 {
      Arrays.sort(ans);
      System.out.println(Arrays.toString(ans));
      return ans;
    }

  public int[] correctfunction(int[] sol)
  {
      Arrays.sort(sol);    
      System.out.println(Arrays.toString(sol)); 
       return sol;
    }

  public static void main(String[] args)
  {   
      arr = new int[] {4,8,3,15,2,21,6,19,11,7};
      SortArr ob=new SortArr();
      ob.correctfunction(arr);
      ob.sortin(arr); 

      if(Arrays.equals(ob.sol == ob.ans))  //non-static method //equals(Object) cannot be referenced from a static context
//variable ob of type SortArr: cannot find symbol
      System.out.println("correct");
      else
      System.out.println("incorrect");
  }
 }

2 Answers2

0

First Arrays.equals(parameter1, parameter2) it takes two parameter, and what you did is totally wrong. To fix that see below code

  public static void main(String[] args)
  {   
      arr = new int[] {4,8,3,15,2,21,6,19,11,7};
      SortArr ob=new SortArr();
      int[] newSol = ob.correctfunction(arr);
      int[] newAns = ob.sortin(arr); 

      if(Arrays.equals(newSol, newAns))
          System.out.println("correct");
      else
          System.out.println("incorrect");
  }
Luminous_Dev
  • 614
  • 6
  • 14
0

The function returns an array. So when you call the method ,save the returned arrays in some array variable! I have made the required changes in your code. Hope they work for you.

import java.util.Arrays;

    class SortArr
  {

        int arr1[];
        int arr2[];
   static int[] arr = new int[10];

 public int[] sortin(int[] ans)
 {
      Arrays.sort(ans);
      System.out.println(Arrays.toString(ans));
      return ans;
    }

  public int[] correctfunction(int[] sol)
  {
      Arrays.sort(sol);    
      System.out.println(Arrays.toString(sol)); 
       return sol;
    }

  public static void main(String[] args)
  {   
      SortArr s = new SortArr(); //make an object of your class
      arr = new int[] {4,8,3,15,2,21,6,19,11,7};
      SortArr ob=new SortArr();
      s.arr1 = ob.correctfunction(arr); // save the returned array
      s.arr2 =ob.sortin(arr);    // save the returned array

      if(s.arr1 == s.arr2)  

      System.out.print("correct");
      else
      System.out.print("incorrect");
  }
 }
JustCurious
  • 780
  • 1
  • 10
  • 29