Skip to content

Instantly share code, notes, and snippets.

View theArjun's full-sized avatar

Arjun Adhikari theArjun

View GitHub Profile
@theArjun
theArjun / DisplayWithCommandLineArguments.java
Created January 11, 2019 12:57
Displays Files and Directories based on Command Line Arguments Passed
// Importing File class from Java io package.
// CLA in documentation refers to Command Line Argument.
import java.io.File;
// Creating a class named StringTest.
class StringTest{
public static void main(String[] args){
// To prevent any of the exception that may arise, I put the codes under try block.
try{
@theArjun
theArjun / README.md
Last active January 20, 2019 15:13
Tower Of Hanoi by Recursion

Tower of Hanoi

How's it is solved ?

Tower Of Hanoi

Long steps ahead, but you're too intelligent to understand

Steps for Tower Of Hanoi

To know more :

@theArjun
theArjun / Factorial.cpp
Last active January 14, 2019 04:25
Factorial By Recursion in Java
#include<iostream>
using namespace std;
int factorial(int);
int main(){
int number, factorialOfNumber;
cout << "Enter a number you want to calculate factorial : ";
cin >> number;
@theArjun
theArjun / Fibonacci.cpp
Last active January 14, 2019 04:35
Fibonacci By Recursion in Java
#include<iostream>
using namespace std;
int fibonacci(int);
int main(){
int number;
cout << "Enter how many elements you want to see in Fibonacci Series : ";
cin >> number;
@theArjun
theArjun / Animal.java
Created January 14, 2019 15:31
Nested Interface Implementation
class Animal{
// Creating the interface inside class; nested interface.
interface Activity{
public default void move(){
System.out.println("Animal is moving.");
}
}
}
@theArjun
theArjun / Override.java
Created January 16, 2019 12:51
Can static and private function be overrided ?
// Can static and private function be overrided ?
class Animal {
static void eat() {
System.out.println("Eating.");
}
private void move() {
System.out.println("Moving.");
}
@theArjun
theArjun / OwnException.java
Created January 16, 2019 12:59
Creating your own Exception in Java
// Create your own Exception.
class MyException extends Exception { // Inherits the partially unchecked exception class Exception.
MyException(String msg) { // Parameterized constructor for MyException.
super(msg); // Calls the constructor of Exception class; super class.
}
}
class ExceptionDemo {
static void isValid(int age) throws MyException { // Denotes this code may contain Exception; declaring exception here.
@theArjun
theArjun / Triangle.java
Last active January 18, 2019 08:06
Prints Triangle using Java
class Five{
public static void main(String[] args){
for(int i = 1; i <= 4; i++){ // First we are running loop from 1 to 4.
for(int j = 0; j < i; j++){ // Then we are running loop from 0 until i.
System.out.print((j+i)%2+" "); // Sum of i and j modulus of 2 equals the alternate of 1 and 0.
}
System.out.println();
}
}
}
@theArjun
theArjun / Alternatively.java
Created January 18, 2019 10:32
Merge Two Text Files Sequentially and Alternatively
// This merges the two text files named "abc.txt" and "xyz.txt" alternatively.
// This question was asked later in Java Class 18 January, 2019.
import java.io.*;
class MergeFiles{
public static void main(String[] args)throws IOException{
try{
PrintWriter pw = new PrintWriter(new File("finalText.txt"));
BufferedReader brOne = new BufferedReader(new FileReader("abc.txt"));
@theArjun
theArjun / MergeTextFiles.java
Created January 18, 2019 11:29
Merges all Text Files into one Text File from given Path
import java.io.*;
class MergeTextFiles {
public static void main(String[] args) throws IOException {
BufferedReader br; // Declaring a variable of BufferedReader data type so that it can be used inside loop to read every .txt files listed there.
String path = "C:/users/arjun/Desktop/"; // This is pre-specified path, you can change it.
PrintWriter pw = new PrintWriter(path + "mergedText.txt");
String line; // This will get the lines on text files.
String location;
File[] filesFromPath = (new File(path)).listFiles();