one无名程序员 2021-12-15 16:19 采纳率: 50%
浏览 39
已结题

java关于类与方法的问题

设计一个学生类,里面有学生的三项成绩:计算机成缋、数学成缋、英语成绩,要求
可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息,问:此类该
如何设计?
程序开发步骤:
(1) 根据要求定义出所要的类
(2)
根据题目中的要求规划出类的属性
name 、age 、computer 、english 、math
(3) 所有的属性必须封装:private
(4) 所有的属性必须通过 getter 及 setter访问
(5) 所有的信息不要在类中直接输出,而是交给调用处输出,在类中不能出现
Sysetm.out.print()语句。

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-12-15 16:33
    关注
    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()) ;
    }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月15日

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。