├── .github ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE │ ├── ---bug-report.md │ ├── ---documentation.md │ ├── ---feature-request.md │ ├── --questions.md │ └── config.yml ├── PULL_REQUEST_TEMPLATE.md └── SUPPORT.md ├── .gitignore ├── Basics ├── DuplicateChars.java ├── Factorial.java ├── Fibonacci.java ├── FizzBuzz.java ├── GP.java ├── Pyramid.java ├── average.java ├── count_odd_or_even.java ├── cutoff.java ├── denomination.java ├── evenandodd.java ├── flag.java ├── fun1.java ├── leapyear.java ├── merge.java ├── palindrome.java ├── power.java ├── prime.java ├── recursion.java ├── reverse.java ├── sorting.java ├── squares.java ├── strong number.java └── tables.java ├── Data_Conversion ├── BinaryCheck.java ├── Bintodecimal.java ├── DeciToHexa.java ├── binary.java ├── decitobinary.java └── decitooctal.java ├── Data_Structures ├── BST.java ├── DLL.java ├── Detect cycle in an undirected graph (using DFS).java ├── DetectCycleInList.java ├── InorderTraversal.java ├── Kruskal’s algorithm.java ├── KthMinAndMaxInArray.java ├── Linear Probing.java ├── PalindromeList.java ├── Queue.java ├── Topological_sort_BFS.java ├── check if a Binary Tree is BST.java ├── circularqueue.java ├── heap sort java │ └── heapsort.java ├── linkedlist.java ├── m_colouring_decision.java ├── m_colouring_optimization.java ├── m_colouring_permutation.java └── stackpr1.java ├── Exception_Handling ├── Balance_addition.java ├── Multicatch_block.java ├── Nested_try1.java ├── Nested_try2.java ├── Throw.java ├── Throw2.java ├── Try_and_catch.java └── Try_and_finally.java ├── LICENSE ├── Miscellaneous ├── BMI_Calculator.java ├── Binaryconcat.java ├── Calculator.java ├── Dungeon quest.java ├── DutchNationalFlagAlgo.java ├── EightQueens.java ├── EvenPerfectSquare.java ├── FCFS.java ├── FibonacciPrime.java ├── FindDept.java ├── GameOfLife.java ├── Game_of_Coordinates.java ├── IndexedFileAllocation.java ├── InterchangeAndAdd.java ├── LRUPageReplacement.java ├── MinMax.java ├── Narcissisticno.java ├── PalindromeCheck.java ├── Pizza.java ├── Producer_Consumer.java ├── PythagoreanPrimes.java ├── RLD.java ├── RRScheduling.java ├── RandomNo.java ├── SJFScheduling.java ├── SportsCatalogue.java ├── TagContentExtractor.java ├── TwoPointersAlgo.java ├── UrlConcat.java ├── amicable_no.java ├── append.java ├── bestFitVsfirstFit.java ├── decimal.java ├── encryption.java ├── flames.java ├── matrix.java ├── next_fitVsfirst_fit.java ├── nonloop.java ├── permstring.java ├── project.java └── resort.java ├── OOPS ├── AbstractClass.java ├── ArrayOfObjects.java ├── Classes.java ├── Constructors.java ├── Encapsulation │ ├── Accessing.java │ └── Encapsulation.java ├── Examples │ ├── Bookstore.java │ ├── Distribution.java │ ├── GenericMethods.java │ ├── Holiday.java │ ├── SemesterMarks.java │ ├── Shape.java │ └── Shapes.java ├── Initializing_objects.java ├── Object.java ├── Polymorphism.java ├── Static.java ├── This_keyword.java └── Variables.java ├── Packages ├── pkg1 │ └── Project1.java └── pkg2 │ └── Project2.java ├── README.md ├── Searching & Sorting ├── Binary Search.java ├── BubbleSort.java ├── Heap Sort.java ├── InsertionSort.java ├── Linear Search.java ├── Merge Sort.java ├── QuickSort.java ├── Radix Sort.java └── Selection Sort.java ├── Threads ├── Matrix.java ├── Test1.java ├── multi_threads.java ├── synchronization.java └── threads.java ├── _config.yml └── assets ├── HackerRank Logo.PNG └── Java_logo_icon.png /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | 3 | ### Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ### Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ### Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ### Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ### Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at svijayb.dev@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ### Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4. 71 | 72 | [homepage]: http://contributor-covenant.org 73 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | We love your input! We want to make contributing to this project as easy and transparent as possible, whether it is: 4 | 5 | - Reporting a bug 6 | - Discussing the current state of the code 7 | - Submitting a fix 8 | - Proposing new features 9 | - Becoming a maintainer 10 | 11 | When contributing to this repository, please first discuss the change you wish to make via issue, 12 | email, or any other method with the owners of this repository before making a change. 13 | 14 | Please note we have a [Code Of Conduct](https://github.com/SVijayB/JavaSpace/blob/master/.github/CODE_OF_CONDUCT.md), please follow it in all your interactions with the project. 15 | 16 | ### General rules 17 | 18 | - Commits must be atomic. Each commit must commit one and only one thing. It has to be a single complete unit of work. 19 | - Commit messages must be meaningful, clear and concise. Stress on your grammar. 20 | - Commits should do exactly what their message says. If a commit affects quite a few files, and does a few related things, mention them in the commit description. 21 | - IDE configuration files,temp files and cached data are not meant to be a part of the repository and they should remain so. 22 | - Variable names like `a` or `x` are unacceptable, use variable names that describes its purpose. 23 | - Format the code correctly before sending a pull request. 24 | - If you've added code that should be tested, add tests. 25 | - If you have a question, think you've discovered an issue, would like to propose a new feature, etc., then find/file an issue **BEFORE** starting work to fix/implement it. 26 | 27 | ### Filing a new Issue 28 | - Before filing a new issue, search existing open and closed issues first. It is likely someone else has found the problem you're seeing, and someone may be working on or have already contributed a fix! 29 | - Don't know whether you're reporting an issue or requesting a feature? File an issue. 30 | - Have a question that you don't see answered in docs, videos, etc.? File an issue. 31 | - Want to know if we're planning on building a particular feature? File an issue. 32 | - Got a great idea for a new feature? File an issue. 33 | - Found an existing issue that describes yours? Great - upvote and add additional commentary / info / repro-steps / etc. 34 | 35 | ### Formatting guidelines 36 | 37 | - For Class, method declarations and anything else, have braces starting in the next line. 38 | - One space between operators and operands is a must. 39 | - Use tabs for intendation. 40 | - Make sure a line doesn't exceed 80 characters. 41 | 42 | ### Pull Request Process 43 | 44 | - Check if there's already a pull request assigned to someone that solves a similar problem to yours. 45 | - Ensure that the pull request is being sent from a hotfix/feature branch and not from `master`. 46 | - Check the issue page if the feature you are trying to add/bug you are trying to fix is already in the list. If it does, mention the issue number in parentheses with the pull request. 47 | - Provide a clear, concise description of why this pull request is necessary and what value it adds. Make sure that your pull request doesn't contain any more or less than what the pull request description says. 48 | - One pull request should do only one thing. No more, no less. If you want to implement two new features, open two pull requests. 49 | 50 | ### Any contributions you make will be under the MIT Software License 51 | 52 | In short, when you submit code changes, your submissions are understood to be under the same [MIT License](https://github.com/SVijayB/JavaSpace/blob/master/LICENSE) that covers the project. Feel free to contact the maintainers if that's a concern. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/---bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F41B Bug report" 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: SVijayB 7 | 8 | --- 9 | 10 | 11 | 12 | 13 | 14 | 15 | ### Description 16 | 17 | 18 | ### Expected Behavior 19 | 20 | 21 | ### Actual Behavior 22 | 23 | 24 | ### Possible Fix 25 | 26 | 27 | ### Steps to Reproduce 28 | 29 | 30 | 1. 31 | 2. 32 | 3. 33 | 4. 34 | 35 | ### Context 36 | 37 | 38 | ### Your Environment 39 | 40 | * Version used: 41 | * Operating System and version: 42 | 43 | 44 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/---documentation.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F4D6 Documentation" 3 | about: Suggest modifications for documentations 4 | title: '' 5 | labels: documentation 6 | assignees: SVijayB 7 | 8 | --- 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/---feature-request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F4A1 Feature request" 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: SVijayB 7 | 8 | --- 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/--questions.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "❓ Questions" 3 | about: Ask any question related to the project 4 | title: '' 5 | labels: question 6 | assignees: SVijayB 7 | 8 | --- 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: E-mail 4 | url: svijayb.dev@gmail.com 5 | about: Feel free to contact me for any reason related to the project. 6 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ### Description 8 | 9 | 10 | ### Related Issue 11 | 12 | 13 | 14 | 15 | This PR closes # 16 | 17 | ### Motivation and Context 18 | 19 | 20 | ### Screenshots (if any) 21 | 22 | ### Types of changes 23 | 24 | - [ ] Bug fix (non-breaking change which fixes an issue) 25 | - [ ] New feature (non-breaking change which adds functionality) 26 | - [ ] Breaking change (fix or feature that would cause existing functionality to change) 27 | 28 | ### Checklist: 29 | 30 | 31 | - [ ] My code follows the code style of this project. 32 | - [ ] My change requires a change to the documentation. 33 | - [ ] I have updated the documentation accordingly. 34 | - [ ] I have read the **CONTRIBUTING** document. 35 | 36 | -------------------------------------------------------------------------------- /.github/SUPPORT.md: -------------------------------------------------------------------------------- 1 | # Getting Support 2 | 3 | Hey, 4 | 5 | If you have any question regarding anything related to this project, feel free to create an issue. 6 | 7 | If this is your first time contributing to open source or your first time on GitHub, go through these documentations : 8 | 9 | - [Creating an Issue](https://help.github.com/en/github/managing-your-work-on-github/creating-an-issue) 10 | - [Creating a Pull Request](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request) 11 | - [Git And GitHub Resources](https://docs.vijaybalaji.me/resources/git-and-github-resources) 12 | 13 | For more help related to GitHub, check out [GitHub Help](https://help.github.com/en) 14 | 15 | Please Read our [Code Of Conduct](https://github.com/SVijayB/JavaSpace/blob/master/.github/CODE_OF_CONDUCT.md) and our [Contributing](https://github.com/SVijayB/JavaSpace/blob/master/.github/CONTRIBUTING.md) files for more details. 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vs/slnx.sqlite 2 | .vs/VSWorkspaceState.json 3 | .vs/Java-Programs/v16/.suo 4 | .vscode/launch.json 5 | -------------------------------------------------------------------------------- /Basics/DuplicateChars.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | 3 | import java.util.Scanner; 4 | 5 | // Check if the given String has duplicate characters. 6 | 7 | public class DuplicateChars { 8 | 9 | public static void main(String[] args) { 10 | 11 | // Our variables 12 | String str = ""; 13 | char[] chars; 14 | boolean hasDuplicate = false; 15 | 16 | // Create a Scanner object 17 | Scanner input = new Scanner(System.in); 18 | 19 | // Ask the user to enter a string 20 | System.out.println("Enter a string to check: "); 21 | str = input.nextLine(); 22 | 23 | // Convert the given string to a char array 24 | chars = str.toCharArray(); 25 | 26 | // Check each characters present in the string 27 | for (int i = 0; i < chars.length; i++) { 28 | hasDuplicate = false; 29 | 30 | for (int j = i + 1; j < chars.length; j++) { 31 | if (chars[i] == chars[j]) { 32 | // Found a duplicate, break out from the loop 33 | hasDuplicate = true; 34 | break; 35 | } 36 | } 37 | 38 | // If there was a duplicate in the loop then break out 39 | if (hasDuplicate) { 40 | break; 41 | } 42 | } 43 | 44 | if (hasDuplicate) { 45 | System.out.println("The string contains duplicate characters."); 46 | } else { 47 | System.out.println("NO duplicate characters present in the string."); 48 | } 49 | 50 | // Close the Scanner 51 | input.close(); 52 | } 53 | } -------------------------------------------------------------------------------- /Basics/Factorial.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | 3 | // write a program to find the factorial of a given number(hackerearth problem) 4 | 5 | import java.util.Scanner; 6 | 7 | public class Factorial { 8 | 9 | public static void main(String[] args) { 10 | 11 | Scanner sc = new Scanner(System.in); 12 | 13 | int n = sc.nextInt(); 14 | int result = 1; 15 | 16 | for (int i = 1; i <= n; i++) { 17 | result *= i; 18 | } 19 | System.out.println(result); 20 | sc.close(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Basics/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | 3 | // Display Fibonacci series up to a given number 4 | public class Fibonacci 5 | { 6 | public static void main(String[] args) 7 | { 8 | 9 | int n = 100, t1 = 0, t2 = 1; 10 | 11 | System.out.print("Upto " + n + ": "); 12 | while (t1 <= n) 13 | { 14 | System.out.print(t1 + " + "); 15 | 16 | int sum = t1 + t2; 17 | t1 = t2; 18 | t2 = sum; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Basics/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | /* 3 | * Write a short program that prints each number from 1 to 100 on a new line. 4 | * For each multiple of 3, print "Fizz" instead of the number. 5 | * For each multiple of 5, print "Buzz" instead of the number. 6 | * For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number. 7 | */ 8 | 9 | public class FizzBuzz { 10 | 11 | public static void main(String[] args) { 12 | String result = ""; 13 | 14 | for (int i = 1; i <= 100; i++) { 15 | result = ""; 16 | 17 | if (i % 3 == 0) { 18 | result += "Fizz"; 19 | } 20 | 21 | if (i % 5 == 0) { 22 | result += "Buzz"; 23 | } 24 | 25 | if (result.length() == 0) { 26 | result = Integer.toString(i); 27 | } 28 | 29 | System.out.println(result); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Basics/GP.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Find the value of Grade pay 3 | import java.util.* ; 4 | class GP 5 | { 6 | public static void main (String args[]) 7 | { 8 | double bp,gp; 9 | int da,hra; 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Enter value of Basic pay: "); 12 | bp = sc.nextDouble(); 13 | System.out.println("Enter value of Dearness Allowance: "); 14 | da = sc.nextInt(); 15 | System.out.println("Enter value of House Rent Allowance: "); 16 | hra = sc.nextInt(); 17 | gp = (bp+(bp*(da/100)+bp*(hra/100))); 18 | System.out.println("The Grade Pay: " + gp); 19 | sc.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Basics/Pyramid.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to print a pyramid with Squares of numbers from 1-10. 3 | class Pyramid 4 | { 5 | public static void main(String args[]) 6 | { 7 | int i,j,k,a=1,d; 8 | for (i=0; i<5; i++) 9 | { 10 | for (j=5-i; j>1; j--) 11 | { 12 | System.out.print(" "); 13 | } 14 | for (k=1; k<=i; k++) 15 | { 16 | d = a*a; 17 | System.out.print(d + " "); 18 | a++; 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Basics/average.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | 3 | // Write a program to find the average of n numbers. 4 | import java.util.*; 5 | 6 | class average { 7 | public static void main(String args[]) { 8 | int n, i, sum = 0; 9 | double avg; 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Enter for how many numbers average is to be found: "); 12 | n = sc.nextInt(); 13 | int a[] = new int[n]; 14 | System.out.println("Enter the numbers for which average is to be found: "); 15 | for (i = 0; i < n; i++) 16 | { 17 | a[i] = sc.nextInt(); 18 | sum = sum + a[i]; 19 | } 20 | avg = (double) sum / (double) n; 21 | System.out.println("Average of given numbers = " + avg); 22 | sc.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Basics/count_odd_or_even.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | 3 | import java.util.Scanner; 4 | 5 | class count_odd_or_even 6 | { 7 | public static void main(String args[]) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("Enter a number >"); 11 | int num = sc.nextInt(); 12 | int even = 0, odd = 0, sum_of_even = 0, sum_of_odd = 0; 13 | while(num>0) 14 | { 15 | int temp = (num%10); 16 | switch(temp%2) 17 | { 18 | case 0: 19 | even++; 20 | sum_of_even = sum_of_even + temp; 21 | break; 22 | case 1: 23 | odd++; 24 | sum_of_odd = sum_of_odd + temp; 25 | break; 26 | } 27 | num = num/10; 28 | } 29 | System.out.println("Number of even numbers -> " + even); 30 | System.out.println("Sum of all the even numbers -> " + sum_of_even); 31 | System.out.println("Number of odd numbers -> " + odd); 32 | System.out.println("Sum of all the odd numbers -> " + sum_of_odd); 33 | sc.close(); 34 | } 35 | } -------------------------------------------------------------------------------- /Basics/cutoff.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to calculate the cutoff. 3 | import java.util.*; 4 | class cutoff 5 | { 6 | public static void main(String args[]) 7 | { 8 | double cc,m,p,c,co; 9 | String name, state; 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Enter student name: "); 12 | name = sc.next(); 13 | System.out.println("Enter Counselling Code: "); 14 | cc = sc.nextDouble(); 15 | System.out.println("Enter State: "); 16 | state = sc.next(); 17 | System.out.println("Enter marks in Maths: "); 18 | m = sc.nextDouble(); 19 | System.out.println("Enter marks in Physics: "); 20 | p = sc.nextDouble(); 21 | System.out.println("Enter marks in Chemistry: "); 22 | c = sc.nextDouble(); 23 | co = (((m+p+c)/300)*100); 24 | System.out.println("Student Name : " + name); 25 | System.out.println("Counselling Code : " + cc); 26 | System.out.println("State : " + state); 27 | System.out.println("Marks obtained in Maths : " + m); 28 | System.out.println("Marks obtained in Physics : " + p); 29 | System.out.println("Marks obtained in Chemistry : " + c); 30 | System.out.println("Total Cut-off : " + co); 31 | sc.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Basics/denomination.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to find denomination of amount entered. 3 | import java.util.*; 4 | class denomination 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int a,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0; 10 | System.out.println("Enter the amount: "); 11 | a = sc.nextInt(); 12 | while(a>=2000) 13 | { 14 | b = a/2000; 15 | a = a%2000; 16 | } 17 | System.out.println("Total number of 2000.0 Rupees Notes :" + b ); 18 | while(a>=500 && a<=2000) 19 | { 20 | c = a/500; 21 | a = a%500; 22 | } 23 | System.out.println("Total number of 500.00 Rupees Notes :" + c ); 24 | while(a>=100 && a<=500) 25 | { 26 | d = a/100; 27 | a = a%100; 28 | } 29 | System.out.println("Total number of 100.00 Rupees Notes :" + d ); 30 | while(a>=50 && a<=100) 31 | { 32 | e = a/50; 33 | a = a%50; 34 | } 35 | System.out.println("Total number of 50.000 Rupees Notes :" + e ); 36 | while(a>=20 && a<=50) 37 | { 38 | f = a/20; 39 | a = a%20; 40 | } 41 | System.out.println("Total number of 20.000 Rupees Notes :" + f ); 42 | while(a>=10 && a<=20) 43 | { 44 | g = a/10; 45 | a = a%10; 46 | } 47 | System.out.println("Total number of 10.000 Rupees Notes :" + g ); 48 | while(a>=5 && a<=10) 49 | { 50 | h = a/5; 51 | a = a%5; 52 | } 53 | System.out.println("Total number of 5.0000 Rupees Notes :" + h ); 54 | while(a>=1 && a<=5) 55 | { 56 | i = a/1; 57 | a = a%1; 58 | } 59 | System.out.println("Total number of 1.0000 Rupees Notes :" + i ); 60 | sc.close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Basics/evenandodd.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to find if numbers entered are Odd or Even. 3 | import java.util.*; 4 | class evenandodd 5 | { 6 | public static void main(String args[]) 7 | { 8 | int n,i; 9 | Scanner sc = new Scanner(System.in); 10 | System.out.print("Enter number of elements: "); 11 | n = sc.nextInt(); 12 | int a[] = new int [n]; 13 | int b[] = new int [n]; 14 | int c[] = new int [n]; 15 | System.out.println("Enter the elements: "); 16 | for(i=0;ii;j--) 13 | { 14 | System.out.print(" *"); 15 | } 16 | if(i!=a-1) 17 | { 18 | System.out.println(""); 19 | } 20 | } 21 | for(i=a+1;i>0;i--) 22 | { 23 | for(j=a+1;j>i;j--) 24 | { 25 | System.out.print(" *"); 26 | } 27 | System.out.println(""); 28 | } 29 | for(i=10;i>0;i--) 30 | { 31 | System.out.println(" *"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Basics/fun1.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | //A Simple fun Text based Game! 3 | import java.util.*; 4 | class fun1 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | String a[] = new String[] {"Strawberry Throw", "Idly Barrage","Water Splash","Manali Scream","Pinch!","Perfect Bite"}; 10 | int i,ans,ph1=100,ph2=100,x=2; 11 | System.out.print("Enter Name of the first player: "); 12 | String p1 = sc.nextLine(); 13 | System.out.print("Enter Name of the Second player: "); 14 | String p2 = sc.nextLine(); 15 | System.out.println(); 16 | System.out.println("Welcome " + p1 + " and " + p2 + " to the game of Manali WAR!"); 17 | System.out.println(); 18 | System.out.println("Rules: The person who runs out of health first loses!"); 19 | System.out.println(); 20 | System.out.println("THE WAR BEGINS!"); 21 | System.out.println("_________________"); 22 | while(ph1>0&&ph2>0) 23 | { 24 | if(x%2==0) 25 | { 26 | x++; 27 | for(i=0;i<6;i++) 28 | { 29 | System.out.println(i+1 + ")" + a[i]); 30 | } 31 | System.out.println(); 32 | System.out.print(p1 + " Pick one of the above choices: "); 33 | ans = sc.nextInt(); 34 | if(ans==1) 35 | { 36 | ph2 = ph2 - 14; 37 | System.out.println(p1 + " has picked " + a[0]); 38 | System.out.println(p2 + " Watch out! Those strawberries are toxic!(Aren't they always?)"); 39 | System.out.println(); 40 | System.out.println("Status: "); 41 | System.out.println("Health of " + p1 + " = " + ph1); 42 | System.out.println("Health of " + p2 + " = " + ph2); 43 | } 44 | else if(ans==2) 45 | { 46 | ph2 = ph2 - 17; 47 | System.out.println(p1 + " has picked " + a[1]); 48 | System.out.println(p2 + " RUN! Those idlies are as hard as rock!"); 49 | System.out.println(); 50 | System.out.println("Status: "); 51 | System.out.println("Health of " + p1 + " = " + ph1); 52 | System.out.println("Health of " + p2 + " = " + ph2); 53 | } 54 | else if(ans==3) 55 | { 56 | ph2 = ph2 - 8; 57 | System.out.println(p1 + " has picked " + a[2]); 58 | System.out.println(p2 + " Water! Agh! Suffer!"); 59 | System.out.println(); 60 | System.out.println("Status: "); 61 | System.out.println("Health of " + p1 + " = " + ph1); 62 | System.out.println("Health of " + p2 + " = " + ph2); 63 | } 64 | else if(ans==4) 65 | { 66 | ph2 = ph2 - 32; 67 | System.out.println(p1 + " has picked " + a[3]); 68 | System.out.println(p2 + " Oopsie! You are doomed!"); 69 | System.out.println(); 70 | System.out.println("Status: "); 71 | System.out.println("Health of " + p1 + " = " + ph1); 72 | System.out.println("Health of " + p2 + " = " + ph2); 73 | } 74 | else if(ans==5) 75 | { 76 | ph2 = ph2 - 29; 77 | System.out.println(p1 + " has picked " + a[4]); 78 | System.out.println("Blood rushes out of " + p2 + "'s hand!"); 79 | System.out.println(); 80 | System.out.println("Status: "); 81 | System.out.println("Health of " + p1 + " = " + ph1); 82 | System.out.println("Health of " + p2 + " = " + ph2); 83 | } 84 | else if(ans==6) 85 | { 86 | ph2 = ph2 - 36; 87 | System.out.println(p1 + " has picked " + a[5]); 88 | System.out.println(p2 + " You Messed up real time in your previous life!"); 89 | System.out.println(); 90 | System.out.println("Status: "); 91 | System.out.println("Health of " + p1 + " = " + ph1); 92 | System.out.println("Health of " + p2 + " = " + ph2); 93 | } 94 | System.out.println("~~~~~~~~~~~~~~"); 95 | } 96 | else 97 | { 98 | x++; 99 | for(i=0;i<6;i++) 100 | { 101 | System.out.println(i+1 + ")" + a[i]); 102 | } 103 | System.out.println(); 104 | System.out.print(p2 + " Pick one of the above choices: "); 105 | ans = sc.nextInt(); 106 | if(ans==1) 107 | { 108 | ph1 = ph1 - 14; 109 | System.out.println(p2 + " has picked " + a[0]); 110 | System.out.println(p1 + " Watch out! Those strawberries are toxic!(Aren't they always?)"); 111 | System.out.println(); 112 | System.out.println("Status: "); 113 | System.out.println("Health of " + p1 + " = " + ph1); 114 | System.out.println("Health of " + p2 + " = " + ph2); 115 | } 116 | else if(ans==2) 117 | { 118 | ph1 = ph1 - 17; 119 | System.out.println(p2 + " has picked " + a[1]); 120 | System.out.println(p1 + " RUN! Those idlies are as hard as rock!"); 121 | System.out.println(); 122 | System.out.println("Status: "); 123 | System.out.println("Health of " + p1 + " = " + ph1); 124 | System.out.println("Health of " + p2 + " = " + ph2); 125 | } 126 | else if(ans==3) 127 | { 128 | ph1 = ph1 - 8; 129 | System.out.println(p2 + " has picked " + a[2]); 130 | System.out.println(p1 + " Water! Agh! Suffer!"); 131 | System.out.println(); 132 | System.out.println("Status: "); 133 | System.out.println("Health of " + p1 + " = " + ph1); 134 | System.out.println("Health of " + p2 + " = " + ph2); 135 | } 136 | else if(ans==4) 137 | { 138 | ph1 = ph1 - 32; 139 | System.out.println(p2 + " has picked " + a[3]); 140 | System.out.println(p1 + " Oopsie! You are doomed!"); 141 | System.out.println(); 142 | System.out.println("Status: "); 143 | System.out.println("Health of " + p1 + " = " + ph1); 144 | System.out.println("Health of " + p2 + " = " + ph2); 145 | } 146 | else if(ans==5) 147 | { 148 | ph1 = ph1 - 29; 149 | System.out.println(p2 + " has picked " + a[4]); 150 | System.out.println("Blood rushes out of " + p1 + "'s hand!"); 151 | System.out.println(); 152 | System.out.println("Status: "); 153 | System.out.println("Health of " + p1 + " = " + ph1); 154 | System.out.println("Health of " + p2 + " = " + ph2); 155 | } 156 | else if(ans==6) 157 | { 158 | ph1 = ph1 - 36; 159 | System.out.println(p2 + " has picked " + a[5]); 160 | System.out.println(p1 + " You Messed up real time in your previous life!"); 161 | System.out.println(); 162 | System.out.println("Status: "); 163 | System.out.println("Health of " + p1 + " = " + ph1); 164 | System.out.println("Health of " + p2 + " = " + ph2); 165 | } 166 | System.out.println("~~~~~~~~~~~~~~"); 167 | } 168 | } 169 | System.out.println(); 170 | if(ph1<0) 171 | { 172 | System.out.println(p1 + " has lost!! Find yourself a corner!"); 173 | System.out.println(p2 + " Congrats!!!"); 174 | } 175 | else 176 | { 177 | System.out.println(p2 + " has lost!! Find yourself a corner!"); 178 | System.out.println(p1 + " Congrats!!! "); 179 | } 180 | sc.close(); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /Basics/leapyear.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to check if entered year is a leap year or not. 3 | import java.util.*; 4 | class leapyear 5 | { 6 | public static void main(String args[]) 7 | { 8 | int year; 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("Enter the year : "); 11 | year = sc.nextInt(); 12 | if(year % 100 == 0 && year % 400 != 0) 13 | { 14 | System.out.println(year + " is not a leap year."); 15 | } 16 | else if(year % 4 ==0) 17 | { 18 | System.out.println(year + " is a leap year."); 19 | } 20 | sc.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Basics/merge.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to merge two one-dimensional arrays 3 | import java.util.*; 4 | class merge 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int n,m,i; 10 | System.out.print("Enter number of elements in array 1: "); 11 | n = sc.nextInt(); 12 | int a[] = new int [n]; 13 | System.out.println("Enter " + n + " elements into array 1: "); 14 | for(i=0;i0) 14 | { 15 | b = a%10; 16 | c = b + c*10; 17 | a = a/10; 18 | } 19 | System.out.println(c); 20 | if(c == num) 21 | { 22 | System.out.println(num + " Is a Palindromic number"); 23 | } 24 | else 25 | { 26 | System.out.println(num + " Is not a Palindromic number"); 27 | } 28 | sc.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Basics/power.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to calculate A raised to B using for loop. 3 | import java.util.*; 4 | class power 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int a,b,i,p=1; 10 | System.out.println("Enter the base: "); 11 | a = sc.nextInt(); 12 | System.out.println("Enter the power: "); 13 | b = sc.nextInt(); 14 | for(i=1;i<=b;i++) 15 | { 16 | p = p*a; 17 | } 18 | System.out.println("Answer: " + p); 19 | sc.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Basics/prime.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to print Prime Numbers from 3-100 3 | import java.util.*; 4 | class prime 5 | { 6 | public static void main(String args[]) 7 | { 8 | double a = 3,b,i,num,c; 9 | Scanner sc = new Scanner(System.in); 10 | while (a>=3 && a<=100) 11 | { 12 | num = a; 13 | c = num; 14 | i = a-1; 15 | while(i>1) 16 | { 17 | b = a%i; 18 | if(b==0) 19 | { 20 | c++; 21 | } 22 | i--; 23 | } 24 | if(num==c) 25 | { 26 | System.out.println(num); 27 | } 28 | a++; 29 | } 30 | sc.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Basics/recursion.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | import java.util.*; 3 | 4 | class fibonacci2 5 | { 6 | static int fib(int n) 7 | { 8 | if (n <= 1) 9 | return n; 10 | return fib(n-1) + fib(n-2); 11 | } 12 | 13 | public static void main(String args[]) 14 | { 15 | Scanner sc = new Scanner(System.in); 16 | System.out.println("How many numbers from the fibonacci series do you want to print? "); 17 | int n = sc.nextInt(); 18 | for(int i=0;i<=n;i++) 19 | { 20 | System.out.println(fib(i) + " "); 21 | } 22 | sc.close(); 23 | } 24 | } -------------------------------------------------------------------------------- /Basics/reverse.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to reverse a String. 3 | import java.util.*; 4 | class reverse 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int i,l; 10 | System.out.println("Enter a Number or String to be reversed: "); 11 | String n = sc.nextLine(); 12 | l = n.length(); 13 | char a[] = n.toCharArray(); 14 | int left=0; 15 | int right=a.length-1; 16 | 17 | 18 | while(lefta[j]) 23 | { 24 | temp = a[i]; 25 | a[i] = a[j]; 26 | a[j] = temp; 27 | } 28 | } 29 | } 30 | System.out.print("Ascending order: "); 31 | for(i=0;i= 1 ; i--) 23 | fac = fac * i; 24 | //storing sum of factorial of all digits of the number 25 | sum = sum + fac; 26 | n=n/10; 27 | } 28 | //condition for strong number 29 | if(sum == number) 30 | System.out.println("Strong Number");//display the result 31 | else 32 | System.out.println("Not a Strong Number"); 33 | //closing scanner class(not compulsory, but good practice) 34 | sc.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Basics/tables.java: -------------------------------------------------------------------------------- 1 | package Basics; 2 | // Write a program to print multiplication tables of a number A from 1 to N. 3 | import java.util.*; 4 | class tables 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int a,i,j,b; 10 | System.out.println("Enter the table till which it is to be calculated: "); 11 | a = sc.nextInt(); 12 | for(i=1;i<=a;i++) 13 | { 14 | for(j=1;j<=10;j++) 15 | { 16 | b = i*j; 17 | System.out.println( i + " X " + j + " = " + b); 18 | } 19 | } 20 | sc.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Data_Conversion/BinaryCheck.java: -------------------------------------------------------------------------------- 1 | package Data_Conversion; 2 | // Write a program to check if number has even number 1's in it's binary format or not. 3 | import java.util.*; 4 | class BinaryCheck 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int a,b,c=0,d=1,e=0,num; 10 | System.out.println("Enter the Decimal Number: "); 11 | a = sc.nextInt(); 12 | while(a>0) 13 | { 14 | b = a%2; 15 | c = c + b*d; 16 | d = d*10; 17 | a = a/2; 18 | } 19 | System.out.println("Binary number is: " + c); 20 | num = c; 21 | while(c>0) 22 | { 23 | b = c%10; 24 | if(b==1) 25 | { 26 | e++; 27 | } 28 | c = c/10; 29 | } 30 | if(e%2==0) 31 | { 32 | System.out.println(num + " Has even number of 1's"); 33 | } 34 | else 35 | { 36 | System.out.println(num + " Does not have even number of 1's"); 37 | } 38 | sc.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Data_Conversion/Bintodecimal.java: -------------------------------------------------------------------------------- 1 | package Data_Conversion; 2 | // Write a program to convert a Binary number into a Decimal number 3 | import java.util.*; 4 | class bintodecimal 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int a,b,s=0,n=1; 10 | System.out.println("Enter the Binary number to be converted: "); 11 | a = sc.nextInt(); 12 | while(a>0) 13 | { 14 | b = a%10; 15 | s = s + b*n; 16 | n = n*2; 17 | a = a/10; 18 | } 19 | 20 | System.out.println("After Conversion: " + s); 21 | sc.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Data_Conversion/DeciToHexa.java: -------------------------------------------------------------------------------- 1 | package Data_Conversion; 2 | 3 | // Program to convert decimal number to Hex string 4 | import java.util.Scanner; 5 | 6 | class DeciToHexa { 7 | public static void main(String args[]) { 8 | Scanner input = new Scanner(System.in); 9 | System.out.print("Enter decimal number : "); 10 | int decimal = input.nextInt(); 11 | input.close(); 12 | String hexa = Integer.toHexString(decimal); 13 | System.out.println("Converted Hex value : " + hexa); 14 | } 15 | } -------------------------------------------------------------------------------- /Data_Conversion/binary.java: -------------------------------------------------------------------------------- 1 | package Data_Conversion; 2 | // Write a program to check if given number is binary or not. 3 | import java.util.*; 4 | class binary 5 | { 6 | public static void main(String args[]) 7 | { 8 | int a,b,c=0,d; 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("Enter the number to be checked: "); 11 | a = sc.nextInt(); 12 | d = a; 13 | while(a>0) 14 | { 15 | b = a%10; 16 | if(b==0||b==1) 17 | { 18 | c = 0; 19 | } 20 | else 21 | { 22 | c = 5; 23 | break; 24 | } 25 | a = a/10; 26 | } 27 | if(c==0||c==1) 28 | { 29 | System.out.println(d + " Is a binary number"); 30 | } 31 | else 32 | { 33 | System.out.println(d + " Is not a binary number"); 34 | } 35 | sc.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Data_Conversion/decitobinary.java: -------------------------------------------------------------------------------- 1 | package Data_Conversion; 2 | // Write a program to convert decimal number to binary 3 | import java.util.*; 4 | class decitobinary 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int a,b,i,l; 10 | String r = ""; 11 | String t = ""; 12 | System.out.println("Enter decimal number to be converted: "); 13 | a = sc.nextInt(); 14 | sc.close(); 15 | while(a>0) 16 | { 17 | b = a%2; 18 | a = a/2; 19 | t = t + String.valueOf(b); 20 | } 21 | l = t.length(); 22 | for(i=l-1;i>=0;i--) 23 | { 24 | r = r + t.charAt(i); 25 | } 26 | System.out.println("After converstion: " + r); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Data_Conversion/decitooctal.java: -------------------------------------------------------------------------------- 1 | package Data_Conversion; 2 | // Write a program to convert a decimal number to octal. 3 | import java.util.*; 4 | class decitooctal 5 | { 6 | public static void main(String args[]) 7 | { 8 | int a,b,sum=0,i=1; 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("Enter the Decimal number: "); 11 | a = sc.nextInt(); 12 | while(a>0) 13 | { 14 | b = a%8; 15 | sum = sum + b*i; 16 | i = i*10; 17 | a = a/8; 18 | } 19 | System.out.println("After conversion: " + sum); 20 | sc.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Data_Structures/BST.java: -------------------------------------------------------------------------------- 1 | package Data_Structures; 2 | //Create binary search tree to insert, delete last node, node with one child and node with two child, Also print number of children. 3 | import java.util.*; 4 | class BST 5 | { 6 | int count = 0; 7 | class Node 8 | { 9 | int key; 10 | Node left, right; 11 | 12 | public Node(int item) 13 | { 14 | key = item; 15 | left = right = null; 16 | } 17 | } 18 | Node root; 19 | 20 | BST() 21 | { 22 | root = null; 23 | } 24 | 25 | void insert(int key) 26 | { 27 | root = insertRec(root, key); 28 | count++; 29 | } 30 | 31 | Node insertRec(Node root, int key) 32 | { 33 | 34 | if (root == null) 35 | { 36 | root = new Node(key); 37 | return root; 38 | } 39 | if (key < root.key) 40 | { 41 | root.left = insertRec(root.left, key); 42 | } 43 | else if (key > root.key) 44 | { 45 | root.right = insertRec(root.right, key); 46 | } 47 | return root; 48 | } 49 | void inorder() 50 | { 51 | inorderRec(root); 52 | } 53 | void inorderRec(Node root) 54 | { 55 | if (root != null) 56 | { 57 | inorderRec(root.left); 58 | System.out.println(root.key); 59 | inorderRec(root.right); 60 | } 61 | } 62 | void deleteKey(int key) 63 | { 64 | root = delete(root, key); 65 | count--; 66 | } 67 | 68 | Node delete(Node root, int key) 69 | { 70 | if (root == null) 71 | { 72 | return root; 73 | } 74 | if (key < root.key) 75 | { 76 | root.left = delete(root.left, key); 77 | } 78 | else if (key > root.key) 79 | { 80 | root.right = delete(root.right, key); 81 | } 82 | else 83 | { 84 | if (root.left == null) 85 | { 86 | return root.right; 87 | } 88 | else if (root.right == null) 89 | { 90 | return root.left; 91 | } 92 | root.key = minValue(root.right); 93 | root.right = delete(root.right, root.key); 94 | } 95 | return root; 96 | } 97 | int minValue(Node root) 98 | { 99 | int minv = root.key; 100 | while (root.left != null) 101 | { 102 | minv = root.left.key; 103 | root = root.left; 104 | } 105 | return minv; 106 | } 107 | 108 | public static void main(String[] args) 109 | { 110 | Scanner sc = new Scanner(System.in); 111 | BST tree = new BST(); 112 | System.out.println("Enter number of elements into the tree : "); 113 | int n = sc.nextInt(); 114 | for(int i=1;i> 5 | adj) { 6 | vis[node] = 1; 7 | // go to all adjacent nodes 8 | for(int adjacentNode: adj.get(node)) { 9 | if(vis[adjacentNode]==0) { 10 | if(dfs(adjacentNode, node, vis, adj) == true) 11 | return true; 12 | } 13 | // if adjacent node is visited and is not its own parent node 14 | else if(adjacentNode != parent) return true; 15 | } 16 | return false; 17 | } 18 | // Function to detect cycle in an undirected graph. 19 | public boolean isCycle(int V, ArrayList> adj) { 20 | int vis[] = new int[V]; 21 | for(int i = 0;i> adj = new ArrayList<>(); 31 | for (int i = 0; i < 4; i++) { 32 | adj.add(new ArrayList < > ()); 33 | } 34 | adj.get(1).add(2); 35 | adj.get(2).add(1); 36 | adj.get(2).add(3); 37 | adj.get(3).add(2); 38 | 39 | Solution obj = new Solution(); 40 | boolean ans = obj.isCycle(4, adj); 41 | if (ans) 42 | System.out.println("1"); 43 | else 44 | System.out.println("0"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Data_Structures/DetectCycleInList.java: -------------------------------------------------------------------------------- 1 | // Problem Statement: - 2 | // Given head, the head of a linked list, determine if the linked list has a cycle in it. 3 | // There is a cycle in a linked list if there is some node in the list that can be reached 4 | // again by continuously following the next pointer. Internally, pos is used to denote the 5 | // index of the node that tail's next pointer is connected to. Note that pos is not passed 6 | // as a parameter. 7 | // Return true if there is a cycle in the linked list. Otherwise, return false. 8 | 9 | // Example 1: 10 | // Sample Input : 11 | // head = [3,2,0,-4], pos = 1 12 | // Output: true 13 | // Explanation: There is a cycle in the linked list, where the tail connects to 14 | // the 1st node (0-indexed). 15 | 16 | import java.util.*; 17 | 18 | class Node{ 19 | int Data; 20 | Node next; 21 | }; 22 | 23 | public class DetectCycleInList { 24 | public Node InsertNode(Node Head, int iNo) 25 | { 26 | Node newn = new Node(); 27 | newn.Data = iNo; 28 | newn.next = null; 29 | 30 | if(Head == null) 31 | { 32 | Head = newn; 33 | } 34 | else 35 | { 36 | Node temp = Head; 37 | while(temp.next != null) 38 | { 39 | temp = temp.next; 40 | } 41 | temp.next = newn; 42 | newn.next = null; 43 | } 44 | return Head; 45 | } 46 | 47 | public void DisplayList(Node Head) 48 | { 49 | Node temp = Head; 50 | 51 | while(temp != null) 52 | { 53 | System.out.print("|"+temp.Data+"| ->"); 54 | temp = temp.next; 55 | } 56 | System.out.print(" NULL\n"); 57 | } 58 | 59 | public void CreateCycle(Node Head) 60 | { 61 | Node temp = Head; 62 | Node temp1 = Head.next.next; 63 | temp.next.next.next.next = temp1; 64 | 65 | } 66 | 67 | public Node DetectCycle(Node Head) 68 | { 69 | HashMap address = new HashMap<>(); 70 | Node temp = Head; 71 | int i = 0; 72 | while(temp != null) 73 | { 74 | if(address.containsKey(temp)) 75 | { 76 | return temp; 77 | } 78 | else 79 | { 80 | address.put(temp, i); 81 | i++; 82 | } 83 | temp = temp.next; 84 | } 85 | return temp; 86 | } 87 | 88 | public boolean Result(Node Head) 89 | { 90 | boolean flag = false; 91 | Node temp = DetectCycle(Head); 92 | if(temp == null) 93 | { 94 | flag = false; 95 | } 96 | else 97 | { 98 | flag = true; 99 | } 100 | 101 | return flag; 102 | } 103 | 104 | public static void main(String args[]) 105 | { 106 | Node Head = null; 107 | Node temp = null; 108 | DetectCycleInList cobj = new DetectCycleInList(); 109 | 110 | Head = cobj.InsertNode(Head, 3); 111 | Head = cobj.InsertNode(Head, 2); 112 | Head = cobj.InsertNode(Head, 0); 113 | Head = cobj.InsertNode(Head, -4); 114 | 115 | cobj.DisplayList(Head); 116 | cobj.CreateCycle(Head); 117 | //cobj.DisplayList(Head); 118 | temp = cobj.DetectCycle(Head); 119 | if(temp == null) 120 | { 121 | System.out.println("Cycle Is Not Present In The Given Linked List !"); 122 | } 123 | else 124 | { 125 | System.out.println("Cycle Is Present In The Given Linked List !"); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /Data_Structures/InorderTraversal.java: -------------------------------------------------------------------------------- 1 | //Tree: Inorder Traversal 2 | package Data_Structures; 3 | 4 | import java.util.*; 5 | 6 | class Node { 7 | Node left; 8 | Node right; 9 | int data; 10 | 11 | Node(int data) { 12 | this.data = data; 13 | left = null; 14 | right = null; 15 | } 16 | } 17 | 18 | class Solution { 19 | 20 | /* 21 | * you only have to complete the function given below. Node is defined as 22 | * 23 | * class Node { int data; Node left; Node right; } 24 | * 25 | */ 26 | 27 | public static void inOrder(Node root) { 28 | if (root == null) 29 | return; 30 | inOrder(root.left); 31 | System.out.print(root.data + " "); 32 | inOrder(root.right); 33 | } 34 | 35 | public static Node insert(Node root, int data) { 36 | if (root == null) { 37 | return new Node(data); 38 | } else { 39 | Node cur; 40 | if (data <= root.data) { 41 | cur = insert(root.left, data); 42 | root.left = cur; 43 | } else { 44 | cur = insert(root.right, data); 45 | root.right = cur; 46 | } 47 | return root; 48 | } 49 | } 50 | 51 | public static void main(String[] args) { 52 | Scanner scan = new Scanner(System.in); 53 | int t = scan.nextInt(); 54 | Node root = null; 55 | while (t-- > 0) { 56 | int data = scan.nextInt(); 57 | root = insert(root, data); 58 | } 59 | scan.close(); 60 | inOrder(root); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Data_Structures/Kruskal’s algorithm.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Edge 4 | { 5 | int src, dest, weight; 6 | 7 | public Edge(int src, int dest, int weight) { 8 | this.src = src; 9 | this.dest = dest; 10 | this.weight = weight; 11 | } 12 | 13 | public String toString() { 14 | return "(" + src + ", " + dest + ", " + weight + ")"; 15 | } 16 | } 17 | class DisjointSet 18 | { 19 | Map parent = new HashMap<>(); 20 | public void makeSet(int N) 21 | { 22 | for (int i = 0; i < N; i++) 23 | parent.put(i, i); 24 | } 25 | private int Find(int k) 26 | { 27 | if (parent.get(k) == k) 28 | return k; 29 | return Find(parent.get(k)); 30 | } 31 | private void Union(int a, int b) 32 | { 33 | int x = Find(a); 34 | int y = Find(b); 35 | 36 | parent.put(x, y); 37 | } 38 | public static List KruskalAlgo(List edges, int N) 39 | { 40 | List MST = new ArrayList(); 41 | DisjointSet ds = new DisjointSet(); 42 | ds.makeSet(N); 43 | 44 | int index = 0; 45 | while (MST.size() != N - 1) 46 | { 47 | Edge next_edge = edges.get(index++); 48 | int x = ds.Find(next_edge.src); 49 | int y = ds.Find(next_edge.dest); 50 | if (x != y) 51 | { 52 | MST.add(next_edge); 53 | ds.Union(x, y); 54 | } 55 | } 56 | return MST; 57 | } 58 | } 59 | 60 | class Main 61 | { 62 | public static void main(String[] args) 63 | { 64 | List edges = Arrays.asList( 65 | new Edge(0, 1, 7), new Edge(1, 2, 8), 66 | new Edge(0, 3, 5), new Edge(1, 3, 9), 67 | new Edge(1, 4, 7), new Edge(2, 4, 5), 68 | new Edge(3, 4, 15), new Edge(3, 5, 6), 69 | new Edge(4, 5, 8), new Edge(4, 6, 9), 70 | new Edge(5, 6, 11) 71 | ); 72 | Collections.sort(edges, Comparator.comparingInt(e -> e.weight)); 73 | 74 | final int N = 7; 75 | 76 | List e = DisjointSet.KruskalAlgo(edges, N); 77 | 78 | for (Edge edge: e) { 79 | System.out.println(edge); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /Data_Structures/KthMinAndMaxInArray.java: -------------------------------------------------------------------------------- 1 | package Data_Structures; 2 | import java.util.*; 3 | 4 | /* Problem Statement - To find the kth Smallest and kth Largest element in an array. 5 | Note: Duplicates are considered. 6 | */ 7 | 8 | class KthMinAndMaxInArray{ 9 | static int kthSmallestElement(int[] array, int k){ // using max-heap 10 | /* A max-heap is a complete binary tree in which the value in each internal node is greater 11 | than or equal to the values in the children of that node. Mapping the elements of a heap 12 | into an array is trivial: if a node is stored an index k, then its left child is stored at 13 | index 2k + 1 and its right child at index 2k + 2. 14 | Note: In max-heap root is the greatest element of the array */ 15 | PriorityQueue p = new PriorityQueue<>(Collections.reverseOrder()); // we use collections to convert min-heap to max-heap 16 | for(int i=0; i p = new PriorityQueue<>(); // a priority queue by default implements a min-heap in java 34 | for(int i=0; ip.peek()){ 39 | p.poll(); 40 | p.add(array[i]); 41 | } 42 | } 43 | return p.peek(); 44 | } 45 | public static void main(String[] args){ 46 | int[] arr = {2,5,80,66,4,5,77,9,3,5,4,99,152,1563,549,2,6595,8963,29655}; 47 | int k = 3; 48 | System.out.println("Given Array is: "); 49 | System.out.println("k is: "+k); 50 | for(int i=0;i snode) 40 | { 41 | boolean bflag = true; 42 | Node temp = Head; 43 | int iData1; 44 | int iData2; 45 | 46 | while(temp != null) 47 | { 48 | iData1 = snode.pop(); 49 | iData2 = temp.Data; 50 | if(iData1 != iData2) 51 | { 52 | bflag = false; 53 | } 54 | temp = temp.next; 55 | } 56 | return bflag; 57 | } 58 | 59 | public boolean isPalindrome(Node head) { 60 | Node temp; 61 | temp = head; 62 | boolean iRet = false; 63 | Stack snode = new Stack(); 64 | 65 | while(temp != null) 66 | { 67 | //System.out.print("|" + temp.data + "| ->\t"); 68 | snode.add(temp.Data); 69 | temp = temp.next; 70 | } 71 | 72 | iRet = CompareEle(head, snode); 73 | 74 | if(iRet == true) 75 | { 76 | iRet = true; 77 | } 78 | else 79 | { 80 | iRet = false; 81 | } 82 | 83 | return iRet; 84 | } 85 | 86 | public static void main(String args[]) { 87 | Node Head = null; 88 | PalindromeList pobj = new PalindromeList(); 89 | boolean iRet = false; 90 | 91 | Head = pobj.InsertNode(Head, 1); 92 | Head = pobj.InsertNode(Head, 2); 93 | Head = pobj.InsertNode(Head, 2); 94 | Head = pobj.InsertNode(Head, 1); 95 | 96 | iRet = pobj.isPalindrome(Head); 97 | 98 | if(iRet == true) 99 | { 100 | System.out.println("Entered Linked List Is Palindrome !"); 101 | } 102 | else 103 | { 104 | System.out.println("Entered Linked List Is Not Palindrome !"); 105 | } 106 | 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Data_Structures/Queue.java: -------------------------------------------------------------------------------- 1 | package Data_Structures; 2 | import java.util.*; 3 | class queue 4 | { 5 | private static int queue[]; 6 | private static int front,rear,size; 7 | 8 | queue(int a) 9 | { 10 | queue = new int [a]; 11 | front = -1; 12 | rear = -1; 13 | } 14 | private static void enqueue(int n) 15 | { 16 | if(front==size-1) 17 | { 18 | System.out.println("OverFlow Condition"); 19 | } 20 | else 21 | { 22 | if(front==-1&&rear==-1) 23 | { 24 | front = 0; 25 | rear = 0; 26 | queue[front] = n; 27 | } 28 | else 29 | { 30 | queue[++rear] = n; 31 | } 32 | } 33 | } 34 | private static void deque() 35 | { 36 | if(front==-1) 37 | { 38 | System.out.println("UnderFlow Condition"); 39 | } 40 | else 41 | { 42 | queue[front] = queue[++front]; 43 | } 44 | } 45 | public static void main(String args[]) 46 | { 47 | Scanner sc = new Scanner(System.in); 48 | System.out.println("Enter the size of the queue"); 49 | size = sc.nextInt(); 50 | queue q = new queue(size); 51 | enqueue(5); 52 | enqueue(6); 53 | deque(); 54 | enqueue(8); 55 | enqueue(9); 56 | deque(); 57 | enqueue(2); 58 | deque(); 59 | deque(); 60 | enqueue(10); 61 | System.out.println("Elements present in the queue: "); 62 | for(int i=front;i<=rear;i++) 63 | { 64 | System.out.println(queue[i]); 65 | } 66 | System.out.println("Front is present in " + front + " position" + " which has element " + queue[front]); 67 | System.out.println("Rear is present in " + rear + " position" + " which has element " + queue[rear]); 68 | sc.close(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Data_Structures/Topological_sort_BFS.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | import java.lang.*; 4 | 5 | class Solution 6 | { 7 | public boolean isCyclic(int N, ArrayList> adj) { 8 | int topo[] = new int[N]; 9 | int indegree[] = new int[N]; 10 | 11 | //finding indegree 12 | for(int i = 0;i q = new LinkedList(); 20 | for(int i = 0;i> adj = new ArrayList < > (); 55 | 56 | // adding new arraylists to 'adj' to add neighbour nodes 57 | for (int i = 0; i < 6; i++) { 58 | adj.add(new ArrayList < > ()); 59 | } 60 | 61 | 62 | adj.get(5).add(2); 63 | adj.get(5).add(0); 64 | adj.get(4).add(0); 65 | adj.get(4).add(1); 66 | adj.get(3).add(1); 67 | adj.get(2).add(3); 68 | 69 | 70 | new Solution().isCyclic(6,adj); 71 | 72 | } 73 | } -------------------------------------------------------------------------------- /Data_Structures/check if a Binary Tree is BST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main { 3 | public static void main(String[] args) { 4 | Node root = new Node(7); 5 | root.left = new Node(5); 6 | root.left.left = new Node(3); 7 | root.left.right = new Node(6); 8 | root.right = new Node(10); 9 | root.right.left = new Node(9); 10 | root.right.right = new Node(15); 11 | Solution ob = new Solution(); 12 | boolean ans = ob.isValidBST(root); 13 | if (ans == true) { 14 | System.out.print("Valid BST"); 15 | } else { 16 | System.out.print("Invalid BST"); 17 | } 18 | } 19 | 20 | } 21 | 22 | class Node { 23 | int data; 24 | Node left, right; 25 | Node(int data) { 26 | this.data = data; 27 | left = null; 28 | right = null; 29 | } 30 | } 31 | class Solution { 32 | private boolean checkBST(Node node, long min, long max) { 33 | if (node == null) return true; 34 | if (node.data <= min || node.data >= max) return false; 35 | 36 | if (checkBST(node.left, min, node.data) && checkBST(node.right, node.data, 37 | max)) 38 | { 39 | return true; 40 | } 41 | return false; 42 | } 43 | public boolean isValidBST(Node root) { 44 | return checkBST(root, Long.MIN_VALUE, Long.MAX_VALUE); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Data_Structures/circularqueue.java: -------------------------------------------------------------------------------- 1 | package Data_Structures; 2 | import java.util.*; 3 | class circularqueue 4 | { 5 | private static int queue[] ; 6 | private static int front, rear, size, len, ele, f; 7 | 8 | circularqueue(int n) 9 | { 10 | size = n; 11 | len = 0; 12 | queue = new int[size]; 13 | front = -1; 14 | rear = -1; 15 | } 16 | public boolean isEmpty() 17 | { 18 | return front == -1; 19 | } 20 | public boolean isFull() 21 | { 22 | return (front==0 && rear == size-1 || front == (rear+1 % size)); 23 | } 24 | public int getSize() 25 | { 26 | return len ; 27 | } 28 | public void peek() 29 | { 30 | if (isEmpty()) 31 | { 32 | System.out.println ("Underflow Exception"); 33 | } 34 | else 35 | { 36 | f=queue[front]; 37 | System.out.println (f); 38 | } 39 | } 40 | public void insert(int i) 41 | { 42 | if (front==0 && rear== size-1 || front == (rear+1)%size) 43 | { 44 | System.out.println("Overflow Exception"); 45 | } 46 | else 47 | { 48 | if (rear == -1) 49 | { 50 | front = 0; 51 | rear = 0; 52 | queue[rear] = i; 53 | } 54 | else 55 | { 56 | rear = (rear + 1) % size; 57 | queue[rear] = i; 58 | } 59 | len++ ; 60 | } 61 | } 62 | public int remove() 63 | { 64 | if (front==-1) 65 | { 66 | System.out.println ("Underflow Exception"); 67 | } 68 | else 69 | { 70 | len-- ; 71 | ele = queue[front]; 72 | if ( front == rear) 73 | { 74 | front = -1; 75 | rear = -1; 76 | } 77 | else 78 | { 79 | front=(front+1)%size; 80 | } 81 | } 82 | return ele; 83 | } 84 | public void display() 85 | { 86 | int front_pos = front, rear_pos = rear; 87 | System.out.print("\nQueue = "); 88 | if (front==-1) 89 | { 90 | System.out.print("Empty\n"); 91 | } 92 | else 93 | { 94 | if(front_pos <= rear_pos) 95 | { 96 | while(front_pos <= rear_pos) 97 | { 98 | System.out.print(queue[front_pos]+" "); 99 | front_pos++; 100 | } 101 | } 102 | else 103 | { 104 | while(front_pos <= size-1) 105 | { 106 | System.out.print(queue[front_pos]+" "); 107 | front_pos++; 108 | } 109 | front_pos = 0; 110 | while(front_pos <= rear_pos) 111 | { 112 | System.out.print(queue[front_pos]+" "); 113 | front_pos++; 114 | } 115 | } 116 | } 117 | System.out.println(); 118 | } 119 | public static void main(String argsp[]) 120 | { 121 | Scanner scan = new Scanner(System.in); 122 | System.out.println("Array Queue Test\n"); 123 | System.out.println("Enter Size of Integer Queue "); 124 | int n = scan.nextInt(); 125 | circularqueue q = new circularqueue(n); 126 | char ch; 127 | do{ 128 | System.out.println("\nQueue Operations"); 129 | System.out.println("1. Insert"); 130 | System.out.println("2. Remove"); 131 | System.out.println("3. Peek"); 132 | System.out.println("4. Check Empty"); 133 | System.out.println("5. Check Full"); 134 | System.out.println("6. Size"); 135 | int choice = scan.nextInt(); 136 | switch (choice) 137 | { 138 | case 1 : 139 | System.out.println("Enter integer element to insert"); 140 | q.insert( scan.nextInt() ); 141 | break; 142 | case 2 : 143 | System.out.println("Removed Element = "+q.remove()); 144 | break; 145 | case 3 : 146 | System.out.print("Peek Element = "); 147 | q.peek(); 148 | break; 149 | case 4 : 150 | System.out.println("Empty status = "+q.isEmpty()); 151 | break; 152 | case 5 : 153 | System.out.println("Full status = "+q.isFull()); 154 | break; 155 | case 6 : 156 | System.out.println("Size = "+ q.getSize()); 157 | break; 158 | default : 159 | System.out.println("Wrong Entry \n "); 160 | break; 161 | } 162 | q.display(); 163 | System.out.println("\nDo you want to continue (Type y or n) \n"); 164 | ch = scan.next().charAt(0); 165 | } 166 | while (ch == 'Y'|| ch == 'y'); 167 | scan.close(); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /Data_Structures/heap sort java/heapsort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Heap Sort 2 | 3 | public class HeapSort { 4 | public void sort(int arr[]) 5 | { 6 | int N = arr.length; 7 | 8 | // Build heap (rearrange array) 9 | for (int i = N / 2 - 1; i >= 0; i--) 10 | heapify(arr, N, i); 11 | 12 | // One by one extract an element from heap 13 | for (int i = N - 1; i > 0; i--) { 14 | // Move current root to end 15 | int temp = arr[0]; 16 | arr[0] = arr[i]; 17 | arr[i] = temp; 18 | 19 | // call max heapify on the reduced heap 20 | heapify(arr, i, 0); 21 | } 22 | } 23 | 24 | // To heapify a subtree rooted with node i which is 25 | // an index in arr[]. n is size of heap 26 | void heapify(int arr[], int N, int i) 27 | { 28 | int largest = i; // Initialize largest as root 29 | int l = 2 * i + 1; // left = 2*i + 1 30 | int r = 2 * i + 2; // right = 2*i + 2 31 | 32 | // If left child is larger than root 33 | if (l < N && arr[l] > arr[largest]) 34 | largest = l; 35 | 36 | // If right child is larger than largest so far 37 | if (r < N && arr[r] > arr[largest]) 38 | largest = r; 39 | 40 | // If largest is not root 41 | if (largest != i) { 42 | int swap = arr[i]; 43 | arr[i] = arr[largest]; 44 | arr[largest] = swap; 45 | 46 | // Recursively heapify the affected sub-tree 47 | heapify(arr, N, largest); 48 | } 49 | } 50 | 51 | /* A utility function to print array of size n */ 52 | static void printArray(int arr[]) 53 | { 54 | int N = arr.length; 55 | 56 | for (int i = 0; i < N; ++i) 57 | System.out.print(arr[i] + " "); 58 | System.out.println(); 59 | } 60 | 61 | // Driver's code 62 | public static void main(String args[]) 63 | { 64 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 65 | int N = arr.length; 66 | 67 | // Function call 68 | HeapSort ob = new HeapSort(); 69 | ob.sort(arr); 70 | 71 | System.out.println("Sorted array is"); 72 | printArray(arr); 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /Data_Structures/linkedlist.java: -------------------------------------------------------------------------------- 1 | package Data_Structures; 2 | //Write a program in java for creating a linked list with elements 5,9,13,25,100,200 and 300. Perform an insertion operation where an element 10 is inserted at the begining. 3 | //Element 500 is inserted at 3rd from last and element 1 is inserted at the last. 4 | //Also perform a deletion operation where the 5th element, element 5 and the last element is deleted. 5 | class linkedlist 6 | { 7 | static node head; 8 | 9 | static int j=1; 10 | static class node 11 | { 12 | int data; 13 | node next; 14 | node prev; 15 | 16 | node(int d) 17 | { 18 | prev = null; 19 | data = d; 20 | next = null; 21 | } 22 | } 23 | public void add(int data) 24 | { 25 | node node = new node(data); 26 | node.next = head; 27 | head = node; 28 | j++; 29 | } 30 | public void insert(node prev, int data) 31 | { 32 | node node = new node(data); 33 | node.next = prev.next; 34 | prev.next = node; 35 | j++; 36 | } 37 | public void print() 38 | { 39 | node node = head; 40 | while(node!=null) 41 | { 42 | System.out.println(node.data); 43 | node = node.next; 44 | } 45 | } 46 | public static void main(String args[]) 47 | { 48 | linkedlist list = new linkedlist(); 49 | list.head = null; 50 | list.add(5); 51 | list.add(9); 52 | list.add(13); 53 | list.add(25); 54 | list.add(100); 55 | list.add(200); 56 | list.add(300); 57 | node temp; 58 | temp = head; 59 | for(int i=1;i<2;i++) 60 | { 61 | temp = temp.next; 62 | } 63 | list.insert(temp,500); 64 | temp = head; 65 | for(int i=1;i 20000) 25 | { 26 | int FixedDeposit = balance - 20000; 27 | int balance = 20000; 28 | String output = ("Your account balance is more than 20K now, so creating FD of amount: " + FixedDeposit); 29 | throw new ExceededMaximumAmount(output); 30 | } 31 | else 32 | { 33 | System.out.println("Total account balance: " + balance); 34 | } 35 | } 36 | 37 | public static void main(String args[]) 38 | { 39 | Scanner sc = new Scanner(System.in); 40 | Balance_addition obj = new Balance_addition(15000); 41 | System.out.println("Your account balance is : " + obj.balance); 42 | System.out.println("Please enter the amount to add in your balance: "); 43 | int add = sc.nextInt(); 44 | 45 | obj.balance = obj.balance + add; 46 | try 47 | { 48 | obj.BalanceCheck(); 49 | } 50 | catch (ExceededMaximumAmount ex) 51 | { 52 | System.out.println(ex.getMessage()); 53 | } 54 | sc.close(); 55 | } 56 | } -------------------------------------------------------------------------------- /Exception_Handling/Multicatch_block.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | public class Multicatch_block 4 | { 5 | public static void main(String[] args) 6 | { 7 | try 8 | { 9 | int a[]=new int[5]; 10 | a[5]=30/0; 11 | System.out.println("Right after the error is encountered, java skips to the catch block."+ 12 | "Hence, next line is not executed in the try block"); 13 | System.out.println(a[10]); 14 | } 15 | catch(ArithmeticException e) 16 | { 17 | System.out.println("Arithmetic Exception occurs"); 18 | } 19 | catch(ArrayIndexOutOfBoundsException e) 20 | { 21 | System.out.println("ArrayIndexOutOfBounds Exception occurs"); 22 | } 23 | catch(Exception e) 24 | { 25 | System.out.println("Parent Exception occurs"); 26 | } 27 | System.out.println("rest of the code"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Exception_Handling/Nested_try1.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | public class Nested_try1 4 | { 5 | public static void main(String args[]) 6 | { 7 | try 8 | { 9 | try 10 | { 11 | System.out.println("going to divide"); 12 | int a[]=new int[5]; 13 | a[5]=4; 14 | } 15 | catch(ArrayIndexOutOfBoundsException e) 16 | { 17 | System.out.println("caught:" + e); 18 | } 19 | 20 | catch(ArithmeticException e) 21 | { 22 | System.out.println("yes caught"); 23 | } 24 | 25 | System.out.println("normal flow1.."); 26 | } 27 | catch(Exception e) 28 | { 29 | System.out.println("handeled: " + e); 30 | } 31 | System.out.println("normal flow2.."); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Exception_Handling/Nested_try2.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | public class Nested_try2 4 | { 5 | public static void main(String args[]) 6 | { 7 | try 8 | { 9 | try 10 | { 11 | System.out.println("going to divide"); 12 | int b =39/0; 13 | } 14 | catch(ArrayIndexOutOfBoundsException e) 15 | { 16 | System.out.println("caught:"+e); 17 | } 18 | catch(ArithmeticException e) 19 | { 20 | System.out.println("yes! it has been caught - 1"); 21 | } 22 | try 23 | { 24 | int a[]=new int[5]; 25 | a[5]=4; 26 | } 27 | catch(ArithmeticException e) 28 | { 29 | System.out.println("yes! it has been caught - 2"); 30 | } 31 | System.out.println("normal flow1.."); 32 | } 33 | catch(Exception e) 34 | { 35 | System.out.println("handeled: "+e); // If inner block is unable to handle the exception. 36 | } 37 | System.out.println("normal flow2.."); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Exception_Handling/Throw.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | // A program can explicitly throw an exception using the throw statement. 4 | public class Throw 5 | { 6 | static void validate(int age) 7 | { 8 | if(age<18) 9 | { 10 | throw new ArithmeticException("not valid"); 11 | } 12 | else 13 | { 14 | System.out.println("You can vote!"); 15 | } 16 | } 17 | public static void main(String args[]) 18 | { 19 | validate(13); 20 | System.out.println("rest of the code..."); //not executed -> After throw, flow of execution stops. 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Exception_Handling/Throw2.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | // To continue execution of code after using the throw keyword. 4 | public class Throw2 5 | { 6 | static void validate(int age) 7 | { 8 | if(age<18) 9 | { 10 | throw new ArithmeticException("not valid"); 11 | } 12 | else 13 | { 14 | System.out.println("welcome to vote"); 15 | } 16 | } 17 | public static void main(String args[]) 18 | { 19 | try 20 | { 21 | try 22 | { 23 | validate(13); 24 | } 25 | catch(NullPointerException e) 26 | { 27 | System.out.println("Thrown error has been caught: "+e); // Since we caught the wrong exception, line wont be executed. 28 | } 29 | } 30 | catch(ArithmeticException e) 31 | { 32 | System.out.println("Thrown error has been caught now: "+e ); // Since correct exception handling, line is executed. 33 | } 34 | System.out.println("rest of the code..."); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Exception_Handling/Try_and_catch.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | import java.util.*; 4 | 5 | class Question1 { 6 | public static void main(String[] args) { 7 | // Create a Scanner 8 | Scanner input = new Scanner(System.in); 9 | boolean continueInput = true; 10 | 11 | // Prompt the user to enter two integers 12 | System.out.print("Enter two numbers: "); 13 | do { 14 | try { 15 | int number1 = input.nextInt(); 16 | int number2 = input.nextInt(); 17 | 18 | // Display the result 19 | System.out.println("The sum is " + (number1 + number2)); 20 | 21 | continueInput = false; 22 | } catch (InputMismatchException ex) { 23 | System.out.println("Try again. (" + "Incorrect input: two integers are required) "); 24 | input.nextLine(); // Discard input 25 | } 26 | } while (continueInput); 27 | input.close(); 28 | } 29 | } -------------------------------------------------------------------------------- /Exception_Handling/Try_and_finally.java: -------------------------------------------------------------------------------- 1 | package Exception_Handling; 2 | 3 | public class Try_and_finally 4 | { 5 | public static void main(String args[]) 6 | { 7 | int[] myNumbers = {1, 2, 3}; 8 | try 9 | { 10 | System.out.println(myNumbers[11]); // error! 11 | } 12 | catch(Exception e) 13 | { 14 | System.out.println("Don't worry! Remaining portion of the code will be executed..."); 15 | System.out.println("The type of runtime error is:"+e); 16 | } 17 | finally 18 | { 19 | System.out.println("This is executed from the finally block"); 20 | } 21 | System.out.println("Line executed after exception handling"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 S Vijay Balaji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Miscellaneous/BMI_Calculator.java: -------------------------------------------------------------------------------- 1 | // BMI Calculator Program 2 | import java.util.Scanner; 3 | public class Example 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter Your weight in kilogram: "); 9 | double weight = sc.nextDouble(); 10 | System.out.print("\nEnter Your height in meters: "); 11 | double height = sc.nextDouble(); 12 | double BMI = weight / (height * height); 13 | System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2"); 14 | } 15 | } -------------------------------------------------------------------------------- /Miscellaneous/Binaryconcat.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | /* You are given a number N. Find the decimal value of the number formed by the concatenation of the binary 3 | representation of the first N positive integers.*/ 4 | 5 | import java.math.*; 6 | import java.util.*; 7 | class Binaryconcat 8 | { 9 | public static void concat(BigInteger n) 10 | { 11 | Queue q = new ArrayDeque<>(); 12 | q.add("1"); 13 | BigInteger i = BigInteger.valueOf(1); 14 | String s = ""; 15 | while (i.compareTo(n) < 0 || i.compareTo(n) == 0) 16 | { 17 | q.add(q.peek() + "0"); 18 | q.add(q.peek() + "1"); 19 | s = s+q.poll(); 20 | BigInteger ran = BigInteger.valueOf(1); 21 | i = i.add(ran); 22 | } 23 | BigInteger a = new BigInteger(s); 24 | BigInteger temp = BigInteger.valueOf(0); 25 | BigInteger b; 26 | BigInteger x = BigInteger.valueOf(0); 27 | BigInteger k = BigInteger.valueOf(1); 28 | BigInteger temp1 = BigInteger.valueOf(10); 29 | BigInteger g; 30 | BigInteger temp2 = BigInteger.valueOf(2); 31 | while(a.compareTo(temp) > 0) 32 | { 33 | b = a.remainder(temp1); 34 | g = b.multiply(k); 35 | x = x.add(g); 36 | k = k.multiply(temp2); 37 | a = a.divide(temp1); 38 | } 39 | System.out.println(x); 40 | } 41 | public static void main(String args[] ) throws Exception 42 | { 43 | Scanner sc = new Scanner(System.in); 44 | System.out.println("Enter a Number for which the binary concatenated decimal number is to be found: "); 45 | BigInteger n = sc.nextBigInteger(); 46 | concat(n); 47 | sc.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Miscellaneous/Calculator.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | // Write a Java program to perform basic Calculator operations. 4 | import java.util.Scanner; 5 | class Calculator 6 | { 7 | public static void main(String[] args) 8 | { 9 | Scanner reader = new Scanner(System.in); 10 | System.out.print("Enter two numbers: "); 11 | double first = reader.nextDouble(); 12 | double second = reader.nextDouble(); 13 | System.out.print("Enter an operator (+, -, *, /): "); 14 | char operator = reader.next().charAt(0); 15 | double result; 16 | switch(operator) 17 | { 18 | case '+': 19 | result = first + second; 20 | break; 21 | case '-': 22 | result = first - second; 23 | break; 24 | case '*': 25 | result = first * second; 26 | break; 27 | case '/': 28 | result = first / second; 29 | break; 30 | 31 | default: 32 | System.out.printf("Error! operator is not correct"); 33 | return; 34 | } 35 | 36 | System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result); 37 | } 38 | } -------------------------------------------------------------------------------- /Miscellaneous/DutchNationalFlagAlgo.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DutchNationalFlagAlgo { 6 | // Algo to sort 0's 1's and 2's 7 | public static void sort(int a[],int a_size) 8 | { 9 | int lo=0; 10 | int mid =0; 11 | int hi=a.length-1; 12 | int temp; 13 | while(mid<=hi) 14 | { 15 | switch(a[mid]) 16 | { 17 | case 0: 18 | { 19 | temp=a[lo]; 20 | a[lo]=a[mid]; 21 | a[mid]=temp; 22 | lo++; 23 | mid++; 24 | break; 25 | } 26 | case 1: 27 | mid++; 28 | break; 29 | 30 | case 2: 31 | { 32 | temp=a[mid]; 33 | a[mid]=a[hi]; 34 | a[hi]=temp; 35 | hi--; 36 | break; 37 | } 38 | } 39 | } 40 | } 41 | 42 | public static void printArray(int arr[] , int length) 43 | { 44 | System.out.println("Elements after sorting"); 45 | for(int i=0;i obstPosx[i] && y > obstPosy[i] && x - obstPosx[i] == y - obstPosy[i]) 17 | d11 = Math.min(d11, x - obstPosx[i] - 1); 18 | if (obstPosx[i] > x && obstPosy[i] > y && obstPosx[i] - x == obstPosy[i] - y) 19 | d12 = Math.min(d12, obstPosx[i] - x - 1); 20 | if (obstPosx[i] > x && y > obstPosy[i] && obstPosx[i] - x == y - obstPosy[i]) 21 | d21 = Math.min(d21, obstPosx[i] - x - 1); 22 | if (x > obstPosx[i] && obstPosy[i] > y && x - obstPosx[i] == obstPosy[i] - y) 23 | d22 = Math.min(d22, x - obstPosx[i] - 1); 24 | if (x == obstPosx[i] && obstPosy[i] < y) 25 | r1 = Math.min(r1, y - obstPosy[i] - 1); 26 | if (x == obstPosx[i] && obstPosy[i] > y) 27 | r2 = Math.min(r2, obstPosy[i] - y - 1); 28 | if (y == obstPosy[i] && obstPosx[i] < x) 29 | c1 = Math.min(c1, x - obstPosx[i] - 1); 30 | if (y == obstPosy[i] && obstPosx[i] > x) 31 | c2 = Math.min(c2, obstPosx[i] - x - 1); 32 | } 33 | return d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2; 34 | } 35 | 36 | public static void main(String[] args) { 37 | int n = 8; 38 | int k = 1; 39 | int Qposx = 4; 40 | int Qposy = 4; 41 | int obstPosx[] = { 3 }; 42 | int obstPosy[] = { 5 }; 43 | System.out.println("the no of minimal moves a queen can make safely is=" 44 | + numberofPosition(n, k, Qposx, Qposy, obstPosx, obstPosy)); 45 | } 46 | } -------------------------------------------------------------------------------- /Miscellaneous/EvenPerfectSquare.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | // Find the sum of numbers at even position of a given number from right to left and check the sum is perfect square or not. 4 | import java.util.*; 5 | class EvenPerfectSquare 6 | { 7 | public static void main(String args[]) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int n,b,c=1,i,sum=0,d=1; 11 | System.out.print("Enter the number: "); 12 | n = sc.nextInt(); 13 | sc.close(); 14 | while(n>0) 15 | { 16 | b = n%10; 17 | if(c%2==0) 18 | { 19 | sum = sum + b; 20 | } 21 | c++; 22 | n = n/10; 23 | } 24 | System.out.println("Sum of even position of number from right to left: " + sum); 25 | for(i=0;i<=sum;i++) 26 | { 27 | if(i*i==sum) 28 | { 29 | d=0; 30 | } 31 | } 32 | if(d==0) 33 | { 34 | System.out.println("Sum is a perfect Square."); 35 | } 36 | else 37 | { 38 | System.out.println("Sum is not a perfect Square."); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Miscellaneous/FCFS.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.Scanner; 4 | 5 | public class FCFS 6 | { 7 | static void findWaitingTime(int processes[], int n, int burst_time[], int waiting_time[], int arrival_time[]) 8 | { 9 | int service_time[] = new int[n]; 10 | service_time[0] = 0; 11 | waiting_time[0] = 0; 12 | for (int i = 1; i < n ; i++) 13 | { 14 | int wasted=0; 15 | service_time[i] = service_time[i-1] + burst_time[i-1]; 16 | waiting_time[i] = service_time[i] - arrival_time[i]; 17 | if (waiting_time[i] < 0) 18 | { 19 | wasted = Math.abs(waiting_time[i]); 20 | waiting_time[i] = 0; 21 | } 22 | service_time[i] = service_time[i] + wasted; 23 | } 24 | } 25 | 26 | static void findTurnAroundTime(int processes[], int n, int burst_time[], int waiting_time[], int total_arival_time[]) 27 | { 28 | for (int i = 0; i < n ; i++) 29 | { 30 | total_arival_time[i] = burst_time[i] + waiting_time[i]; 31 | } 32 | } 33 | 34 | static void findavgTime(int processes[], int n, int burst_time[], int arrival_time[]) 35 | { 36 | int waiting_time[] = new int[n], total_arival_time[] = new int[n]; 37 | findWaitingTime(processes, n, burst_time, waiting_time, arrival_time); 38 | findTurnAroundTime(processes, n, burst_time, waiting_time, total_arival_time); 39 | System.out.print("Processes " + " Burst Time " + " Arrival Time " 40 | + " Waiting Time " + " Turn-Around Time " 41 | + " Completion Time \n"); 42 | int total_waiting_time = 0, total_total_arival_time = 0; 43 | for (int i = 0 ; i < n ; i++) 44 | { 45 | total_waiting_time = total_waiting_time + waiting_time[i]; 46 | total_total_arival_time = total_total_arival_time + total_arival_time[i]; 47 | int compl_time = total_arival_time[i] + arrival_time[i]; 48 | System.out.println(i+1 + "\t\t" + burst_time[i] + "\t\t" 49 | + arrival_time[i] + "\t\t" + waiting_time[i] + "\t\t " 50 | + total_arival_time[i] + "\t\t " + compl_time); 51 | } 52 | 53 | System.out.print("Average waiting time = " + (float)total_waiting_time / (float)n); 54 | System.out.print("\nAverage turn around time = " + (float)total_total_arival_time / (float)n); 55 | } 56 | 57 | public static void main(String args[]) 58 | { 59 | Scanner sc = new Scanner(System.in); 60 | System.out.println("Enter number of processes"); 61 | int n = sc.nextInt(); 62 | int processes[] = new int[n]; 63 | int burst_time[] = new int[n]; 64 | int arrival_time[] = new int[n]; 65 | for(int i=0;i dx || sy > dy) 43 | return false; 44 | if (sx == dx && sy == dy) 45 | return true; 46 | return (solve(sx + sy, sy, dx, dy) || solve(sx, sy + sx, dx, dy)); 47 | } 48 | } -------------------------------------------------------------------------------- /Miscellaneous/IndexedFileAllocation.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class IndexedFileAllocation 7 | { 8 | public static void main(String[] args) 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | System.out.print("Enter number of blocks : "); 12 | int blockSize = sc.nextInt(); 13 | System.out.print("Enter maximum data block hold by index blocks : "); 14 | int maxDataBlockSize = sc.nextInt(); 15 | boolean blocks[] = new boolean[blockSize]; 16 | System.out.print("Do you wish to allocate new file ?(Y/N) : "); 17 | char ans = sc.next().charAt(0); 18 | while (ans == 'Y' || ans == 'y') 19 | { 20 | System.out.print("Enter index block number : "); 21 | int idxBlock = sc.nextInt(); 22 | if (blocks[idxBlock] == false) 23 | { 24 | ArrayList arr = new ArrayList(); 25 | boolean in = true; 26 | for (int i = 0; i < maxDataBlockSize; i++) 27 | { 28 | System.out.print("Enter the " + (i + 1) + "data block no. : "); 29 | int bn = sc.nextInt(); 30 | if (bn >= 0 && bn < blocks.length && blocks[bn] == false && bn != idxBlock) 31 | { 32 | if (arr.contains(bn)) 33 | { 34 | in = false; 35 | break; 36 | } 37 | arr.add(bn); 38 | } 39 | else if (bn == -1) 40 | { 41 | continue; 42 | } 43 | else 44 | { 45 | in = false; 46 | break; 47 | } 48 | } 49 | if (in) 50 | { 51 | for (int idx : arr) 52 | { 53 | blocks[idx] = true; 54 | } 55 | blocks[idxBlock] = true; 56 | System.out.println("File Allocated Successfully !"); 57 | } 58 | else 59 | { 60 | System.out.println("File Allocation Failed!"); 61 | } 62 | } 63 | else 64 | { 65 | System.out.println("File Allocation Failed !"); 66 | } 67 | System.out.print("Do you wish to continue ? (Y/N) :"); 68 | ans = sc.next().charAt(0); 69 | sc.close(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Miscellaneous/LRUPageReplacement.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.HashMap; 4 | import java.util.HashSet; 5 | import java.util.Iterator; 6 | 7 | class LRUPageReplacement 8 | { 9 | static int pageFaults(int pages[], int n, int max) 10 | { 11 | HashSet s = new HashSet<>(max); 12 | HashMap indexes = new HashMap<>(); 13 | int result = 0; 14 | for (int i=0; i itr = s.iterator(); 31 | while (itr.hasNext()) { 32 | int temp = itr.next(); 33 | if (indexes.get(temp) < lru) 34 | { 35 | lru = indexes.get(temp); 36 | val = temp; 37 | } 38 | } 39 | s.remove(val); 40 | indexes.remove(val); 41 | s.add(pages[i]); 42 | result++; 43 | } 44 | indexes.put(pages[i], i); 45 | } 46 | } 47 | return result; 48 | } 49 | public static void main(String args[]) 50 | { 51 | int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2}; 52 | int max = 4; 53 | System.out.println(pageFaults(pages, pages.length, max)); 54 | } 55 | } -------------------------------------------------------------------------------- /Miscellaneous/MinMax.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.*; 4 | public class MinMax 5 | { 6 | static int minimax(int depth, int nodeIndex, boolean isMax, int scores[], int h) 7 | { 8 | if (depth == h) 9 | return scores[nodeIndex]; 10 | if (isMax) 11 | return Math.max(minimax(depth+1, nodeIndex*2, false, scores, h), minimax(depth+1, nodeIndex*2 + 1, false, scores, h)); 12 | else 13 | return Math.min(minimax(depth+1, nodeIndex*2, true, scores, h),minimax(depth+1, nodeIndex*2 + 1, true, scores, h)); 14 | } 15 | static int log2(int n) 16 | { 17 | return (n==1)? 0 : 1 + log2(n/2); 18 | } 19 | public static void main (String[] args) 20 | { 21 | Scanner in =new Scanner (System.in); 22 | System.out.print("Enter the number of terms : "); 23 | int n = in.nextInt(); 24 | System.out.println("Enter the values : "); 25 | int scores[] = new int [n]; 26 | for (int i=0;i0)//Creating another loop to find the number of digits of n. 18 | { 19 | i = i/10; 20 | count++; 21 | } 22 | while(j>0) 23 | { 24 | k = j%10;//Removing each digit of the number 25 | sum = sum + Math.pow(k,count);//Finding digit^(no. of digits of n) 26 | j = j/10; 27 | } 28 | if(num == sum)//If the condition satisfies, then it is a Narcissistic number. 29 | { 30 | System.out.println(num); 31 | } 32 | } 33 | 34 | //Armstrong number : The numbers in which the sum of the cubes of the digits is equal to the number itself, are called Armstrong number. 35 | System.out.println("Finding Armstrong Numbers from 1 to 100000"); 36 | for(int n=1;n<100000;n++) 37 | { 38 | int a = n; 39 | int b; 40 | double s = 0; 41 | while(a>0) 42 | { 43 | b = a%10; 44 | s = s + Math.pow(b,3); 45 | a = a/10; 46 | } 47 | if(s == n) 48 | { 49 | System.out.println(n); 50 | } 51 | } 52 | } 53 | } 54 | //Note : From above program, we can say that an Armstrong number is a Narcissistic number but a Narcissitic number need not be an Armstrong number. -------------------------------------------------------------------------------- /Miscellaneous/PalindromeCheck.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.Scanner; 4 | 5 | class ChkPalindrome { 6 | public static void main(String args[]) { 7 | String str, rev = ""; 8 | Scanner sc = new Scanner(System.in); 9 | 10 | System.out.println("Enter a string:"); 11 | str = sc.nextLine(); 12 | sc.close(); 13 | int length = str.length(); 14 | 15 | for (int i = length - 1; i >= 0; i--) 16 | rev = rev + str.charAt(i); 17 | 18 | if (str.equals(rev)) 19 | System.out.println(str + " is a palindrome"); 20 | else 21 | System.out.println(str + " is not a palindrome"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Miscellaneous/Pizza.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import javax.swing.*; 4 | import java.awt.event.*; 5 | import javax.swing.border.TitledBorder; 6 | 7 | public class Pizza implements ItemListener,ActionListener 8 | { 9 | JFrame f; 10 | TitledBorder Size,Toppings; 11 | JCheckBox plain,sausage,mushroom,pepperoni; 12 | JRadioButton small,medium,large; 13 | JLabel l1,l2; 14 | JButton bill; 15 | JTextArea val; 16 | static int total=0; 17 | Pizza() 18 | { 19 | f=new JFrame("Pizza Billing"); 20 | f.setBounds(200,200,400,450); 21 | f.setLayout(null); 22 | f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 23 | 24 | JLabel l1=new JLabel(); 25 | l1.setBorder(new TitledBorder("PizzaSize")); 26 | l1.setBounds(10,10,350,70); 27 | f.add(l1); 28 | small=new JRadioButton("Small"); 29 | small.setBounds(20,40,80,30); 30 | f.add(small); 31 | medium=new JRadioButton("Medium"); 32 | medium.setBounds(100,40,80,30); 33 | f.add(medium); 34 | large=new JRadioButton("Large"); 35 | large.setBounds(180,40,80,30); 36 | f.add(large); 37 | ButtonGroup size=new ButtonGroup(); 38 | size.add(small); 39 | size.add(medium); 40 | size.add(large); 41 | 42 | JLabel l2=new JLabel(); 43 | l2.setBorder(new TitledBorder("Toppings")); 44 | l2.setBounds(10,100,350,130); 45 | f.add(l2); 46 | plain=new JCheckBox("Plain"); 47 | plain.setBounds(20,130,80,30); 48 | f.add(plain); 49 | sausage=new JCheckBox("Sausage"); 50 | sausage.setBounds(100,130,80,30); 51 | f.add(sausage); 52 | mushroom=new JCheckBox("Mushroom"); 53 | mushroom.setBounds(180,130,100,30); 54 | f.add(mushroom); 55 | pepperoni=new JCheckBox("Pepperoni"); 56 | pepperoni.setBounds(20,160,100,30); 57 | f.add(pepperoni); 58 | 59 | bill=new JButton("Generate Bill"); 60 | bill.setBounds(100,230,150,30); 61 | f.add(bill); 62 | 63 | val=new JTextArea(); 64 | val.setBounds(10,300,360,80); 65 | val.setVisible(false); 66 | f.add(val); 67 | 68 | bill.addActionListener(this); 69 | plain.addItemListener(this); 70 | sausage.addItemListener(this); 71 | mushroom.addItemListener(this); 72 | pepperoni.addItemListener(this); 73 | f.setVisible(true); 74 | 75 | } 76 | 77 | public void actionPerformed(ActionEvent e) 78 | { 79 | if(small.isSelected()) 80 | { 81 | total=total+100; 82 | } 83 | if(medium.isSelected()) 84 | { 85 | total=total+130; 86 | } 87 | if(large.isSelected()) 88 | { 89 | total=total+160; 90 | } 91 | val.setVisible(true); 92 | val.setText("\nOrder placed Successfully!"+"\nAmount: Rs."+total); 93 | } 94 | 95 | public void itemStateChanged(ItemEvent e) 96 | { 97 | if(e.getSource()==plain) 98 | { 99 | total=total+0; 100 | } 101 | if(e.getSource()==sausage) 102 | { 103 | total=total+20; 104 | } 105 | if(e.getSource()==mushroom) 106 | { 107 | total=total+20; 108 | } 109 | if(e.getSource()==pepperoni) 110 | { 111 | total=total+20; 112 | } 113 | } 114 | 115 | public static void main(String[]args) 116 | { 117 | new Pizza(); 118 | } 119 | } -------------------------------------------------------------------------------- /Miscellaneous/Producer_Consumer.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.*; 4 | 5 | class Producer_Consumer 6 | { 7 | public static int ip = 1, in = 0, out = 3, p = 0; 8 | 9 | public static void main(String[] args) 10 | { 11 | int c; 12 | System.out.print("1.producer\n2.consumer\n3.exit"); 13 | while(true) 14 | { 15 | System.out.print("\nchoice :"); 16 | c = sc.nextInt(); 17 | switch(c) 18 | { 19 | case 1: 20 | if(ip == 1 && out != 0) 21 | { 22 | producer(); 23 | } 24 | else 25 | { 26 | System.out.println("buffer full"); 27 | } 28 | break; 29 | case 2: 30 | if(ip == 1 && in != 0) 31 | { 32 | consumer(); 33 | } 34 | else 35 | { 36 | System.out.println("buffer empty"); 37 | } 38 | break; 39 | case 3: 40 | System.exit(0); 41 | break; 42 | } 43 | } 44 | } 45 | 46 | public static int cons(int s) 47 | { 48 | return --s; 49 | } 50 | 51 | public static int prod(int s) 52 | { 53 | return ++s; 54 | } 55 | 56 | public static void producer() 57 | { 58 | ip = cons(ip); 59 | in = prod(in); 60 | out = cons(out); 61 | p++; 62 | System.out.println("produces " + p); 63 | ip = prod(ip); 64 | } 65 | 66 | public static void consumer() 67 | { 68 | ip = cons(ip); 69 | in = cons(in); 70 | out = prod(out); 71 | System.out.println("consumes " + p); 72 | p--; 73 | ip = prod(ip); 74 | } 75 | 76 | public final static Scanner sc = new Scanner(System.in); 77 | } 78 | -------------------------------------------------------------------------------- /Miscellaneous/PythagoreanPrimes.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | //Write a program to check if given number is a pythagorean prime or not. 3 | //A prime number of the form 4*n + 1 is a Pythagorean prime. 4 | import java.util.*; 5 | class PythagoreanPrimes 6 | { 7 | public static void main(String args[] ) throws Exception 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | for(int i = 0;i0) 17 | { 18 | c = c+1; 19 | n = n/10; 20 | } 21 | 22 | 23 | for(i=1;i 0) 20 | { 21 | done = false; 22 | if (x[i] > q) 23 | { 24 | t =t + q; 25 | x[i] -= q; 26 | } 27 | else 28 | { 29 | t = t + x[i]; 30 | wt[i] = t - bt[i]; 31 | x[i] = 0; 32 | } 33 | } 34 | } 35 | if (done == true) 36 | break; 37 | } 38 | } 39 | static void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) 40 | { 41 | for (int i = 0; i < n ; i++) 42 | { 43 | tat[i] = bt[i] + wt[i]; 44 | } 45 | 46 | } 47 | static void findavgTime(int processes[], int n, int bt[], int quantum) 48 | { 49 | int wt[] = new int[n], tat[] = new int[n]; 50 | int total_wt = 0, total_tat = 0; 51 | findWaitingTime(processes, n, bt, wt, quantum); 52 | findTurnAroundTime(processes, n, bt, wt, tat); 53 | System.out.println("Processes " + " Burst time " + " Waiting time " + " Turn around time"); 54 | for (int i=0; i= max) 16 | { 17 | System.out.println("max must be greater than min"); 18 | } 19 | else 20 | { 21 | System.out.println(n + " Random numbers between " + min + " and " + max + " are : "); 22 | for (int i = 0; i < n; i++) 23 | { 24 | System.out.println(rand.nextInt((max - min) + 1) + min); 25 | } 26 | } 27 | sc.close(); 28 | } 29 | } -------------------------------------------------------------------------------- /Miscellaneous/SJFScheduling.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Scanner; 6 | 7 | public class SJFScheduling { 8 | final static Scanner scanner = new Scanner(System.in); 9 | static float avgWT = 0, avgTAT = 0; 10 | 11 | public static void main(String[] args) { 12 | System.out.println("Shortest Job First Job Scheduling"); 13 | System.out.println("================================="); 14 | 15 | System.out.print("Enter the total number of Processes: "); 16 | int n = scanner.nextInt(); 17 | 18 | ArrayList List = new ArrayList(n); 19 | for (int index = 0; index < n; index++) 20 | { 21 | List.add(processInput(index)); 22 | } 23 | 24 | Sorting(List); 25 | calculateCT(List); 26 | calculateWT(List); 27 | calculateTAT(List); 28 | 29 | System.out.println("\tName" + " \tAT " + " \tBT " + " \tCT " + " \tWT " + "\tTAT "); 30 | for (Process process : List) 31 | { 32 | Output(process); 33 | avgWT = avgWT + process.wt; 34 | avgTAT = avgTAT + process.tat; 35 | } 36 | 37 | System.out.println("[x] Average WaitingTime = " + avgWT / List.size()); 38 | System.out.println("[x] Average TurnAroundTime = " + avgTAT / List.size()); 39 | } 40 | 41 | public static Process processInput(int index) 42 | { 43 | System.out.println("\nProcess - " + (index + 1)); 44 | Process process = new Process(); 45 | 46 | process.ProcessIndex = index; 47 | System.out.print("Enter the Process name: "); 48 | process.name = scanner.next(); 49 | System.out.print("Enter the Process Burst Time (BT): "); 50 | process.BurstTime = scanner.nextInt(); 51 | return process; 52 | } 53 | 54 | public static void Output(Process obj) 55 | { 56 | System.out.println("\t " + obj.name + "\t\t " + obj.ArrivalTime + "\t\t " + obj.BurstTime + "\t\t " + obj.CompleteTime + "\t\t " + obj.wt + "\t\t " + obj.tat); 57 | } 58 | 59 | public static void Sorting(ArrayList List) 60 | { 61 | for (int i = 0; i < List.size(); i++) 62 | for (int j = i; j < List.size(); j++) 63 | if (List.get(j).BurstTime < List.get(i).BurstTime) 64 | Collections.swap(List, i, j); 65 | } 66 | 67 | public static void calculateCT(ArrayList List) 68 | { 69 | if (!List.isEmpty()) 70 | { 71 | List.get(0).CompleteTime = List.get(0).BurstTime; 72 | for (int i = 0; i < List.size() - 1; i++) 73 | List.get(i + 1).CompleteTime = List.get(i).CompleteTime + List.get(i + 1).BurstTime; 74 | } 75 | } 76 | 77 | public static void calculateWT(ArrayList List) 78 | { 79 | if (!List.isEmpty()) 80 | { 81 | List.get(0).wt = 0; 82 | for (int i = 0; i < List.size() - 1; i++) 83 | List.get(i + 1).wt = List.get(i).CompleteTime - List.get(i + 1).ArrivalTime; 84 | } 85 | } 86 | 87 | public static void calculateTAT(ArrayList List) 88 | { 89 | if (!List.isEmpty()) 90 | for (Process process : List) 91 | process.tat = process.wt + process.BurstTime; 92 | } 93 | 94 | } 95 | 96 | class Process { 97 | String name; 98 | int ProcessIndex, ArrivalTime, BurstTime, CompleteTime, wt, tat; 99 | Process() { 100 | this.ProcessIndex = -1; 101 | this.ArrivalTime = 0; 102 | this.BurstTime = -1; 103 | this.CompleteTime = -1; 104 | this.wt = -1; 105 | this.tat = -1; 106 | this.name = "process"; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Miscellaneous/TagContentExtractor.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | // Obtaining string between html tags. Eg : input ->

