Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Kaushal Shah Kaushal28

🎯
Focusing
  • Crest Data Systems
  • Ahmedabad
View GitHub Profile
View InstanceVarHiding.java
class Test {
int instanceVar = 10;
void hideInstance() {
int instanceVar = 15;
System.out.println("Local variable: " + instanceVar); //This will print local variable, as instance variable is hidden
//To access instance variable here, use this.instanceVariable
System.out.println("Instance variable: " + this.instanceVar);
}
}
View TypesOfExceptions.java
import java.io.FileNotFoundException;
/**
* Most common checked and unchecked exceptions
* https://stackoverflow.com/questions/1263128/most-common-checked-and-unchecked-java-exceptions
*/
class ExceptionExample {
//This is throwing (possibility of throwing) checked exception
void sampleMethod() throws FileNotFoundException {
View OverridingAndExceptions.java
/**
* Rule #1:
* If the super-class overridden method does not throw an exception,
* subclass overriding method can only throws the unchecked exception,
* throwing checked exception will lead to compile-time error.
*
* Rule #2:
* If the super-class overridden method does throws an exception, subclass
* overriding method can only throw same, subclass exception. Throwing parent
* exception in Exception hierarchy will lead to compile time error. Also there
View MultipleInheritance.java
interface one {
public void printOne();
}
interface two {
public void printTwo();
}
interface three extends one, two{
public void printThree();
View SwappingObjs.java
class Car {
int number;
Car(int number) {
this.number = number;
}
void printNumber() {
System.out.println(this.number);
}
View LabelsExample.java
public class LabelsExample {
public static void main (String[] args) {
//Labels with break; Similar functionality like goto
outerLoop:
for (int i=0;i<10;i++) {
for (int j=0;j<10;j++) {
if (j == 1) {
System.out.println(j);
break outerLoop;
View FinalVariableExample.java
class TestFinalVariable {
public final int foo;
// If variable is static and final, it must be initialized right away.
public static final int bar = 10;
//In constructor, initialization of final variable is allowed, as compiler knows that the constructor can only be called 1 time per object.
//Static variables can not be initialized even in constructor, as they are shared between multiple objects and that's why multiple object
//initialization can updathe the same copy of final variable, which is not allowed and thus static final variable must be initialized on declaration
TestFinalVariable(int foo) {
this.foo = foo;
View EnumsExample.java
import java.util.EnumSet;
import java.util.EnumMap;
enum Directions {
// Abstract method needs to be overriden in each of the enums
// First line of Enum should be always enum declaration. Constructor, variables declaration not allowed.
EAST(0) {
@Override
public void printDirection() {
View ShutDownHookExample.java
class ShutDownHookExample {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Shutdown hook is running.");
}
});
System.out.println("Application terminating.");
}
}
View transition_matrix.py
import numpy as np
T = np.load('datasets/T.npy')
for i in range(12):
for j in range(4):
print (T[i,:,j])
'''
[0.9 0.1 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ] for starting state (1,1) and action Down
[0.9 0. 0. 0. 0.1 0. 0. 0. 0. 0. 0. 0. ] for starting state (1,1) and action Left
[0.1 0.1 0. 0. 0.8 0. 0. 0. 0. 0. 0. 0. ] for starting state (1,1) and action Up