[英]Sort List Student by Grade
I have a exercise OOP.我有一个练习 OOP。 Define abstract class Human with first name and last name.
使用名字和姓氏定义抽象类 Human。 Define new class Student which is derived from Human and has new field – grade.
定义派生自 Human 并具有新领域 - 等级的新类 Student。 Define class Worker derived from Human with new property WeekSalary and WorkHoursPerDay and method MoneyPerHour() that returns money earned by hour by the worker.
使用新属性 WeekSalary 和 WorkHoursPerDay 以及方法 MoneyPerHour() 定义从 Human 派生的 Worker 类,该方法返回工人按小时赚取的收入。 Define the proper constructors and properties for this hierarchy.
为此层次结构定义适当的构造函数和属性。 Initialize a list of 10 students and sort them by grade in ascending order.
初始化一个包含 10 名学生的列表,并按年级升序对他们进行排序。 Initialize a list of 10 workers and sort them by money per hour in descending order.
初始化一个包含 10 个工人的列表,并按每小时的钱数以降序对他们进行排序。 Merge the lists and sort them by first name and last name.
合并列表并按名字和姓氏对它们进行排序。
I have create classes: Human, Student, and Worker.我创建了类:Human、Student 和 Worker。 Now I want to sort list Student by Grade.
现在我想按年级对学生列表进行排序。 What should I code in java ?
我应该在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);
}
}
In an effort not to solve your homework for you I'm not going to post the code.为了不为您解决作业,我不会发布代码。 but you need to look at how you will overload the compare() method in these different subclasses of human.
但是您需要查看如何在人类的这些不同子类中重载 compare() 方法。 That way depending on what is being compared you can sort them on name, grade, or salary.
这样,根据比较的内容,您可以按姓名、等级或薪水对它们进行排序。
Use a Comparator when sorting the collection:排序集合时使用比较器:
Collections.sort(students, (Student o1, Student o2) -> {
return o1.getGrade().compareTo(o2.getGrade()); // Tune this to your needs
});
If you are not using Java 8 you must use the (old-fashioned) new Comparator
instead of the lambda expression.如果您不使用 Java 8,则必须使用(老式)
new Comparator
而不是 lambda 表达式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2DArrays
{
class Program
{
static void Main(string[] args)
{
//try
//{
int p;
do
{
Console.WriteLine("Enter Student number");
int s = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Course number");
int c = Convert.ToInt32(Console.ReadLine());
string[] name = new string[s];
double[] GPA = new double[s];
string[] course = new string[c];
int[] Chr = new int[c];
double[,] score = new double[s, c];
double[,] gradevalue = new double[s, c];
string[,] grade = new string[s, c];
double[] copyGPA = new double[c];
for (int i = 0; i < c; i++)
{
Console.WriteLine("Enter " + (i + 1) + " course");
course[i] = Console.ReadLine();
Console.WriteLine("Enter " + course[i] + " course credit hour");
Chr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Clear();
for (int i = 0; i < s; i++)
{
Console.WriteLine("Enter " + (i + 1) + " student name");
name[i] = Console.ReadLine();
double g = 0;
int ch = 0;
for (int j = 0; j < c; j++)
{
Console.WriteLine("Enter " + course[j] + " course score for " + name[i]);
score[i, j] = Convert.ToDouble(Console.ReadLine());
if (score[i, j] >= 85)
{
grade[i, j] = "A";
gradevalue[i, j] = 4;
}
else if (score[i, j] >= 70)
{
grade[i, j] = "B";
gradevalue[i, j] = 3;
}
else if (score[i, j] >= 60)
{
grade[i, j] = "C";
gradevalue[i, j] = 2;
}
else
{
grade[i, j] = "D";
gradevalue[i, j] = 1;
}
ch = ch + Chr[j];
g = g + (gradevalue[i, j] * Chr[j]);
}
GPA[i] = g / ch;
}
//GPA=(copyGPA, 0);
//Array.Sort();
Console.Clear();
for (int i = 0; i < s; i++)
{
Console.WriteLine("\t\t\t\tSTUDENT'S GRADE REPORT SHEET @ 2020");
Console.WriteLine("________________________________STUDENT NAME: " + name[i] +"________________________");
Console.WriteLine("COURSE\t\tCHOUR\t\tSCORE\t\tGRADE\t\tGVALUE");
for (int j = 0; j < c; j++)
{
Console.WriteLine(course[j] + "\t\t" + Chr[j] + "\t\t" + score[i, j] + "\t\t" + grade[i, j] + "\t\t" + gradevalue[i, j]);
}
// Console.WriteLine("GPA:" + GPA[i]);
Console.WriteLine("________________________________GPA: " + GPA[i] +"________________________________\n\n");
}
Console.WriteLine("IF YOU WANT TO TRY AGAIN PRESS 1");
p = Convert.ToInt32(Console.ReadLine());
Console.Clear();
} while (p == 1);
Console.ReadKey();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.