1.机器人设计
设计一种机器人,可以移动,变形。机器人有控制芯片,可以更换,并且根据机器人内部的芯片,更改行为方式,比如移动方式有步行,跑步等,变形可以变成汽车,飞机等。
要求:
1、给出面向对象的设计(类图);
2、给出Java代码实现。
interface ControlChip{
void move(); //控制芯片接口
void transform();
}
class Robot{
//机器人类
ControlChip controlChip;
public Robot(ControlChip controlChip){
this.controlChip=controlChip; //构造方法
}
public void move(){
controlChip.move(); //控制芯片对应的移动方法
}
public void transform(){
controlChip.transform();
}
}
class WalkControlChip implements ControlChip{
//步行芯片
@Override
public void move() {
System.out.println("步行。。。");
}
@Override
public void transform() {
System.out.println("变形");
}
}
class RunningControlChip implements ControlChip{
//跑步芯片
@Override
public void move() {
System.out.println("跑步。。。");
}
@Override
public void transform() {
System.out.println("变形");
}
}
class CarControlChip implements ControlChip{
//汽车芯片
@Override
public void move() {
System.out.println("移动");
}
@Override
public void transform() {
System.out.println("汽车变形");
}
}
class AirPlaneControlChip implements ControlChip{
//飞机芯片
@Override
public void move() {
System.out.println("移动");
}
@Override
public void transform() {
System.out.println("飞机变形");
}
}
public class RobotTest {
public static void main(String[] args) {
WalkControlChip w=new WalkControlChip();
RunningControlChip r=new RunningControlChip();
CarControlChip c=new CarControlChip();
AirPlaneControlChip a=new AirPlaneControlChip();
Robot r1=new Robot(w);
r1.move();
r1.transform();
Robot r2=new Robot(c);
r2.move();
r2.transform();
}
}
2.智能形状识别器
设计一种智能形状识别器,能够识别出具体形状并输出相关信息。如传入圆形对象,则识别器则输出圆形、半径与面积等信息;传入矩形对象,能输出矩形、面积、长宽信息。
程序中识别功能不能使用if语句进行形状判断。
要求:
1、给出面向对象的设计(类图);
2、给出Java代码实现。
import java.util.List;
interface Shape{
double calculateArea();//形状接口,抽象出求面积方法
}
class Circle implements Shape{
//圆类
double radius;
double area; //面积
public Circle(double radius){
this.radius=radius;
}
@Override
public double calculateArea() {
return Math.PI*radius*radius;
}
public double getRadius(){
return this.radius;
}
}
class Rect implements Shape{
//矩形类
double length;
double width;
public Rect(double length,double width){
this.length=length;
this.width=width;
}
@Override
public double calculateArea(){
return length*width;
}
public double getLength(){
return length;
}
public double getWidth(){
return width;
}
}
class Recognize{
//识别器类
Shape shape; //形状
public Recognize(Shape shape){
this.shape=shape;
double area=shape.calculateArea();
if(shape instanceof Circle){
//判断是圆
Circle circle=(Circle) shape; //向下转换
System.out.println("Shape: Circle");
System.out.println("Circle Radius:"+circle.getRadius());
System.out.println("Circl Areae:"+area);
}else if(shape instanceof Rect){
Rect rect=(Rect) shape;
System.out.println("Shape: Rect");
System.out.println("Rect Length:"+rect.getLength());
System.out.println("Rect Width:"+rect.getWidth());
System.out.println("Rect Area:"+area);
}
}
}
public class RecognizerTest {
public static void main(String[] args) {
Circle circle=new Circle(10);
Rect rect=new Rect(6,8);
Recognize recognize1=new Recognize(circle);
Recognize recognize2=new Recognize(rect);
}
}
3.狗嗅+狗咬人
狗有多种嗅的功能,嗅到骨头流口水,嗅到老虎吓得跑,嗅到主人很高兴。
人有生命值(假设100),所有的狗都会咬人,藏獒(或泰迪)攻击一次可减少50(或1)个生命值。我先后养了藏獒、泰迪,都取了相同的宠物名-小黄。
结构小黄都咬人了。
要求:
1、给出面向对象的设计(类图);
2、给出Java代码实现。
class Person{
//人类
int health=100;
private Dog dog;
public int getHealth(){
return health;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void setHealth(int health){
this.health=health;
}
}
class Dog{
String name;
public void sniff(Object object){
if(object instanceof Bone){
System.out.println(name+"嗅到骨头流口水");
}else if(object instanceof Tiger){
System.out.println(name+"嗅到老虎吓得跑");
}else if(object instanceof Person){
System.out.println(name+"嗅到主人很高兴");
}
}
public void bite(Person person){
System.out.println(name+"咬人");
}
}
class Bone{
}
class Tiger{
}
class Tibetan extends Dog{
public Tibetan(String name){
this.name=name;
}
@Override
public void bite(Person person){
person.setHealth(person.getHealth()-50);
System.out.println(name+"咬人了");
}
}
class Teddy extends Dog{
public Teddy(String name){
this.name=name;
}
@Override
public void bite(Person person){
person.setHealth(person.getHealth()-1);
System.out.println(name+"咬人了");
}
}
public class DogTest {
public static void main(String[] args) {
Person p=new Person();
Tibetan t1=new Tibetan("小黄1");
Teddy t2=new Teddy("小黄2");
Bone b=new Bone();
p.setDog(t1);
t1.sniff(b);
t1.bite(p);
System.out.println(p.getHealth());
p.setDog(t2);
t2.sniff(b);
t2.bite(p);
System.out.println(p.getHealth());
}
}
4.银行账户攻防
假定银行账户能够提供存款、取款和查询余额操作。
系统受到黑客攻击,设计出一种程序,将取款多少变成存款多少。请实现上述要求。
要求:
1、给出面向对象的设计(类图);
2、给出Java代码实现。
class BankAccount{
private double balance;
public BankAccount(double balance){
this.balance=balance; //原有多少钱
}
public void deposit(double money){
this.balance=balance+money;
}
public void withdraw(double money){
deposit(money);
} //取款变成存款
public double getBalance(){
return balance;
}
}
public class BankTest {
public static void main(String[] args) {
BankAccount bankAccount=new BankAccount(1400);
System.out.println(bankAccount.getBalance());
bankAccount.deposit(400);
System.out.println(bankAccount.getBalance());
bankAccount.withdraw(500);
System.out.println(bankAccount.getBalance());
}
}