import java.util.*;

public class Main{
   public static void main(String[] args) {
    System.out.println("Hello ten budha world!");
    System.out.println("Hello  budha world!");
      System.out.println("Hello human world!");

   MyStack a = new MyStack();
   a.push(48);
   a.push(10);
   a.push(5);
   a.push(40);
   a.push(7);
   a.push(8);
   a.push(3);
   a.push(3);
   a.push(1);
  a.push(5);
  a.displayStack();

  // a.sortStack();
  
  // a.displayStack();
  
   }
            
}
  public class MyStack {

 private static class StackNode {
 private int data;
 private StackNode next;
// private int min = 0;

 public StackNode(int data) {
 this.data = data;
 }
 }
// private int min = 0;

 private StackNode top;

 public int pop() {
// if (top == null) return;
int item = top.data;
top= top.next;
return item;
   }
public void push(int item) {
StackNode t = new StackNode(item);
t.next = top;
top= t;
// if(min > item) min = item;
  }
public int peek() {
if (top== null) throw new Error();
return top.data;
 }
// public int min() {
// return min;
// }

public boolean isEmpty() {
return top== null;
  }
public void displayStack(){
  System.out.println("The display of Stack is: \n");
  while(top!= null) System.out.println(pop());
}
  
public void sortStack(){

  
  // ============
  MyStack second = new MyStack();
  // int tmp = this.pop();
  while(!this.isEmpty()){
  // if(this.isEmpty()) return;
  // if(tmp> this.peek()){
    // second.push(tmp);
    int tmp = this.pop();
    while(!second.isEmpty() && tmp< second.peek()){
    // if(tmp< second.peek()){
      this.push(second.pop());
    // }
    }
    second.push(tmp);
    // tmp = this.pop();
  // } else{
  //   second.push(tmp);
  //   tmp = this.pop();
  // }
}
while(!second.isEmpty()){
  this.push(second.pop());
}
    //  so since it's recursive
    //  so in the end of every if else, condition need to have 
    //  the recursive call too
    //  and so need to have the base case at first so to stop the 
    //  recursive process
    //  so the base call here is the 1st stack empty??
    //  and here since there's no input value, so what to call for recursive???
    //  then from the suggestion of solution code, and so
    //  cleverly take on while loop approach :)))
    // here feel like the identifying problem step is quite important in figure
    //  solution, before, when stuck
    //  instead of look for what problem is, simply turn panic, terror, 
    //  freaked out, anxious, palpitating, sweating, tension,
    
    //  and so turn disappointed, discourage, scrub hair, ***** kair,
  //  tear my hair out, scratch skin and give up
  
  //  feel like another difficulty is to generalize specific example here
  //  into code

}
}
public class StackWithMin2 extends MyStack {
  // public static void main(String[] args) {
  //   System.out.println("Hello ten budha world!");
  //       System.out.println("Hello  budha world!");

  //   StackWithMin2 one = new StackWithMin2();
  //   one.push(7);
  //   System.out.println(one.peek());
  //   one.push(8);
  //   one.push(-42);
  //   System.out.println(one.min());

  //   one.push(49);
  //   one.push(1);
  //   one.push(-61);

  //   System.out.println(one.min());
  //   one.push(-20);
  //   System.out.println(one.min());
  //   one.push(-97);
  //   one.push(82);
  //   System.out.println(one.min());
  //   //  try experiment with array resizing
  //   int[] a = {0,1,3,4};
  //   int[] dest = new int[11];
  //   // int[] dest = Arrays.copyOf(a, 15);
  //   System.arraycopy(a,0,dest,0,a.length);
  //   a = dest;
  //   a[5] = 9; a[6] = 60;
  //   for(int i =0; i<a.length;i++){
  //     System.out.println("the value of a[i]: " + a[i]);
  //   }
  //   String[] b = new String[5];
  //   b[4] = "don't want to know";
  //   for(int i =0; i<b.length;i++){
  //     System.out.println("the value of b[i]: " + b[i]);
  //   }
  //   System.out.println("check if the null is also copy:");
  //   b = Arrays.copyOf(b, 10);
  //   for(int i =0; i<b.length;i++){
  //     System.out.println("the value of b[i]: " + b[i]);
  //   }
                  


            
  // }
  MyStack s2;
  public StackWithMin2() {
 s2 = new MyStack();
 }

 public void push(int value){
 if (value <= min()) {
 s2.push(value);
 }
 super.push(value);
 }
 public int pop() {
 int value = super.pop();
 if (value == min()) {
 s2.pop();
 }
 return value;
 }
 public int min() {
 if (s2.isEmpty()) {
 return Integer.MAX_VALUE;
 } else {
 return s2.peek();
 }
 }
 }



// wonder how to make a set of stack class that hand this??
// wonder if this set can extend stack or not???
//  feel like here it relate greatly to inheritance concept 
// much as min stack extend stack and create one stack
//  wonder how it allocate and save each stack created???
// 
public class setStack {
  // inital basic variables
  //  for to check if the stack is exceeding the thresold, need to track of current one
  int capacity;
  // stack_size to track the size of each stack
  int [] stack_size = new int[stack_num+1];
  static int stack_num = 3;
  public static int latest_stack_index = 0;
  //  another array to save the stack when need to call out maybe
  MyStack [] stack_array;
  