Hello

would give the output -> Hello 4 | import java.util.Scanner; 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | public class TagContentExtractor 9 | { 10 | public static void main(String[] args) 11 | { 12 | Scanner scan = new Scanner(System.in); 13 | int testCases = Integer.parseInt(scan.nextLine()); 14 | while (testCases-- > 0) 15 | { 16 | String line = scan.nextLine(); 17 | boolean matchFound = false; 18 | Pattern r = Pattern.compile("<(.+)>([^<]+)"); 19 | Matcher m = r.matcher(line); 20 | while (m.find()) 21 | { 22 | System.out.println(m.group(2)); 23 | matchFound = true; 24 | } 25 | if (!matchFound) 26 | { 27 | System.out.println("None"); 28 | } 29 | } 30 | scan.close(); 31 | } 32 | } -------------------------------------------------------------------------------- /Miscellaneous/TwoPointersAlgo.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.*; 4 | 5 | class TwoPointersAlgo { 6 | // This function prints all pairs in the array that sum to a number X. If no 7 | // such pair exists then output will be -1. 8 | static void twoSum(int A[], int X) { 9 | Arrays.sort(A); 10 | 11 | // Array sorting is necessary for this algo to function correctly 12 | 13 | int n = A.length; 14 | int i = 0, j = n - 1, flag = 0; 15 | // Implementation of the algorithm starts 16 | while (i < j) { 17 | if (A[i] + A[j] == X) { 18 | System.out.println(A[i] + " " + A[j] + " " + X); 19 | flag = 1; 20 | i++; 21 | j--; 22 | } else if (A[i] + A[j] < X) { 23 | i++; 24 | } else 25 | j--; 26 | } 27 | 28 | // Implementation ends 29 | 30 | if (flag == 0) 31 | System.out.println(-1); 32 | }// end of function twoSum 33 | 34 | public static void main(String[] args) { 35 | Scanner in = new Scanner(System.in); 36 | int t, n; 37 | t = in.nextInt(); 38 | // for number of test cases 39 | while (t > 0) { 40 | t--; 41 | n = in.nextInt(); 42 | int a[] = new int[n]; 43 | for (int i = 0; i < n; i++) { 44 | a[i] = in.nextInt(); 45 | } 46 | // taking array input 47 | int k = in.nextInt(); // the total of the pair entered by the user 48 | twoSum(a, k); 49 | } 50 | in.close(); 51 | }// end of main 52 | }// end of class 53 | 54 | /* 55 | * Sample Input/Output Input: 2 7 1 2 3 4 5 6 7 98 7 1 2 3 4 5 6 7 8 Output: -1 56 | * 1 7 8 2 6 8 3 5 8 57 | */ -------------------------------------------------------------------------------- /Miscellaneous/UrlConcat.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | import java.util.*; 3 | import java.awt.datatransfer.StringSelection; 4 | import java.awt.Toolkit; 5 | import java.awt.datatransfer.Clipboard; 6 | class UrlConcat 7 | { 8 | public static void main(String args[]) 9 | { 10 | System.out.println("\nAfter Each Entry is completed, final result is copied to your clipboard"); 11 | Scanner sc = new Scanner(System.in); 12 | System.out.println("Enter number of entries : "); 13 | int n = sc.nextInt(); 14 | sc.nextLine(); 15 | System.out.println("Enter Base URL :"); 16 | String base_url = sc.nextLine(); 17 | String para = ""; 18 | for(int j=0;j"); 32 | String end_tag = (""); 33 | String link = start_tag+name+end_tag; 34 | int space_length = 30-name.length(); 35 | String space = ""; 36 | for(int i=0;i "); 32 | String a = sc.nextLine(); 33 | char str1[] = a.toCharArray(); 34 | a = rmd(str1); 35 | System.out.println("Enter the name of the second person(Do not provide spaces or any special characters): "); 36 | System.out.print("\t -> "); 37 | String b = sc.nextLine(); 38 | char str2[] = b.toCharArray(); 39 | b = rmd(str2); 40 | 41 | //System.out.println("String A after removing duplicate elements : " + a); 42 | //System.out.println("String B after removing duplicate elements : " + b); 43 | 44 | String cmn = ""; 45 | for(int i=0;i1) 72 | { 73 | for(int i=1;i Hey! You guys are gonna be Amazing Friends!!"); 115 | } 116 | else if(result=='L') 117 | { 118 | System.out.println("\t -> Look at that! You guys are so gonna Love each other!!!"); 119 | } 120 | else if(result=='A') 121 | { 122 | System.out.println("\t -> You both are gonna be affectionate about each other"); 123 | } 124 | else if(result=='M') 125 | { 126 | System.out.println("\t -> Look at that, You would end up marrying each other? Well Congrats!"); 127 | } 128 | else if(result=='E') 129 | { 130 | System.out.println("\t -> Ha, Looks like the odds are against you both. You both are gonna be enemies apparently XD"); 131 | } 132 | else if(result=='S') 133 | { 134 | System.out.println("\t -> Well, Brother or Sister from another mother! Siblingsss!!!"); 135 | } 136 | sc.close(); 137 | } 138 | } -------------------------------------------------------------------------------- /Miscellaneous/matrix.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | class JavaExample { 4 | public static void main(String[] args) { 5 | int rows = 2, columns = 4; 6 | 7 | // Declaring the two matrices as multi-dimensional arrays 8 | int[][] MatrixA = { { 1, 1, 1, 1 }, { 2, 3, 5, 2 } }; 9 | int[][] MatrixB = { { 2, 3, 4, 5 }, { 2, 2, 4, -4 } }; 10 | 11 | /* 12 | * Declaring a matrix sum, that will be the sum of MatrixA and MatrixB, the sum 13 | * matrix will have the same rows and columns as the given matrices. 14 | */ 15 | int[][] sum = new int[rows][columns]; 16 | for (int i = 0; i < rows; i++) { 17 | for (int j = 0; j < columns; j++) { 18 | sum[i][j] = MatrixA[i][j] + MatrixB[i][j]; 19 | } 20 | } 21 | // Displaying the sum matrix 22 | System.out.println("Sum of the given matrices is: "); 23 | for (int i = 0; i < rows; i++) { 24 | for (int j = 0; j < columns; j++) { 25 | System.out.print(sum[i][j] + " "); 26 | } 27 | System.out.println(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Miscellaneous/next_fitVsfirst_fit.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | 3 | import java.util.Arrays; 4 | 5 | class next_fitVsfirst_fit 6 | { 7 | static void firstFit(int blockSize[], int m, int processSize[], int n) 8 | { 9 | 10 | int alloc[] = new int[n]; 11 | for (int i = 0; i < alloc.length; i++) 12 | { 13 | alloc[i] = -1; 14 | } 15 | for (int i = 0; i < n; i++) 16 | { 17 | for (int j = 0; j < m; j++) 18 | { 19 | if (blockSize[j] >= processSize[i]) 20 | { 21 | alloc[i] = j; 22 | blockSize[j] -= processSize[i]; 23 | 24 | break; 25 | } 26 | } 27 | } 28 | 29 | System.out.println("\nProcess No.\tProcess Size\tBlock no."); 30 | for (int i = 0; i < n; i++) 31 | { 32 | System.out.print(" " + (i+1) + "\t\t" + processSize[i] + "\t\t"); 33 | if (alloc[i] != -1) 34 | System.out.print(alloc[i] + 1); 35 | else 36 | System.out.print("Not Allocated"); 37 | System.out.println(); 38 | } 39 | } 40 | 41 | static void NextFit(int blockSize[], int m, int processSize[], int n) 42 | { 43 | int alloc[] = new int[n], j = 0; 44 | Arrays.fill(alloc, -1); 45 | for (int i = 0; i < n; i++) 46 | { 47 | while (j < m) 48 | { 49 | 50 | if (blockSize[j] >= processSize[i]) 51 | { 52 | alloc[i] = j; 53 | blockSize[j] -= processSize[i]; 54 | 55 | break; 56 | } 57 | j = (j + 1) % m; 58 | } 59 | } 60 | 61 | System.out.print("\nProcess No.\tProcess Size\tBlock no.\n"); 62 | for (int i = 0; i < n; i++) 63 | { 64 | System.out.print( i + 1 + "\t\t" + processSize[i] + "\t\t"); 65 | if (alloc[i] != -1) 66 | { 67 | System.out.print(alloc[i] + 1); 68 | } 69 | else 70 | { 71 | System.out.print("Not Allocated"); 72 | } 73 | System.out.println(""); 74 | } 75 | } 76 | 77 | public static void main(String[] args) 78 | { 79 | int blockSize[] = {5, 10, 20}; 80 | int processSize[] = {10, 20, 5}; 81 | int m = blockSize.length; 82 | int n = processSize.length; 83 | 84 | System.out.println("First Fit :"); 85 | firstFit(blockSize, m, processSize, n); 86 | 87 | int blockSize2[] = {5, 10, 20}; 88 | int processSize2[] = {10, 20, 5}; 89 | m = blockSize.length; 90 | n = processSize.length; 91 | System.out.println("\nNext Fit :"); 92 | NextFit(blockSize2, m, processSize2, n); 93 | } 94 | } -------------------------------------------------------------------------------- /Miscellaneous/nonloop.java: -------------------------------------------------------------------------------- 1 | package Miscellaneous; 2 | import java.util.*; 3 | class nonloop 4 | { 5 | static int pos = 0;//Intialising the value of pos. 6 | static int a[] = new int [500];//Creating a static array. 7 | static void save(int n) 8 | { 9 | a[pos] = n;//Basically, inputing the value scanned from recursion into the array. 10 | pos++;//Incrementing the value to keep adding into the array consecutively. 11 | } 12 | static void print(int i) 13 | { 14 | if(i11000) 34 | { 35 | amt = days*charges*1.02; 36 | } 37 | return amt; 38 | } 39 | static void dispInfo(double amt) 40 | { 41 | System.out.println("Roll Number : " + Rno); 42 | System.out.println("Name : " + name); 43 | System.out.println("Charges : " + charges); 44 | System.out.println("Days : " + days); 45 | System.out.println("Amount of money to be paid : " + amt); 46 | } 47 | } -------------------------------------------------------------------------------- /OOPS/AbstractClass.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | import java.util.*; 4 | 5 | abstract class Book { 6 | String title; 7 | String author; 8 | Book(String t, String a) { 9 | title = t; 10 | author = a; 11 | } 12 | abstract void display(); 13 | } 14 | 15 | class MyBook extends Book { 16 | int price; 17 | 18 | MyBook(String title, String author, int price) { 19 | super(title, author); 20 | this.price = price; 21 | } 22 | 23 | void display() { 24 | System.out.println("Title: " + title); 25 | System.out.println("Author: " + author); 26 | System.out.println("Price: " + price); 27 | } 28 | } 29 | 30 | public class AbstractClass { 31 | public static void main(String[] args) { 32 | Scanner sc = new Scanner(System.in); 33 | String title = sc.nextLine(); 34 | String author = sc.nextLine(); 35 | int price = sc.nextInt(); 36 | Book new_novel = new MyBook(title, author, price); 37 | new_novel.display(); 38 | sc.close(); 39 | } 40 | } -------------------------------------------------------------------------------- /OOPS/ArrayOfObjects.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | public class ArrayOfObjects 4 | { 5 | public static void main(String args[]) 6 | { 7 | Account obj[] = new Account[2] ; 8 | // The above line exactly creates an array of two reference variables, i.e., obj[0] and obj[1]. 9 | obj[0] = new Account();// creates object and assigns to the reference variable 10 | obj[1] = new Account(); 11 | 12 | obj[0].setData(1,2); 13 | 14 | obj[1].setData(3,4); 15 | 16 | System.out.println("For Array Element 0"); 17 | obj[0].showData(); 18 | 19 | System.out.println("For Array Element 1"); 20 | obj[1].showData(); 21 | } 22 | } 23 | class Account 24 | { 25 | int a; 26 | int b; 27 | public void setData(int c,int d) 28 | { 29 | a=c; 30 | b=d; 31 | } 32 | public void showData() 33 | { 34 | System.out.println("Value of a = "+a); 35 | System.out.println("Value of b = "+b); 36 | } 37 | } -------------------------------------------------------------------------------- /OOPS/Classes.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | class employee 4 | { 5 | int id = 5; 6 | String name = "Vijay"; 7 | 8 | public static void main(String args[]) 9 | { 10 | employee e = new employee(); //Object 11 | 12 | System.out.println(e.id); //Accessing through reference variable 13 | System.out.println(e.name); 14 | } 15 | } 16 | 17 | // Creating a seperate class. 18 | 19 | class employee1 20 | { 21 | int id = 5; 22 | String name = "Vijay"; 23 | } 24 | 25 | class case1 26 | { 27 | public static void main(String args[]) 28 | { 29 | employee1 s1=new employee1(); 30 | System.out.println(s1.id); 31 | System.out.println(s1.name); 32 | } 33 | } -------------------------------------------------------------------------------- /OOPS/Constructors.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | class Book2 4 | { 5 | public String title; 6 | public String author; 7 | public int cost; 8 | 9 | void initialize(String t, String a, int c) 10 | { 11 | title = t; 12 | author = a; 13 | cost = c; 14 | } 15 | } 16 | 17 | public class Constructors 18 | { 19 | public static void main(String args[]) 20 | { 21 | Book2 obj = new Book2(); 22 | Book2 obj2 = new Book2(); 23 | 24 | // Initializing instance variable by approach 1 25 | obj.title = "Harry Potter"; 26 | obj.author = "J.K Rowling"; 27 | obj.cost = 600; 28 | System.out.println(obj.title + "\n" + obj.author + "\n" + obj.cost); 29 | 30 | // Initializing instance variable by approach 2 31 | obj2.initialize("Percy Jackson", "Rick riordan", 700); 32 | System.out.println(obj2.title + "\n" + obj2.author + "\n" + obj2.cost); 33 | } 34 | } -------------------------------------------------------------------------------- /OOPS/Encapsulation/Accessing.java: -------------------------------------------------------------------------------- 1 | package OOPS.Encapsulation; 2 | 3 | /* 4 | Accessing private variables using public methods. 5 | Any class that wants to access these variables must access them through these getters and setters(Access point methods) 6 | */ 7 | 8 | public class Accessing 9 | { 10 | public static void main(String args[]) 11 | { 12 | Encapsulation enc = new Encapsulation(); 13 | enc.setName("James"); 14 | enc.setAge(20); 15 | enc.setIdNo("12343ms"); 16 | 17 | System.out.println("Name : " + enc.getName()); 18 | System.out.println("Age : " + enc.getAge()); 19 | } 20 | } -------------------------------------------------------------------------------- /OOPS/Encapsulation/Encapsulation.java: -------------------------------------------------------------------------------- 1 | package OOPS.Encapsulation; 2 | 3 | /*Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. 4 | In encapsulation, the variables of a class will be hidden from other classes, 5 | and can be accessed only through the methods of their current class. 6 | Therefore, it is also known as data hiding. 7 | */ 8 | 9 | class Encapsulation 10 | { 11 | private String name; 12 | private String idNo; 13 | private int age; 14 | 15 | public int getAge() 16 | { 17 | return age; 18 | } 19 | 20 | public String getName() 21 | { 22 | return name; 23 | } 24 | 25 | public String getIdNum() 26 | { 27 | return idNo; 28 | } 29 | 30 | public void setAge(int Age) 31 | { 32 | age = Age; 33 | } 34 | 35 | public void setName(String Name) 36 | { 37 | name = Name; 38 | } 39 | 40 | public void setIdNo( String Id) 41 | { 42 | idNo = Id; 43 | } 44 | } -------------------------------------------------------------------------------- /OOPS/Examples/Bookstore.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | 3 | class Author 4 | { 5 | private String fName; 6 | private String lName; 7 | 8 | public Author (String firstName, String lastName) 9 | { 10 | fName = firstName; 11 | lName = lastName; 12 | } 13 | public void setFName (String firstName) 14 | { 15 | fName = firstName; 16 | } 17 | public void setLName (String lastName) 18 | { 19 | lName = lastName; 20 | } 21 | public String getFName() 22 | { 23 | return fName; 24 | } 25 | public String getLName() 26 | { 27 | return lName; 28 | } 29 | public String toString() 30 | { 31 | String name = (fName + " " + lName); 32 | return name; 33 | } 34 | } 35 | 36 | class Book 37 | { 38 | private String title; 39 | private Author author; 40 | private double price; 41 | 42 | public Book(String t, Author a, double p) 43 | { 44 | title = t; 45 | author = a; 46 | price = p; 47 | } 48 | public void setTitle(String t) 49 | { 50 | title = t; 51 | } 52 | public void setAuthor(Author a) 53 | { 54 | author = a; 55 | } 56 | public void setPrice(double p) 57 | { 58 | price = p; 59 | } 60 | public String getTitle() 61 | { 62 | return title; 63 | } 64 | public Author getAuthor() 65 | { 66 | return author; 67 | } 68 | public double getPrice() 69 | { 70 | return price; 71 | } 72 | public String toString() 73 | { 74 | String temp = ("Title: " + title + "\nAuthor: " + author + "\nPrice: Rs " + price); 75 | return temp; 76 | } 77 | } 78 | 79 | public class Bookstore 80 | { 81 | public static void main(String[] args) 82 | { 83 | // Various test cases for the different methods present in the Book class. 84 | Author author1 = new Author("Herbert","Schildert"); 85 | Book Book1 = new Book("Object Oriented Programming Systems", author1 ,1000); 86 | System.out.println(Book1 + "\n"); // Printing the values of Book1 87 | 88 | Author author2 = new Author("J.K","Rowling"); 89 | Book Book2 = new Book("Harry potter",author2,700); // Creating a second object. 90 | System.out.println(Book2 + "\n"); 91 | Book2.setTitle("Percy Jackson"); // Updating it's values. 92 | Author author3 = new Author("Rick","Riordan"); 93 | Book2.setAuthor(author3); 94 | Book2.setPrice(735); 95 | System.out.println("Title : " + Book2.getTitle() + "\nAuthor : " + Book2.getAuthor() + "\nPrice : Rs " + Book2.getPrice()); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /OOPS/Examples/Distribution.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | 3 | public class Distribution 4 | { 5 | int children; 6 | int sweets; 7 | 8 | public Distribution(int c, int s) 9 | { 10 | children = c; 11 | sweets = s; 12 | } 13 | 14 | float distribute() 15 | { 16 | float result = 0; 17 | try 18 | { 19 | result = sweets/children; 20 | } 21 | catch (Exception e) 22 | { 23 | System.out.println("ERROR : Cannot divide by Zero"); 24 | System.exit(0); 25 | } 26 | return result; 27 | } 28 | } 29 | 30 | class result 31 | { 32 | public static void main(String args[]) 33 | { 34 | Distribution x = new Distribution(50,50); 35 | System.out.println("Each child gets " + x.distribute() + " sweets."); 36 | Distribution y = new Distribution(25,50); 37 | System.out.println("Each child gets " + y.distribute() + " sweets."); 38 | Distribution z = new Distribution(0,50); 39 | System.out.println("Each child gets : " + z.distribute() + " sweets."); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /OOPS/Examples/GenericMethods.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | 3 | class GenericMethods 4 | { 5 | x obj1; 6 | y obj2; 7 | z obj3; 8 | 9 | public GenericMethods(x obj1, y obj2, z obj3) 10 | { 11 | this.obj1 = obj1; 12 | this.obj2 = obj2; 13 | this.obj3 = obj3; 14 | } 15 | 16 | public void Display(a obj1, b obj2, c obj3) 17 | { 18 | System.out.println("|\t" + obj1 + "\t|\t" + obj2 + "\t|\t" + obj3 + "\t|"); 19 | } 20 | 21 | public static void main(String args[]) 22 | { 23 | System.out.println("Table : "); 24 | System.out.println("-------------------------------------------------"); 25 | GenericMethods a = new GenericMethods<>(18, 10861, "Virat"); 26 | a.Display(18, 10861, "Virat"); 27 | GenericMethods b = new GenericMethods<>("Sachin", 10, 18426); 28 | b.Display("Sachin", 10, 18426); 29 | GenericMethods c = new GenericMethods<>(7, "Dhoni", 10534); 30 | c.Display(7, "Dhoni", 10534); 31 | GenericMethods d = new GenericMethods<>(10201, 333, "Gayle"); 32 | d.Display(10201, 333, "Gayle"); 33 | GenericMethods e = new GenericMethods<>("ABD", 9577, 17); 34 | e.Display("ABD", 9577, 17); 35 | System.out.println("-------------------------------------------------"); 36 | } 37 | } -------------------------------------------------------------------------------- /OOPS/Examples/Holiday.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | 3 | class Holiday 4 | { 5 | String name; 6 | private int day; 7 | private String month; 8 | 9 | //Constructor 10 | public Holiday(String n, int d, String m) 11 | { 12 | name = n; 13 | day = d; 14 | month = m; 15 | } 16 | 17 | //Method to check if months of two different holidays are same or not. 18 | public boolean inSameMonth(Holiday hol) 19 | { 20 | if(this.month == hol.month) 21 | { 22 | return true; 23 | } 24 | else 25 | { 26 | return false; 27 | } 28 | } 29 | 30 | //Method to calculate the average value of date from different Holidays. 31 | public static double avgDate(Holiday[] hol) 32 | { 33 | double result; 34 | double sum = 0; 35 | for(int i = 0; i < hol.length; i++) 36 | { 37 | sum = sum + hol[i].day; 38 | } 39 | result = sum/hol.length; 40 | return result; 41 | } 42 | 43 | public static void main(String args[]) 44 | { 45 | //Creating a Holiday instance and printing it's values. 46 | Holiday hol = new Holiday("Independance day", 15, "August"); 47 | System.out.println("Name of the holiday : " + hol.name); 48 | System.out.println("Holiday is on : " + hol.day + " of " + hol.month); 49 | 50 | //Creating a holiday instance to check if the months match. 51 | Holiday hol1 = new Holiday("New year",1,"January"); 52 | System.out.println("\nChecking if New year and Independance day are on the same month : "); 53 | if(hol.inSameMonth(hol1)) 54 | { 55 | System.out.println("Holidays are in the same month!"); 56 | } 57 | else 58 | { 59 | System.out.println("Holidays are in different months"); 60 | } 61 | 62 | //Creating another holiday instance to check if the months match this time. 63 | Holiday hol2 = new Holiday("Sankranthi",14,"January"); 64 | System.out.println("\nChecking if New year and Sankranthi are on the same month : "); 65 | if(hol1.inSameMonth(hol2)) 66 | { 67 | System.out.println("Holidays are in the same month!"); 68 | } 69 | else 70 | { 71 | System.out.println("Holidays are in different months"); 72 | } 73 | 74 | //Creating an array of objects. 75 | Holiday obj[] = new Holiday[5] ; 76 | obj[0] = new Holiday("Diwali", 14, "November"); 77 | obj[1] = new Holiday("Republic Day", 26, "January"); 78 | obj[2] = new Holiday("Gandhi Jayanti", 2, "October"); 79 | obj[3] = new Holiday("Christmas", 25, "December"); 80 | obj[4] = new Holiday("Labour's Day", 1, "May"); 81 | System.out.println("\nAverage value of days for the holidays given : " + avgDate(obj)); 82 | System.out.println(); 83 | } 84 | } -------------------------------------------------------------------------------- /OOPS/Examples/SemesterMarks.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | 3 | class Sem1 4 | { 5 | double m1,m2,m3,m4,avg1; 6 | public Sem1() 7 | { 8 | m1 = 78; 9 | m2 = 86; 10 | m3 = 84; 11 | m4 = 58; 12 | avg1 = (m1 + m2 + m3 + m4)/4; 13 | } 14 | } 15 | 16 | class Sem2 extends Sem1 17 | { 18 | double m5,m6,m7,m8,avg2; 19 | public Sem2() 20 | { 21 | m5 = 87; 22 | m6 = 63; 23 | m7 = 78; 24 | m8 = 99; 25 | avg2 = (m5 + m6 + m7 + m8)/4; 26 | } 27 | } 28 | 29 | class Sem3 extends Sem2 30 | { 31 | 32 | double m9,m10,m11,m12,avg3; 33 | public Sem3() 34 | { 35 | m9 = 73; 36 | m10 = 68; 37 | m11 = 94; 38 | m12 = 100; 39 | avg3 = (m9 + m10 + m11 + m12)/4; 40 | } 41 | } 42 | 43 | class Sem4 extends Sem3 44 | { 45 | double m13,m14,m15,m16,avg4; 46 | public Sem4() 47 | { 48 | m13 = 87; 49 | m14 = 63; 50 | m15 = 48; 51 | m16 = 98; 52 | avg4 = (m13 + m14 + m15 + m16)/4; 53 | } 54 | 55 | double totatavg() 56 | { 57 | double totalAvg = (avg1 + avg2 + avg3 + avg4)/4; 58 | return totalAvg; 59 | } 60 | } 61 | 62 | class SemesterMarks 63 | { 64 | public static void main(String args[]) 65 | { 66 | Sem4 s4 = new Sem4(); 67 | System.out.println("----------------------------------"); 68 | System.out.println("Average marks of each semesters : "); 69 | System.out.println("Semester 1 average marks : " + s4.avg1); 70 | System.out.println("Semester 2 average marks : " + s4.avg2); 71 | System.out.println("Semester 3 average marks : " + s4.avg3); 72 | System.out.println("Semester 4 average marks : " + s4.avg4); 73 | System.out.println("----------------------------------"); 74 | System.out.println("Total average marks of each semesters : "); 75 | System.out.println("Total average marks : " + s4.totatavg()); 76 | } 77 | } -------------------------------------------------------------------------------- /OOPS/Examples/Shape.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | 3 | public abstract class Shape 4 | { 5 | protected String shapeName; 6 | 7 | public Shape (String shapeName) 8 | { 9 | this.shapeName = shapeName; 10 | } 11 | 12 | abstract double area(); 13 | 14 | public String toString() 15 | { 16 | return shapeName; 17 | } 18 | } 19 | 20 | class Sphere extends Shape 21 | { 22 | private double radius; 23 | public Sphere(double radius) 24 | { 25 | super("Sphere"); 26 | this.radius = radius; 27 | } 28 | public double area() 29 | { 30 | return 4*Math.PI*radius*radius; 31 | } 32 | } 33 | 34 | 35 | class Rectangle extends Shape 36 | { 37 | private double length; 38 | private double width; 39 | 40 | public Rectangle (double length, double width) 41 | { 42 | super("Rectangle"); 43 | this.length = length; 44 | this.width = width; 45 | } 46 | 47 | public double area() 48 | { 49 | return length * width; 50 | } 51 | } 52 | 53 | class Cylinder extends Shape 54 | { 55 | private double radius; 56 | private double height; 57 | 58 | public Cylinder (double radius, double height) 59 | { 60 | super("Cylinder"); 61 | this.radius = radius; 62 | this.height = height; 63 | } 64 | 65 | public double area() 66 | { 67 | return Math.PI * radius * radius * height; 68 | } 69 | } 70 | 71 | class Paint 72 | { 73 | private double coverage; 74 | public Paint(double coverage) 75 | { 76 | this.coverage = coverage; 77 | } 78 | public double amount(Shape s) 79 | { 80 | double finalAmount = s.area()/coverage; 81 | System.out.println("Quantity needed for " + s + " is " + finalAmount); 82 | return finalAmount; 83 | } 84 | } 85 | 86 | class PaintThings 87 | { 88 | public static void main (String[] args) 89 | { 90 | final double coverage = 350; 91 | Paint paint = new Paint(coverage); 92 | Rectangle deck = new Rectangle (20,30); 93 | Sphere bigBall = new Sphere (15); 94 | Cylinder tank = new Cylinder (10,30); 95 | double deckAmt, ballAmt, tankAmt; 96 | 97 | deckAmt = paint.amount(deck); 98 | ballAmt = paint.amount(bigBall); 99 | tankAmt = paint.amount(tank); 100 | 101 | System.out.println ("\nQuantity of paint needed : \n--------------"); 102 | System.out.println ("Deck " + deckAmt); 103 | System.out.println ("Big Ball " + ballAmt); 104 | System.out.println ("Tank " + tankAmt); 105 | } 106 | } -------------------------------------------------------------------------------- /OOPS/Examples/Shapes.java: -------------------------------------------------------------------------------- 1 | package OOPS.Examples; 2 | import java.util.*; 3 | 4 | 5 | 6 | public class Shapes { 7 | 8 | public int setArea(int Height, int Base) { 9 | int Area = Height * Base/2; 10 | return Area; 11 | } 12 | public int setArea(int Side) { 13 | int Area = Side*Side; 14 | return Area; 15 | } 16 | public double setArea(double Pie, int Radius) { 17 | double Area = Pie*Radius*Radius; 18 | return Area; 19 | } 20 | 21 | 22 | } 23 | 24 | class Triangle extends Shapes{ 25 | 26 | int Height; 27 | int Base; 28 | void getInputs() { 29 | System.out.print("Enter height of the triangle:"); 30 | Scanner s = new Scanner(System.in); 31 | Height = s.nextInt(); 32 | System.out.print("Enter base of the triangle:"); 33 | Base = s.nextInt(); 34 | System.out.println("The Area of Triangle is: "+setArea(Height,Base)); 35 | 36 | } 37 | 38 | } 39 | 40 | class Square extends Shapes{ 41 | int Side; 42 | void getInputs(){ 43 | System.out.print("\n\nEnter side of the square:"); 44 | Scanner sc = new Scanner(System.in); 45 | Side = sc.nextInt(); 46 | System.out.println("The Area of Square is: "+setArea(Side)); 47 | 48 | } 49 | } 50 | 51 | class Circle extends Shapes{ 52 | int Radius; 53 | double Pie = Math.PI; 54 | void getInputs() { 55 | System.out.print("\n\nEnter radius of the circle:"); 56 | Scanner sc1 = new Scanner(System.in); 57 | Radius = sc1.nextInt(); 58 | System.out.println("The Area of Circle is: "+String.format("%.2f",setArea(Pie,Radius))); 59 | } 60 | } 61 | 62 | class Tester{ 63 | 64 | public static void main(String args[]) { 65 | Triangle triangle = new Triangle(); 66 | Square square = new Square(); 67 | Circle circle = new Circle(); 68 | 69 | triangle.getInputs(); 70 | square.getInputs(); 71 | circle.getInputs(); 72 | } 73 | } 74 | 75 | 76 | -------------------------------------------------------------------------------- /OOPS/Initializing_objects.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | /* There are 3 ways to initialize object in Java. 4 | 5 | 1) By reference variable 6 | 2) By method 7 | 3) By constructor */ 8 | 9 | // Referencing variables 10 | class Employee2 11 | { 12 | int id; 13 | String name; 14 | } 15 | 16 | class case2 17 | { 18 | public static void main(String args[]) 19 | { 20 | Employee2 e1 = new Employee2(); 21 | Employee2 e2 = new Employee2(); 22 | 23 | e1.id=101; 24 | e1.name="Vijay"; 25 | e2.id=102; 26 | e2.name="Unknown"; 27 | 28 | System.out.println(e1.id+" "+e1.name); 29 | System.out.println(e2.id+" "+e2.name); 30 | } 31 | } -------------------------------------------------------------------------------- /OOPS/Object.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | public class Object 4 | { 5 | public String title="Playing with JAVA"; 6 | public String author="Mr. X"; 7 | public String ISBN= "ISBN20201307"; 8 | public int cost= 100; 9 | 10 | Object obj; 11 | 12 | public static void main(String args[]) 13 | { 14 | Object obj = new Object(); 15 | System.out.println(obj.title); 16 | } 17 | } -------------------------------------------------------------------------------- /OOPS/Polymorphism.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | class Polymorphism 4 | { 5 | public int area(int side) 6 | { 7 | int value = side*side*side; 8 | return value; 9 | } 10 | public double area(double radius) 11 | { 12 | double value = 3.14*radius*radius; 13 | return value; 14 | } 15 | public void area(String message) 16 | { 17 | System.out.println("Hello " + message); 18 | } 19 | public static void main(String args[]) 20 | { 21 | System.out.print("Polymorphism is the ability of an object to take on many forms.\n"+ 22 | "The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.\n"+ 23 | "You only have to change the argument list."); 24 | Polymorphism obj1 = new Polymorphism(); 25 | System.out.println(obj1.area(5)); 26 | System.out.println(obj1.area(1.5)); 27 | obj1.area("World!"); 28 | } 29 | } -------------------------------------------------------------------------------- /OOPS/Static.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | class JavaExample 4 | { 5 | static int var1 = 77; 6 | String var2; 7 | 8 | static {System.out.println("This is declared static, hence printed first XD");} 9 | public static void main(String[] args) 10 | { 11 | JavaExample obj1 = new JavaExample(); 12 | JavaExample obj2 = new JavaExample(); 13 | 14 | var1 = 88; 15 | obj1.var2 = "Initial value -> But updated thanks to static!"; 16 | 17 | var1 = 98; 18 | obj2.var2 = "I have updated but... -> Works cause not static :("; 19 | 20 | System.out.println(obj1.var2); 21 | System.out.println(var1); 22 | System.out.println(obj2.var2); 23 | System.out.println(var1); 24 | } 25 | } -------------------------------------------------------------------------------- /OOPS/This_keyword.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | //“this” can be used inside any method to refer to the current object. 4 | //That is, “this” is always a reference to the object on which the method was invoked. 5 | 6 | 7 | public class This_keyword 8 | { 9 | String title = "Playing with JAVA"; 10 | public static void main(String[] args) 11 | { 12 | This_keyword obj = new This_keyword(); 13 | System.out.println(obj.title); 14 | obj.show(); 15 | obj.show1(); 16 | } 17 | public void show() 18 | { 19 | String title = "Playing with Python"; //local variable 20 | System.out.println(title); 21 | } 22 | public void show1() 23 | { 24 | String title = "Playing with C"; //local variable 25 | System.out.println(this.title); 26 | System.out.println(title); 27 | } 28 | } 29 | 30 | // NOTE : Cannot use this keyword in a static method. -------------------------------------------------------------------------------- /OOPS/Variables.java: -------------------------------------------------------------------------------- 1 | package OOPS; 2 | 3 | public class Variables 4 | { 5 | int A = 50; //instance variable -> Inside class, outside method. 6 | 7 | static int B = 70; //Static variable -> Shared among all instances of the class. 8 | 9 | void random() 10 | { 11 | int C = 90; //Local variable -> Inside method and can be used only within the method. 12 | System.out.println("Local Variable : " + C); 13 | } 14 | public static void main(String args[]) 15 | { 16 | Variables rn = new Variables(); 17 | System.out.println("Instance variable A : "+rn.A); 18 | System.out.println("Static variable B : " + B); 19 | rn.random(); 20 | //System.out.println(C); 21 | } 22 | } -------------------------------------------------------------------------------- /Packages/pkg1/Project1.java: -------------------------------------------------------------------------------- 1 | package Packages.pkg1; 2 | import java.util.Scanner; 3 | 4 | import Packages.pkg2.Project2; 5 | 6 | class Project1 7 | { 8 | public static void main(String args[]) 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Enter result of student"); 12 | int result = sc.nextInt(); 13 | System.out.println("Result -> " + result); 14 | Project2 answer = new Project2(); 15 | answer.validate(result); 16 | sc.close(); 17 | } 18 | } -------------------------------------------------------------------------------- /Packages/pkg2/Project2.java: -------------------------------------------------------------------------------- 1 | package Packages.pkg2; 2 | public class Project2 3 | { 4 | public void validate(int result) 5 | { 6 | if(result<=20) 7 | { 8 | throw new ArithmeticException("FAILED :'( BETTER LUCK NEXT TIME..."); 9 | } 10 | else 11 | { 12 | System.out.println("EASY PASSED OFF :)"); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Searching & Sorting/Binary Search.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class BinarySearch 4 | { 5 | int binarySearch(int array[], int element) 6 | { 7 | int left = 0, right = array.length - 1; 8 | while (left <= right) 9 | { 10 | int mid = left + (right - left) / 2; 11 | if (array[mid] == element) 12 | { 13 | return mid; 14 | } 15 | if (array[mid] < element) 16 | { 17 | left = mid + 1; 18 | } 19 | else 20 | { 21 | right = mid - 1; 22 | } 23 | } 24 | return -1; 25 | } 26 | public static void main(String args[]) 27 | { 28 | BinarySearch bs = new BinarySearch(); 29 | Scanner sc = new Scanner(System.in); 30 | System.out.println("Enter the number of elements : "); 31 | int n = sc.nextInt(); 32 | System.out.println("Enter " + n + " elements"); 33 | int array[] = new int [n]; 34 | for(int i=0;i arr[j+1]) { 18 | swap(arr,j,j+1); 19 | } 20 | } 21 | } 22 | } 23 | 24 | public static void swap(int[] arr, int a, int b){ 25 | arr[a] = arr[a]+arr[b]; 26 | arr[b] = arr[a]-arr[b]; 27 | arr[a] = arr[a]-arr[b]; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Searching & Sorting/Heap Sort.java: -------------------------------------------------------------------------------- 1 | class HeapSort 2 | { 3 | public static void sort(int arr[]) 4 | { 5 | int n = arr.length; 6 | for (int i = (n/2 - 1); i >= 0; i--) 7 | { 8 | heapify(arr, n, i); 9 | } 10 | for (int i = n - 1; i > 0; i--) 11 | { 12 | int temp = arr[0]; 13 | arr[0] = arr[i]; 14 | arr[i] = temp; 15 | 16 | heapify(arr, i, 0); 17 | } 18 | } 19 | 20 | public static void heapify(int array[], int n, int i) 21 | { 22 | int largest = i; 23 | int left = (2 * i) + 1; 24 | int right = (2 * i) + 2; 25 | 26 | if (left < n && array[left] > array[largest]) 27 | { 28 | largest = left; 29 | } 30 | if (right < n && array[right] > array[largest]) 31 | { 32 | largest = right; 33 | } 34 | if (largest != i) 35 | { 36 | int temp = array[i]; 37 | array[i] = array[largest]; 38 | array[largest] = temp; 39 | heapify(array, n, largest); 40 | } 41 | } 42 | 43 | static void output(int arr[]) 44 | { 45 | int n = arr.length; 46 | for (int i=0; i= 0 && arr[j] > key) 14 | { 15 | arr[j + 1] = arr[j]; 16 | j = j - 1; 17 | } 18 | arr[j + 1] = key; 19 | } 20 | } 21 | 22 | 23 | static void printArray(int arr[]) 24 | { 25 | int n = arr.length; 26 | for (int i = 0; i < n; ++i) 27 | System.out.print(arr[i] + " "); 28 | 29 | System.out.println(); 30 | } 31 | 32 | public static void main(String args[]) 33 | { 34 | int arr[] = { 12, 11, 13, 5, 6 }; 35 | 36 | InsertionSort ob = new InsertionSort(); 37 | ob.sort(arr); 38 | 39 | printArray(arr); 40 | } 41 | } -------------------------------------------------------------------------------- /Searching & Sorting/Linear Search.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class LinearSearch 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("Enter the number of elements : "); 9 | int n = sc.nextInt(); 10 | int numbers[] = new int [n]; 11 | 12 | System.out.println("Enter " + n + " elements"); 13 | for(int i=0;i mx) 11 | mx = arr[i]; 12 | return mx; 13 | } 14 | 15 | 16 | static void countSort(int arr[], int n, int exp) 17 | { 18 | int output[] = new int[n]; 19 | int i; 20 | int count[] = new int[10]; 21 | Arrays.fill(count, 0); 22 | 23 | 24 | for (i = 0; i < n; i++) 25 | count[(arr[i] / exp) % 10]++; 26 | for (i = 1; i < 10; i++) 27 | count[i] += count[i - 1]; 28 | for (i = n - 1; i >= 0; i--) { 29 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 30 | count[(arr[i] / exp) % 10]--; 31 | } 32 | for (i = 0; i < n; i++) 33 | arr[i] = output[i]; 34 | } 35 | static void radixsort(int arr[], int n) 36 | { 37 | int m = getMax(arr, n); 38 | for (int exp = 1; m / exp > 0; exp *= 10) 39 | countSort(arr, n, exp); 40 | } 41 | 42 | 43 | static void print(int arr[], int n) 44 | { 45 | for (int i = 0; i < n; i++) 46 | System.out.print(arr[i] + " "); 47 | } 48 | 49 | public static void main(String[] args) 50 | { 51 | int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; 52 | int n = arr.length; 53 | radixsort(arr, n); 54 | print(arr, n); 55 | } 56 | } -------------------------------------------------------------------------------- /Searching & Sorting/Selection Sort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class SelectionSort 3 | { 4 | void selsort(int arr[]) 5 | { 6 | int n = arr.length; 7 | int i,j,k; 8 | int min_index; 9 | 10 | for(i=0;i l; 8 | column(int n,int arr[][],int j,List l) 9 | { 10 | this.n = n; 11 | this.arr = arr; 12 | this.j = j; 13 | this.l = l; 14 | } 15 | public void run() 16 | { 17 | int sum = 0; 18 | for(int i = 0; i < n; i++) 19 | { 20 | sum += arr[i][j]; 21 | } 22 | System.out.println("sum of " + j + "th column is " + sum); 23 | l.add(sum); 24 | } 25 | } 26 | class Sum extends Thread 27 | { 28 | int n; 29 | List l; 30 | int sum = 0; 31 | Sum(int n, List l) 32 | { 33 | this.n = n; 34 | this.l = l; 35 | } 36 | public void run() 37 | { 38 | for (int i: l) 39 | { 40 | sum += i; 41 | } 42 | //System.out.println(sum); 43 | } 44 | } 45 | public class Matrix 46 | { 47 | static int n; 48 | static int arr[][]; 49 | public static void main(String args[]) throws InterruptedException 50 | { 51 | Scanner sc = new Scanner(System.in); 52 | System.out.println("Enter value of n for nXn matrix"); 53 | n = sc.nextInt(); 54 | arr = new int[n][n]; 55 | 56 | List l = new ArrayList<>(); 57 | System.out.println("Enter values of matrix"); 58 | for(int i = 0; i < n; i++) 59 | { 60 | for(int j = 0; j < n; j++) 61 | { 62 | System.out.println("Enter value of ("+i+","+j+")"); 63 | arr[i][j] = sc.nextInt(); 64 | } 65 | } 66 | for(int i = 0; i < n; i++) 67 | { 68 | column c = new column(n,arr,i,l); 69 | c.start(); 70 | c.join(); 71 | } 72 | Sum s = new Sum(n,l); 73 | s.start(); 74 | s.join(); 75 | System.out.println("sum of matrix elements is " + s.sum); 76 | sc.close(); 77 | } 78 | } -------------------------------------------------------------------------------- /Threads/Test1.java: -------------------------------------------------------------------------------- 1 | package Threads; 2 | 3 | class Reverse 4 | { 5 | static int num; 6 | 7 | int find(int num) 8 | { 9 | int reversed = 0; 10 | while(num != 0) { 11 | int digit = num % 10; 12 | reversed = reversed * 10 + digit; 13 | num /= 10; 14 | } 15 | return reversed; 16 | } 17 | } 18 | 19 | class ReverseR extends Reverse 20 | { 21 | int find(int num) 22 | { 23 | if (num < 10) 24 | { 25 | System.out.println(num); 26 | return num; 27 | } 28 | else 29 | { 30 | System.out.print(num % 10); 31 | find(num/10); 32 | } 33 | return num; 34 | } 35 | } 36 | public class Test1 37 | { 38 | public static void main(String args[]) 39 | { 40 | ReverseR obj = new ReverseR(); 41 | obj.find(5741); 42 | Reverse obj2 = new ReverseR(); 43 | obj2.find(5741); 44 | Reverse obj3 = new Reverse(); 45 | int z = obj3.find(5741); 46 | System.out.println(z); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Threads/multi_threads.java: -------------------------------------------------------------------------------- 1 | package Threads; 2 | 3 | import java.util.Scanner; 4 | 5 | class Lab9 6 | { 7 | synchronized void prime(int n) 8 | { 9 | System.out.println("Prime numbers are : "); 10 | for (int i = 1; i <= n; i++) 11 | { 12 | int temp=0; 13 | int num=0; 14 | for(num = i; num >= 1; num--) 15 | { 16 | if(i % num == 0) 17 | { 18 | temp++; 19 | } 20 | } 21 | if (temp == 2) 22 | { 23 | System.out.print(i + " "); 24 | } 25 | } 26 | System.out.println("\n"); 27 | try 28 | { 29 | Thread.sleep(400); 30 | } 31 | catch(Exception e) 32 | { 33 | System.out.println(e); 34 | } 35 | } 36 | synchronized void composite(int n) 37 | { 38 | System.out.println("Composite numbers are:"); 39 | for (int i = 1; i <= n; i++) 40 | { 41 | int temp = 0; 42 | int num = 0; 43 | for(num = i; num >= 1; num--) 44 | { 45 | if(i %num ==0) 46 | { 47 | temp++; 48 | } 49 | } 50 | if (temp > 2) 51 | { 52 | System.out.print(i + " "); 53 | } 54 | } 55 | try 56 | { 57 | Thread.sleep(400); 58 | } 59 | catch(Exception e) 60 | { 61 | System.out.println(e); 62 | } 63 | } 64 | } 65 | 66 | class Main 67 | { 68 | public static void main(String args[]) 69 | { 70 | Lab9 obj = new Lab9(); 71 | Scanner sc=new Scanner(System.in); 72 | System.out.println("Enter the dynamic input:"); 73 | int m=sc.nextInt(); 74 | System.out.print("\n"); 75 | Thread t1=new Thread(new Runnable(){public void run(){obj.prime(m);}}); 76 | Thread t2=new Thread(new Runnable(){public void run(){obj.composite(m);}}); 77 | t1.start(); 78 | t2.start(); 79 | sc.close(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Threads/synchronization.java: -------------------------------------------------------------------------------- 1 | package Threads; 2 | 3 | class Customer 4 | { 5 | int amount = 10000; 6 | 7 | synchronized void withdraw(int amount) 8 | { 9 | System.out.println("Available Balance: " + this.amount); 10 | System.out.println("About to withdraw: " + amount); 11 | 12 | if (this.amount < amount) 13 | { 14 | System.out.println("Insufficient Balance – Need to Deposit."); 15 | try 16 | { 17 | wait(); 18 | } catch (Exception e) 19 | { 20 | System.out.println(e.getMessage()); 21 | } 22 | } 23 | this.amount -= amount; 24 | System.out.println("Detected amaount: " + amount); 25 | System.out.println("Balance amount : " + this.amount); 26 | } 27 | 28 | synchronized void deposit(int amount) 29 | { 30 | System.out.println("About to deposit: " + amount); 31 | this.amount += amount; 32 | System.out.println("Available Balance: " + this.amount); 33 | System.out.println("Transaction completed.\n"); 34 | notify(); 35 | } 36 | } 37 | 38 | class Test 39 | { 40 | public static void main(String arg[]) 41 | { 42 | final Customer c = new Customer(); 43 | new Thread() 44 | { 45 | public void run() 46 | { 47 | c.withdraw(15000); 48 | } 49 | }.start(); 50 | new Thread() 51 | { 52 | public void run() 53 | { 54 | c.deposit(10000); 55 | } 56 | }.start(); 57 | new Thread() 58 | { 59 | public void run() 60 | { 61 | c.deposit(10000); 62 | } 63 | }.start(); 64 | } 65 | } -------------------------------------------------------------------------------- /Threads/threads.java: -------------------------------------------------------------------------------- 1 | package Threads; 2 | 3 | public class threads implements Runnable { 4 | public void run() { 5 | for (int i = 1; i <= 10; i++) { 6 | System.out.println(Thread.currentThread().getName() + " -> " + i); 7 | } 8 | System.out.println("End of " + Thread.currentThread().getName()); 9 | } 10 | 11 | public static void main(String[] args) { 12 | threads obj = new threads(); 13 | Thread a = new Thread(obj); 14 | Thread b = new Thread(obj); 15 | Thread c = new Thread(obj); 16 | a.setPriority(Thread.MIN_PRIORITY); 17 | b.setPriority(a.getPriority()); 18 | c.setPriority(Thread.MAX_PRIORITY); 19 | a.start(); 20 | b.start(); 21 | c.start(); 22 | } 23 | } -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /assets/HackerRank Logo.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SVijayB/JavaSpace/6cea9ef89fe92bc8b1363f3c1189ab4214be88f2/assets/HackerRank Logo.PNG -------------------------------------------------------------------------------- /assets/Java_logo_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SVijayB/JavaSpace/6cea9ef89fe92bc8b1363f3c1189ab4214be88f2/assets/Java_logo_icon.png --------------------------------------------------------------------------------