I'm going to job interviews soon. How I can improve my coding style and write better code?
package com.amoghantarkar.Stack;
import java.util.*;
class ArrayStack{
private int capacity;
private int top;
private int[] array;
public ArrayStack(int capacity){
top = -1;
this.capacity = capacity;
array = new int[capacity];
}
public boolean isEmpty(){
return (top == -1);
}
public boolean isFull(){
return (top == (capacity-1));
}
public void push(int data){
if(isFull()){
System.out.println("Stack Overflow");
}
else{
array[++top] = data;
}
}
public void pop(){
if(isEmpty()){
System.out.println("Stack Underflow");
}
else{
System.out.println(array[top--]);
}
}
public void deleteStack(){
top = -1;
}
public void printTop(){
if(!isEmpty()){
System.out.println(array[top]);
}
else{
System.out.println("Stack is empty");
}
}
}
public class StackImplementation{
public static void main(String[] args){
ArrayStack testStack1 = new ArrayStack(10);
Scanner in = new Scanner(System.in);
while(true){
System.out.println("Stack Operations");
System.out.println(" 1.isEmpty\t 2.isFull\t 3. push\t 4.pop\t 5.printStack\t 6.Delete Stack");
int ch = in.nextInt();
switch(ch){
case 1:
boolean ans = testStack1.isEmpty();
System.out.println(ans);
break;
case 2:
ans = testStack1.isFull();
System.out.println(ans);
break;
case 3:
System.out.println("Enter data to push");
int data = in.nextInt();
testStack1.push(data);
System.out.println("Pushed data");
break;
case 4:
testStack1.pop();
System.out.println("Popped data");
break;
case 5:testStack1.printTop();
break;
case 6:testStack1.deleteStack();
break;
default: System.out.println("Enter valid number choice");
break;
}System.out.println("want to continue");
String str = in.next();
if(!str.equals("y")){
break;
}
}
}
}