  public boolean SetIsFull(){
    return stack_num+1 == stack_size.length;
  }
  public void update_stack_size(){
    // int [] reallocate_size = new int[2*stack_num +1] ;
    stack_num = 2*stack_num +1;
    // System.arraycopy(stack_size, 0 , reallocate_size, 0, stack_size.length);
    stack_size = Arrays.copyOf(stack_size, stack_num+1);
    stack_array = Arrays.copyOf(stack_array, stack_num+1);
    Arrays.fill(stack_size,-1);
    // wonder here if the = equal assignment is proper or not???
  }
  
  
  public setStack(int capacity){
    this.capacity = capacity;
    MyStack base = new MyStack();
    stack_size = new int[stack_num+1];
    stack_array = new MyStack[stack_num+1];
    // var to check current values in the the stack is easily spot through the update_stack_size
    //  but for check in the latest index, maybe need to specify it;
  }
  public void push(int data){
    //  arise a problem, if the initial set stack is isEmpty
    //  and so the setStack[0] is empty and so only need to create new stack_size
    //  and so not to increment the latest_stack_index as in setStack with existing elements
    // so think of adding one element in array so to easily handle the zero case 
    //  and also here to avoid misunderstand between 0 of no stack vs 0 of no element after popped, 
    //  think of replacing it by -1
    if(stack_size[latest_stack_index] == -1 || stack_size[latest_stack_index] == capacity){
      if(SetIsFull()){
        update_stack_size();
      }
      latest_stack_index++;
      stack_size[latest_stack_index] = 1;
      stack_array[latest_stack_index] = new MyStack();
      stack_array[latest_stack_index].push(data);
    } else{
      stack_size[latest_stack_index]++;
    
      stack_array[latest_stack_index].push(data);
     }
   }
   
    //  so here need to check two thing maybe
    //  if the current stack is full or not
    //  and second, how to figure out the latest stack
    //  and thus how to call out the latest stack or specific stack to use???
    //  so create an array to hold such stack too???
    
    // =================
    //  feel maybe here is where the problem arise when confront a complex problem
    //  since before, try to solve the inequality, math, number theory, 
    // combination, number sequence, graphic theory, geometry
    //  each single problem combine tons of smaller
    //  the capacity at the time is to deal with easy, one-shot problem
    
    //  and so all the advanced one is always multitudious problem
    //  and so stuck in awe, at best
    //  and turn panic, freaked out, frozen, paralyzed
    // === 
    //  feel like here arise the new wonder
    //  if create array and so push and save data there, so how to save such stack
    // or simply create it and it's there
    //  the computer will handle it out
    
  
    //  when the push operation is full, what to do/??
    //  and wonder this will work as fullfill approach, which mean
    //  when the current one is full, new stack is automatically created/???
    //  find it not clear to do, assume that do in the first approach, then what//???
    
    public void pop(){
      //  so here when pop also consider the case when there's only one stack with one member left, and so
      //  after popped, will it turn to the zero index position???
      //  and will the stack be removed or still saved in array??? after popping all the element inside??
      // so to be safe first approach, temporarily remove such stack after popping to 0 maybe
      // and so case to deal with there's nothing in stack, what to return??
      if(stack_size[latest_stack_index] == -1 ){
        System.out.println("Stack is empty, nothing to pop!!!");
        return;
      }
      if(stack_size[latest_stack_index] ==1){
        stack_array[latest_stack_index].pop();
        stack_array[latest_stack_index] = null;
        stack_size[latest_stack_index] = -1;
        latest_stack_index --;
      } else{
        stack_array[latest_stack_index].pop();
        stack_size[latest_stack_index]--;
      }
    }
    
    // 
    
// 
  
}
 
by

Java online compiler

Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.

Taking inputs (stdin)

OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).

import java.util.Scanner;
class Input {
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	System.out.println("Enter your name: ");
    	String inp = input.next();
    	System.out.println("Hello, " + inp);
    }
}

Adding dependencies

OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies

apply plugin:'application'
mainClassName = 'HelloWorld'

run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }

repositories {
    jcenter()
}

dependencies {
    // add dependencies here as below
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}

About Java

Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.

Syntax help

Variables

short x = 999; 			// -32768 to 32767
int   x = 99999; 		// -2147483648 to 2147483647
long  x = 99999999999L; // -9223372036854775808 to 9223372036854775807

float x = 1.2;
double x = 99.99d;

byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;

Loops

1. If Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
  // code
} else {
  // code
}

Example:

int i = 10;
if(i % 2 == 0) {
  System.out.println("i is even number");
} else {
  System.out.println("i is odd number");
}

2. Switch:

Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.

switch(<conditional-expression>) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.

for(Initialization; Condition; Increment/decrement){  
    //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(<condition>){  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (<condition>); 

Classes and Objects

Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.

How to create a Class:

class keyword is required to create a class.

Example:

class Mobile {
    public:    // access specifier which specifies that accessibility of class members 
    string name; // string variable (attribute)
    int price; // int variable (attribute)
};

How to create a Object:

Mobile m1 = new Mobile();

How to define methods in a class:

public class Greeting {
    static void hello() {
        System.out.println("Hello.. Happy learning!");
    }

    public static void main(String[] args) {
        hello();
    }
}

Collections

Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.

Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:

  1. Interfaces
  2. Classes
  3. Algorithms

This framework also defines map interfaces and several classes in addition to Collections.

Advantages:

  • High performance
  • Reduces developer's effort
  • Unified architecture which has common methods for all objects.
CollectionDescription
SetSet is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc
ListList is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors
QueueFIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue.
DequeDeque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail)
MapMap contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc.