定义一个学生类Student,它继承自person类 (用java编写)

用java编写

要求:
1)Student类有以下几个变量 继承自父类的变量:姓名(name),字符串 类型(String);性别(sex),字符型(char);年龄(age),整型(int)。
子类新增加的变量: 学号(number),长整型;三门功课的成绩:哲学 (phi),整型;英语(eng),整型;计算机(comp),整型。

2)Student类有以下几个方法
子类新增加的方法:求三门功课的平均成绩aver():该方法没有参数,返回值类型为double型;
求三门功课成绩的最高分max():该方法没有参数,返回值为int型;
求三门功课成绩的最低分min():该方法没有参数,返回值为int型。
覆盖父类的同名方法: toString():获取学号、姓名、性别、平均分、最高分、最低分信息。

Person.java:

public class Person {
private String name;
private String sex;
private int age;
public Person(){};
public Person(String name,String sex,int age)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

}

Student.java:

import java.util.*;
public class Student extends Person {
private long number;
private int phi;
private int eng;
private int comp;
private int[] num;
public Student(){};
public Student(String name,String sex,int age,long number,int phi,int eng,int comp)
{
super(name,sex,age);
this.number=number;
this.phi=phi;
this.eng=eng;
this.comp=comp;
}
public int getComp() {
return comp;
}
public void setComp(int comp) {
this.comp = comp;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public int getPhi() {
return phi;
}
public void setPhi(int phi) {
this.phi = phi;
}
//求平均分数
public double avg()
{
return (getPhi()+getEng()+getComp())/3;
}
public int max()
{
sort();
return num[num.length-1];
}
public int min()
{
sort();
return num[0];
}
private void sort()
{
num=new int[]{getPhi(),getEng(),getComp()};
Arrays.sort(num);
}
public String toString()
{
return "学号:"+getNumber()+" 名字:"+getName()+" 性别:"+getSex()+" 平均分:"+avg()+" 最高分:"+max()+" 最低分:"+min();
}

}

测试类:
Test.java:
public class Test {
public static void main(String[] args) {
Student s=new Student("张三","男",20,11203,80,70,90);
System.out.println(s);

}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-29
“天天”的代码正确啦,但是貌似把楼主的意思理解错了,题目的意思应该是Student类中的toString()方法用来输出该Student的信息,这是本人的理解,如果楼主是这个意思的话,应该将“天天”的构造函数部分的代码放到toString()函数中,也就是
public void toString() {
System.out.println("子类的toString()");
System.out.println("学号:" + this.getNumber());
System.out.println("姓名:" + super.name);
System.out.println("性别:" + super.getSex());
System.out.println("平均分:" + this.aver());
System.out.println("最高分:" + this.max());
System.out.println("最低分信息:" + this.min());
}
若是只要返回信息不用输出的话,就是这样了:
public String toString() {
String info=“”;
info="学号:" + this.getNumber()+"\n"+"姓名:" + super.name+"\n"+"性别:" +super.getSex()+"\n"+"平均分:" + this.aver())+"\n"+"最高分:" + this.max()+"\n"+"最低分信息:" + this.min();
return info;
}
第2个回答  2013-10-29
public class Person{
public String name;
public char sex;
public int age;

}

public class Student extends Person{
public long number;
public int phi;
public int eng;
public int comp;
public double aver(){
return (phi+eng+comp)/3;
}
public int max(){
return phi >eng ? (phi>comp?phi:comp):(eng>comp?eng:comp);
}
public int min(){
return phi <eng ? (phi<comp?phi:comp):(eng<comp?eng:comp);
}
public String toString(){
return "学号:"+number+" 姓名:"+name+" 性别:"+sex+"平均分:"+aver()+" 最高分:"+max()+" 最低分信息"+min();
}
}
第3个回答  2013-10-29
public class Person {

protected String name = "wxk";
protected char sex = '男';
protected int age = 20;

public Person() {
System.out.println("父类的构造器");
}

public String toString(){
System.out.println("父类的toString()");
return "";
}
/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the sex
*/
public char getSex() {
return sex;
}

/**
* @param sex
* the sex to set
*/
public void setSex(char sex) {
this.sex = sex;
}

/**
* @return the age
*/
public int getAge() {
return age;
}

/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}

}本回答被网友采纳
相似回答