This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util'); | |
function getRandomInt(min, max) { | |
const minCeiled = Math.ceil(min); | |
const maxFloored = Math.floor(max); | |
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if a class has more functionality rather than just running as Thread | |
// you should use Runnable interface instead of extending Thread class | |
// because a class can implement multiple interfaces | |
class Player{ | |
void move(){ | |
System.out.println("Moving..."); | |
} | |
} | |
class MyRunnable extends Player implements Runnable{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyThread extends Thread{ | |
@Override | |
public void run(){ | |
try{ | |
for(int i=0; i<5; i++){ | |
System.out.println(Thread.currentThread().getName() + " -> " + i); | |
Thread.sleep(1000); | |
} | |
} | |
catch(InterruptedException e){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
class FileHandling{ | |
// this method does not handle the exception but the caller need to handle it | |
// if a method can throw a checked exception, it must indicate it in | |
// the exception list using 'throws' | |
static String getFileContent(String filePath) throws IOException{ | |
BufferedReader file = new BufferedReader(new FileReader(filePath)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Troop{ | |
void move(){ | |
System.out.println("Troop is moving!"); | |
} | |
} | |
class Barbarian extends Troop{ | |
@Override | |
void move(){ | |
System.out.println("Barbarian is moving!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserMethods{ | |
static int add(int a, int b){ | |
return a+b; | |
} | |
static String add(String a, String b){ | |
return a+b; | |
} | |
static String add(char a, char b){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
// defining a custom Exception | |
class UserDefinedException extends Exception{ | |
UserDefinedException(String str){ | |
super(str); | |
} | |
} | |
public class FactorialWithException{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player{ | |
String name; | |
int x, y; | |
int width, height; | |
int hp; | |
Player(String name, int x, int y, int width, int height, int hp){ | |
this.name = name; | |
this.x = x; | |
this.y = y; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* finalize() method is a method of Object class that is used to perform cleanup | |
* activity before destroying any object | |
* it is callled by garbage collector (System.gc()) beofre destroying objects from | |
* memory | |
* helps garbage collector to close all the resources used by the objects | |
* finalize() is non static and protected method of java.lang.Object class | |
* Object class is superclass of all Java classes | |
* finalize() method is called only once per object | |
* JVM calls garbage collector to delete unreferenced objects at runtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle{ | |
double length; | |
double breadth; | |
Rectangle(double length, double breadth){ | |
this.length = length; | |
this.breadth = breadth; | |
} |
NewerOlder