Java面向对象第三章封装与继承练习题

练习1:使用封装实现企鹅类

需求说明 使用封装实现电子宠物系统的企鹅类正确输入健康值和亲密度 保证健康值的有效性(0-100),否则取默认值60 保证亲密度的有效性(0-100),否则取默认值60

package com.hz.ch01;
/**
  * 企鹅类
 * @author 26255
 *
 */

import java.util.Scanner;

public class Penguin {
	Scanner sc = new Scanner(System.in);
	private int health;
	private int intimacy;
	
	public int getHealth() {
		health = sc.nextInt();
		if(health<0){
			health = 60;
		}
		return health;
		
	}
	
	public int getIntimacy() {
		intimacy = sc.nextInt();
		if(intimacy<0) {
			intimacy = 60;
		}
		return intimacy;
	}

}
package com.hz.ch01;

import java.util.Scanner;

public class Lianxi1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		Penguin m = new Penguin();
		System.out.println("欢迎您来到宠物店!");
		System.out.print("请输入要领养宠物的名字:");
		String name = sc.next();
		System.out.print("请选择要领养的宠物类型:(1.狗狗、2.企鹅)");
		int l = sc.nextInt();
		String x ;
		
		if(l==1) {
			System.out.println("请选择狗狗的性别");
			 x = sc.next();
		}else {
				System.out.println("请选择企鹅的性别");
				 x = sc.next();
		}
		System.out.println("请输入"+name+"的健康值:");
		
		int dd = m.getHealth();
		
		System.out.println("请输入"+name+"亲密度:");
		int qw = m.getIntimacy();
		System.out.println("宠物的自白:");
		System.out.println("我的名字叫"+name+"健康值是"+dd+"和主人的亲密度是"+qw+"我的性别是"+x);
		
	}

}

欢迎您来到宠物店!
请输入要领养宠物的名字:11
请选择要领养的宠物类型:(1.狗狗、2.企鹅)1
请选择狗狗的性别
1
请输入11的健康值:
11
请输入11亲密度:
22
宠物的自白:
我的名字叫11健康值是11和主人的亲密度是22我的性别是1

 练习2:选民投票

需求说明 模拟实现选民投票过程:一群选民进行投票,每个选民只允许投一次票,并且当投票总数达到100时,就停止投票

package com.hz.ch02;

import java.util.Scanner;

/**
 * 选民投票类
 * @author 26255
 *
 */
public class Vote {
	Scanner sc = new Scanner(System.in);
	private String name;
	private int p ;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getP() {
		System.out.println("现有票数:");
		p = sc.nextInt();
		
		for(;p>=0;p++) {
			
			
			if(p<100) {
				System.out.println("请输入名字:");
				name = sc.next();
				System.out.println(name+"投票一次现有票数"+(p+1));
			}else {
				System.out.println(name+"票数已满100,程序结束");
				break;
			}
			
		}
		
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	
	

}

package com.hz.ch02;

public class Lianxi {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vote m = new Vote();
		m.getP();
	}

}

 运行:

现有票数:
99
请输入名字:
111
111投票一次现有票数100
111票数已满100,程序结束

 练习3:使用继承实现电子宠物系统

需求说明 使用继承优化电子宠物系统 抽取父类,创建子类 在子类中使用super调用父类构造方法

package com.hz.ch03;
/**
 * 狗狗类
 * @author 26255
 *
 */
public class Dog extends Pet {
		private String strain;//品种

		public String getStrain() {
			return strain;
		}

		public void setStrain(String strain) {
			this.strain = strain;
		}
		
		public Dog() {
			
		}
		public Dog(String name,int health,int love,String strain) {
			super(name,health,love);
			this.strain = strain;
		}
		public void print() {
			super.print();
			System.out.println("我是一只"+strain);
		}
		
}
package com.hz.ch03;
/**
 * 企鹅类
 * @author 26255
 *
 */
public class Penguin extends Pet{
	private String sex;//性别

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public Penguin() {
		
	}
	
	public Penguin(String name,int health,int love,String sex) {
		super(name,health,love);
		this.sex = sex;
	}
	
	public void print() {
		super.print();
		System.out.println("我的性别是"+sex);
	}
	
}

package com.hz.ch03;
/**
 * 宠物类
 * @author 26255
 *
 */
