“学生”类:
类名:Student
属性:姓名、性别、年龄、学号、5门课程的成绩
方法1:在控制台输出各个属性的值、
方法2:计算平均成绩
方法3:输出各个属性的值和平均成绩
测试类
创建2个对象,调用方法,要求:对象各个属性的值,从键盘输入。
*/
学生类:
1 package bbb; 2 public class Student 3 { 4 private String name; 5 private char sex; 6 private int age; 7 private String studentId; 8 private double scores[]=new double[5]; 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public char getSex() { 16 return sex; 17 } 18 public void setSex(char sex) { 19 this.sex = sex; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 public String getStudentId() { 28 return studentId; 29 } 30 public void setStudentId(String studentId) { 31 this.studentId = studentId; 32 } 33 public double[] getScores() { 34 return scores; 35 } 36 public void setScores(double[] scores) { 37 this.scores = scores; 38 } 39 public Student(String name, char sex, int age, String studentId, double[] scores) { 40 super(); 41 this.name = name; 42 this.sex = sex; 43 this.age = age; 44 this.studentId = studentId; 45 this.scores = scores; 46 } 47 public void PrintInformation() 48 { 49 50 System.out.println("姓名:"+name+' '+"性别:"+sex+' '+"年龄:"+age+' '+"学号:"+studentId+' '+"5门课程的成绩:"); 51 for(double x:scores) 52 { 53 System.out.print(x+" "); 54 } 55 56 } 57 public double getAverage() 58 { 59 double s=0.0; 60 for(double x:scores) 61 { 62 s=s+x; 63 } 64 double average=s/scores.length; 65 System.out.println("平均成绩为:"+average); 66 return average; 67 } 68 public void Property() 69 { 70 this.PrintInformation(); 71 this.getAverage(); 72 System.out.println(); 73 74 } 75 }
测试类:
1 package bbb; 2 import java.util.Scanner; 3 public class TestStudent 4 { 5 public static void main(String[] args) 6 { 7 Scanner sc=new Scanner(System.in); 8 Student ZhangSan; 9 Student LiSi; 10 String name; 11 char sex; 12 int age; 13 String studentId; 14 double scores[]=new double[5]; 15 16 System.out.println("请输入学生张三的信息:"); 17 name=sc.next(); 18 String temp=sc.next(); 19 sex=temp.charAt(0); 20 age=sc.nextInt(); 21 studentId=sc.next(); 22 for(int i=0;i<scores.length;i++) 23 { 24 scores[i]=sc.nextInt(); 25 } 26 ZhangSan=new Student(name,sex,age,studentId,scores); 27 28 System.out.println("请输入学生李四的信息:"); 29 name=sc.next(); 30 temp=sc.next(); 31 sex=temp.charAt(0); 32 age=sc.nextInt(); 33 studentId=sc.next(); 34 for(int i=0;i<scores.length;i++) 35 { 36 scores[i]=sc.nextInt(); 37 } 38 LiSi=new Student(name,sex,age,studentId,scores); 39 40 System.out.println("学生信息如下:"); 41 ZhangSan.Property(); 42 LiSi.Property(); 43 } 44 }