public class FirstDemo {
/**
* 封装+构造方法小例子
*/
//
private String student;
private String name;
private float math;
private float english;
private float computer;
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 属性
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
public float getComputer() {
return computer;
}
public void setComputer(float computer) {
this.computer = computer;
}
// 方法
public FirstDemo() {
super();
// 无参构造
}
public FirstDemo(String s, String n, float m, float e, float c) {
// 含参数构造
this.setStudent(s);
this.setName(n);
this.setMath(m);
this.setEnglish(e);
this.setComputer(c);
}
public float sum() {
// 求和
return math + english + computer;
}
public float avg() {
// 平均数
return this.sum() / 3;
}
public float max() {
// 三科中的最大值
float max = math;// 初始化数学为最高成绩
// 三目运算符---如果数学成绩大于计算机成绩,max=数学成绩否则max=computer
// 三目运算符---如果数学成绩大于英语成绩,max=数学成绩否则max=english
// 通过两次运算获得三科中最大值
max = max > computer ? max : computer;
max = max > english ? max : english;
return max;
}
public float min() {
// 三科中的最小值
float min = math;// 初始化数学为最高成绩
// 三目运算符---如果数学成绩大于计算机成绩,min=数学成绩否则min=computer
// 三目运算符---如果数学成绩大于英语成绩,min=数学成绩否则min=english
// 通过两次运算获得三科中最大值
min = min < computer ? min : computer;
min = min < english ? min : english;
return min;
}
public static void main(String[] args) {
// 具体赋值
FirstDemo firstDemo = new FirstDemo("01", "a1", 89, 98, 33);
System.out.print("学生编号:" + firstDemo.getStudent());
System.out.print("\t学生名称" + firstDemo.getName());
System.out.print("\t数学成绩" + firstDemo.getMath());
System.out.print("\t英语成绩" + firstDemo.getEnglish());
System.out.print("\t计算机成绩" + firstDemo.getComputer());
System.out.print("\t总成绩" + firstDemo.sum());
System.out.print("\n平均分" + firstDemo.avg());
System.out.print("\n最大值" + firstDemo.max());
System.out.print("\n最小值" + firstDemo.min());
}
}
/**
* 封装+构造方法小例子
*/
//
private String student;
private String name;
private float math;
private float english;
private float computer;
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 属性
public float getMath() {
return math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return english;
}
public void setEnglish(float english) {
this.english = english;
}
public float getComputer() {
return computer;
}
public void setComputer(float computer) {
this.computer = computer;
}
// 方法
public FirstDemo() {
super();
// 无参构造
}
public FirstDemo(String s, String n, float m, float e, float c) {
// 含参数构造
this.setStudent(s);
this.setName(n);
this.setMath(m);
this.setEnglish(e);
this.setComputer(c);
}
public float sum() {
// 求和
return math + english + computer;
}
public float avg() {
// 平均数
return this.sum() / 3;
}
public float max() {
// 三科中的最大值
float max = math;// 初始化数学为最高成绩
// 三目运算符---如果数学成绩大于计算机成绩,max=数学成绩否则max=computer
// 三目运算符---如果数学成绩大于英语成绩,max=数学成绩否则max=english
// 通过两次运算获得三科中最大值
max = max > computer ? max : computer;
max = max > english ? max : english;
return max;
}
public float min() {
// 三科中的最小值
float min = math;// 初始化数学为最高成绩
// 三目运算符---如果数学成绩大于计算机成绩,min=数学成绩否则min=computer
// 三目运算符---如果数学成绩大于英语成绩,min=数学成绩否则min=english
// 通过两次运算获得三科中最大值
min = min < computer ? min : computer;
min = min < english ? min : english;
return min;
}
public static void main(String[] args) {
// 具体赋值
FirstDemo firstDemo = new FirstDemo("01", "a1", 89, 98, 33);
System.out.print("学生编号:" + firstDemo.getStudent());
System.out.print("\t学生名称" + firstDemo.getName());
System.out.print("\t数学成绩" + firstDemo.getMath());
System.out.print("\t英语成绩" + firstDemo.getEnglish());
System.out.print("\t计算机成绩" + firstDemo.getComputer());
System.out.print("\t总成绩" + firstDemo.sum());
System.out.print("\n平均分" + firstDemo.avg());
System.out.print("\n最大值" + firstDemo.max());
System.out.print("\n最小值" + firstDemo.min());
}
}
转载于:https://blog.51cto.com/haiyuanxi/913679