To know more :
This file contains hidden or 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
| // 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{ |
This file contains hidden or 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
| #include<iostream> | |
| using namespace std; | |
| int factorial(int); | |
| int main(){ | |
| int number, factorialOfNumber; | |
| cout << "Enter a number you want to calculate factorial : "; | |
| cin >> number; |
This file contains hidden or 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
| #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; |
This file contains hidden or 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 Animal{ | |
| // Creating the interface inside class; nested interface. | |
| interface Activity{ | |
| public default void move(){ | |
| System.out.println("Animal is moving."); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| // Can static and private function be overrided ? | |
| class Animal { | |
| static void eat() { | |
| System.out.println("Eating."); | |
| } | |
| private void move() { | |
| System.out.println("Moving."); | |
| } |
This file contains hidden or 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
| // 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. |
This file contains hidden or 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 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(); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| // 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")); |
This file contains hidden or 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.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(); |

