Skip to content

Instantly share code, notes, and snippets.

View hisuperaman's full-sized avatar
🎯
Focusing

Aman hisuperaman

🎯
Focusing
View GitHub Profile
@hisuperaman
hisuperaman / timetable_generator.js
Created May 19, 2024 07:03
Automatic Timetable generator in Javascript - Noob approach
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);
}
@hisuperaman
hisuperaman / RunnableExample.java
Created December 3, 2023 16:50
Multithreading example in Java using Runnable interface
// 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{
@hisuperaman
hisuperaman / ThreadExample.java
Last active December 3, 2023 16:50
Multithreading example in Java using Thread class
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){
@hisuperaman
hisuperaman / ThrowsExample.java
Created November 29, 2023 16:46
Java Program demonstrating the throws keyword in Exception Handling
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));
@hisuperaman
hisuperaman / OverridingExample.java
Created November 29, 2023 15:56
Java Program demonstrating method overriding
class Troop{
void move(){
System.out.println("Troop is moving!");
}
}
class Barbarian extends Troop{
@Override
void move(){
System.out.println("Barbarian is moving!");
@hisuperaman
hisuperaman / OverloadingExample.java
Created November 29, 2023 15:44
Java Program demonstrating method overloading
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){
@hisuperaman
hisuperaman / FactorialWithException.java
Last active November 29, 2023 16:10
Factorial of a number with Exception handling in Java
import java.util.*;
// defining a custom Exception
class UserDefinedException extends Exception{
UserDefinedException(String str){
super(str);
}
}
public class FactorialWithException{
@hisuperaman
hisuperaman / InheritanceExample.java
Created November 14, 2023 15:15
Inheritance example in Java
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;
@hisuperaman
hisuperaman / FinalizeExample.java
Created November 13, 2023 16:20
Finalize method example in Java
/**
* 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
@hisuperaman
hisuperaman / ConstructorExample.java
Created November 12, 2023 16:59
Class constructor example in Java
class Rectangle{
double length;
double breadth;
Rectangle(double length, double breadth){
this.length = length;
this.breadth = breadth;
}