java程序设计

完成下面父类及子类的声明:

(1) 声明Student类。

属性包括:学号、姓名、英语成绩、数学成绩、计算机成绩、总成绩。

方法包括:构造方法、get方法、set方法、 toString方法、compare方法(比较二个学生的总成绩大小,结果分为大于、等于、小于),sum方法(计算总成绩)、testScore方法(计算测评成绩)。

(2)声明StudentXW(学习委员)类为Student类的子类。

在StudentXW类中增加责任属性,并重写testScore方法(测评成绩=平均分+3)。

(3)声明StudentBZ类中增加责任属性,并重写testScore方法(测评成绩=平均分+5)。

(4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的测评成绩。

Student类

代码:

package test1;

public class Student {
	protected int id;
	protected String name;
	protected int English;
	protected int math;
	protected int computer;
	protected int sum;
	protected double average;
	public void set_Id(int id){
		this.id=id;
	}
	public void set_Name(String name){
		this.name=name;
	}
	public void set_English(int English){
		this.English=English;
	}
	public void set_Math(int math){
		this.math=math;
	}
	public void set_Computer(int computer){
		this.computer=computer;
	}
	
	 
	public int get_Id(){
		return id;
	}
	public String get_Name(){
		return name;
	}
	public int get_English(){
		return English;
	}
	public int get_Math(){
		return math;
	}
	public int get_Computer(int computer){
		return computer;
	}
	public int get_Sun(){
		sum=English+math+computer;
		return sum;
	}
	public double testScore(){
		average=(double)((English+math+computer)/3);
		return average;
	}
	Student(){
		
	}
	public Student(int id,String name,int English,int math,int computer){
		this.id=id;
		this.name=name;
		this.English=English;
		this.math=math;
		this.computer=computer;
	}
	public void compare(int a,int b){
		if(a==b)
			System.out.println("总分相等");
		else if(a>b)
			System.out.println(a+"大于"+b);
		else
			System.out.println(a+"小于"+b);
	}
	public String toString(){
		return("姓名:"+name+" "+"英语="+English+" "+"数学="+math+" "+"电脑="+computer+" "+"总分="+sum);
	}
	
StudentXZ

代码:

package test1;

public class StudentXW  extends Student{
	private String xwduty;
	StudentXW(String xwduty,int id,String name,int English,int math,int computer){
	 super(id, name, English, math, computer);
		this.xwduty=xwduty;
	}
	public void zhiwu(){
		System.out.print("职务:"+xwduty+" ");
	}
	@Override
	public double testScore(){
		average=(double)((English+math+computer)/3+3);
		return average;

	}
}

StudentBZ类

代码:

package test1;

public class StudentBZ extends Student{
	protected String bzduty;
	public StudentBZ(String bzduty,int id,String name,int English,int math,int computer){
		super( id, name, English, math, computer);
		this.bzduty=bzduty;
	}
	public void zhiwu(){
		System.out.print("职务:"+bzduty+" ");
	}
	@Override
	public double testScore(){
		average=(double)((English+math+computer)/3+5);
		return average;
	}

}

测试类

代码:

package test1;

public class tsetClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Student s1=new Student(36,"zhang",75,80,85);
		s1.get_Sun();
		System.out.println(s1.toString());
		System.out.println("average="+s1.testScore());
		StudentXW s2=new StudentXW("XueXiWeiYuan",36,"zhang",76,80,85);
		s2.get_Sun();
		s2.zhiwu();
		System.out.println(s2.toString());
		System.out.println("average="+s2.testScore());
		StudentBZ s3=new StudentBZ("BanZhang",39,"wang",76,89,95);
		s3.get_Sun();
		s3.zhiwu();
		System.out.println(s3.toString());
		System.out.println("average="+s3.testScore());
		s1.compare(s2.get_Sun(), s3.get_Sun());
	}

}
运行结果:





  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值