2018-12-12作业4

在这里插入图片描述

public abstract class Student {
	private int id;
	private String name;
	private char sex;
	private int age;
	private double scores;
	
	public Student(int id, String name, char sex, int age) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}

	public double getScores() {
		return scores;
	}
	
	public void setScores(double scores) {
		this.scores = scores;
	}
	
	public abstract void calculate();
}
public class English extends Student {
	private int speech;
	private int finalExam;
	private int period;

	public English(int id, String name, char sex, int age,int speech, int finalExam, int period) {
		super(id,name,sex,age);
		this.speech = speech;
		this.finalExam = finalExam;
		this.period = period;
	}

	@Override
	public void calculate() {
		double sum = speech*0.5+finalExam*0.25+period*0.25;
		setScores(sum);
	}

}
public class Computer extends Student {
	private int operation;
	private int english;
	private int finalExam;
	private int period;

	public Computer(int id, String name, char sex, int age,int operation, int english, int finalExam, int period) {
		super(id,name,sex,age);
		this.operation = operation;
		this.english = english;
		this.finalExam = finalExam;
		this.period = period;
	}

	@Override
	public void calculate() {
		double sum = operation*0.4+english*0.2+finalExam*0.2+period*0.2;
		setScores(sum);
	}

}
public class Literature extends Student {
	private int speech;
	private int works;
	private int finalExam;
	private int period;

	public Literature(int id, String name, char sex, int age,int speech, int works, int finalExam, int period) {
		super(id,name,sex,age);
		this.speech = speech;
		this.works = works;
		this.finalExam = finalExam;
		this.period = period;
	}

	@Override
	public void calculate() {
		double sum = speech*0.35+works*0.35+finalExam*0.15+period*0.15;
		setScores(sum);
	}

}
import java.util.Random;

public class Test {

	public static void main(String[] args) {
		Random r = new Random();
		Student[] stus = new Student[5];
		stus[0] = new English(1,"s1",'f',20,r.nextInt(101), r.nextInt(101), 
				r.nextInt(101));
		stus[1] = new Computer(2,"s2",'f',19,r.nextInt(101), r.nextInt(101), 
				r.nextInt(101), r.nextInt(101));
		stus[2] = new Literature(3,"s3",'m',21,r.nextInt(101), r.nextInt(101),
				r.nextInt(101), r.nextInt(101));
		stus[3] = new Computer(4,"s4",'m',19,r.nextInt(101), r.nextInt(101), 
				r.nextInt(101), r.nextInt(101));
		stus[4] = new Literature(5,"s5",'f',20,r.nextInt(101), r.nextInt(101),
				r.nextInt(101), r.nextInt(101));
		
		for(Student i : stus){
			i.calculate();
			System.out.println("学号:"+i.getId()+"\t姓名:"+i.getName()
			+"\t性别:"+i.getSex()+"\t年龄:"+i.getAge()+"\t综合成绩:"+i.getScores());
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值