Homework Clinic

Science Clinic => Computer Science => Topic started by: armygirl on May 6, 2020

Title: What is the output of the following code?import java.util.*;public class Test { public static void ...
Post by: armygirl on May 6, 2020
What is the output of the following code?

import java.util.*;

public class Test {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Student("Peter", 65));
list.add(new Student("Jill", 50));
list.add(new Student("Sarah", 34));
Collections.sort(list);
System.out.print(list + " ");
Collections.sort(list, new StudentComparator1());
System.out.println(list);
}
 
static class StudentComparator1 implements Comparator {
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name);
}
}
 
static class Student implements Comparable {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}

public int compareTo(Student s) {
return this.age - s.age;
}

public String toString() {
return "[" + name + ", " + age + "]";
}
}
}

◦ [[Jill, 50], [Peter, 65], [Sarah, 34]] [[Jill, 50], [Peter, 65], [Sarah, 34]]
◦ [[Sarah, 34], [Jill, 50], [Peter, 65]] [[Jill, 50], [Peter, 65], [Sarah, 34]]
◦ [[Sarah, 34], [Jill, 50], [Peter, 65]] [[Sarah, 34], [Jill, 50], [Peter, 65]]
◦ [[Jill, 50], [Peter, 65], [Sarah, 34]] [[Sarah, 34], [Jill, 50], [Peter, 65]]
Title: What is the output of the following code?import java.util.*;public class Test { public static void ...
Post by: makaylafy on May 6, 2020
[[Sarah, 34], [Jill, 50], [Peter, 65]] [[Jill, 50], [Peter, 65], [Sarah, 34]]
Title: Re: What is the output of the following code?import java.util.*;public class Test { public static vo
Post by: Anthony Gonzalez on Dec 16, 2020
Thank you