Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person implements Comparable<Person>{
- private int salary;
- private String name;
- public Person(String name, int salary) {
- this.name = name;
- this.salary = salary;
- }
- public String getName() {
- return name;
- }
- public int getSalary() {
- return salary;
- }
- @Override
- public int compareTo(Person person) {
- return this.salary - person.getSalary();
- }
- @Override
- public String toString() {
- return name + " " + salary;
- }
- }
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Collections;
- public class Main {
- public static void main(String[] args) {
- List<Person> people = new ArrayList<Person>();
- people.add(new Person("Matti", 150000));
- people.add(new Person("Pekka", 3000));
- people.add(new Person("Mikko", 300));
- people.add(new Person("Arto", 10));
- people.add(new Person("Merja", 500));
- people.add(new Person("Pertti", 80));
- System.out.println(people);
- /*
- * When you have implemented the compareTo-method, remove comment below.
- */
- Collections.sort(people);
- System.out.println(people);
- }
- }
- // Error
- warning: [options] bootstrap class path not set in conjunction with -source 1.6
- C:\Users\Laptop\Documents\NetBeansProjects\2013-OOProgrammingWithJava-PART2\week8-week8_13.RichFirstPoorLast\src\Person.java:2: error: type Comparable does not take parameters
- public class Person implements Comparable<Person>{
- 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>)
- Collections.sort(people);
- ^
- method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
- (cannot instantiate from arguments because actual and formal argument lists differ in length)
- method Collections.<T#2>sort(List<T#2>) is not applicable
- (inferred type does not conform to declared bound(s)
- inferred: Person
- bound(s): Comparable<? super Person>)
- where T#1,T#2 are type-variables:
- T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
- T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
- 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
- @Override
- ^
- 3 errors
- 1 warning
- 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:
- 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.
- BUILD FAILED (total time: 0 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement