java类设计的标准格式

题目:

         设计一个类表示学生的类,里面有学生的三项成绩:计算机成绩;数学成绩,英语成绩,要求可以求总分,平均分,最高,最低分,并要求能输出一个学生的完整信息,问:该类该如何设计?

       程序开发步骤:

       1.根据要求定义出所要的类

        2.根据题目中的要求定义出类的属性:name,age,computer,math,english

        3.所有的属性必须封装:private

        4.所有的属性必须通过getter以及setter访问

        5.如果需要增加构造方法,为属性赋值

        6.所有的信息不要再类中直接输出,而是交给调用处输出

           * 在类中不能出现System.out.println()语句

package 猪猪侠;
class Student {
private String name;
private int age;
private float computer;
private float math;
private float english;

public Student(String n,int a,float c,float m,float e){
	this.setName(n);
	this.setAge(a);
	this.setComputer(c);
	this.setMath(m);
	this.setEnglish(e);
}
public float sum(){
	return computer+math+english;
}
public float avg(){
	return this.sum()/3;
}
public float max(){
	float max=computer>math?math:computer;
	max=max>english?max:english;
	return max;
}
public float min(){
	float min=computer<math?math:computer;
	min=min<english?min:english;
	return min;
}
public String getInto(){
	return "学生信息:\n"+
            "\t姓名:"+this.getName()+"\n"+
			"\t年龄:"+this.getAge()+"\n"+
            "\t计算机成绩:"+this.getComputer()+"\n"+
			"\t数学成绩:"+this.getEnglish();
}
public void setName(String n){
	this.name=n;
}
public void setAge(int a){
	this.age=a;
}
public void setComputer(float c){
	this.computer=c;
}
public void setMath(float m){
	this.math=m;
}
public void setEnglish(float e){
	this.english=e;
}
public String getName(){
	return name;
}
public int getAge(){
	return age;
}
public float getComputer(){
	return computer;
}
public float getMath(){
	return math;
}
public float getEnglish(){
	return english;
}
}
public class Demo{
	public static void main(String[] args){
		Student stu=new Student("张思德",45,86.0f,83.0f,90.0f);
		stu.getInto();
		System.out.println("总分:"+stu.sum());
		System.out.println("平均分:"+stu.avg());
		System.out.println("最高分:"+stu.max());
		System.out.println("最低分:"+stu.min());
	}
}






猜你喜欢

转载自blog.csdn.net/baidu_39067385/article/details/76794320
今日推荐