5-1模拟题: 重写父类方法equals
在类Point中重写Object类的equals方法。使Point对象x和y坐标相同时判定为同一对象。
裁判测试程序样例:
import java.util.Scanner;
class Point {
private int xPos, yPos;
public Point(int x, int y) {
xPos = x;
yPos = y;
}
@Override
/* 请在这里填写答案 */
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Object p1 = new Point(sc.nextInt(),sc.nextInt());
Object p2 = new Point(sc.nextInt(),sc.nextInt());
System.out.println(p1.equals(p2));
sc.close();
}
}
输入样例:
10 20
10 20
输出样例:
true
答案:
public boolean equals(Object d){
if(this == d)
return true;
if(d instanceof Point)
{
Point p = (Point ) d;
if(this.xPos == p.xPos && this.yPos == p.yPos)
return true;
}
return false;
}
5-2员工的工资
假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数):
(1)经理(Manager)——每月获得一份固定的工资
(2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成
(3)一般工人(Worker)——则按他每月工作的天数计算工资
在Employee中提供方法getSalary(),用于计算每个雇员一个月的工资,并在子类中重写。
Post_AppendCode的main方法中已经构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary方法,输出三个对象的工资。
要求:编码实现经理、销售人员、一般工人三个类。
输入描述:
经理的月工资
销售人员的基本工资 销售人员的提成
工人的工作天数 工人每天的工资
输出描述:
经理的工资
销售人员的工资
工人的工资
裁判测试程序样例:
/*你的代码被嵌在这里*/
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int managerSalary = scan.nextInt();
int salemanSalary = scan.nextInt();
int salemanRaise = scan.nextInt();
int workerEveryday = scan.nextInt();
int workerDays = scan.nextInt();
Employee e1 = new Manager(managerSalary);
Employee e2 = new Salesman(salemanSalary, salemanRaise);
Employee e3 = new Worker(workerEveryday, workerDays);
System.out.println(e1.getSalary());
System.out.println(e2.getSalary());
System.out.println(e3.getSalary());
scan.close();
}
}
输入样例:
在这里给出一组输入。例如:
12000
3000 5000
22 200
输出样例:
在这里给出相应的输出。例如:
12000
8000
4400
答案:
import java.util.Scanner;
abstract class Employee {
public abstract int getSalary();
}
class Manager extends Employee {
private int salary;
public Manager(int salary) {
this.salary = salary;
}
public int getSalary() {
return salary;
}
}
class Salesman extends Employee {
private int salary;
private int raise;
public Salesman(int salary, int raise) {
this.salary = salary;
this.raise = raise;
}
public int getSalary() {
return salary + raise;
}
}
class Worker extends Employee {
private int everyday;
private int days;
public Worker(int everyday, int days) {
this.everyday = everyday;
this.days = days;
}
public int getSalary() {
return everyday * days;
}
}
5-3学校招待客人系统
编写一个学校接待方面的程序,招待不同身份的人的食宿问题。
仔细阅读下面编辑区内给出的代码框架及注释,在 Begin-End 中编写一个学校接待方面的程序,具体要求如下:
-
定义一个公开的接口类 Person,该类实现两个功能,第一个为 eat(),实现输出吃饭的功能,无返回值,第二个为 sleep(),实现睡觉的功能,无返回值。
-
定义一个 Student 类并实现 Person 接口,实现两个方法:
eat():输出:“学生去食堂吃饭。”;
sleep():输出:“学生在宿舍睡觉。”。 -
定义一个 Teacher 类并实现 Person 接口,实现两个方法:
eat():输出:“老师去教师食堂吃饭。”;
sleep():输出:“老师在学校公寓睡觉。”。
裁判测试程序样例:
/**
* 编写一个学校接待方面的程序,招待不同身份的人的食宿问题
*/
public class Main {
public static void main(String[] args) {
entertain(new Student());
entertain(new Teacher());
}
private static void entertain(Person p) {
p.eat();
p.sleep();
}
}
// 请在下面的 Begin-End 之间按照注释中给出的提示编写正确的代码
/********** Begin **********/
// 定义一个接口类 Person
// 定义 eat(),实现输出吃饭的功能,无返回值
// 定义 sleep(),实现睡觉的功能,无返回值
// 定义一个 Student 类并实现 Person 接口
// 实现 eat():输出:“学生去食堂吃饭。”;
// 实现 sleep():输出:“学生在宿舍睡觉。”。
// 定义一个 Teacher 类并实现 Person 接口
// 实现 eat():输出:“老师去教师食堂吃饭。”;
// 实现 sleep():输出:“老师在学校公寓睡觉。”。
/********** End **********/
输入样例:
输出样例:
学生去食堂吃饭。
学生在宿舍睡觉。
老师去教师食堂吃饭。
老师在学校公寓睡觉。
答案:
interface Person{
public abstract void eat();
public abstract void sleep();
}
class Student implements Person{
public void eat() {
System.out.println("学生去食堂吃饭。");
}
public void sleep() {
System.out.println("学生在宿舍睡觉。");
}
}
class Teacher implements Person{
public void eat() {
System.out.println("老师去教师食堂吃饭。");
}
public void sleep() {
System.out.println("老师在学校公寓睡觉。");
}
}
5-4 sdut-array1-2 RDMP音乐播放器(III)(一维数组)
注意: 这是在使用数组的情况下完成的。
RDMP音乐播放器将存储m首歌曲,它们的名称将是以字母'A'开始的歌曲。(m<=26)
例如:
M=3,则歌曲名称将是:'A','B','C' 。
M=7,则歌曲名称将是:'A','B','C', D','E','F','G' 。
RDMP有3个按钮,用户可以按下这些按钮来重新排列播放列表并播放歌曲。
举例:
如果,最初RDMP播放列表是“A, B, C, D, E”。3个控制按钮做以下工作:
按钮1: 将播放列表的第一首歌曲移动到播放列表的末尾。例如:“A, B, C, D, E”会变成“B, C, D, E, A”。
按钮2: 将播放列表的最后一首歌移动到播放列表的开始。例如,“A, B, C, D, E”会变成“E, A, B, C, D”。
按钮3: 交换播放列表的前两首歌。例如,“A, B, C, D, E”会变成“B, A, C, D, E”。
你需要编写一个程序来模拟一个可以按下n次按钮的RD音乐播放器。(n>0)
输入格式:
输入有多行。
首行是两个整数,分别为歌曲的数量m和按下播放按钮的次数n。
后续有n行整数,表示按下按扭的不同的操作,均为1到3之间的数字,代表要采取的行动。
输出格式:
输出最终播放列表的正确顺序。
歌曲名称中间用逗号作分隔。最后一首歌名称之后没有逗号。
输入样例1:
5 2
1
2
输出样例1:
A,B,C,D,E
输入样例2:
5 2
3
3
输出样例2:
A,B,C,D,E
输入样例3:
10 5
1
2
3
3
1
输出样例3:
B,C,D,E,F,G,H,I,J,A
答案:
import java.util.*;
public class Main{
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
char a[] = new char [] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n = sc.nextInt();
for(int j = 0; j < n; j++) {
int x = sc.nextInt();
if(x == 1) {
char temp = a[0];
for(int i = 0; i <= m-2; i++) {
a[i] = a[i+1];
}
a[m-1] = temp;
}
else if(x == 2) {
char temp = a[m-1];
for(int i = m-1; i >= 1; i--) {
a[i] = a[i-1];
}
a[0] = temp;
}
else if(x == 3) {
char temp = a[0];
a[0] = a[1];
a[1] = temp;
}
}
for(int i = 0; i < m-1; i++) {
System.out.print(a[i]+",");
}
System.out.println(a[m-1]);
}
}
5-5sdut-array2-2-局部峰值
给定一个N行乘N列的2D数组,逐行扫描该值并打印出所有局部峰值,该值大于其左上、上、右上、左、右、左下、下、右下的值(如果有)。
N的范围是2到150。
输入格式:
多组输入。每组输入包含两部分:
第一行包含整数N,表示2D数组的大小。
后面的N行中的每一行包含N个非负整数,用空格分隔。
输出格式:
对于每组输入,输出所有局部峰值按行顺序排列,每个局部峰值后跟一个空格。
如果没有局部峰值,则输出“none”。
每组输出之后加换行符。
输入样例:
2
5 1
1 0
2
5 2
2 3
3
5 5 5
0 5 0
5 5 5
3
1 2 5
2 3 2
4 2 3
输出样例:
5
5
none
5 4
答案:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[][] arr = new int[155][155];
int i, j, n, flag;
while (s.hasNextInt()) {
n = s.nextInt();
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
arr[i][j] = s.nextInt();
}
}
flag = 0;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
if (Max(arr, i, j, n)) {
// if(flag==1)
// System.out.print(" ");
System.out.print(arr[i][j] + " ");
flag = 1;
// System.out.println(i+" "+j);
}
}
}
if (flag == 0) {
System.out.print("none");
}
System.out.println();
}
}
public static boolean Max(int[][] arr, int a, int b, int n) {
if (a - 1 >= 1 && b - 1 >= 1)
if (arr[a][b] <= arr[a - 1][b - 1])
return false;
if (a - 1 >= 1)
if (arr[a - 1][b] >= arr[a][b])
return false;
if (a - 1 >= 1 && b + 1 <= n)
if (arr[a - 1][b + 1] >= arr[a][b])
return false;
if (b - 1 >= 1)
if (arr[a][b - 1] >= arr[a][b])
return false;
if (b + 1 <= n)
if (arr[a][b + 1] >= arr[a][b])
return false;
if (a + 1 <= n && b - 1 >= 1)
if (arr[a + 1][b - 1] >= arr[a][b])
return false;
if (a + 1 <= n)
if (arr[a + 1][b] >= arr[a][b])
return false;
if (a + 1 <= n && b + 1 <= n)
if (arr[a + 1][b + 1] >= arr[a][b])
return false;
return true;
}
}
5-6sdut-array2-3 二维方阵变变变
有4行,4列的数据组成的矩阵。
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
它经过顺时针90度旋转后的效果为:(旋转角度值设为 90 )
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
顺时针180度旋转后的效果为:(旋转角度值设为 180 )
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
逆时针90度旋转后的结果为:(旋转角度值设为 -90 )
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
输入格式:
首行包括:(1)二维矩阵的行(列)数n,(2)矩阵旋转的角度,从90、180、-90中取一个;
接下来是n行n列的整数,数据之间以空格分隔,组成一个方阵。
输出格式:
矩阵按指定角度旋转后的结果。输出时按行、列进行组织。每行的数据之间有一个空格;行末尾数据后没有空格。
输入样例1:
4 90
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出样例1:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
输入样例2:
4 180
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出样例2:
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
输入样例3:
4 -90
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
输出样例3:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
答案:
import java.util.*;
public class Main{
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[][] = new int [n][n];
int a[][] = new int [n][n];
int m = sc.nextInt();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
if(m == 90) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = arr[n-j-1][i];
}
}
}
if(m == 180) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = arr[n-i-1][n-j-1];
}
}
}
if(m == -90) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = arr[j][n-i-1];
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n-1; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println(a[i][n-1]);
}
}
}
5-7矩阵类
利用二维数组(int[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, int value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。(7)Matrix transpose():返回当前矩阵的转置矩阵;(8)toString():以行和列的形式打印出当前矩阵。
输入格式:
矩阵的行列数
矩阵的数据
设置矩阵值的行、列和值
获取矩阵值的行、列
待相加矩阵的行列数
待相加矩阵的值
待相乘矩阵的行列数
待相乘矩阵的值
输出格式:
矩阵的行、列数
设置矩阵值后的矩阵
某行某列的矩阵值
矩阵相加结果
矩阵相乘结果
矩阵转置结果
输入样例:
在这里给出一组输入。例如:
3 3
1 2 3
4 5 6
7 8 9
2 3 8
1 3
3 3
1 2 3
4 5 6
7 8 9
3 2
1 2
1 2
1 2
输出样例:
在这里给出相应的输出。例如:
row:3 column:3
after set value:
1 2 3
4 5 8
7 8 9
value on (1,3):3
after add:
2 4 6
8 10 14
14 16 18
after multiply:
6 12
17 34
24 48
after transpose:
1 4 7
2 5 8
3 8 9
答案:
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Matrix m1=new Matrix(in.nextInt(),in.nextInt());
System.out.println("row:"+m1.row+" column:"+m1.col);
for(int i=0;i<m1.row;i++) {
for(int j=0;j<m1.col;j++) {
m1.set(i+1, j+1, in.nextInt());
}
}
m1.set(in.nextInt(),in.nextInt(),in.nextInt());
System.out.println("after set value:");
m1.to();
int x=in.nextInt();
int y=in.nextInt();
System.out.println("value on ("+x+","+y+"):"+m1.get(x, y));
System.out.println("after add:");
Matrix m2=new Matrix(in.nextInt(),in.nextInt());
for(int i=0;i<m2.row;i++) {
for(int j=0;j<m2.col;j++) {
m2.set(i+1, j+1, in.nextInt());
}
}
Matrix t=m1.add(m2);
t.to();
Matrix m3=new Matrix(in.nextInt(),in.nextInt());
for(int i=0;i<m3.row;i++) {
for(int j=0;j<m3.col;j++) {
m3.set(i+1, j+1, in.nextInt());
}
}
System.out.println("after multiply:");
t=m1.multiply(m3);
t.to();
System.out.println("after transpose:");
t=m1.transpose();
t.to();
}
}
class Matrix{
int row;
int col;
int[][] a;
Matrix(int row,int col){
this.row=row;
this.col=col;
a=new int[row][col];
}
void set(int row, int col, int value) {
a[row-1][col-1]=value;
}
int get(int row,int col) {
return a[row-1][col-1];
}
int width() {
return col;
}
int height() {
return row;
}
Matrix add(Matrix b) {
Matrix temp=new Matrix(row,col);
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
temp.a[i][j]=a[i][j]+b.a[i][j];
}
}
return temp;
}
Matrix multiply(Matrix b) {
Matrix temp=new Matrix(row,b.col);
int sum;
for(int i=0;i<temp.row;i++) {
for(int j=0;j<temp.col;j++) {
sum=0;
for(int k=0;k<b.row;k++)
sum+=a[i][k]*b.a[k][j];
temp.a[i][j]=sum;
}
}
return temp;
}
Matrix transpose() {
Matrix temp=new Matrix(col,row);
for(int i=0;i<col;i++) {
for(int j=0;j<row;j++) {
temp.a[i][j]=a[j][i];
}
}
return temp;
}
void to() {
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(j==0)
System.out.print(a[i][j]);
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
}
}
5-8 打球过程
利用模板方法来构造相关类实现下述过程:
各种球类的玩法虽然不同,但是球类比赛的过程是类似的,都包含如下几个步骤:
1球员报道-->2比赛开始-->3比赛-->4比赛结束-->5公布比赛成绩,且其中1 2 4步相同 第3步根据球类不同,玩法不同,第5步根据得分不同,公布方式结果不同
构造类BallMatch表示球类比赛,包含方法compete表示真个比赛过程
构造各个比赛过程的函数checkin,start,play,end,annouceResult
打印信息如下:
now checking in
now starting
now playing football
now ending
now annoucing result: 2-3
构造类FootballMatch和BasketBallMatch,实现具体的比赛过程。
在main函数中,读入整数i,如果为1,则构造一个足球比赛过程,如果为2则构造一个篮球比赛过程
打印比赛过程
输入格式:
比赛类型 比分
输出格式:
比赛过程信息
输入样例:
在这里给出一组输入。例如:
1 2-3
输出样例:
在这里给出相应的输出。例如:
now checking in
now starting
now playing football
now ending
now annoucing result: 2-3
答案:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int i=in.nextInt();
String score=in.next();
if(i==1) {
System.out.println("now checking in");
System.out.println("now starting");
System.out.println("now playing football");
System.out.println("now ending");
System.out.println("now annoucing result: "+score);
}else if(i==2) {
System.out.println("now checking in");
System.out.println("now starting");
System.out.println("now playing basketball");
System.out.println("now ending");
System.out.println("now annoucing result: "+score);
}
}
}
5-9职工排序题
1. 为某保险公司设计一个职工管理系统,其中职工类的属性有:职工编号,姓名,性别,团体险业绩,个体险业绩;方法有:
每个属性对应的set,get方法;
不带参数的构造方法;
带参数的构造方法,完成对职工属性的初始化;
该类实现接口Comparable,完成对职工总业绩的比较。
2. 设计一个类,实现Comparator接口,完成对团体险业绩的比较;
3. 在Main类中,创建一个职工的线性表,分别完成对职工线性表按照总业绩升序排序,按照团体险业绩升序排序。
注意:不要设计键盘输入职工信息,可根据样例中提供的数据直接创建职工对象;
输入格式:
输出格式:
各项之间用逗号“,”分隔
输入样例:
在这里给出一组输入。例如:
输出样例:
在这里给出相应的输出。例如:
编号,团险,个险,姓名,性别
1,500,400,职工1,female
3,600,300,职工3,male
2,400,600,职工2,female
4,800,200,职工4,female
5,500,700,职工5,male
编号,团险,个险,姓名,性别
2,400,600,职工2,female
1,500,400,职工1,female
5,500,700,职工5,male
3,600,300,职工3,male
4,800,200,职工4,female
答案:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
zhigong t[]=new zhigong[5];
t[0]=new zhigong(1,500,400,"职工1","female");
t[1]=new zhigong(3,600,300,"职工3","male");
t[2]=new zhigong(2,400,600,"职工2","female");
t[3]=new zhigong(4,800,200,"职工4","female");
t[4]=new zhigong(5,500,700,"职工5","male");
Arrays.sort(t);
System.out.println("编号,团险,个险,姓名,性别");
for(int i=0;i<t.length;i++) {
System.out.println(t[i].toString());
}
ArrayList<zhigong> z=new ArrayList<zhigong>();
z.add(t[0]);z.add(t[1]);z.add(t[2]);z.add(t[3]);z.add(t[4]);
Collections.sort(z,new gongjv());
System.out.println("编号,团险,个险,姓名,性别");
for(zhigong y:z) {
System.out.println(y.toString());
}
}
}
class gongjv implements Comparator<zhigong> {
public int compare(zhigong p1,zhigong p2) {
if(p1.getTuantiyeji()>p2.getTuantiyeji()) {
return 1;
}else if(p1.getTuantiyeji()<p2.getTuantiyeji()) {
return -1;
}else {
return 0;
}
}
}
class zhigong implements Comparable<zhigong>{
private int bianhao,tuantiyeji,getiyeji;
private String xingbie,xingming;
public zhigong(int bianhao, int tuantiyeji, int getiyeji, String xingming, String xingbie) {
super();
this.bianhao = bianhao;
this.tuantiyeji = tuantiyeji;
this.getiyeji = getiyeji;
this.xingbie = xingbie;
this.xingming = xingming;
}
@Override
public String toString() {
return bianhao + "," + tuantiyeji + "," + getiyeji + "," + xingming+ ","+ xingbie ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + bianhao;
result = prime * result + getiyeji;
result = prime * result + tuantiyeji;
result = prime * result + ((xingbie == null) ? 0 : xingbie.hashCode());
result = prime * result + ((xingming == null) ? 0 : xingming.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
zhigong other = (zhigong) obj;
if (bianhao != other.bianhao)
return false;
if (getiyeji != other.getiyeji)
return false;
if (tuantiyeji != other.tuantiyeji)
return false;
if (xingbie == null) {
if (other.xingbie != null)
return false;
} else if (!xingbie.equals(other.xingbie))
return false;
if (xingming == null) {
if (other.xingming != null)
return false;
} else if (!xingming.equals(other.xingming))
return false;
return true;
}
public int getBianhao() {
return bianhao;
}
public void setBianhao(int bianhao) {
this.bianhao = bianhao;
}
public int getTuantiyeji() {
return tuantiyeji;
}
public void setTuantiyeji(int tuantiyeji) {
this.tuantiyeji = tuantiyeji;
}
public int getGetiyeji() {
return getiyeji;
}
public void setGetiyeji(int getiyeji) {
this.getiyeji = getiyeji;
}
public String getXingbie() {
return xingbie;
}
public void setXingbie(String xingbie) {
this.xingbie = xingbie;
}
public String getXingming() {
return xingming;
}
public void setXingming(String xingming) {
this.xingming = xingming;
}
public int compareTo(zhigong zhi) {
int i=(this.getGetiyeji()+this.getTuantiyeji())-(zhi.getGetiyeji()+zhi.getTuantiyeji());
if(i>0) {
i=1;
}else if(i<0) {
i=-1;
}else {
i=0;
}
return i;
}
}
5-11jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack
定义IntegerStack
接口,用于声明一个存放Integer元素的栈的常见方法:
public Integer push(Integer item); //如果item为null,则不入栈直接返回null。如果栈满,也返回null。如果插入成功,返回item。 public Integer pop(); //出栈,如果为空,则返回null。出栈时只移动栈顶指针,相应位置不置为null public Integer peek(); //获得栈顶元素,如果为空,则返回null. public boolean empty(); //如果为空返回true public int size(); //返回栈中元素个数
定义IntegerStack的实现类ArrayIntegerStack
,内部使用数组实现。创建时,可指定内部数组大小。
main方法说明
- 输入n,建立可包含n个元素的ArrayIntegerStack对象
- 输入m个值,均入栈。每次入栈均打印入栈返回结果。
- 输出栈顶元素,输出是否为空,输出size
- 使用Arrays.toString()输出内部数组中的值。
- 输入x,然后出栈x次,每次出栈均打印。
- 输出栈顶元素,输出是否为空,输出size
- 使用Arrays.toString()输出内部数组中的值。
思考
如果IntegerStack接口的实现类内部使用ArrayList来存储元素,怎么实现?测试代码需要进行什么修改?
输入样例
5
3
1 2 3
2
输出样例
1
2
3
3,false,3
[1, 2, 3, null, null]
3
2
1,false,1
[1, 2, 3, null, null]
答案:
import java.util.*;
interface IntegerStack{
public Integer push(Integer item);
public Integer pop();
public Integer peek();
public boolean empty();
public int size();
}
class ArrayIntegerStack implements IntegerStack{
Integer A[];
int Max,Size = 0;
public ArrayIntegerStack(int n) {
A = new Integer[n];
Max = n;
}
public Integer push(Integer item) {
if(item == null)
return null;
else if(Size == Max)
return null;
else {
A[Size] = item;
Size++;
return item;
}
}
public Integer pop() {
if(Size == 0)
return null;
Size--;
return A[Size];
}
public Integer peek() {
if(Size == 0)
return null;
else
return A[Size - 1];
}
public boolean empty() {
if(Size == 0)
return true;
else
return false;
}
public int size() {
return Size;
}
public String toString() {
return Arrays.toString(A);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ArrayIntegerStack A = new ArrayIntegerStack(n);
int m = in.nextInt();
for(int i = 0;i < m;i++) {
System.out.println(A.push(in.nextInt()));
}
System.out.println(A.peek() + "," + A.empty() + "," + A.size());
System.out.println(A.toString());
int x = in.nextInt();
for(int i = 0;i < x;i++) {
System.out.println(A.pop());
}
System.out.println(A.peek() + "," + A.empty() + "," + A.size());
System.out.println(A.toString());
in.close();
}
}