Advertisement
wpinda

Sorting

Feb 11th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. public class Person implements Comparable<Person>{
  2.  
  3.     private int salary;
  4.     private String name;
  5.  
  6.     public Person(String name, int salary) {
  7.         this.name = name;
  8.         this.salary = salary;
  9.     }
  10.  
  11.     public String getName() {
  12.         return name;
  13.     }
  14.  
  15.     public int getSalary() {
  16.         return salary;
  17.     }
  18.    
  19.    
  20.     @Override
  21.     public int compareTo(Person person) {
  22.         return this.salary - person.getSalary();
  23.     }
  24.    
  25.    
  26.     @Override
  27.     public String toString() {
  28.         return name + " " + salary;
  29.     }
  30. }
  31.  
  32.  
  33.  
  34.  
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Collections;
  38.  
  39. public class Main {
  40.  
  41.     public static void main(String[] args) {
  42.         List<Person> people = new ArrayList<Person>();
  43.         people.add(new Person("Matti", 150000));
  44.         people.add(new Person("Pekka", 3000));
  45.         people.add(new Person("Mikko", 300));
  46.         people.add(new Person("Arto", 10));
  47.         people.add(new Person("Merja", 500));
  48.         people.add(new Person("Pertti", 80));
  49.  
  50.         System.out.println(people);
  51.  
  52.         /*
  53.          * When you have implemented the compareTo-method, remove comment below.
  54.          */
  55.        
  56.         Collections.sort(people);
  57.        
  58.         System.out.println(people);
  59.     }
  60. }
  61.  
  62.  
  63. // Error
  64. warning: [options] bootstrap class path not set in conjunction with -source 1.6
  65. C:\Users\Laptop\Documents\NetBeansProjects\2013-OOProgrammingWithJava-PART2\week8-week8_13.RichFirstPoorLast\src\Person.java:2: error: type Comparable does not take parameters
  66. public class Person implements Comparable<Person>{
  67. C:\Users\Laptop\Documents\NetBeansProjects\2013-OOProgrammingWithJava-PART2\week8-week8_13.RichFirstPoorLast\src\Main.java:23: error: no suitable method found for sort(List<Person>)
  68.         Collections.sort(people);
  69.                    ^
  70.     method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
  71.       (cannot instantiate from arguments because actual and formal argument lists differ in length)
  72.     method Collections.<T#2>sort(List<T#2>) is not applicable
  73.       (inferred type does not conform to declared bound(s)
  74.         inferred: Person
  75.         bound(s): Comparable<? super Person>)
  76.   where T#1,T#2 are type-variables:
  77.     T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
  78.     T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
  79. C:\Users\Laptop\Documents\NetBeansProjects\2013-OOProgrammingWithJava-PART2\week8-week8_13.RichFirstPoorLast\src\Person.java:21: error: method does not override or implement a method from a supertype
  80.     @Override
  81.     ^
  82. 3 errors
  83. 1 warning
  84. C:\Users\Laptop\Documents\NetBeansProjects\2013-OOProgrammingWithJava-PART2\week8-week8_13.RichFirstPoorLast\nbproject\build-impl.xml:603: The following error occurred while executing this line:
  85. C:\Users\Laptop\Documents\NetBeansProjects\2013-OOProgrammingWithJava-PART2\week8-week8_13.RichFirstPoorLast\nbproject\build-impl.xml:245: Compile failed; see the compiler error output for details.
  86. BUILD FAILED (total time: 0 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement