标签:要求 属性 string tables stream 生成 jta taf 推荐
hashset
自动去重package ex0101;
//inner implementation
public class Student implements Comparable<Student> {
//variables
private String id;
private String name;
private String gender;
private String dept;
private int score;
//constructors
public Student() {}
public Student(String id,String name,String gender,String dept,int score) {
this.id=id;
this.name=name;
this.gender=gender;
this.dept=dept;
this.score=score;
}
//methods-getter setter
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender=gender;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept=dept;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score=score;
}
//methods-show info
public void showInfo() {
System.out.print("id:"+id+"\t");
System.out.print("name:"+name+"\t");
// System.out.print("gender:"+gender+"\t");
System.out.print("dept:"+dept+"\t");
// System.out.print("score:"+score+"\t");
System.out.println();
}
//methods-cmp
@Override
public int compareTo(Student o) {
return this.score-o.score;
}
}
package ex0101;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
public class StudentUtil {
public void getDataFromFile() throws FileNotFoundException {
//variables
//methods-input data
File f=new File("D:\\mesrc\\java_vscode\\file\\javascore.txt");
Scanner scanner=new Scanner(f);
ArrayList<Student> ar=new ArrayList<Student>();
while(scanner.hasNext()) {
Student Student=new Student(scanner.next(),scanner.next(),scanner.next(),scanner.next(),scanner.nextInt());
ar.add(Student);
}
scanner.close();
//sort data
sortInteger(ar);
//output data
printInfo(ar);
analyseRandom(ar);
}
public void sortInteger(ArrayList<Student> ar){
//inner sorting
Collections.sort(ar);
}
public void printInfo(ArrayList<Student> ar){
//1
//按照成绩进行排序
//按照分数段显示学生的姓名、学号、学院、如下分数段人数汇总
//并输出相关信息
System.out.println("<1> 不同分数段的学生信息:");
//fliter
System.out.println("-->优秀:");
ar.stream()
.filter((Student s)->s.getScore()>=85)
.forEach(u->u.showInfo());
System.out.println("-->良好:");
ar.stream()
.filter((Student s)->s.getScore()>=80&&s.getScore()<85)
.forEach(u->u.showInfo());
System.out.println("-->及格:");
ar.stream()
.filter((Student s)->s.getScore()>=70&&s.getScore()<80)
.forEach(u->u.showInfo());
System.out.println("-->不及格:");
ar.stream()
.filter((Student s)->s.getScore()<70)
.forEach(u->u.showInfo());
System.out.println();
}
public void analyseRandom(ArrayList<Student> ar){
// get random num with HashSet
Random r=new Random();
HashSet<Integer> hs=new HashSet<>();
while(hs.size()<10){
hs.add(r.nextInt(ar.size())+1);
}
double a_num=0;
double b_num=0;
double c_num=0;
double cnt_IE=0;
double a_num_IE=0;
double b_num_IE=0;
double c_num_IE=0;
for (Integer i:hs) {
int thisScore=ar.get(i).getScore();
if(thisScore>=85) a_num++;
else if(thisScore>=80) b_num++;
else if(thisScore>=70) c_num++;
if(ar.get(i).getDept().equals("机电学院")){
cnt_IE++;
if(thisScore>=85) a_num_IE++;
else if(thisScore>=80) b_num_IE++;
else if(thisScore>=70) c_num_IE++;
}
}
//2
//随机抽查10份不同的成绩样本数据
//进行及格率、良好率、优秀率的统计输出显示
System.out.println("<2> 随机抽查10份不同的成绩样本数据中:");
System.out.println("-->优秀率:"+100*a_num/10.0+"%");
System.out.println("-->良好率:"+100*b_num/10.0+"%");
System.out.println("-->及格率:"+100*c_num/10.0+"%");
System.out.println();
//3
//随机抽查10份不同的成绩样本数据
//所有机电学院的学生的人数和分段信息
System.out.println("<3> 随机抽查10份不同的成绩样本数据中:");
System.out.println("-->机电学院的学生人数:"+(int)cnt_IE);
System.out.println("-->机电学院的学生优秀率:"+100*a_num_IE/cnt_IE+"%");
System.out.println("-->机电学院的学生良好率:"+100*b_num_IE/cnt_IE+"%");
System.out.println("-->机电学院的学生及格率:"+100*c_num_IE/cnt_IE+"%");
//Collections.sort() 实现
// long cnt_IE=ar.stream()
// .filter((Student s)->s.getDept().equals("机电学院"))
// .count();
}
}
package ex0101;
import java.io.FileNotFoundException;
public class TestComparable {
public static void main(String[] args) throws FileNotFoundException {
StudentUtil su=new StudentUtil();
su.getDataFromFile();
}
}
package ex0102;
import java.text.Collator;
//inner implementation
public class Student implements Comparable<Student> {
//variables
private String id;
private String name;
private String gender;
private String dept;
private int score;
//constructors
public Student() {}
public Student(String id,String name,String gender,String dept,int score ) {
this.id=id;
this.name=name;
this.gender=gender;
this.dept=dept;
this.score=score;
}
//methods-getter setter
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender=gender;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept=dept;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score=score;
}
//methods-show info
public void showInfo() {
System.out.print("id:"+id+"\t");
System.out.print("name:"+name+"\t");
System.out.print("gender:"+gender+"\t");
System.out.print("dept:"+dept+"\t");
System.out.print("score:"+score+"\t");
System.out.println();
}
//methods-cmp
@Override
public int compareTo(Student o) {
Collator cmp=Collator.getInstance(java.util.Locale.CHINA);
int compare=cmp.compare(this.name,o.name);
return compare;
}
}
package ex0102;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class StudentUtil {
public void getDataFromFile() throws FileNotFoundException {
//variables
//methods-input data
File f=new File("D:\\mesrc\\java_vscode\\file\\javascore.txt");
Scanner scanner=new Scanner(f);
ArrayList<Student> ar=new ArrayList<Student>();
while(scanner.hasNext()) {
Student student=new Student(scanner.next(),scanner.next(),scanner.next(),scanner.next(),scanner.nextInt());
ar.add(student);
}
scanner.close();
//sort data
sortCharacter(ar);
//output data
printInfo(ar);
analyseIE(ar);
}
public void sortCharacter(ArrayList<Student> ar){
//inner sorting
Collections.sort(ar);
}
public void printInfo(ArrayList<Student> ar){
//print first ten students‘s info
//Ques: how to print only 10 by Lambda?
System.out.println("Students in all departments,first ten students‘s info:");
int cnt=0;
for(Student s:ar) {
s.showInfo();
cnt++;
if(cnt>9) break;
}
System.out.println();
}
public void analyseIE(ArrayList<Student> ar){
//print IE Department‘s info
//filter
System.out.println("Students In IE department:");
String d=new String("机电学院");
ar.stream().filter((Student s)->s.getDept().equals(d)).forEach(u->u.showInfo());
System.out.println();
int cnt_male=0;
int cnt_female=0;
String g=new String("男");
double sum_score=0;
int max_score=0;
int min_score=100;
for(Student s:ar) {
if(s.getDept().equals(d)){
//nums female/male
if(s.getGender().equals(g)) cnt_male++;
else cnt_female++;
//average score
sum_score+=s.getScore();
//max/min score
if(min_score>s.getScore()) min_score=s.getScore();
if(max_score<s.getScore()) max_score=s.getScore();
}
}
System.out.println("In IE department:");
System.out.println(cnt_male+" students are male.");
System.out.println(cnt_female+" students are female.");
System.out.print("Average Score:\t");
System.out.println(String.format("%.2f", sum_score/(double)(cnt_male+cnt_female)));
System.out.println("Max Score:\t"+max_score);
System.out.println("Min Score:\t"+min_score);
}
}
package ex0102;
import java.io.FileNotFoundException;
public class TestComparable {
public static void main(String[] args) throws FileNotFoundException {
StudentUtil su=new StudentUtil();
su.getDataFromFile();
}
}
标签:要求 属性 string tables stream 生成 jta taf 推荐
原文地址:https://www.cnblogs.com/lunarwhite/p/13904289.html