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. java多线程基本概述(一)——线程的基本认知

    1.1.概念: 进程:进程是操作系统结构的基础,是一次程序的执行:是一个程序及其数据再处理器上顺序执行时所发生的活动:是程序再一个数据集合上运行的过程,它是系统进行系统资源分配和调度的最小单元. 线程 ...

  2. html 数字不转行问题

    代码如下 <div style="width:20px;height:20px"> 111111111111111111111111111111111111111111 ...

  3. windows平台把UliPad添加到右键菜单

    对.py文件支持右键用UliPad打开方式支持: 1.打开注册表(win+R,运行框输入regedit) 2.先对*.py文件进行设置.找到注册表目录HKEY_CLASSES_ROOT\Python. ...

  4. ASP.Net零碎

    ASP.Net零碎 ServerPush 什么是ServerPush,服务器向客户端浏览器“推送”,其实就是“长连接”. 只有浏览器请求服务器端,服务器端才有给浏览器响应数据,不会主动向浏览器推送数据 ...

  5. css写出三角形(兼容IE)

    css写出三角形   利用css写三角形,兼容IE7 .arrow-up { width:0px; height:0px; border-left:10px solid transparent; bo ...

  6. FME中通过HTMLExtractor向HTML要数据

    如何不断扩充数据中心的数据规模,提升数据挖掘的价值,这是我们思考的问题,数据一方面来自于内部生产,一部分数据可以来自于互联网,互联网上的数据体量庞大,形态多样,之前blog里很多FMEer已经提出了方 ...

  7. redis3.05安装

    #yum -y install gcc #cd /usr/local/src #tar -zxvf redis-3.0.5.tar.gz #cd redis-3.05/ #make PREFIX=/u ...

  8. 解决xmapp中Apache端口号占用问题

    [原]解决 "安装xmapp后Apache不能正常启动" 问题 小伙伴们安装xmapp后发现Apache不能正常开启,下面给出了不同情况的解决办法,可以分为以下几种情况分析问题: ...

  9. Telegram学习解析系列(二):这我怎么给后台传输数据?

    写在前面: 在iOS开发的过程中,有很多时候我们都在和数据打交道,最基本的就是数据的下载和上传了,估计很多很多的小伙伴都在用AFNetworking与后台数据打交道,可有没有想过,哪天AFNetwor ...

  10. redis性能优化

    redis日志截图: