java练习 封装

定义一个表示学生信息的类Student,要求如下:

(1)类Student的成员变量:

sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。

(2)类Student带参数的构造方法:

在构造方法中通过形参完成对成员变量的赋值操作。

(3)类Student的方法成员:

getNo():获得学号;

getName():获得姓名;

getSex():获得性别;

getAge()获得年龄;

getJava():获得Java 课程成绩

根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。

public class Student {
	private String sNo;
	private String sName;
	private String sSex;
	private int sAge ;
	private int sJava;
	//get&set方法
	public String getsNo() {
		return sNo;
	}
	public void setsNo(String sNo) {
		this.sNo = sNo;
	}
	public String getsName() {
		return sName;
	}
	public void setsName(String sName) {
		this.sName = sName;
	}
	public String getsSex() {
		return sSex;
	}
	public void setsSex(String sSex) {
		this.sSex = sSex;
	}
	public int getsAge() {
		return sAge;
	}
	public void setsAge(int sAge) {
		this.sAge = sAge;
	}
	public int getsJava() {
		return sJava;
	}
	public void setsJava(int sJava) {
		this.sJava = sJava;
	}
	public Student(String sNo, String sName, String sSex, int sAge, int sJava) {
		this.sNo = sNo;
		this.sName = sName;
		this.sSex = sSex;
		this.sAge = sAge;
		this.sJava = sJava;
	}
//	public Student() {
//	}
	
}
public class Test1 {
	public static void main(String[] args) {
		Student[] students = new Student[5];
		students[0] = new Student("220110", "Tom", "男", 18, 85);
		students[1] = new Student("220111", "Tohm", "男", 18, 82);
		students[2] = new Student("220112", "Tomf", "女", 18, 79);
		students[3] = new Student("220113", "WTom", "男", 18, 85);
		students[4] = new Student("220114", "seTom", "男", 18, 90);
		int sum = 0;
		int average = 0;
		int max = students[0].getsJava();
		int min = students[0].getsJava();
		for (Student student : students) {
			int java = student.getsJava();
			sum += java;
			if (max < java) {
				max = java;
			}
			if (min > java) {
				min = java;
			}
		}
		average = sum/(students.length);
		System.out.println("Java语言的平均成绩是:"+average);
		System.out.println("Java语言成绩的最大值是:"+max);
		System.out.println("Java语言成绩的最小值是:"+min);
	}
	
}

 

 

你可能感兴趣的:(重新开始学Java-2020)