import java.util.*; public class Main { public static void main(String[] args) { Date now = new Date(); System.out.println(""); System.out.println("now: " + now); public static void main (string args [])} { scanner num scanner (system.in); decimal format deci= new decimal format ("0.0000") system.out.println (" ") double num1= num next double () system.out.println(deci.format (num1)); } public class item{ public float public int {scanner num= new scanner num); public class BTNode<T> { private T value; private BTNode<T> left; private BTNode<T> right; BTNode(T v, BTNode<T> l, BTNode<T> r) { value = v; left = l; right = r; } public T getValue() {return value;} public BTNode<T> getLeft() {return left;} public BTNode<T> getRight() {return right;} public void setValue(T v) {value = v;} public void setLeft(BTNode<T> l) {left = l;} public void setRight(BTNode<T> r) {right = r;} } public class BTree<T> { private BTNode<T> root; BTree() { root = null; } public BTNode<T> getRoot() {return root;} public void setRoot(BTNode<T> r) {root = r;} public boolean isEmpty() { return root == null; } public int numberNodes() { return numberNodes(root); } private int numberNodes(BTNode<T> n) { if (n == null) return 0; return 1 + numberNodes(n.getLeft()) + numberNodes(n.getRight()); } public int depth() { return depth(root); } private int depth(BTNode<T> n) { if (n == null) return -1; return 1 + Math.max(depth(n.getLeft()), depth(n.getRight())); } public boolean contains(T value) { return contains(root, value); } private boolean contains(BTNode<T> n, T value) { if (n==null) return false; if (n.getValue().equals(value)) return true; return contains(n.getLeft(), value) || contains(n.getRight(), value); } public void printPreOrder() { System.out.print("PreOrder:"); printPreOrder(root); System.out.println(); } private void printPreOrder(BTNode<T> n) { if (n==null) return; System.out.print(" " + n.getValue() ); printPreOrder(n.getLeft()); printPreOrder(n.getRight()); } public void printInOrder() { System.out.print("InOrder:"); printInOrder(root); System.out.println(); } private void printInOrder(BTNode<T> n) { if (n==null) return; printInOrder(n.getLeft()); System.out.print(" " + n.getValue()); printInOrder(n.getRight()); } public void printPostOrder() { System.out.print("PostOrder:"); printPostOrder(root); System.out.println(); } private void printPostOrder(BTNode<T> n) { if (n==null) return; printPostOrder(n.getLeft()); printPostOrder(n.getRight()); System.out.print(" " + n.getValue()); } public void printBFS() { System.out.print("BFS:"); MyQueue<BTNode<T>> q = new LinkedListQueue<BTNode<T>>(); q.enqueue(root); while (!q.isEmpty()) { BTNode<T> cur = q.dequeue(); if (cur != null) { System.out.print(" " + cur.getValue()); q.enqueue(cur.getLeft()); q.enqueue(cur.getRight()); } } System.out.println(); } public void printDFS() { System.out.print("DFS:"); MyStack<BTNode<T>> q = new LinkedListStack<BTNode<T>>(); q.push(root); while (!q.isEmpty()) { BTNode<T> cur = q.pop(); if (cur != null) { System.out.print(" " + cur.getValue()); q.push(cur.getLeft()); q.push(cur.getRight()); } } System.out.println(); } } import java.util.Scanner; public class TestBTree { public static void main(String[] args) { Scanner in = new Scanner(System.in); BTree<Integer> t = LibBTree.readIntTree(in); System.out.println("numberNodes = " + t.numberNodes()); System.out.println("depth = " + t.depth()); System.out.println("contains(2) = " + t.contains(2)); System.out.println("contains(3) = " + t.contains(3)); t.printPreOrder(); t.printInOrder(); t.printPostOrder(); t.printBFS(); t.printDFS(); } } import java.util.Scanner; public class LibBTree { public static BTree<Integer> readIntTree(Scanner in) { BTree<Integer> t = new BTree<Integer>(); t.setRoot(readIntNode(in)); return t; } private static BTNode<Integer> readIntNode(Scanner in) { String s = in.next(); if (s.equals("N")) return null; Integer value = Integer.parseInt(s); BTNode<Integer> left = readIntNode(in); BTNode<Integer> right = readIntNode(in); return new BTNode<Integer>(value, left, right); public class BSTNode<T extends Comparable<? super T>> { private T value; private BSTNode<T> left; private BSTNode<T> right; BSTNode(T v, BSTNode<T> l, BSTNode<T> r) { value = v; left = l; right = r; } public T getValue() {return value;} public BSTNode<T> getLeft() {return left;} public BSTNode<T> getRight() {return right;} public void setValue(T v) {value = v;} public void setLeft(BSTNode<T> l) {left = l;} public void setRight(BSTNode<T> r) {right = r;} } public class BSTree<T extends Comparable<? super T>> { private BSTNode<T> root; BSTree() { root = null; } public BSTNode<T> getRoot() {return root;} public void setRoot(BSTNode<T> r) {root = r;} public boolean isEmpty() { return root == null; } public void clear() { root = null; } public int numberNodes() { return numberNodes(root); } private int numberNodes(BSTNode<T> n) { if (n == null) return 0; return 1 + numberNodes(n.getLeft()) + numberNodes(n.getRight()); } public boolean contains(T value) { return contains(root, value); } private boolean contains(BSTNode<T> n, T value) { if (n==null) return false; if (value.compareTo(n.getValue()) < 0) return contains(n.getLeft(), value); if (value.compareTo(n.getValue()) > 0) return contains(n.getRight(), value); } public boolean insert(T value) { if (contains(value)) return false; root = insert(root, value); return true; } private BSTNode<T> insert(BSTNode<T> n, T value) { if (n==null) return new BSTNode<T>(value, null, null); else if (value.compareTo(n.getValue()) < 0) n.setLeft(insert(n.getLeft(), value)); else if (value.compareTo(n.getValue()) > 0) n.setRight(insert(n.getRight(), value)); return n; } public boolean remove(T value) { if (!contains(value)) return false; root = remove(root, value); return true; } private BSTNode<T> remove(BSTNode<T> n, T value) { if (value.compareTo(n.getValue()) < 0) n.setLeft(remove(n.getLeft(), value)); else if (value.compareTo(n.getValue()) > 0) n.setRight(remove(n.getRight(), value)); else if (n.getLeft() == null) n = n.getRight(); else if (n.getRight() == null) n = n.getLeft(); else { BSTNode<T> max = n.getLeft(); while (max.getRight() != null) max = max.getRight(); n.setValue(max.getValue()); n.setLeft(remove(n.getLeft(), max.getValue())); } return n; } public int depth() { return depth(root); } private int depth(BSTNode<T> n) { if (n == null) return -1; return 1 + Math.max(depth(n.getLeft()), depth(n.getRight())); } public void printPreOrder() { System.out.print("PreOrder:"); printPreOrder(root); System.out.println(); } private void printPreOrder(BSTNode<T> n) { if (n==null) return; System.out.print(" " + n.getValue() ); printPreOrder(n.getLeft()); printPreOrder(n.getRight()); } public void printInOrder() { System.out.print("InOrder:"); printInOrder(root); System.out.println(); } private void printInOrder(BSTNode<T> n) { if (n==null) return; printInOrder(n.getLeft()); System.out.print(" " + n.getValue()); printInOrder(n.getRight()); } public void printPostOrder() { System.out.print("PostOrder:"); printPostOrder(root); System.out.println(); } private void printPostOrder(BSTNode<T> n) { if (n==null) return; printPostOrder(n.getLeft()); printPostOrder(n.getRight()); System.out.print(" " + n.getValue()); } public void printBFS() { System.out.print("BFS:"); MyQueue<BSTNode<T>> q = new LinkedListQueue<BSTNode<T>>(); q.enqueue(root); while (!q.isEmpty()) { BSTNode<T> cur = q.dequeue(); if (cur != null) { System.out.print(" " + cur.getValue()); q.enqueue(cur.getLeft()); q.enqueue(cur.getRight()); } } System.out.println(); } public void printDFS() { System.out.print("DFS:"); MyStack<BSTNode<T>> q = new LinkedListStack<BSTNode<T>>(); q.push(root); while (!q.isEmpty()) { BSTNode<T> cur = q.pop(); if (cur != null) { System.out.print(" " + cur.getValue()); q.push(cur.getLeft()); q.push(cur.getRight()); } } System.out.println(); } } class TestBSTree { public static void main(String[] args) { BSTree<Integer> t = new BSTree<Integer>(); int[] data = {14, 4, 18, 3, 9, 16, 20, 7, 15, 17, 5}; for (int i=0; i<data.length; i++) t.insert(data[i]); System.out.println("numberNodes = " + t.numberNodes()); System.out.println("depth = " + t.depth()); System.out.println("contains(2) = " + t.contains(2)); System.out.println("contains(3) = " + t.contains(3)); t.printPreOrder(); t.printInOrder(); t.printPostOrder(); t.remove(14); t.printPreOrder(); t.printInOrder(); t.printPostOrder(); import java.util.Comparator; public class MinHeap<T> { private T[] data; private int size; private Comparator<T> comparator; @SuppressWarnings("unchecked") MinHeap(int capacity) { data = (T[]) new Object[capacity+1]; size = 0; comparator = null; } MinHeap(int capacity, Comparator<T> comp) { this(capacity); comparator = comp; } public int size() { return size; } public boolean isEmpty() { return size==0; } public void insert(T value) { if (size + 1 >= data.length) throw new RuntimeException("Heap is full"); size++; data[size] = value; upHeap(size); } public T min() { if (isEmpty()) return null; return data[1]; } public T removeMin() { if (isEmpty()) return null; T min = data[1]; data[1] = data[size]; data[size] = null; size--; downHeap(1); return min; } private void upHeap(int i) { while (i>1 && smaller(i, i/2)) { swap(i, i/2); i = i/2; } } private void downHeap(int i) { while (2*i <= size) { int j = i*2; if (j<size && smaller(j+1, j)) j++; if (smaller(i, j)) break; swap(i, j); } } @SuppressWarnings("unchecked") private boolean smaller(int i, int j) { if (comparator == null) return ((Comparable<? super T>) data[i]).compareTo(data[j]) < 0; else return comparator.compare(data[i], data[j]) < 0; } private void swap(int i, int j) { T tmp = data[i]; data[i] = data[j]; data[j] = tmp; } } import java.util.Comparator; public class TestMinHeap { public static void main(String[] args) { MinHeap<Integer> h1 = new MinHeap<>(100); int[] v1 = {10,4,3,12,9,1,7,11,5,4}; for (int i=0; i<v1.length; i++) h1.insert(v1[i]); for (int i=0; i<v1.length; i++) System.out.print(h1.removeMin() + " "); System.out.println(); MinHeap<String> h2 = new MinHeap<>(100); String[] v2 = {"bbb", "aaaaa", "ee", "cccc", "d"}; for (int i=0; i<v2.length; i++) h2.insert(v2[i]); for (int i=0; i<v2.length; i++) System.out.print(h2.removeMin() + " "); System.out.println(); MinHeap<String> h = new MinHeap<>(100, new LengthComparator()); for (int i=0; i<v2.length; i++) h.insert(v2[i]); for (int i=0; i<v2.length; i++) System.out.print(h.removeMin() + " "); System.out.println(); } } class LengthComparator implements Comparator<String> { public int compare(String a, String b) { return a.length() - b.length(); public class BSTMapNode<K extends Comparable<? super K>, V> { private K key; private V value; private BSTMapNode<K,V> left; private BSTMapNode<K,V> right; BSTMapNode(K k, V v, BSTMapNode<K,V> l, BSTMapNode<K,V> r) { key = k; value = v; left = l; right = r; } public K getKey() {return key;} public V getValue() {return value;} public BSTMapNode<K,V> getLeft() {return left;} public BSTMapNode<K,V> getRight() {return right;} public void setKey(K k) {key = k;} public void setValue(V v) {value = v;} public void setLeft(BSTMapNode<K,V> l) {left = l;} public void setRight(BSTMapNode<K,V> r) {right = r;} } import java.util.LinkedList; public class BSTMap<K extends Comparable<? super K>,V> { private BSTMapNode<K,V> root; BSTMap() { root = null; } public BSTMapNode<K,V> getRoot() {return root;} public void setRoot(BSTMapNode<K,V> r) {root = r;} public boolean isEmpty() { return root == null; } public void clear() { root = null; } public int size() { return size(root); } private int size(BSTMapNode<K,V> n) { if (n == null) return 0; return 1 + size(n.getLeft()) + size(n.getRight()); } public V get(K key) { return get(root, key); } private V get(BSTMapNode<K,V> n, K key) { if (n==null) return null; if (key.compareTo(n.getKey()) < 0) return get(n.getLeft(), key); if (key.compareTo(n.getKey()) > 0) return get(n.getRight(), key); return n.getValue(); // se nao e menor ou maior, e porque e igual } public void put(K key, V value) { root = put(root, key, value); } private BSTMapNode<K,V> put(BSTMapNode<K,V> n, K key, V value) { if (n==null) return new BSTMapNode<K,V>(key, value, null, null); else if (key.compareTo(n.getKey()) < 0) n.setLeft(put(n.getLeft(), key, value)); else if (key.compareTo(n.getKey()) > 0) n.setRight(put(n.getRight(), key, value)); else n.setValue(value); return n; } public boolean remove(K key) { if (get(key)==null) return false; root = remove(root, key); return true; } private BSTMapNode<K,V> remove(BSTMapNode<K,V> n, K key) { if (key.compareTo(n.getKey()) < 0) n.setLeft(remove(n.getLeft(), key)); else if (key.compareTo(n.getKey()) > 0) n.setRight(remove(n.getRight(), key)); else if (n.getLeft() == null) n = n.getRight(); else if (n.getRight() == null) n = n.getLeft(); else { BSTMapNode<K,V> max = n.getLeft(); while (max.getRight() != null) max = max.getRight(); n.setKey(max.getKey()); n.setValue(max.getValue()); n.setLeft(remove(n.getLeft(), max.getKey())); } return n; } public LinkedList<K> keys() { LinkedList<K> list = new LinkedList<K>(); keys(root, list); return list; } private void keys(BSTMapNode<K,V> n, LinkedList<K> l) { if (n==null) return; keys(n.getLeft(), l); l.addLast(n.getKey()); keys(n.getRight(), l); } } import java.util.LinkedList; public class TestBSTMap { public static void main(String[] args) { BSTMap<String,Integer> map = new BSTMap<String,Integer>(); map.put("Life", 42); map.put("Zero", 0); map.put("Infinity", 999); System.out.println("size = " + map.size()); System.out.println("Value of \"Life\" = " + map.get("Life")); System.out.println("Value of \"Data\" = " + map.get("Data")); LinkedList<String> keys = map.keys(); System.out.println("keys = " + keys); System.out.println("All (key,value) pairs:"); for (String k : map.keys()) System.out.println("- (" + k + "," + map.get(k) + ")"); map.put("Life", 22); System.out.println("Value of \"Life\" = " + map.get("Life")); map.remove("Life"); System.out.println("Value of \"Life\" = " + map.get("Life")); }
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.
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);
}
}
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'
}
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.
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;
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");
}
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;
}
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
}
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
}
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>);
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.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
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:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |