0

I'm new here, and I'm new to programming too. This is the code I'm writing, this is one of the two classes in the project, "Student":

interface Comparable{
    public boolean youngerThan(Comparable c);
}

public class Student implements Comparable{
      private String name;
      private int age;


  public Student(String n, int a){
    this.name=n;
    this.age=a;
}

  public String getName(){
      return this.name;
  }

  public int getAge(){
      return this.age;
  }
  public void setStudent(String n, int a){
      this.name=n;
      this.age=a;
  }
  public String toString() {

      return this.getName()+" ("+this.getAge()+"years)";
  }
  public boolean youngerThan(Comparable c){
      Student s;
      s=(Student)c;
      if(this.getAge()>s.getAge())
          return false;
      if (this.getAge()==s.getAge())
          return false;
      else 
          return true;
  }

  public static void main(String[] args){
    Student[] student= new Student[5];

    student[0].setStudent("Marco", 20);
    student[1].setStudent("Giacomo", 19);
    student[2].setStudent("Susan", 24);
    student[3].setStudent("Billie", 25);
    student[4].setStudent("Max", 18);


   }

 }


 }

And the other one, which consists of the method "Ordinator", that uses the bubble sort to put in order the "Students" based on the age, from the younger to the older:

public class Ordinator {
    public static void order(Comparable[]list){
        int imin;
        for(int ord=0; ord<list.length-1;ord++){
            imin=ord;
            for(int i=ord +1; i<list.length; i++){
                if(list[i].youngerThan(list[imin]));
                  Comparable temp=list[i];
                  list[i]=list[imin];
                  list[imin]=temp;
            }
        }
    }
}

Now I have to test the method order with Junit to see if it works. But I don't know how to test an array of objects, in particular in this situation. Hope someone can help! Thanks in advance :)

4
  • take a look at assertj there are a lot of useful utils for test Commented Apr 20, 2017 at 17:59
  • Hi, and welcome to StackOverflow! You're basically asking for a tutorial, which is not appropriate for this site. There are lots of great tutorials available all over the web. Please see stackoverflow.com/help/how-to-ask for help asking good questions. If you find a tutorial, and are still stuck, post your code, explain your problem, and people will be happy to help. Commented Apr 20, 2017 at 18:01
  • You should decide on some simple input and output. Put those arrays (the known correct input and output) in your test case. Then call your method and use a JUnit assert like assertArraysEqual() to verify the method produced the correct output. Commented Apr 20, 2017 at 18:03
  • If you don't know how to use JUnit, this might help. Commented Apr 20, 2017 at 18:05

1 Answer 1

1

You just iterate your list, making individual comparisons.

for (int i=1; i<list.length; i++) {
  assertTrue(list[i].compareTo(list[i-1]) >= 0);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.