我有锻炼OOP。 使用名字和姓氏定义抽象类Human。定义新类Student,该类源自Human并具有新的字段等级。使用新属性WeekSalary和WorkHoursPerDay以及方法MoneyPerHour()定义派生自Human的类Worker,返回工人按小时赚取的钱。为此层次结构定义正确的构造函数和属性。初始化10名学生的列表,并按年级按升序对其进行排序。初始化10名工人的清单,并按降序按每小时的金额对其进行分类。合并列表并按名字和姓氏对它们进行排序。
我创建了类:Human,Student和Worker。现在我想按等级对学生进行排序。我应该在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);
}
}
答案 0 :(得分:0)
为了不为你解决你的功课,我不打算发布代码。但是你需要看一下如何在人类的这些不同子类中重载compare()方法。这种方式取决于所比较的内容,您可以根据名称,等级或薪水对其进行排序。
答案 1 :(得分: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表达式。
答案 2 :(得分:0)
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();
}
}
}