public class Pet {
		private String name;//姓名
		private int health;//健康值
		private int love;//亲密度
		
		
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getHealth() {
			return health;
		}
		public  Pet() {
			
		}
		public Pet(String name,int health,int love) {
			this.name = name;
			this.health = health;
			this.love = love ;
		}
		public void setHealth(int health) {
			if(health<0 || health>100) {
				System.out.println("健康值应该在0-100之间,默认值为60。");
				this.health = 60;
				return;
			}
			this.health = health;
		}
		public int getLove() {
			return love;
		}
		public void setLove(int love) {
			if(love<0 || love>100) {
				System.out.println("亲密值应该在0-100之间,默认值为60。");
				this.love = 60;
				return;
			}
			this.love = love;
		}
		
		public void  print() {
			
			System.out.println("宠物的自白:");
			System.out.println("我的名字叫"+name+",健康值是"+health+",和主人的亲密度是"+love);
		}
		
}
package com.hz.ch03;

public class Exercise {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Dog dog = new Dog("zhang",77,49,"中华田园犬");
		Penguin pg = new Penguin("ddd",100,65,"q妹");
		dog.print();
		pg.print();
	}

}

 运行:

宠物的自白:
我的名字叫zhang,健康值是77,和主人的亲密度是49
我是一只中华田园犬
宠物的自白:
我的名字叫ddd,健康值是100,和主人的亲密度是65
我的性别是q妹

1.某公司要开发新游戏,请用面向对象的思想,设计游戏中的蛇怪和蜈蚣精
设定
1)蛇怪类:
属性包括:怪物名字,生命值,攻击力
方法包括:攻击,移动(曲线移动),补血(当生命值<10时,可以补加20生命值)
2)蜈蚣精类:
属性包括:怪物名字,生命值,攻击力
方法包括:攻击,移动(飞行移动)
要求
1)分析蛇怪和蜈蚣精的公共成员,提取出父类—怪物类
2)利用继承机制,实现蛇怪类和蜈蚣精类
3)攻击方法,描述攻击状态。内容包括怪物名字,生命值,攻击力
4)编写测试类,分别测试蛇怪和蜈蚣精的对象及相关方法

package com.hz.ch04;
/**
 * 蜈蚣类
 * @author 26255
 *
 */
public class Centipede extends Monster{

	public Centipede(String name, double life, double attack) {
		super(name, life, attack);
		
	}
	
	public void move() {
		System.out.println("飞行移动");
		
	}
}

package com.hz.ch04;



/**
 * 怪物类
 * @author 26255
 *
 */
public class Monster {
	private String name;//名字
	private double life;//生命
	private double attack;//公鸡力
	
	
	public Monster(String name, double life, double attack) {
		super();
		this.name = name;
		this.life = life;
		this.attack = attack;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getLife() {
		return life;
	}
	public void setLife(double life) {
		this.life = life;
	}
	public double getAttack() {
		return attack;
	}
	public void setAttack(double attack) {
		this.attack = attack;
	}
	//公鸡方法
	public void attack(Monster monster) {
		System.out.println(name+"展开了攻击");
		System.out.println("生命值是:"+life);
		System.out.println("攻击力是:"+attack);
		monster.life -= attack;
		System.out.println(name+"正在攻击"+monster.name+","+monster.name+"掉血"+attack+"剩余血量"+life);
		
	}
	public void move() {
		System.out.println(name+"移动了");
	}
	
	
}
package com.hz.ch04;
/**
 * 蛇怪类
 * @author 26255
 *
 */
public class Snake extends Monster{

	public Snake(String name, double life, double attack) {
		super(name, life, attack);
		
	}

	public void move() {
		System.out.println("曲线移动");
	}
	public void buxue() {
		if(getLife() < 10 &&getLife()>0) {
			
			System.out.println("补血术当前为:"+getLife()+20);
		}
	}
	
}

package com.hz.ch04;

import java.util.Scanner;

public class Exercise {

