Java面向对象练习题(3)

一、题目描述

        假设一个房间内铺有m行n列瓷砖 一个跳蚤随机从一个瓷砖开始,每次随机选择一个方向,前进一个瓷砖, 当碰到墙时,代表此方向不能前进,试编程模拟此过程, 当跳蚤遍历所有瓷砖时,输出每块瓷砖被经历的次数和跳蚤跳跃的总次数, 要求严格按照面向对象的理论,要抽象出相应的类、进行必要的封装。

二、思路

根据题目的要求应该需要分成两个类,分别实现跳蚤的动作和房间的内容

House类的实现

1、私有成员变量的设置需要m,n以及整个房间的地面,用数组实现

2、构造方法的实现需要实现数组的初始化

3、自动生成需要的方法,对于数组的数据获取和设置需要单独实现

4、需要有一个检测有没有空格的方法

5、需要实现遍历地面所有瓷砖的方法

TiaoZao类的实现

1、设置跳蚤的跳跃方向为私有静态常量,私有成员变量为跳蚤的坐标,和跳跃的总次数以及初始化房间变量

2、构造方法需要对跳蚤的最初坐标进行初始化

3、自动生成需要的成员变量的获取和设置方法,以及其他方法

4、实现跳蚤之后的跳跃动作方法,根据跳蚤跳跃的方向进行数组的重新填制,每次遍历需要

5、实现跳蚤的移动方法,对每次遍历都需要对数组值和总次数值的改变

三、代码

package project;

class House {
	//私有化变量,对于地面的长宽和地面进行设置
	private int m;
	private int n;
	//利用数组设置瓷砖地面
	private int[][] a;
	
	//无参构造方法
	public House() {
		//房间一共十行十列
		m = 10;
		n = 10;
		a = new int[m][n];
		//对数组进行初始化
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				a[i][j] = 0;
	}
	
	//带参构造方法
	public House(int m, int n) {
		this.m = m;
		this.n = n;
		a = new int[m][n];
		//对数组进行初始化
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				a[i][j] = 0;
	}
	
	//获取房间瓷砖的列数
	public int getM() {
		return m;
	}
	
	//获取房间瓷砖的排数
	public int getN() {
		return n;
	}
	
	//获取房间地面的总数
	public int[][] getA() {
		return a;
	}
	
	//获取数组的数据
	public int getElement(int i, int j) {
		return a[i][j];
	}
	
	//设置数组的数据
	public void setElement(int i, int j, int v) {
		a[i][j] = v;
	}
	
	/**
	 * 实现检测房间瓷砖没有跳过的地方
	 * @return boolean 返回true 则有,反之则没有
	 */
	public boolean checkZero() {
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++) {
				if (a[i][j] == 0)
					return true;
			}
		return false;
	}
	
	/**
	 * 实现对数组的内容进行遍历,每一排换行
	 */
	public void display() {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				//输出数组的内容
				System.out.printf("%4d", a[i][j]);
			}
			System.out.println();
		}
	}
}

public class Tiaozao {
	//对于跳蚤的跳跃的方向设置成为静态
	private static final int UP = 0;
	private static final int DOWN = 1;
	private static final int RIGHT = 2;
	private static final int LEFT = 3;
	//设置跳蚤的坐标
	private int x, y;
	//设置跳跃的总数
	private int totals;
	//实现房间类型
	private House ahouse;
	
	//无参构造方法
	public Tiaozao(House h) {
		ahouse = h;
		totals = 0;
		//对跳蚤的坐标进行初始化
		x = (int) (Math.random() * ahouse.getM());
		y = (int) (Math.random() * ahouse.getN());

	}
	
	//获取跳跃的总次数
	public int getTotals() {
		return totals;
	}

	/**
	 * 设置跳蚤的移动方法,体现在跳蚤的坐标变化和数组内容的变化
	 * @param int direction 传入跳蚤跳跃的方向
	 * @return boolean 如果碰到墙即数组的边界,则不能移动,返回false,其他返回true
	 */
	public boolean walk(int direction) {
		switch (direction) {
		case UP:
			if (y == 0)
				return false;
			else {
				ahouse.setElement(x, y, ahouse.getElement(x, y) + 1);
				y = y - 1;
			}
			return true;
		case DOWN:
			if (y == ahouse.getN() - 1)
				return false;
			else {
				ahouse.setElement(x, y, ahouse.getElement(x, y) + 1);
				y = y + 1;
			}
			return true;
		case LEFT:
			if (x == 0)
				return false;
			else {
				ahouse.setElement(x, y, ahouse.getElement(x, y) + 1);
				x = x - 1;
			}
			return true;
		case RIGHT:
			if (x == ahouse.getM() - 1)
				return false;
			else {
				ahouse.setElement(x, y, ahouse.getElement(x, y) + 1);
				x = x + 1;
			}
			return true;
		default:
			System.out.println("非法移动!");
			return false;
		}
	}

	/**
	 * 实现跳蚤的移动方法,在跳过所有的瓷砖后退出循环
	 */
	public void move() {
		//设置方向
		int nextdirection;
		//设置移动是否成功
		boolean success;
		do {
			//随机方向的值
			nextdirection = (int) (Math.random() * 4);
			//实现跳蚤的移动
			success = walk(nextdirection);
			if (success)
				//移动成功,则总次数增加
				totals++;
		} while (ahouse.checkZero());//当遍历过所有的瓷砖后退出
	}
}

四、测试类

public class Lianxi5 {

	public static void main(String[] args) {
		House ahouse = new House(24, 22);
		Tiaozao atiaozao = new Tiaozao(ahouse);
		atiaozao.move();
		ahouse.display();
		System.out.println("Totals=" + atiaozao.getTotals());
	}

}

猜你喜欢

转载自blog.csdn.net/hc1151310108/article/details/80716110