JAVA day06:代码练习题(封装部分)

 拓展题目1:访问修饰符的使用

将第一题进行扩展,套餐名称,价格,有效期,剩余流量;方法为输入套餐名称,展示套餐内容,输入要使用的流量,看套餐够不够,在输入使用天数,判断是否能够使用。

//网虫类
package Test01;

public class WangChongTaoCan {
	//名称,价格,有效期,剩余流量
	private String name;
	private double price;
	private int day;
	private double flow;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public double getFlow() {
		return flow;
	}
	public void setFlow(double flow) {
		this.flow = flow;
	}
	public WangChongTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	public WangChongTaoCan(String name, double price, int day, double flow) {
		super();
		this.name = name;
		this.price = price;
		this.day = day;
		this.flow = flow;
	}
}

//超人类
package Test01;

public class ChaoRenTaoCan {
	//名称,价格,有效期,剩余流量
	private String name;
	private double price;
	private int day;
	private double flow;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public double getFlow() {
		return flow;
	}
	public void setFlow(double flow) {
		this.flow = flow;
	}
	public ChaoRenTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	public ChaoRenTaoCan(String name, double price, int day, double flow) {
		super();
		this.name = name;
		this.price = price;
		this.day = day;
		this.flow = flow;
	}
}

//话痨类
package Test01;

public class HuaLaoTaoCan {
	//名称,价格,有效期,剩余流量
	private String name;
	private double price;
	private int day;
	private double flow;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public double getFlow() {
		return flow;
	}
	public void setFlow(double flow) {
		this.flow = flow;
	}
	public HuaLaoTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	public HuaLaoTaoCan(String name, double price, int day, double flow) {
		super();
		this.name = name;
		this.price = price;
		this.day = day;
		this.flow = flow;
	}
}


//测试类
package Test01;

import java.util.Scanner;

import Test4.Cat;

public class Test6 {
	public static void main(String[] args) {
		//名称,价格,有效期,剩余流量
		WangChongTaoCan wc=new WangChongTaoCan("网虫",6.55,1,8.55);
		ChaoRenTaoCan cr=new ChaoRenTaoCan("超人",5.55,10,8.55);
		HuaLaoTaoCan hl=new HuaLaoTaoCan("话痨",8.55,30,8.55);
		
		Scanner sc=new Scanner(System.in);
		int taocan=0;//1网虫 2超人 3话痨
		while(true)
		{
			System.out.println("请输入套餐的名称:");
			String taoCanName=sc.next();
			if(wc.getName().equals(taoCanName)){
				System.out.println("名称:"+wc.getName());
				System.out.println("价格:"+wc.getPrice());
				System.out.println("有效期:"+wc.getDay());
				System.out.println("剩余流量:"+wc.getFlow());
				taocan=1;
				break;
			}else if(cr.getName().equals(taoCanName)){
				System.out.println("名称:"+cr.getName());
				System.out.println("价格:"+cr.getPrice());
				System.out.println("有效期:"+cr.getDay());
				System.out.println("剩余流量:"+cr.getFlow());
				taocan=2;
				break;
			}else if(hl.getName().equals(taoCanName)){
				System.out.println("名称:"+hl.getName());
				System.out.println("价格:"+hl.getPrice());
				System.out.println("有效期:"+hl.getDay());
				System.out.println("剩余流量:"+hl.getFlow());
				taocan=3;
				break;
			}else{
				System.out.println("没有这个套餐,请重新输入");
			}
		}
		
		System.out.println("请输入套餐的使用天数:");
		int taoCanDay=sc.nextInt();
		switch (taocan) {
			case 1:
				if(taoCanDay<=wc.getDay()){
					System.out.println("可以正常使用");
				}else{
					System.out.println("天数不足,不可以正常使用");
				}
				break;
			case 2:
				if(taoCanDay<=cr.getDay()){
					System.out.println("可以正常使用");
				}else{
					System.out.println("天数不足,不可以正常使用");
				}
				break;
			case 3:
				if(taoCanDay<=hl.getDay()){
					System.out.println("可以正常使用");
				}else{
					System.out.println("天数不足,不可以正常使用");
				}
				break;
			default:
				break;
		}
		
	}
}

 

题目1:访问修饰符的使用

训练要点
访问修饰符的使用
需求说明
封装网虫套餐、超人套餐、 话唠套餐的成员变量

实现思路
1. 为属性添加访问修饰符;
2、创建setter、getter。

//网虫套餐
public class WangChongTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
}


//超人套餐
public class ChaoRenTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
}


