简体   繁体   English

按年级排序列表学生

[英]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.

相关问题 根据他们的老师ID对学生列表进行排序 - Sort Student list based on their teacher id Java Program打印学生成绩 - Java Program print student Grade 输入不匹配异常错误学生成绩和姓名 - input mismatch exception error student grade and name Java学生和班级名册更新等级 - Java Student and Class Roster Update Grade 成绩计算器不会循环到第二名学生 - Grade Calculator Does Not Loop to Second Student SQL查询可根据学生的表现自动选择和汇总“成绩栏” - SQL query to automatically select and sum 'grade columns' based on student performance 学生班。 如何根据单位输入来打印等级? - Student Class. How to print Grade based off input of units? 如何获得我的学生成绩计算器程序中类似成绩字母的总数 - how can i get the total number of similar grade letters in my student grade calculator program 我需要创建一个程序来添加,删除,列出,保存和排序已创建的学生,本科和研究生班级中的学生 - I need to create a program that will add, remove, list, save, and sort students in a created Student, Undergrad, and Graduate classes 与学生一起创建一个文件,并查看从 5 分起平均成绩更高的学生的信息以及每个学生的平均成绩 - Create a file with students and view the information from the students that have more average grade from 5 and the average grade from each student
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM