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(); }
}

javaSE_07Java中类和对象-封装特性--练习的更多相关文章

  1. javaSE_07Java中类和对象-封装特性

    一.谈谈什么是面向对象的思维 理解面向对象,重点是要思考以下的问题 面向过程 vs 面向对象 Ø 谈谈什么是面向过程的编程思想? Ø 为什么有面向过程还要有面向对象? Ø 谈谈什么是面向对象的编程思想 ...

  2. javaSE_07Java中类和对象-封装特性-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 : 2)右击在新标签中打开放大查看 (IE不支持,搜狗,360可以):

  3. JS中类或对象的定义说明

    本篇文章主要是对JS中类或对象的定义进行说明介绍.我们知道,JS是面向对象的.谈到面向对象,就不可避免的要涉及类的概念.一般像c#,java这些强类型语言都有固定的定义类的语法.而JS的不同之处在于它 ...

  4. JS 对象封装的常用方式

    JS是一门面向对象语言,其对象是用prototype属性来模拟的,下面,来看看如何封装JS对象. 常规封装 function Person (name,age,sex){ this.name = na ...

  5. Javascript的对象封装和继承有哪些方法?优劣分别是什么?

    1.对象封装方法 (1)原始模式生成对象 直接将我们的成员写入对象中,用函数返回. 缺点:很难看出是一个模式出来的实例. function Stu(name, score) {             ...

  6. jfinal对象封装Record原理

    /*DbPro.class*/ public transient Record findFirst(String sql, Object paras[]{ List result = find(sql ...

  7. Ajax发送FormData对象封装的表单数据

    前端页面: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...

  8. Swift --- 面向对象中类和对象的属性

    Swift中类和对象的属性分为三种:储存属性,计算属性和类属性. import Foundation class Person { // 储存属性必须赋初值 var score1: Int = 20 ...

  9. 利用js对象的特性,去掉数组中的重复项

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

随机推荐

  1. 深度理解 Virtual DOM

    目录: 1 前言 2 技术发展史 3 Virtual DOM 算法 4 Virtual DOM 实现 5 Virtual DOM 树的差异(Diff算法) 6 结语 7 参考链接 1 前言 我会尽量把 ...

  2. 使用Spring标签<form:textarea>时,用readonly=“readonly”属性时不起作用。

    最近的一个项目用到了Spring标签<form:textarea>,当在设置其只读属性时,使用readonly="readonly"不起作用,还是能修改内容. 在网上找 ...

  3. 初学strurs基础

    Struts2基础学习总结 Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1. ...

  4. 使用CSharp编写Google Protobuf插件

    什么是 Google Protocol Buffer? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 ...

  5. MySQL之使用DDL语句创建表

    一.使用DDL语句创建表 DDL语言全面数据定义语言(Data Define Language) 主要的DDL动词: CREATE(创建).DROP(删除).ALTER(修改) TRUNCATE(截断 ...

  6. 04(2) 基于上下文相关的GMM-HMM声学模型2之参数共享

    1.三音素建模存在的问题 问题一:很多三音素在训练数据中没有出现(尤其跨词三音素) 问题二:在训练数据中出现过的三音素有相当一部分出现的频次较少 因此,三音素模型训练时存在较严重的数据不足问题 2.参 ...

  7. IOS中的UIScrollView

    要引用UIScrollView 首先要遵循UIScrollViewDelegate协议 然后重写 //1.拖拽方法 -(void)scrollViewDidScroll:(UIScrollView * ...

  8. jQuery中的选择器(上)

    从学习jquery开始,现在已经是第三遍看锋利的jQuery这本书了,现在打算对jQuery中的各种选择器进行一下总结,主要是是为了进一步系统的巩固自己对知识的掌握,另外也可以为那些学习jQuery并 ...

  9. Ext JS 实现建议词模糊动态搜索功能

    代码: new Ext.form.ComboBox({ store: new Ext.data.JsonStore({ idProperty: 'VehicleNo', url: '../ajax/t ...

  10. WinRAR5.01注册码附注册机

    把下面的注册码复制到"记事本"中,另存为"rarreg.key"文件,放到WinRAR安装目录即完成注册.RAR registration datakjcy8U ...