设计一个学生类,里面有学生的三项成绩:计算机成缋、数学成缋、英语成绩,要求
可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息,问:此类该
如何设计?
程序开发步骤:
(1) 根据要求定义出所要的类
(2)
根据题目中的要求规划出类的属性
name 、age 、computer 、english 、math
(3) 所有的属性必须封装:private
(4) 所有的属性必须通过 getter 及 setter访问
(5) 所有的信息不要在类中直接输出,而是交给调用处输出,在类中不能出现
Sysetm.out.print()语句。
class Student{
private String name ;
private int age ;
private float english ;
private float computer ;
private float math ;
public Student(){}
public Student(String n,int a,float e,float c,float m){
this.setName(n) ;
this.setAge(a) ;
this.setEnglish(e) ;
this.setComputer(c) ;
this.setMath(m) ;
}
public float sum(){
return english + computer + math ;
}
public float avg(){
return this.sum() / 3 ;
}
public float max(){
float max = computer>math?computer:math ;
max = max>english?max:english ;
return max ;
}
public float min(){
float min = computer<math?computer:math ;
min = min<english?min:english ;
return min ;
}
public String getInfo(){
return "学生信息: \n" +
"\t|- 姓名:" + this.getName() + "\n" +
"\t|- 年龄:" + this.getAge() + "\n" +
"\t|- 数学成绩:" + this.getMath() + "\n" +
"\t|- 英语成绩:" + this.getEnglish() + "\n" +
"\t|- 计算机成绩:" + this.getComputer() ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
age = a ;
}
public void setEnglish(float e){
english = e ;
}
public void setComputer(float c){
computer = c ;
}
public void setMath(float m){
math = m ;
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
public float getEnglish(){
return english ;
}
public float getComputer(){
return computer ;
}
public float getMath(){
return math ;
}
}
public class ExecDemo{
public static void main(String args[]){
Student stu = new Student("张三",30,89.0f,91.0f,87.0f) ;
System.out.println("总分:" + stu.sum()) ;
System.out.println("平均分:" + stu.avg()) ;
System.out.println("最高分:" + stu.max()) ;
System.out.println("最低分:" + stu.min()) ;
System.out.println(stu.getInfo()) ;
}
}
package test;
/*
* 实现一个Student类,包含如下内容:
成员变量:学号id,姓名name,数学成绩:math,语文成绩:chinese,外语成绩:english
成员方法:打印总分printtotal,打印平均分printaverage
用java怎么编写,不胜感激!
* */
public class Student {
int id;
String name;
int math;
int chinese;
int english;
public Student() {}
public Student(int id,String name,int math,int chinese,int english) {
this.id = id;
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
}
public void printTotal() {
System.out.println("总分:" + (math+chinese+english));
}
public void printAverage() {
System.out.println("平均分:" + (math+chinese+english)/3.0);
}
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 int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
}
class Student {
private String name;
private int age;
private float computer;
private float math;
private float english;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 获取最高分
public String getMaxScore() {
float max = Math.max(computer, Math.max(math, english));
String sub;
if (max == computer) {
sub = "计算机";
} else if (max == math) {
sub = "数学";
} else {
sub = "英语";
}
return "最高分是" + sub + ",得分:" + computer;
}
// 获取最低分
public String getMinScore() {
float min = Math.min(computer, Math.min(math, english));
String sub;
if (min == computer) {
sub = "计算机";
} else if (min == math) {
sub = "数学";
} else {
sub = "英语";
}
return "最低分是" + sub + ",得分:" + computer;
}
public String getAverage() {
return "平均分是:" + ((computer + math + english) / 3);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getComputer() {
return computer;
}
public void setComputer(float computer) {
this.computer = computer;
}
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
}