//话痨套餐
public class HuaLaoTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
}

题目2:属性的封装

训练要点:
属性的封装

需求说明:
使用类的封装原则创建学生类;
成员变量包括ID、姓名、年龄、性别;
对所有成员变量进行封装。

实现思路:
1. 增加private修饰符
2. 增加getter和setter方法
3. 对性别进行合法性判断

//学生类
public class Student {
	private String id;
	private String name;
	private int age;
	private String sex;
	
	public void setId(String id){
		this.id=id;
	}
	public String getId(){
		return this.id;
	}
	
	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 class Test {

	public static void main(String[] args) {
		Student zhangsan =new Student();
		zhangsan.setId("2184");
		System.out.println(zhangsan.getId());
		
		zhangsan.setName("淳淳");
		System.out.println(zhangsan.getName());
		
		zhangsan.setAge(18);
		System.out.println(zhangsan.getAge());
		
		zhangsan.setSex("女");
		System.out.println(zhangsan.getSex());
	}
}

(拔高)题目3:综合案例1

1. 案例题目描述:   
定义一个快递员类,并进行封装
 步骤:
 1.将变量私有化
 2.提供公有访问方法getter和setter
    该类中有以下信息:
 1) 有姓名,员工编号,员工工资几个变量
 2) 定义一个显示自己信息的方法
    3)创建测试类,调用getter(),setter()来赋值和取值。并调用显示信息的方法
2. 评分标准:
(1) 将变量私有化 (10分)
(2) 提供公有访问方法getter和setter(10分)
(3) 创建测试类(10分)
(4) 调用方法输出(10分)
(5) 添加注释(5分)

//快递员类
public class ExpressPerson {
	private String no;
	private String name;
	private int salary;
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	//无参构造方法
	public ExpressPerson(){
		System.out.println("这是一个无参构造方法");
		System.out.println("定义即运行");
		this.no="jingdong01";
		this.name="刘强东";
		this.salary=20000;
	}
	//有参构造方法
	public ExpressPerson(String no, String name,int salary){
		System.out.println("这是一个有参构造方法");
		System.out.println("定义才运行");
		this.no=no;
		this.name=name;
		this.salary=salary;
	}
	//当添加自定义构造方法后,编译器不再添加无参构造方法
}
//测试类1
public class Test2 {

	public static void main(String[] args) {
		ExpressPerson zhangsan =new ExpressPerson();
		
		zhangsan.setNo("2184");
		zhangsan.setName("刘刘");
		zhangsan.setSalary(18000);
		
		System.out.println(zhangsan.getNo());
		System.out.println(zhangsan.getName());
		System.out.println(zhangsan.getSalary());
	}
}
//测试类2
public class TestExpress2 {

	public static void main(String[] args) {
		//ExpressPerson liuqiangdong = new ExpressPerson();
		ExpressPerson liuqiangdong = new ExpressPerson("taobao01","马云",123456);
		
		System.out.println(liuqiangdong.getNo());
		System.out.println(liuqiangdong.getName());
		System.out.println(liuqiangdong.getSalary());
	}

}

(拔高)题目4:综合案例2

1. 案例题目描述:   按要求完成以下题目
张老太养了两只猫:一只叫小花,今年3岁白色。另一只叫小白,今年3岁花色。请 编写一个程序,当用户输入小猫的名字时,就显示该猫的名字、年龄、颜色。如果输入的小猫名错误,则显示:张老太没有这只猫。
2. 实现思路及评分标准:
     
创建猫类,定义三个变量:姓名 年龄 颜色,变量均私有 (10分)
给这3个变量提供set和get方法
定义打印信息的方法   (10分)
创建测试类
创建2只猫,用set方式赋值,信息和题意一致  (10分)
创建键盘录入对象,录入猫的姓名
判断键盘录入的姓名和这2只猫的姓名是否一致,如果一致就打印这只猫的信息,
      如果不一致提示没有这只猫   (10分)

//猫类
package Test4;

public class Cat {
	private String name;
	private int age;
	private String col;
	
