第一题
编写程序,使用一维数组,模拟栈数据结构。
要求:
1、这个栈可以存储java中的任何引用类型的数据。
2、在栈中提供push方法模拟压栈。(栈满了,要有提示信息。)
3、在栈中提供pop方法模拟弹栈。(栈空了,也有有提示信息。)
4、编写测试程序,new栈对象,调用push pop方法来模拟压栈弹栈的动作。
主类
// 测试类
public class Test {
public static void main(String[] args) {
MyStack ms = new MyStack(5);
Object s = new String("我是栈内元素");
ms.pop();
ms.push(s);
ms.push(s);
ms.push(s);
ms.push(s);
ms.push(s);
ms.push(s);
ms.pop();
ms.pop();
ms.pop();
ms.pop();
ms.pop();
ms.pop();
}
}
MyStack类
// 栈类
public class MyStack {
// 数组长度,即栈的大小
private int max;
// 数组
private Object[] stack;
// 栈帧
private int index;
// 构造函数
public MyStack(){
}
public MyStack(int max) {
this.max = max;
stack = new Object[max];
this.index = -1;
}
// setter and getter
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
// 得到栈帧元素
public Object getStack() {
return stack[getIndex()];
}
// 修改栈帧元素
public void setStack(Object stack) {
this.stack[index - 1] = stack;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
// 入栈
public void push(Object obj){
if(this.stack[getMax() - 1] != null){
System.out.println("栈已满,入栈失败!");
}else{
this.stack[getIndex() + 1] = obj;
this.setIndex(getIndex() + 1);
System.out.println("入栈成功,目前栈帧为" + this.getIndex());
}
}
// 弹栈
public void pop(){
if(this.stack[0] == null){
System.out.println("栈已空,弹栈失败!");
}else{
this.stack[getIndex()] = null;
this.setIndex(getIndex() - 1);
System.out.println("弹栈成功,目前栈帧为" + getIndex());
}
}
}
运行结果
栈已空,弹栈失败!
入栈成功,目前栈帧为0
入栈成功,目前栈帧为1
入栈成功,目前栈帧为2
入栈成功,目前栈帧为3
入栈成功,目前栈帧为4
栈已满,入栈失败!
弹栈成功,目前栈帧为3
弹栈成功,目前栈帧为2
弹栈成功,目前栈帧为1
弹栈成功,目前栈帧为0
弹栈成功,目前栈帧为-1
栈已空,弹栈失败!
Process finished with exit code 0
第二题
(java软件工程师人生路上第一个小型项目。锻炼一下面向对象。)
为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台。
2、酒店使用一个二维数组来模拟。“Room[][] rooms;”
3、酒店中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲.
5、系统应该对外提供的功能:
可以预定房间:用户输入房间编号,订房。
可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。
系统入口【主类】
// 系统主入口
public class Test {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
Function fc = new Function();
System.out.print("请输入酒店楼层:");
int floor = s.nextInt();
System.out.print("每层房间数:");
int roomNum = s.nextInt();
fc.setRoom(floor,roomNum);
System.out.println("请完成房间信息录入!");
for (int i = 0; i < fc.getRoom().length; i++) {
for (int j = 0; j < fc.getRoom()[i].length; j++) {
System.out.println("请依次输入房间号、房间类型、房间是否空闲(填是或否):");
fc.getRoom()[i][j] = new Room();
fc.getRoom()[i][j].setNum(s.nextInt());
fc.getRoom()[i][j].setType(s.next());
fc.getRoom()[i][j].setFree(s.next().equals("是"));
}
}
while(true){
System.out.println("**********************************");
System.out.println(" 欢迎来到酒店管理系统");
System.out.println(" 1:查询单个房间信息");
System.out.println(" 2:预定房间");
System.out.println(" 3:退房");
System.out.println(" 4:查询所有房间信息");
System.out.println(" 0:退出系统");
System.out.println(" 其他功能等待后续更新");
System.out.println("**********************************");
int i = -1;
switch (s.nextInt()) {
case 1 -> {
System.out.println("**********************************");
System.out.print("请输入查找房间号(输入0:返回上一步):");
if ((i = s.nextInt()) != 0)
fc.searchSingleRoom(i);
}
case 2 -> {
System.out.println("**********************************");
System.out.print("请输入查找房间号(输入0:返回上一步):");
if ((i = s.nextInt()) != 0)
fc.reserve(i);
}
case 3 -> {
System.out.println("**********************************");
System.out.print("请输入退房房间号(输入0:返回上一步):");
if ((i = s.nextInt()) != 0)
fc.checkOut(i);
}
case 4 -> {
System.out.println("**********************************");
fc.searchAllRoom();
}
case 5 -> System.exit(0);
default -> System.out.println("请输入正确的选项!");
}
}
}
}
房间类
// 房间类
public class Room {
// 房间编号、类型、是否空闲
private int num;
private String type;
private boolean isFree;
//构造函数
public Room(){
}
public Room(int num, String type, boolean isFree) {
this.num = num;
this.type = type;
this.isFree = isFree;
}
// setter and getter
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isFree() {
return isFree;
}
public void setFree(boolean free) {
isFree = free;
}
@Override
public String toString() {
return "房间信息:" +
"\n房间号:" + num +
"\n类型:" + type +
"\n状态:" + (isFree ? "空闲\n" : "已满\n");
}
}
功能类
public class Function {
private Room[][] room;
// 构造函数
public Function(){
}
public Function(int floor, int roomNum){
this.room = new Room[floor][roomNum];
}
// setter and getter
// 获取单个房间对象
public Room[][] getRoom() {
return this.room;
}
// 设置单个房间对象
public void setRoom(int floor, int roomNum) {
this.room = new Room[floor][roomNum];
}
// 预定房间
public void reserve(int num){
for (Room[] rooms : this.room) {
for (Room value : rooms) {
if (value.getNum() == num) {
if(value.isFree()){
value.setFree(false);
System.out.println("预定成功!");
}else{
System.out.println("该房间已被预定!");
}
return;
}
}
}
System.out.println("房间号输入错误,没有此房间!");
}
// 退房
public void checkOut(int num){
for (Room[] rooms : this.room) {
for (Room value : rooms) {
if (value.getNum() == num) {
if(!value.isFree()){
value.setFree(true);
System.out.println("该房间已退!");
}else{
System.out.println("错误,房间未住人!");
}
return;
}
}
}
System.out.println("房间号输入错误,没有此房间!");
}
// 查询单个房间状态
public void searchSingleRoom(int num){
for (Room[] rooms : this.room) {
for (Room value : rooms) {
if (value.getNum() == num) {
System.out.print(value);
return;
}
}
}
System.out.println("未找到该房间!");
}
// 查询所有房间信息
public void searchAllRoom(){
for (Room[] rooms : this.room) {
for (Room value : rooms) {
System.out.print(value);
}
}
}
}