	public static void main(String[] args) {
		Snake snake = new Snake("蛇怪",100,100);
		snake.move();
		snake.buxue();
		
		Centipede centipede = new Centipede("蜈蚣怪",200,10);
		centipede.move();
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入1开始游戏,输入0结束游戏");
		int sum = sc.nextInt(); 
		if(sum ==1 ) {
			System.out.println("开始游戏");
			System.out.println(centipede.getName()+"  VS  "+snake.getName()+"对战");
			for(int i = 1 ;i>0;i++) {
				System.out.println("第"+i+"回合");
				snake.attack(centipede);
				if(centipede.getLife()<=0) {
					System.out.println("蜈蚣死亡,蛇怪获胜!");
					break;
				}
				centipede.attack(snake);
				if(snake.getLife()<=0) {
					System.out.println("蛇怪获胜,蜈蚣死亡!");
					break;
				}
			}
		}else if(sum == 0) {
			System.out.println("游戏结束!");
		}else {
			System.out.println("输入错误,请重新输入");
		}
	}
	
}

运行:这个偷懒了

曲线移动
飞行移动
请输入1开始游戏,输入0结束游戏1
开始游戏
蜈蚣怪  VS  蛇怪对战
第1回合
蛇怪展开了攻击
生命值是:100.0
攻击力是:100.0
蛇怪正在攻击蜈蚣怪,蜈蚣怪掉血100.0剩余血量100.0
蜈蚣怪展开了攻击
生命值是:100.0
攻击力是:10.0
蜈蚣怪正在攻击蛇怪,蛇怪掉血10.0剩余血量100.0
第2回合
蛇怪展开了攻击
生命值是:90.0
攻击力是:100.0
蛇怪正在攻击蜈蚣怪,蜈蚣怪掉血100.0剩余血量90.0
蜈蚣死亡,蛇怪获胜!


2.请用面向对象的思想,设计自定义类描述演员和运动员的信息
设定
1)演员类:
属性包括:姓名,年龄,性别,毕业院校,代表作
方法包括:自我介绍
2)运动员类:
属性包括:姓名,年龄,性别,运动项目,历史最好成绩
方法包括:自我介始
要求
3)分析演员和运动员的公共成员,提取出父类—人类
4)利用继承机制,实现演员类和运动员类
5)编写测试类,分别测试人类,演员类和运动员类对象及相关方法
6)定义名为act的包存人类,演员类,运动员类和测试类

package com.hz.ch05;
/**
 * 演员类
 * @author 26255
 *
 */
public class Actor extends People {
	private String graduate = "坤学";
	private String masterpiece = "你太美";
	
	public Actor(String name) {
		super(name);
	}
	
	//自我介绍方法
		public void show() {
			super.show();
			System.out.println("毕业院校:"+this.graduate+"\t代表作:"+this.masterpiece);
			
		}
				
}
ackage com.hz.ch05;
/**
 * 运动员类
 * @author 26255
 *
 */
public class Athletes extends People{
		private String project = "跳远";
		private String best = "第一";
		
		public Athletes(String name) {
			super(name);
		}
		public void show() {
			super.show();
			System.out.println("项目:"+this.project+"\t最好成绩"+this.best);
		}
}
package com.hz.ch05;
/**
 * 人类
 * @author 26255
 *
 */
public class People {
	private String name;//姓名
	private int age ;//年龄
	private String sex;//性别
	//自我介绍
	public void show() {
		System.out.println("姓名:"+this.name+"\t年龄:"+this.age+"\t性别:"+this.sex);
	}
	
	public  People(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

package com.hz.ch05;

public class Exercise {

	public static void main(String[] args) {
		Actor actor = new Actor("爱坤");
		actor.setAge(25);
		actor.setSex("男坤");
		actor.show();		
		Athletes athlete = new Athletes("坤坤");
		athlete.setAge(22);
		athlete.setSex("未知坤");
		athlete.show();
	}

}

 运行:

姓名:爱坤	年龄:25	性别:男坤
毕业院校:坤学	代表作:你太美
姓名:坤坤	年龄:22	性别:未知坤
项目:跳远	最好成绩第一

4.程序分析题:
 public class A{
    public A(){
        System.out.println("a");
    }

 }
 public class B extends A{
     public B(){
    System.out.println("b");

    }
 
 }
 B b = new B();

 写出输出结果:

package com.hz.ch06;

public class Aa {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		B b = new B();
		
	}

}
package com.hz.ch06;

public class B extends Q{
    public B(){
	System.out.println("b");

   }
}
package com.hz.ch06;

public class Q {
	public Q(){
		System.out.println("q");
	}

}

运行:

q
b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒洋洋大魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值