	public Cat() {
		super();
	}
	public Cat(String name, int age, String col) {
		super();
		this.name = name;
		this.age = age;
		this.col = col;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getCol() {
		return col;
	}
	public void setCol(String col) {
		this.col = col;
	}
}

//测试类
package Test4;

import java.util.Scanner;

public class Test4 {
	public static void xinXi(Cat c){
		System.out.println("姓名:"+c.getName());
		System.out.println("年龄:"+c.getAge());
		System.out.println("花色:"+c.getCol());
	}
	public static void main(String[] args) {
		/*
		Cat xiaohua=new Cat();
		Cat xiaobai=new Cat();
		xiaohua.setName("小花");
		xiaohua.setAge(3);
		xiaohua.setCol("白色");
		
		xiaobai.setName("小白");
		xiaobai.setAge(3);
		xiaobai.setCol("花色");
		*/
		//用有参构造更简洁
		Cat xiaohua=new Cat("小花",3,"白色");
		Cat xiaobai=new Cat("小白",3,"花色");
		
		Scanner sc=new Scanner(System.in);
		while(true)
		{
			System.out.println("请输入猫的姓名:");
			String catName=sc.next();
			if(xiaohua.getName().equals(catName)){
				/*
				System.out.println("姓名:"+xiaohua.getName());
				System.out.println("年龄:"+xiaohua.getAge());
				System.out.println("花色:"+xiaohua.getCol());*/
				//代码冗余,封装成方法调用
				xinXi(xiaobai);
				break;
			}else if(xiaobai.getName().equals(catName)){
				/*
				System.out.println("姓名:"+xiaobai.getName());
				System.out.println("年龄:"+xiaobai.getAge());
				System.out.println("花色:"+xiaobai.getCol());*/
				xinXi(xiaobai);
				break;
			}else{
				System.out.println("没有这只猫,请重新输入");
			}
		}
	}
}

题目5:this关键字的应用

训练要点:
this关键字的使用

需求说明:
优化Student类,修改有参构造方法,调用其它构造方法
修改计算方法,使用this关键字进行优化

实现思路:
1. 修改有参构造方法,使用this调用其它构造方法
2. 修改计算方法,使用this调用成员变量

public class Student {
	private String id;
	private String name;
	private int age;
	private String sex;
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String id, String name, int age, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public void setId(String id){
		this.id=id;
	}
	public String getId(){
		return this.id;
	}
	
	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;
	}
}

题目6:构造方法的使用 

训练要点
构造方法的创建和使用
需求说明
修改网虫套餐、超人套餐、 话唠套餐类
分别增加无参构造和有参构造
实现思路
1. 增加无参构造,并测试
2. 增加有参构造,并测试

//网虫类
package Test01;

public class WangChongTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public WangChongTaoCan(String name, double price, int no) {
		super();
		this.name = name;
		this.price = price;
		this.no = no;
	}
	public WangChongTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
}


//超人类
package Test01;

public class ChaoRenTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public ChaoRenTaoCan(String name, double price, int no) {
		super();
		this.name = name;
		this.price = price;
		this.no = no;
	}
	public ChaoRenTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

//话痨类
package Test01;

public class HuaLaoTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public HuaLaoTaoCan(String name, double price, int no) {
		super();
		this.name = name;
		this.price = price;
		this.no = no;
	}
	public HuaLaoTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

//测试类
package Test01;

import Test4.Cat;

public class Test6 {
	public static void main(String[] args) {
		WangChongTaoCan wc1=new WangChongTaoCan("网虫1",6.55,1);
		ChaoRenTaoCan cr1=new ChaoRenTaoCan("超人1",5.55,2);
		HuaLaoTaoCan hl1=new HuaLaoTaoCan("话痨1",8.55,3);
		
		WangChongTaoCan wc2=new WangChongTaoCan();
		wc2.setName("网虫2");
		wc2.setPrice(6.22);
		wc2.setNo(4);
		ChaoRenTaoCan cr2=new ChaoRenTaoCan();
		cr2.setName("超人2");
		cr2.setPrice(7.58);
		cr2.setNo(5);
		HuaLaoTaoCan hl2=new HuaLaoTaoCan();
		hl2.setName("话痨2");
		hl2.setPrice(3.25);
		hl2.setNo(6);
		
		System.out.println(wc1.getName());
		System.out.println(wc1.getPrice());
		System.out.println(wc1.getNo());
		
		System.out.println(wc2.getName());
		System.out.println(wc2.getPrice());
		System.out.println(wc2.getNo());
		
		System.out.println(cr1.getName());
		System.out.println(cr1.getPrice());
		System.out.println(cr1.getNo());
		
		System.out.println(cr2.getName());
		System.out.println(cr2.getPrice());
		System.out.println(cr2.getNo());
		
		System.out.println(hl1.getName());
		System.out.println(hl1.getPrice());
		System.out.println(hl1.getNo());
		
		System.out.println(hl2.getName());
		System.out.println(hl2.getPrice());
		System.out.println(hl2.getNo());
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值