public class Student {
private String name;
private int id;
private double englishScore;
private double chineseScore;
private double mathScore;
public Student(String name, int id, double englishScore, double chineseScore, double mathScore) {
super();
thisname = name;
thisid = id;
thisenglishScore = englishScore;
thischineseScore = chineseScore;
thismathScore = mathScore;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
thisenglishScore = englishScore;
}
public double getChineseScore() {
return chineseScore;
}
public void setChineseScore(double chineseScore) {
thischineseScore = chineseScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
thismathScore = mathScore;
}
public double getsumScore(){
return englishScore+chineseScore+mathScore;
}
}import javautilScanner;
public class StudentDemo {
private Student[] datas;
public void inputData(){
Scanner scanner = new Scanner(Systemin);
int studentNum = scannernextInt();
datas =new Student[studentNum];
for (int i = 0; i < dataslength; i++) {
Systemoutprintln("请输入学生学号:");
int id=scannernextInt();
Systemoutprintln("请输入学生姓名:");
String name = scannernext();
Systemoutprintln("请输入学生英语成绩:");
double englishScore=scannernextDouble();
Systemoutprintln("请输入学生数学成绩:");
double mathScore=scannernextDouble();
Systemoutprintln("请输入语文成绩:");
double chineseScore=scannernextDouble();
Student student = new Student(name, id, englishScore, chineseScore, mathScore);
datas[i]=student;
}
}
public String getScores(){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < dataslength; i++) {
sbappend("学生"+datas[i]getName()+"的总分是:"+datas[i]getsumScore()+",");
}
return sbtoString();
}
public double getAllAvgScore(){
int len = dataslength;
double sum=0;
for (int i = 0; i < dataslength; i++) {
sum +=datas[i]getsumScore();
}
double avg=sum/len;
return avg;
}
public double[] getEachavgScore(){
int len=dataslength;
double chinessum=0;
double mathsum=0;
double englishsum=0;
for (int i = 0; i < dataslength; i++) {
chinessum+=datas[i]getChineseScore();
mathsum+=datas[i]getMathScore();
englishsum+=datas[i]getEnglishScore();
}
double chinAvg=chinessum/len;
double mathAvg=mathsum/len;
double englishAvg=englishsum/len;
double[] avgs ={chinAvg,mathAvg,englishAvg};
return avgs;
}
}public class Test {
public static void main(String[] args) {
StudentDemo demo = new StudentDemo();
demoinputData();
String scores = demogetScores();
Systemoutprintln(scores);
double[] eachavgScore = demogetEachavgScore();
Systemoutprintln("语文的平均总分是:"+eachavgScore[0]);
Systemoutprintln("数学的平均总分是:"+eachavgScore[1]);
Systemoutprintln("英语的平均总分是:"+eachavgScore[2]);
double allAvgScore = demogetAllAvgScore();
Systemoutprintln("全班的平均总分是"+allAvgScore);
}
}结果:
请输入学生个数:
3
请输入学生学号:
0001
请输入学生姓名:
李晓明
请输入学生英语成绩:
78
请输入学生数学成绩:
88
请输入语文成绩:
69
请输入学生学号:
0002
请输入学生姓名:
张华
请输入学生英语成绩:
97
请输入学生数学成绩:
88
请输入语文成绩:
86
请输入学生学号:
0003
请输入学生姓名:
余小慧
请输入学生英语成绩:
78
请输入学生数学成绩:
98
请输入语文成绩:
65
学生晓明的总分是:2350,学生张华的总分是:2710,学生余小慧的总分是:2410,
语文的平均总分是:7333333333333333
数学的平均总分是:9133333333333333
英语的平均总分是:8433333333333333
全班的平均总分是2490
abstract class 员工{
private String 姓名;
private String 工号;
private double 工资;
private String 奖金属性;
public 员工(String 姓名, String 工号, double 工资, String 奖金属性){
this姓名 = 姓名;
this工号 = 工号;
this工资 = 工资;
this奖金属性 = 奖金属性;
}
//生成上面的set get方法。
}
class 张三 extends 员工{
public 张三(String 姓名, String 工号, double 工资, String 奖金属性) {
super(姓名, 工号, 工资, 奖金属性);
}
}
class 经理 extends 员工{
public 经理(String 姓名, String 工号, double 工资, String 奖金属性) {
super(姓名, 工号, 工资, 奖金属性);
}
}
/
测试类
/
public class Util{
public static void main(String[] args){
//多态的理解思路
员工 zhang = new 张三("张三","001",20001,"年终奖");
}
}
public class Test {
public static void main(String[] args) {
Teacher teacher = new Teacher("Wang Laoshi", false, 19910123, "qinghua", 3);
Systemoutprintln(teachertoString());
CollegeStudent collStu = new CollegeStudent("College Student", true, 19851231, "Beijingdaxue","001", 2, "Computer", 3);
Systemoutprintln(collStutoString());
MiddleSchStudent middStu = new MiddleSchStudent("Middle schoole student", false, 19910705, "Di yi zhongxue", "002", 17, "English", 5);
Systemoutprintln(middStutoString());
}
}
abstract class Person {
protected String name;
protected boolean sex;
protected int birthday;
public Person(){}
public Person(String name, boolean sex, int birthday){
thisname =name;
thissex = sex;
thisbirthday = birthday;
}
public String toString(){
return "name:" + name + ", sex:" + sex + ", birthday: " + birthday;
}
}
class Teacher extends Person{
private String school;
private int schoolType;
public Teacher(){
}
public Teacher(String name, boolean sex, int birthday, String school, int schoolType){
super(name, sex, birthday);
thisschool = school;
thisschoolType = schoolType;
}
public String toString(){
return supertoString() + ", school: " + school + ", school type: " + schoolType;
}
}
abstract class Student extends Person{
protected String school;
protected String no;
protected int grade;
public Student(){
}
public Student(String name, boolean sex, int birthday, String school, String no, int grade){
super(name, sex, birthday);
thisschool = school;
thisno = no;
thisgrade = grade;
}
public String toString(){
return supertoString() + ", school:" + school + ", student no: " + no + ", studeng grade: " + grade;
}
}
class CollegeStudent extends Student{
private String major;
private int grade;
public CollegeStudent(){
}
public CollegeStudent(String name, boolean sex, int birthday, String school, String no, int grade, String major, int colledgeGrade){
super(name, sex, birthday,school, no,grade);
thismajor = major;
thisgrade = colledgeGrade;
}
public String toString(){
return supertoString() + ", major:" + major + ", grade: " + grade;
}
}
class MiddleSchStudent extends Student{
private int grade;
public MiddleSchStudent(){
}
public MiddleSchStudent(String name, boolean sex, int birthday, String school, String no, int grade, String major, int colledgeGrade){
super(name, sex, birthday,school, no,grade);
thisgrade = colledgeGrade;
}
public String toString(){
return supertoString() + ", grade: " + grade;
}
}
---------------测试
name:Wang Laoshi, sex:false, birthday: 19910123, school: qinghua, school type: 3
name:College Student, sex:true, birthday: 19851231, school:Beijingdaxue, student no: 001, studeng grade: 2, major:Computer, grade: 3
name:Middle schoole student, sex:false, birthday: 19910705, school:Di yi zhongxue, student no: 002, studeng grade: 17, grade: 5
欢迎分享,转载请注明来源:浪漫分享网
评论列表(0条)