java:定义学生类Student, 其中属性有 name, id, score ,分别表示姓名,学好,成绩.....
构造方法 : Student(String n, String i, int s);
成员方法: int getScore();
main函数中创建5个Student的对象,输入姓名学号成绩,并输出他们的平均成绩,最高分和最低分。
本人是初学者,望大神给出代码,最好有注释,谢了。 展开
定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
(4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。*/
class student{
String sNO,sName,sSex;
int sAge;
double sJava;
public student(String a,String b,String c,int d ,double e){
sNO=a;
sName=b;
sSex=c;
sAge=d;
sJava=e;}
String getNo(){
return sNO;}
String getName(){
return sName;}
String getSex(){
return sSex;}
int getAge(){
return sAge;}
double getJava(){
return sJava;}
void show(){
System.out.println("姓名:"+getName());
System.out.println("学号:"+getNo());
System.out.println("性别:"+getSex());
System.out.println("年龄:"+getAge());
System.out.println("Java成绩:"+getJava());}
}
public class chengji{
public static void main(String[] args){
double avrage,K1,K2,K3,K4,K5;
student A1=new student("13190310","李一","男",18,80);
student A2=new student("13190311","刘二","女",19,83);
student A3=new student("13190312","张三","男",19,82);
student A4=new student("13190313","杨四","女",20,88);
student A5=new student("13190314","韩舞","女",20,90);
A1.show();
System.out.println();
A2.show();
System.out.println();
A3.show();
System.out.println();
A4.show();
System.out.println();
A5.show();
System.out.println();
K1=A1.getJava();
K2=A2.getJava();
K3=A3.getJava();
K4=A4.getJava();
K5=A5.getJava();
avrage=(K1+K2+K4+K3+K5)/5;
System.out.println("Java平均成绩:"+avrage);
double a[]={K1,K2,K3,K4,K5};
double max=0,min=0,ck;
for(int i=0;i<4;i++){
max=K1;
if(max<a[i+1]){
ck=max;
max=a[i+1];
a[i+1]=ck;} }
System.out.println("Java的最高分为:"+max);
for(int i=0;i<4;i++){
max=K1;
if(min>a[i+1]){
ck=min;
min=a[i+1];
a[i+1]=ck;}
}
System.out.println("Java的最低分为:"+max);
}
}
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author xlhu
*
*/
public class Zhidao {
public static void main(String[] args) {
Students s1 = new Students();
s1.setId(1);
s1.setName("aaa");
s1.setScore(80.5);
Students s2 = new Students();
s2.setId(2);
s2.setName("bbb");
s2.setScore(70.5);
Students s3 = new Students();
s3.setId(3);
s3.setName("ccc");
s3.setScore(60.5);
Students s4 = new Students();
s4.setId(4);
s4.setName("ddd");
s4.setScore(90);
Students s5 = new Students();
s5.setId(5);
s5.setName("eee");
s5.setScore(100);
List<Students> list = new ArrayList<Students>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
Map<String,Double> map = getScote(list);
System.out.println("最高分:"+map.get("max"));
System.out.println("最低分:"+map.get("min"));
System.out.println("平均分:"+map.get("pingjun"));
}
public static Map<String,Double> getScote(List<Students> list){
Map<String,Double> map = new HashMap<String, Double>();
double sco=0,max=0,min= 100,sum=0;
for (Students students : list) {
sco = students.getScore();
sum = sum+sco;
if(max<sco){
max = sco;
}
if(min>sco){
min = sco;
}
}
double pingjun = 0;
pingjun = sum/(double)list.size();
map.put("max", max);
map.put("min", min);
map.put("pingjun", pingjun);
return map;
}
}
class Students{
private String name;
private int id;
private double score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
输出:
最高分:100.0
最低分:60.5
平均分:80.3
import java.util.Arrays;
public class nnn {
public static void main(String[] args) {
// 创建5个Student对象
Student s1 = new Student("name1", "10001", 88);
Student s2 = new Student("name2", "10002", 98);
Student s3 = new Student("name3", "10003", 98);
Student s4 = new Student("name4", "10004", 77);
Student s5 = new Student("name5", "10005", 87);
// 平均成绩
double avgScore = (s1.getScore() + s2.getScore() + s3.getScore() + s4.getScore() + s5.getScore()) / 5;
// 创建一个一维数组,共5个元素,用来保存上面创建的5个Studnet对象。
// 为什么要用数组呢?因为要使用Arrays的sort()方法来对student排序,这样就方便计算最高分和最低分。
Student[] ss = new Student[5];
ss[0] = s1;
ss[1] = s2;
ss[2] = s3;
ss[3] = s4;
ss[4] = s5;
// 按照分数从低到高排序
Arrays.sort(ss);
// 输出平均成绩
System.out.println(avgScore);
// 输出最低分和最高分。
System.out.println(ss[0].getScore() + "," + ss[4].getScore());
}
}
// 要实现Comparable接口,这样在调用Arrays.sort()方法时,Arrays才知道按照什么规则比较
class Student implements Comparable<Object>{
private String name;
private String id;
private double score;
public Student() {
}
public Student(String name,String id,double score) {
setName(name);
setId(id);
setScore(score);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", score=" + score
+ "]";
}
// 重写Comparable接口的compareTo方法,在该方法中根据分数来比较。
// 如果要比较的对象的分数比当前分数低则返回1;相等返回0;要比较的对象的分数大于当前分数则返回-1
@Override
public int compareTo(Object o) {
Student s = (Student) o;
if (getScore() > s.getScore())
return 1;
if (getScore() == s.getScore())
return 0;
return -1;
}
}
public class Student {
private int id;
private String name;
private int score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
Student(int id,String name,int score){
this.id = id;
this.name = name;
this.score = score;
}
public static void main(String[] args) {
//长度为5的数组。存放学生
Student ss[] = new Student[5];
//学生对象
Student s1 = new Student(1, "zhang san", 69);
Student s2 = new Student(2, "li si", 59);
Student s3 = new Student(3, "wang wu", 99);
Student s4 = new Student(4, "adf san", 29);
Student s5 = new Student(5, "1111111", 76);
//学生对象放入数组中
ss[0] = s1;
ss[1] = s2;
ss[2] = s3;
ss[3] = s4;
ss[4] = s5;
//求总分数
int sum = 0;
for (Student s : ss) {
sum += s.getScore();
}
System.out.println("平均分为:"+sum/5);
}
}