当前位置:   article > 正文

Java程序———定义一个Student类,通过主类调用,其中学生的生日表示方法嵌套DAta类_studentdata类java

studentdata类java

题目内容:

        定义一个Student类,通过主类调用,其中学生的生日表示方法嵌套DAta类,并通过调用系统当前年份,计算学生年龄并输出


        Student类包含 

               变量:sno(学号),name(姓名),age(年龄),sex(性别),birth(出生年月日)

               构造函数:Student(String sn)、Student()

                                 和Student(String sn,String na,int ag,DAte bri)

               功能函数:String get***( )   //通过键盘获取***成员变量值

                                  void put***( )      //输入***成员变量的值


Student类:

  1. import java.util.Scanner;
  2. import java.text.SimpleDateFormat;//获取系统时间
  3. import java.util.Date;
  4. class Student {
  5. String sno; //学号
  6. String name; //姓名
  7. int age; //年龄
  8. String sex; //性别
  9. DAte birth; //出生年月日
  10. Student(){}
  11. Student(String sn){
  12. sno=sn;
  13. name="Null";
  14. age=0;
  15. sex="Null";
  16. }
  17. Student(String sn,String na,int ag,DAte bri){
  18. sno=sn;
  19. name=na;
  20. age=ag;
  21. birth=bri;
  22. }
  23. public String getSno() {
  24. Scanner sc=new Scanner(System.in);
  25. try {
  26. System.out.println("请输入学生学号:");
  27. sno=sc.next();
  28. return sno;
  29. }
  30. finally {
  31. sc.close(); //关闭系统资源,习惯
  32. }
  33. }
  34. public void putSno() {
  35. System.out.print("学号:"+sno+"\t");
  36. }
  37. public String getName() {
  38. Scanner sc=new Scanner(System.in);
  39. try {
  40. System.out.println("请输入学生姓名:");
  41. name=sc.next();
  42. return name;
  43. }
  44. finally {
  45. sc.close();
  46. }
  47. }
  48. public void putName() {
  49. System.out.print("姓名:"+name+"\t");
  50. }
  51. public int getAge() { //年龄
  52. SimpleDateFormat sd=new SimpleDateFormat("yyyy");
  53. Date data=new Date();
  54. String st=sd.format(data);
  55. age=Integer.parseInt(st)-birth.year;
  56. return age;
  57. }
  58. public void putAge() {
  59. System.out.print("该学生的年龄为:"+age+"岁\t\t");
  60. }
  61. public String getSex() {
  62. Scanner sc=new Scanner(System.in);
  63. try {
  64. System.out.println("请输入学生性别:");
  65. sex=sc.next();
  66. return sex;
  67. }
  68. finally {
  69. sc.close();
  70. }
  71. }
  72. public void putSex() {
  73. System.out.print("性别:"+sex+"\t");
  74. }
  75. public DAte getBrith() {
  76. Scanner sc=new Scanner(System.in);
  77. try {
  78. System.out.println("请输入学生出生年、月、日:");
  79. birth=new DAte();
  80. birth.getDate();
  81. return birth;
  82. }
  83. finally {
  84. sc.close();
  85. }
  86. }
  87. public void putBrith() {
  88. System.out.print("生日为:");
  89. birth.putDate();
  90. System.out.print("\t");
  91. }
  92. }

DAte:

  1. import java.util.Scanner;
  2. class DAte {
  3. int year;
  4. int month;
  5. int day;
  6. DAte(){
  7. }
  8. void getDate() {
  9. Scanner sc=new Scanner(System.in);
  10. try {
  11. System.out.println("请输入年:");
  12. year=sc.nextInt();
  13. System.out.println("请输入月:");
  14. month=sc.nextInt();
  15. System.out.println("请输入日:");
  16. day=sc.nextInt();
  17. }
  18. finally {
  19. sc.close();
  20. }
  21. }
  22. void putDate()
  23. {
  24. System.out.print(year+"年");
  25. System.out.print(month+"月");
  26. System.out.print(day+"日");
  27. }
  28. }

主类调用:

  1. class MainStudent {
  2. public static void main(String arge[]) {
  3. Student[] stu=new Student[10];
  4. System.out.println("接下来我们会进行输入同学的个人信息,分别包含(学号,姓名,性别,出生年月日),若提前结束请在学号项输入-1即可");
  5. int i;
  6. for(i=0;i<10;i++)
  7. {
  8. stu[i]=new Student(); //需要给每个数组成员分别定义数组空间
  9. if(Integer.parseInt(stu[i].getSno())==-1)break;
  10. stu[i].getName();
  11. stu[i].getSex();
  12. stu[i].getBrith();
  13. stu[i].getAge();
  14. }
  15. for(int j=i-1;j>=0;j--)
  16. {
  17. stu[j].putSno();
  18. stu[j].putName();
  19. stu[j].putSex();
  20. stu[j].putBrith();
  21. stu[j].putAge();
  22. System.out.println();
  23. }
  24. }
  25. }

有任何问题,恳请指正

手敲不易,如果觉得不错还请点赞支持下~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/244511
推荐阅读
相关标签
  

闽ICP备14008679号