Can someone help me to correct the error of this code so that it is able to run at Jeliot tool?
Can someone help me to correct the error of this code so that it is able to run at Jeliot tool? Thanks.
package com.company;
import jeliot.io.Output;
import java.util.ArrayList;
public class Main {
static MyStack stack = new MyStack();
public static void main(String[] args) {
beginLine();
firstInspect();
secondInspect();
thirdInspect();
}
static void beginLine() {
System.out.println("Beginning vehicle line...");
System.out.println("Pushing numer 2 to stack");
System.out.println("Pushing numer 1 to stack");
System.out.println("Pushing numer 0 to stack");
stack.push(2);
stack.push(1);
stack.push(0);
}
static void firstInspect() {
int item = stack.pop();
System.out.print("First inspection, popping first item: " + item);
System.out.println();
}
static void secondInspect() {
int item = stack.pop();
System.out.print("Second inspection, popping second item: " + item);
System.out.println();
}
static void thirdInspect() {
int item = stack.pop();
System.out.print("Third inspection, popping third item: " + item);
System.out.println();
}
static class MyStack {
ArrayList<Integer> list = new ArrayList<>();
void clear() {
this.list.clear();
}
int length() {
return this.list.size();
}
void push(int num) {
if (this.list.size() >= 3) {
Output.println("Stack is full!");
return;
}
this.list.add(num);
}
int pop() {
if (this.list.size() <= 0) {
Output.println("Stack is empty!");
return 0;
}
int length = this.list.size();
int target = this.list.get(length - 1);
this.list.remove(length - 1);
return target;
}
int topValue() {
if (this.list.size() <= 0) {
Output.println("Stack is empty!");
return 0;
}
return this.list.get(this.list.size() - 1);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images