首页 > 其他 > 详细

实例讲解——类设计分析

时间:2015-03-24 18:52:47      阅读:82      评论:0      收藏:0      [点我收藏+]

定义并测试一个名为Student的类,包括的属性有学号,姓名以及3门课程数学,英语和计算机的成绩,包括的方法有3门课程的总分,平均分,最高分及最低分。

class Student{
	private String stuno;
	private  String name;
	private  float math;
	private  float english;
	private float computer;
	public  Student(){}
	public Student(String stuno,String name,float math,float english,float computer){
		this.setStuno(stuno);
		this.setName(name);
		this.setMath(math);
		this.setEnglish(english);
		this.setComputer(computer);
		
	}
	public String getStuno() {
		return stuno;
	}
	public void setStuno(String s) {
		stuno = s;
	}
	public String getName() {
		return name;
	}
	public void setName(String n) {
		name = n;
	}
	public float getMath() {
		return math;
	}
	public void setMath(float m) {
		math = m;
	}
	public float getEnglish() {
		return english;
	}
	public void setEnglish(float e) {
		english = e;
	}
	public float getComputer() {
		return computer;
	}
	public void setComputer(float c) {
		computer = c;
	}
	
public float sum(){
	return math+english+computer;
}
public float avg(){
	return this.sum()/3;
}
public float max(){
	float max=math;
	max=max>english?max:english;
	max=max>computer?max:computer;
		return max;
	
}
public float min(){
	float min=math;
	min=min<english?min:english;
	min=min<computer?min:computer;
		return min;
	
}
}
public class ExampleDemo01{
	public static void main(String args[]){
		Student stu=null;
		stu=new Student("mldn-33","李兴华",95.0f,89.0f,96.0f);
		System.out.println("学生编号:"+stu.getStuno());
		System.out.println("学生姓名:"+stu.getName());
		System.out.println("数学成绩:"+stu.getMath());
		System.out.println("英语成绩:"+stu.getEnglish());
		System.out.println("计算机成绩:"+stu.getComputer());
		System.out.println("最高分:"+stu.max());
		System.out.println("最低分:"+stu.min());
		System.out.println("平均分:"+stu.avg());
		
	}
}

  程序结果

学生编号:mldn-33
学生姓名:李兴华
数学成绩:95.0
英语成绩:89.0
计算机成绩:96.0
最高分:96.0
最低分:89.0
平均分:93.333336

  

实例讲解——类设计分析

原文:http://www.cnblogs.com/dung/p/4363467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!