java编程题,求高手帮忙.
//定义一个学生类,维护学生的语文,数学,英语三门课的成绩.要求:至少两个构造方法,
//能够计算总分和平均分.使用getters,setters方法.通过主函数创建对象,调用功能.
class Student{
public String name;
private double yuwen;
private double shuxue;
private double yingyu;
private double pingjun;
private double zongfen;
public Student(String name){//第一个构造方法
this.name=name;
System.out.println("学生的姓名为:"+name);
}
public Student(double a,double b,double c){//第二个构造方法
this.yuwen=a;
this.shuxue=b;
this.yingyu=c;
this.zongfen=a+b+c;
this.pingjun=(a+b+c)/3;
}
public String getName(){
return name;}
public double getYuwen(){
return yuwen;}
public double getShuxue(){
return shuxue;}
public double getYingyu(){
return yingyu;}
public double getPingjun(){
return pingjun;}
public double getZongfen(){
return zongfen;}
public void setName(String name){
this.name=name;
}
public void setYuwen(double yuwen){
this.yuwen=yuwen;
}
public void setShuxue(double shuxue){
this.shuxue=shuxue;
}
public void setYingyu(double yingyu){
this.yingyu=yingyu;
}
public void setYuwen(double a,double b,double c){
this.pingjun=(a+b+c)/3;
}
public void setZongfen(double a,double b,double c){
this.zongfen=(a+b+c);
}
}
public class UseStudent{
public static void main(String[] args){
System.out.println("======================现在开始统计学生信息===================");
Student Student1=new Student("我爱你谢蛋蛋");
Student Student2=new Student(87,89,96);
//以下通过调用getter调用学生属性
System.out.println("语文成绩:"+Student2.getYuwen()+"\n"+"数学成绩:"+Student2.getShuxue()+"\n"+"英语成绩:"+Student2.getYingyu()+"\n"+"总分为:"+Student2.getZongfen()+"\n"+"平均分为:"+Student2.getPingjun());
//假设学生数学成绩有错需要该为99,以下通过setter改变学生成绩并且输出
System.out.println("=========由于该同学数学成绩输入有错,需要重新改为99分========");
Student2.setShuxue(99);
System.out.println("数学成绩改为"+Student2.getShuxue());
}
}//运行结果如图
public Student(){}
public Student(int chinese,int math,int english){
this.chinese=chinese;
this.math=math;
this.english=english;
}
private int chinese;
private int math;
private int english;
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public double getAvg(){
return getSum()/3.0;
}
public int getSum(){
return chinese+math+english;
}
public static void main(String[] args){
Student s1 =new Student();
s1.setChinese(95);
s1.setEnglish(85);
s1.setMath(90);
System.out.println("s1的总分为:"+s1.getSum()+" 平均分为:"+s1.getAvg());
Student s2=new Student(70,90,96);
System.out.println("s2的总分为:"+s2.getSum()+" 平均分为:"+s2.getAvg());
}
}
}
几个意思- -