javaSE_07Java中类和对象-封装特性--练习
1.编写封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,语文成绩,一个学生类,我们关注姓名,年龄,学历等信息,要求年龄必须在19-40岁之间,默认为19,学历必须是大专,本科,研究生这几个值的范围内,默认为大专。创建对象,测试其相关方法
类的创建:
/**
* 3.编写一个学生类,我们关注姓名,年龄,学历等信息,
* 要求年龄必须在19-40岁之间,默认为19,学历必须是大专,
* 本科,研究生这几个值的范围内,默认为大专。创建对象,测试其相关方法
* @author Administrator
*
*/
public class Student {
private String name;
private int age;
private String education;
//set get
public void setName(String name){
this.name=name;//这里必须加this,因为出现了局部变量和成员变量重名,必须指明name是哪个,下面同理
}
public String getName(){
return this.name;
}
public void setAge(int age){
if(age<19||age>40){
this.age=19;
}else{
this.age=age;
}
}
public int getAge(){
return this.age;
}
public void setEducation(String education){
if(education.equals("大专")||education.equals("本科")||education.equals("研究生")){
this.education=education;
}else{
this.education="大专";
}
}
public String getEducation(){
return this.education;
}
}
类的测试:
public class StudentTest {
public static void main(String[] args){
//1,创建对象
Student student=new Student();
//2,赋值
student.setName("张三");
student.setAge(28);
student.setEducation("本科");
//3,获取
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(student.getEducation());
System.out.println();
//再创建一个对象.用于验证不符合条件情况
Student student2=new Student();
student2.setName("李四");
student2.setAge(10);
student2.setEducation("博士");
System.out.println(student2.getName());
System.out.println(student2.getAge());
System.out.println(student2.getEducation());
}
}
2.封装方法,求总分,平均分,最高分,最低分,以及打印学生的信息。创建对象,测试其相关方法
类的创建:
/**
* 4.封装一个学生类,有姓名,有年龄,有性别,有英语成绩,数学成绩,
* 语文成绩,封装方法,求总分,平均分,最高分,最低分,
* 以及打印学生的信息。创建对象,测试其相关方法
* @author Administrator
*
*/
public class Student2 {
private String name;
private int age;
private String sex;
private double english;
private double math;
private double chinese;
//set get
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setSex(String sex){
this.sex=sex;
}
public String getSex(){
return this.sex;
}
public void setEnglish(double english){
this.english=english;
}
public double getEnglish(){
return this.english;
}
public void setMath(double math){
this.math=math;
}
public double getMath(){
return this.math;
}
public void setChinese(double chinese){
this.chinese=chinese;
}
public double getChinese(){
return this.chinese;
}
//构造函数实现直接给属性赋值
public Student2(){}
public Student2(String name,int age,String sex,double english,double math,double chinese){
this.setName(name);
this.setAge(age);
this.setSex(sex);
this.setEnglish(english);
this.setMath(math);
this.setChinese(chinese);
}
//getSum
public double getSum(){
return this.english+this.math+this.chinese;
}
//getAverage
public double getAverage(){
return this.getSum()/3;
}
//getMax
public double getMax(){
double max=this.english>this.math?this.english:this.math;
max=max>this.chinese?max:this.chinese;
return max;
}
//getMin
public double getMin(){
double min=this.english<this.math?this.english:this.math;
min=min<this.chinese?min:this.chinese;
return math;
}
}
类的测试:
public class Student2Test {
public static void main(String[] args){
Student2 student=new Student2("张飞",99,"男",88,77,79);
System.out.println(student.getName()+"成绩分析如下:");
System.out.println("总分:"+student.getSum());
System.out.println("平均分:"+student.getAverage());
System.out.println("最高分分:"+student.getMax());
System.out.println("最低分:"+student.getMin());
System.out.println();
//也可以和上一个例题一个个赋值
Student2 student2=new Student2();
student2.setName("刘备");
student2.setAge(78);
student2.setEnglish(99);
student2.setMath(88);
student2.setChinese(89);
System.out.println(student2.getName()+"成绩分析如下:");
System.out.println("总分:"+student2.getSum());
System.out.println("平均分:"+student2.getAverage());
System.out.println("最高分分:"+student2.getMax());
System.out.println("最低分:"+student2.getMin());
}
}
3.编写一个随机点名系统,两个功能,一个是抽取学员回答问题,一个是记录学员被命中的次数
类的创建:
package day09;
/**
* 编写一个随机点名系统,两个功能,
* 一个是抽取学员回答问题,一个是记录学员被命中的次数
* @author Administrator
*
*/
public class NameSystem {
private String name;
private int times;
//构造方法
public NameSystem(){
}
public NameSystem(String name,int times){
this.setName(name);
this.setTimes(times);
}
//set get
public void setName(String name){
this.name=name;
}
public void setTimes(int times){
this.times=times;
}
public String getName(){
return this.name;
}
public int getTimes(){
return this.times;
}
}
类的调用:
package day09;
import java.util.Scanner;
public class NameSystemTest {
public static void main(String[] args){
//1,创建对象
/*
NameSystem[] persons=new NameSystem[5];
persons[0].setName("张三");
persons[0].setTimes(0);
//动态赋值
persons[0]=new NameSystem("小李",0);
persons[1]=new NameSystem("小张",0);
persons[2]=new NameSystem("小王",0);
persons[3]=new NameSystem("小明",0);
persons[4]=new NameSystem("小刚",0);*/
//静态赋值
NameSystem[] persons={new NameSystem("小李",0),new NameSystem("小张",0),
new NameSystem("小王",0),new NameSystem("小明",0),new NameSystem("小刚",0)};
Scanner input=new Scanner(System.in);
String isContinue;
//2,点名
do{
int index=(int)(Math.random()*persons.length);
System.out.println("本次抽中的学员是:"+persons[index].getName());
persons[index].setTimes(persons[index].getTimes()+1);
System.out.println("继续请输入y,其他任意键退出");
isContinue=input.next();
}while(isContinue.equals("y"));
System.out.println("本次点名的结果统计:");
for(NameSystem p:persons){
System.out.println(p.getName()+":"+p.getTimes());
}
//关闭io流
input.close();
}
}