2015-06-04 79 views
-3

我有一個練習面向對象。 定義具有名字和姓氏的抽象類人類。定義從人類衍生而來的新班級學生,並具有新的現場級別。使用新屬性WeekSalary和WorkHoursPerDay和方法MoneyPerHour()定義從Human派生的類Worker,該方法返回工作人員按小時獲得的資金。定義此層次結構的正確構造函數和屬性。初始化一個10名學生的列表,並按升序對他們進行排序。初始化一個10名工人的清單,並按照每小時按降序排序。合併列表並按名字和姓氏排序。排序列表學生按等級

我已經創建了類:人類,學生和工作者。現在我想按年級對學生進行排序。我應該在java中編碼?

package Exercise2; 

import java.util.*; 

public class Main { 

    public static void main(String[] args) { 

     List<Student> students = Arrays.asList(
       new Student("Tam","Le Trung Ngoc ", "2011"), 
       new Student("Thai","Le Hoang Thai ", "2012"), 
       new Student("Phuong", "Vu Thi Thu ","2011"), 
       new Student("Nguyen", "Tran ", "2013"), 
       new Student("Hoang", "Pham Minh ", "2009"), 
       new Student("Tri","Truong Hoang ", "2011"), 
       new Student("Tuan", "Nguyen Anh ","2015"), 
       new Student("Tai", "Le Minh ", "2015"), 
       new Student("Cuong", "Nguyen Manh ", "2012"), 
       new Student("An","Pham Phuoc ", "2011")); 
     System.out.println("------------STUDENTS------------"); 
     System.out.println(students); 

     System.out.println("------------WORKERS------------"); 

     List<Worker> workers = Arrays.asList(
       new Worker("An","Le Minh ",150,8), 
       new Worker("Hai","Nguyen Thanh ",200,4), 
       new Worker("Lan","Nguyen Thi ",350,12), 
       new Worker("A","Nguyen Van ",150,5), 
       new Worker("Lua","Tran Thi Kim ",150,4), 
       new Worker("Tu","Nguyen Thanh ",125,6), 
       new Worker("Phong","Le Trung ",375,8), 
       new Worker("Trang","Nguyen Thi Yen ",500,8), 
       new Worker("Thien","Le Ngoc ",1500,8), 
       new Worker("Vu","Le ",150,8) 
       ); 
     System.out.println(workers); 
    } 

} 
+5

您應該先嚐試編碼。如果它不符合你的期望,請發佈該代碼,然後我們可以提供幫助。 – Shar1er80

+2

Google「Java排序列表」。進行嘗試或10.如果您有更多問題,請回來。 – Ordous

回答

0

使用排序集合時Comparator

Collections.sort(students, (Student o1, Student o2) -> { 
    return o1.getGrade().compareTo(o2.getGrade()); // Tune this to your needs 
}); 

如果你沒有使用Java 8則必須使用(老式)new Comparator代替lambda表達式。

0

爲了不爲您解決功課,我不會發布代碼。但是你需要看看你將如何重載這些不同的人類子類中的compare()方法。這種方式取決於比較什麼,你可以按姓名,年級或薪水對他們進行分類。

+0

此外,您的列表需要是Human類型,因此您可以根據其父類合併兩個列表(列表) – James