├── 01Day1 ├── HELLOWORLD │ └── helloworld.java ├── MORE_ABOUT_OUTPUT_STATEMENT │ ├── first.java │ ├── second.java │ └── third.java ├── Task │ ├── Arithmetic.java │ ├── Tasksolution.java │ └── task1.txt ├── VARIABLES │ ├── variable1.java │ ├── variable2.java │ └── variable3.java └── input │ └── input.java ├── 02Day2 ├── Task │ ├── Solution2.java │ ├── TaskDay2.txt │ └── liftcode.java ├── conditional-statements │ ├── do-while-loop │ │ └── Dowhile.java │ ├── for-loop │ │ └── For.java │ ├── if-else │ │ ├── Bonusifelse.java │ │ ├── Elseif.java │ │ └── Ifelse.java │ ├── switch │ │ └── Switch.java │ └── while-loop │ │ └── While.java └── method-function │ └── Method.java ├── 03Day3 ├── Array │ ├── Array.java │ ├── Array2.java │ └── Subarray.java ├── String-functions │ ├── String.java │ └── String2.java └── Task │ ├── Q1.java │ ├── Q2.java │ ├── Q3.java │ ├── pattern1.java │ ├── pattern2.java │ └── task.txt ├── 04Day4 ├── Sorting-algos │ ├── Bubblesort.java │ ├── Insertionsort.java │ └── Mergesort.java └── Task │ ├── Solution-of-Q4.java │ └── Task.txt ├── 05DAY5 ├── Fileoperations │ ├── CreateAFile │ │ └── File1.java │ ├── Delete │ │ └── File13.java │ ├── FileCopy │ │ └── File10.java │ ├── FileRecord │ │ └── File11.java │ ├── Fileconvertion │ │ ├── File7.java │ │ ├── File8.java │ │ └── File9.java │ ├── ReadAFile │ │ ├── File5.java │ │ └── File6.java │ ├── WriteAFile │ │ ├── File2.java │ │ └── File3.java │ ├── checkfile │ │ └── File4.java │ └── rename │ │ └── File12.java └── Task │ └── Task.txt ├── 06Day6 ├── Task │ ├── Solution-Q1.java │ └── Task.txt ├── class-oops │ └── Classobj.java └── stack-queue │ ├── Quueue.java │ └── Sttack.java ├── 07Day7 ├── Task │ ├── Inheritance.java │ └── Task.txt └── inheritance │ ├── EX1 │ └── err.java │ ├── EX2 │ └── Game.java │ ├── EX3 │ └── Call.java │ ├── EX4 │ └── Employee.java │ └── EX5 │ └── Dailyyworker.java ├── 08Day8 ├── Interface │ ├── EX1 │ │ └── Inter1.java │ ├── EX2 │ │ └── Inter2.java │ ├── EX3 │ │ └── Inter3.java │ └── EX4 │ │ └── Inter4.java └── Task │ ├── Interface1.java │ ├── Interface2.java │ └── Task.txt ├── 09Day9 ├── Task │ ├── Screenshot (75).png │ ├── Seating.java │ └── seatingarr.java └── Threads-Collections │ ├── Collections │ ├── Collection1.java │ ├── Collection2.java │ └── Collection3.java │ └── Threads │ ├── Thread1.java │ ├── Thread2.java │ ├── Thread3.java │ └── Thread4.java ├── 10Day10 ├── Regex │ ├── Regex1.java │ ├── Regex2.java │ ├── Regex3.java │ ├── check.java │ └── regex.java ├── Task │ ├── Q1.java │ ├── Q2.java │ └── Task.txt └── methods │ ├── Overloading │ ├── Overloading1.java │ └── Overloading2.java │ └── Overriding │ └── Overrriding1.java ├── 11Day11 ├── Task │ └── Task.txt └── try-catch-throw │ ├── Exep1.java │ ├── Exep2.java │ ├── Exep3.java │ ├── Exep4.java │ ├── Exep5.java │ ├── Exep6.java │ ├── Exep7.java │ └── Exep8.java ├── 12Day12-13Day13 ├── SOLUTION │ ├── Fileoperations │ │ ├── CreateAFile │ │ │ └── File1.java │ │ ├── Delete │ │ │ └── File13.java │ │ ├── FileCopy │ │ │ └── File10.java │ │ ├── FileRecord │ │ │ └── File11.java │ │ ├── Fileconvertion │ │ │ ├── File7.java │ │ │ ├── File8.java │ │ │ └── File9.java │ │ ├── ReadAFile │ │ │ ├── File5.java │ │ │ └── File6.java │ │ ├── WriteAFile │ │ │ ├── File2.java │ │ │ └── File3.java │ │ ├── checkfile │ │ │ └── File4.java │ │ └── rename │ │ │ └── File12.java │ ├── Regex │ │ ├── Regex1.java │ │ ├── Regex2.java │ │ ├── Regex3.java │ │ ├── check.java │ │ └── regex.java │ ├── Sorting-algos │ │ ├── Bubblesort.java │ │ ├── Insertionsort.java │ │ └── Mergesort.java │ ├── Text.txt │ ├── inheritance │ │ ├── EX1 │ │ │ └── err.java │ │ ├── EX2 │ │ │ └── Game.java │ │ ├── EX3 │ │ │ └── Call.java │ │ ├── EX4 │ │ │ └── Employee.java │ │ └── EX5 │ │ │ └── Dailyyworker.java │ └── stack-queue │ │ ├── Quueue.java │ │ └── Sttack.java └── TASK │ ├── Task1.txt │ ├── Task2.txt │ ├── Task3.txt │ ├── Task4.txt │ ├── Task5.txt │ ├── Task6.txt │ ├── Task7.txt │ └── Task8.txt ├── 12Day12 └── SOLUTION │ ├── Solution2.java │ ├── Solution3.java │ ├── Solution4.java │ ├── Solution6.java │ ├── Solution7.java │ └── Solution8.java ├── 14Day14 ├── Solution │ ├── Soluton.java │ └── text.txt └── Task │ └── Task.txt ├── 15Day15 ├── Interface │ ├── EX1.java │ ├── EX2.java │ ├── EX3.java │ ├── EX4.java │ └── EX5.java └── Task │ └── Task.txt ├── 16Day16 ├── SOLUTION │ ├── Solution.java │ └── text.txt └── TASK │ └── task.txt ├── 17Day17 ├── SOLUTION │ ├── Radix-sort.java │ └── text.txt └── TASK │ └── Task.txt ├── 18Day18 ├── SOLFOLDER │ ├── Solution.java │ └── Text.txt └── TASK │ └── Task.txt ├── 19Day19-20 ├── SOL │ └── sol.txt └── TASK │ └── text.txt ├── 21Day21 ├── Algos │ └── Knapsack.java ├── SOL │ ├── Solution.txt │ └── text.txt ├── TASK │ └── Task.txt └── Tree-graphs │ ├── Graph.java │ ├── Tree.java │ └── Tree2.java ├── 22Day22 ├── N-QUEENS │ └── info.txt ├── SOL │ ├── N-Queens.java │ └── text.txt └── TASK │ └── task.txt ├── 23DAY23 ├── N-QUEENS │ ├── nqueens.java │ └── text.txt ├── SOL │ └── text.txt └── TASK │ └── Task.txt ├── 24Day24 ├── SOL │ ├── 8-Queens.java │ └── text.txt ├── TASk │ └── task.txt └── src │ ├── Algo.java │ ├── Board.java │ └── EightQueens.java ├── 25Day25 ├── Eight-Queens │ ├── Algo.java │ ├── Board.java │ └── EightQueens.java ├── Factorial │ └── Factorial.java ├── Fibonacci │ └── Fibonacci.java ├── FizzBuzz │ └── Fizzbuzz.java ├── Knight-Tour │ ├── Board.java │ ├── Knight.java │ └── KnightTour.java └── Task │ └── Task.txt ├── 26Day26 ├── Problems │ ├── Problem1.txt │ ├── Problem2.txt │ ├── Problem3.txt │ ├── Problem4.txt │ └── Problem5.txt └── SOL │ └── SOL.txt ├── 27Day27 ├── Regex │ ├── Regex1.java │ ├── Regex2.java │ ├── Regex3.java │ ├── check.java │ └── regex.java ├── SOl │ └── sol.txt └── Task │ └── Task.txt ├── 28Day28 ├── JFrame │ ├── Button │ │ ├── Button.java │ │ └── Buttonphoto.java │ ├── Label │ │ ├── Label.java │ │ └── Label2.java │ ├── TextArea │ │ ├── Textarea1.java │ │ └── Textarea2.java │ └── TextField │ │ ├── Textfield1.java │ │ └── Textfield2.java ├── SOL │ ├── Solution.java │ └── sol.txt └── Task │ └── Task.txt ├── 29Day29 ├── SOL │ └── SOL.txt ├── Servelet │ ├── calserv │ │ ├── cal.java │ │ ├── index.html │ │ └── web.xml │ ├── q1 │ │ ├── index.html │ │ ├── servelet.java │ │ └── web.xml │ ├── q2 │ │ ├── index.html │ │ ├── multigetandpost.java │ │ └── web.xml │ ├── q3 │ │ ├── Post.java │ │ └── web.xml │ └── q4 │ │ ├── index.html │ │ ├── q3.java │ │ ├── supperclass.java │ │ └── web.xml └── Task │ ├── Servelet.txt │ └── Task.txt ├── 30Day30 ├── JDBC │ ├── connection │ │ └── Conn.java │ └── insertion │ │ └── insert.java ├── SOL │ └── SOL.txt └── Task │ └── Task.txt ├── CONTRIBUTING.md ├── LICENSE └── README.md /01Day1/HELLOWORLD/helloworld.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | public class Hello_World 4 | //It is a good practice to start the name of the Class with a capital letter 5 | { 6 | public static void main(String[] args) 7 | //This line acts as the entry point of the programme 8 | { 9 | System.out.println("HELLO WORLD"); 10 | // here System.out.println is used to print the argument present between the quotation mark 11 | //like cout or printf in cpp and c respectively 12 | } 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /01Day1/MORE_ABOUT_OUTPUT_STATEMENT/first.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | public class first { 4 | public static void main(String[] args) { 5 | System.out.println("HELLO");// first statement 6 | // here if you see print is occupied with ln 7 | // it means that it will automatically switch or change lines after first 8 | // statement 9 | // and will print second statement 10 | System.out.println("WORLD"); 11 | // so your OUTPUT WILL BE 12 | // HELLO 13 | // WORLD 14 | // like this 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /01Day1/MORE_ABOUT_OUTPUT_STATEMENT/second.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | package DAY1; 4 | 5 | public class second { 6 | public static void main(String[] args) { 7 | System.out.println("HELLO");// first statement 8 | // here after reading and seeing the first.java file you must have realized by 9 | // know that after printing HELLO java will change and start printing from next 10 | // line 11 | // first it will print -> HELLO 12 | System.out.print("***");// second statement 13 | // now if you see second statement do not have ln after print so her it will not 14 | // change line and start printing the third statement after the second statement 15 | // second it will be-> HELLO 16 | // *** 17 | System.out.println("WORLD");// third statement 18 | // and now the third statement will execute and start printing the statement 19 | // from the same line from where it left the second statement 20 | // third will be->HELLO 21 | // ***WORLD 22 | // now it will change line to print the fourth statement but its not there 23 | // so the program will terminate and this is how the output will be printed 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /01Day1/MORE_ABOUT_OUTPUT_STATEMENT/third.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | public class third { 4 | public static void main(String[] args) { 5 | System.out.printf("HELLO %s!%n", "WORLD"); 6 | // A bonus formating statement 7 | // here you can see that i have putten ! between HELLO and WORLD but 8 | // here is the surprise the output will be 9 | // -> HELLO WORLD! 10 | // we will discuss on this formating statement in future because its pretty 11 | // complex in java 12 | // so till them try to find about it more that why i called it a bonus 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /01Day1/Task/Arithmetic.java: -------------------------------------------------------------------------------- 1 | public class Arithmetic { 2 | 3 | public static void main(String[] args) { 4 | Scanner scan = new Scanner(System.in); 5 | double mealCost = scan.nextDouble(); // original meal price 6 | int tipPercent = scan.nextInt(); // tip percentage 7 | int taxPercent = scan.nextInt(); // tax percentage 8 | scan.close(); 9 | 10 | // Write your calculation code here. 11 | double tip= ((mealCost*tipPercent)/100); 12 | double tax=((mealCost*taxPercent)/100); 13 | double numberToRoundHere= tip + tax + mealCost; 14 | 15 | // cast the result of the rounding operation to an int and save it as totalCost 16 | int totalCost = (int) Math.round(numberToRoundHere); 17 | 18 | // Print your result 19 | System.out.printf("The total meal cost is %d dollars.",totalCost); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /01Day1/Task/Tasksolution.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | import java.util.Scanner; //imported java Scanner class 4 | 5 | public class Tasksolution { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in);// created an object in for taking an input 8 | double meal_cost = in.nextDouble();// took mealcost in double data type pay attention on data types in this 9 | // documentation 10 | int tip_percent = in.nextInt();// took tippercent in int data type 11 | int tax_percent = in.nextInt();// took tax percent in int data type 12 | double tax = (double) tax_percent / 100 * meal_cost;// now here is a twist were most of the new students make 13 | // mistake here if you divide the int value (taxpercent) by 14 | // 100 and the value of tax percent is less than 100 you 15 | // will get output 0 so overall in the end value for tax 16 | // will be zero. to prevent this we have to change it into 17 | // double and to that you have to add (double before the 18 | // operation) now again i am giving you time to find more 19 | // about data type convertion 20 | // System.out.println(tax); 21 | double tip = (double) tip_percent / 100 * meal_cost;// did same for tip 22 | // System.out.println(tip); 23 | int totaclcost = (int) (meal_cost + tip + tax);// now did same but converted the value of operation to int 24 | // because total cause is defined as int 25 | System.out.println("Your total bill is of :" + totaclcost); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /01Day1/Task/task1.txt: -------------------------------------------------------------------------------- 1 | Objective 2 | In this challenge, you'll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video! 3 | 4 | Task 5 | Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. 6 | 7 | Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result! 8 | 9 | Input Format 10 | 11 | There are lines of numeric input: 12 | The first line has a double,MEALCOST(the cost of the meal before tax and tip). 13 | The second line has an integer,TIPPERCENT(the percentage of MEALCOST being added as tip). 14 | The third line has an integer,TACPERCENT(the percentage of MEALCOST being added as tax). 15 | 16 | Output Format 17 | 18 | Print the total MEALCOST, where TOTALCOST is the rounded integer result of the entire bill (MEALCOST with added tax and tip). 19 | 20 | Sample Input 21 | 22 | 12.00 23 | 20 24 | 8 25 | Sample Output 26 | 27 | 15 28 | -------------------------------------------------------------------------------- /01Day1/VARIABLES/variable1.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | public class variable1 { 4 | 5 | public static void main(String[] args) { 6 | int xyz = 21;// we all know about int here xyz is variable that holds 21 7 | String abc = "HELLO WORLD";// here String abc holds HELLO WORLD 8 | long A = 2;// long hnn this might be new for you all this is used n place of int because 9 | // Sometimes the numbers are so large that exceeds so long is used to take care 10 | // of that 11 | System.out.println(A); 12 | // first statement prints the value stored in long 13 | System.out.print(abc); 14 | // second statement is used to print the value stored in the String 15 | System.out.println(xyz); 16 | // third statement is used to print value stored in int 17 | // NOTE:- JAVA is a case sensitive language so S must be capital in the printing 18 | // Statements as well as in String in future you all will get to know that 19 | // leaving this where you have to type in caps 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /01Day1/VARIABLES/variable2.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | public class variable2 { 4 | public static void main(String[] args) { 5 | int a = 21, b = 21, c = 3, j = 89; 6 | // this is how you can declare multiple variables of same type 7 | String firstname = "harshil", lastname = "bhardwaj"; 8 | // same goes with every datatype 9 | // now we will be performing some operations on them +,*,/and- 10 | // and with the String we will concatinate String and the out put 11 | int sum = a + b; 12 | // we declared the sum and applied + operations on a and b to give sum its value 13 | int mul = c * j; 14 | // we declared the sum and applied * operations on c and j to give sum its value 15 | int minus = j - a; 16 | // we declared the sum and applied - operations on a and j to give sum its value 17 | int div = b / c; 18 | // we declared the sum and applied / operations on b and c to give sum its value 19 | System.out.println("SUM IS: " + sum); 20 | System.out.println("PRODUCT IS: " + mul); 21 | System.out.println("DIFFERENCE IS: " + minus); 22 | System.out.println("QUESTIENT IS: " + div); 23 | System.out.println(firstname + " " + lastname); 24 | // here we concatenated the out put with + sign so that we can manipulate our 25 | // out put statement 26 | // our out put will be 27 | // SUM IS: 42 28 | // PRODUCT IS: 267 29 | // DIFFERENCE IS: 68 30 | // QUESTIENT IS: 7 31 | // harshil bhardwaj 32 | // so this is how we concatenate out put statements 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /01Day1/VARIABLES/variable3.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | public class variable3 { 4 | public static void main(String[] args) { 5 | // in previous tutorial file we saw that we used int while doing division 6 | // but what if the things are not completely divisible 7 | // lets see 8 | int num1 = 23, num2 = 12; 9 | double num3 = 23, num4 = 12; 10 | // now instead for first applying and operation then printing the value we will 11 | // directly print the values and lets see the difference between int and double 12 | System.out.println(num1 / num2);// first statement 13 | // now if you see that num1 and num2 are int data types so there result will 14 | // also be int so the and they both are not fully divisible the answer should be 15 | // 1.something 16 | // but this first statement will give us only 1 the roundoff value 17 | // output of first statement-> 1 18 | System.out.println(num3 / num4);// second statement 19 | // now if you see here the second statement gives you the out put in double a 20 | // perfect decimal digit 21 | // output of second statement -> 1.9166666666666667 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /01Day1/input/input.java: -------------------------------------------------------------------------------- 1 | package DAY1; 2 | 3 | import java.util.Scanner; 4 | // And this is how we import the Scanner utility in our code 5 | 6 | public class Input { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | // In JAVA we have a Scanner class that allows us to take input and this is the 10 | // statement how we call it in our main class 11 | // So lets say i need you age , your name and your grades in 'double' 12 | int age = in.nextInt();// to take age from user we call .nextInt() function from our imported Scanner 13 | // class 14 | double grade = in.nextDouble(); 15 | // And this function is used to take double input from user 16 | // String name = in.next();now for string we have two functions the first 17 | // function stops taking input as soon as you hit space 18 | String name = in.nextLine();// but this function takes space between two words as well thats why i used this 19 | // in my code 20 | if (name.isEmpty()) {// Bonus information if you take input like thid String after int java will Show 21 | // you error hit is almost written in this code try to find out why XD you rhome 22 | // work. so to remove that error we need to check that if from the first input 23 | // statement name does not take any value we need to store again a value in it 24 | name = in.nextLine();// and finally your code works 25 | } 26 | System.out.println("Your Age :" + age); 27 | System.out.println("Your name :" + name); 28 | System.out.println("Your grade :" + grade); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /02Day2/Task/Solution2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner 2 | 3 | public class lift{ 4 | public static void main(String args[]) 5 | { 6 | Scanner in= new Scanner(System.in); 7 | System.out.println("Etner the max no of floors:- "); 8 | int tf=in.nextInt(); 9 | int A=0,B=tf; 10 | System.out.println("Enter the number of test cases that you want to run"); 11 | int test = in.nextInt(); 12 | 13 | for (int i = 0; i < test; i++) { 14 | lift_function(lift_A, lift_B, top_floor); 15 | } 16 | } 17 | public static void lift_function(int A, int B, int tf) 18 | { 19 | Scanner inp=new Scanner(System.in); 20 | System.out.println("Which floor :- "); 21 | int call=inp.nextInt(); 22 | if(call<=tf) { 23 | if(call>A && call b-call) { 29 | system.out.ptintln("Lift"); 30 | A=call; 31 | } 32 | } 33 | else if(call>A && call>B) { 34 | if(a-call< call-b){ 35 | system.out.println("Lift"); 36 | A=call; 37 | } 38 | else if (A-call > call-B) { 39 | System.out.println("Lift"); 40 | B=call; 41 | } 42 | else { 43 | System.out.println("Lift"); 44 | A=call; 45 | } 46 | } 47 | } 48 | else { 49 | System.out.println("Invalid floor"); 50 | } 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /02Day2/Task/TaskDay2.txt: -------------------------------------------------------------------------------- 1 | There are n floors in BH3 and only 2 lifts. Initially Lift A is at the ground floor and Lift B at the top floor. Whenever someone calls the lift from N th floor, the lift closest to that floor comes to pick him up. If both the lifts are at equidistant from the N th floor, them the lift from the lower floor comes up. 2 | 3 | INPUT 4 | 5 | First line contains a integer T denoting the number of test cases. 6 | 7 | Next T lines contains a single integer N denoting the floor from which lift is called. 8 | 9 | OUTPUT 10 | 11 | Output T lines containing one character "A" if the first lift goes to N th floor or "B" for the second lift. 12 | 13 | CONTRAINTS 14 | 15 | 0 <= N <= n 16 | 17 | 1 <= T <= 100000 18 | 19 | Explanation 20 | 3rd floor calls, Lift A(0) goes there. 21 | 22 | 5th floor calls, Lift A(3) and Lift B(7) are equidistant from 5, so Lift from lowest floor goes there i.e. A. 23 | -------------------------------------------------------------------------------- /02Day2/Task/liftcode.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class liftcode { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | // enter the top floor number 7 | System.out.println("Enter the number of floors building contains"); 8 | int top_floor = in.nextInt(); 9 | // defining the lift positions 10 | int lift_A = 0, lift_B = top_floor; 11 | // number of test cases 12 | System.out.println("Enter the number of test cases that you want to run"); 13 | int test = in.nextInt(); 14 | // running the number of test cases for test times 15 | for (int i = 0; i < test; i++) { 16 | lift_function(lift_A, lift_B, top_floor); 17 | } 18 | } 19 | 20 | public static void lift_function(int lift_A, int lift_B, int top_floor) { 21 | // ask for the call on the floor of demand 22 | Scanner inp = new Scanner(System.in); 23 | System.out.println("on which floor do you want to call the lift"); 24 | int call = inp.nextInt(); 25 | // checking the the call that is made on exists or not 26 | if (call <= top_floor) { 27 | if (call > lift_A && call < lift_B) { 28 | if (call - lift_A < lift_B - call) { 29 | System.out.println("lift a is arriving"); 30 | lift_A = call; 31 | } else if (call - lift_A > lift_B - call) { 32 | System.out.println("lift b is arriving"); 33 | lift_B = call; 34 | } else { 35 | System.out.println("lift a is arriving"); 36 | lift_A = call; 37 | } 38 | } else if (call < lift_A && call > lift_B) { 39 | if (lift_A - call < call - lift_B) { 40 | System.out.println("lift a is comming"); 41 | lift_A = call; 42 | } else if (lift_A - call > call - lift_B) { 43 | System.out.println("lift b is arriving"); 44 | lift_B = call; 45 | } else { 46 | System.out.println("lift a is arriving"); 47 | lift_A = call; 48 | } 49 | } 50 | } else { 51 | System.out.println("invalid floor"); 52 | } 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/do-while-loop/Dowhile.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Dowhile { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int number = in.nextInt(); 9 | int sum = 0; 10 | do {// in this you first add to sum and then iterate while rest everything is same 11 | // as while 12 | // so if you change the statement and experiment yourself you will find how 13 | // while and do while are different 14 | sum++; 15 | } while (sum < number); 16 | System.out.println(sum); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/for-loop/For.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class For { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int iteration = in.nextInt(); 9 | int sum = 0; 10 | for (int i = 0; i < iteration; i++) {// the for loop will run the number of times depends on the input taken in 11 | // iteration variable EX if iteration is =2 the for loop will run two 12 | // times 13 | sum++;// every time for loop runs sum gets plus one if the loops runs 2 times the 14 | // value of the sum will be 2 15 | } // and finally i printed sum so that you can see the sum value 16 | System.out.println(sum); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/if-else/Bonusifelse.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bonusifelse { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int agrigate_score = in.nextInt(); 9 | String result = agrigate_score > 75 ? "pass" : "fail"; // if you have to compare between only two you can use if 10 | // else in this way. So here i said result will be pass 11 | // if its greater then 75 or fail if less then 75 12 | // after you enter your score it will store pass or fail in result and this 13 | // output statement will print whatever is stored in result 14 | // your home work for this is to change the data types and store a value in int 15 | // for example (int x=y>23?3:4;) 16 | System.out.println("you are :" + result); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/if-else/Elseif.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Elseif { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | String name = in.nextLine(); 9 | int age = in.nextInt(); 10 | if (age > 0 && age <= 12) { // now here i wanted to check the age two times if it statisfied both the times 11 | // then only i wanted to print that the user is a kid. So i used (&) which is 12 | // called and so if the age is greater then 0 and less then 12 or equal to 12 it 13 | // will print the user is a kid 14 | System.out.println(name + " " + "is a kid"); 15 | } else if (age >= 13 && age < 18) {// same here if age is greater then 12 means from 13 to less then 18 means 17 16 | // (13-17) it will print the user is a teen 17 | System.out.println(name + " " + "is a teen"); 18 | } else if (age >= 18 && age < 60) {// if the age is 18 or greater 18 and is less then 60 means (18-59) the user 19 | // is an adult 20 | System.out.println(name + " " + "is a adult"); 21 | } else {// and else any age beyond 59 means from 60 till he/she dies he/she is a senior 22 | // citizen 23 | System.out.println(name + " " + "is a senior citizen"); 24 | } 25 | } 26 | }// your home work is to study about (or) i told you about (and) because this will be very help full for todays task 27 | 28 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/if-else/Ifelse.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Ifelse { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in);// called the scanner class through in object 8 | String name = in.nextLine(); 9 | int age = in.nextInt(); 10 | if (age < 18) {// checks if the age is smaller than 18 if it is print the statement under if 11 | System.out.println(name + " " + "is a kid"); 12 | } else {// else print the statement under else 13 | System.out.println(name + " " + "is an adult"); 14 | } // now what if we wanted we want to describe more like if you are a baby or a 15 | // kid or a teen or and adult or what if you are a senior citizen 16 | // for that check out the else-if document 17 | 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/switch/Switch.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Switch { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | System.out.println("Tell us what you are"); 9 | System.out.println("1) kid"); 10 | System.out.println("2) Teen"); 11 | System.out.println("3) Adult"); 12 | System.out.println("4) senior citizen"); 13 | int number = in.nextInt(); 14 | switch (number) {// switch is widely used for menu driven programs so here we gave user an opti 15 | // to chose and enter the number out of four 16 | case 1: {// if he entered 1 this case will run 17 | System.out.println("you are a kid"); 18 | break;// here break is used because if we dont use break and case one runs so after 19 | // that all the case will run so to stop the program after running the desired 20 | // case we use break to exit the switch condition 21 | } 22 | case 2: {// if he entered 2 this case will run 23 | System.out.println("you are a Teen"); 24 | break; 25 | } 26 | case 3: {// if he entered 3 this case will run 27 | System.out.println("you are a Adult"); 28 | break; 29 | } 30 | case 4: {// if he entered 4 this case will run 31 | System.out.println("you are a senior citezen"); 32 | break; 33 | } 34 | default:// if enters any number expect 1,2,3,4 this case will run 35 | throw new IllegalArgumentException("Unexpected value: " + number);// exception of an illegal argument 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /02Day2/conditional-statements/while-loop/While.java: -------------------------------------------------------------------------------- 1 | package DAY2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class While { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int number = in.nextInt(); 9 | int sum = 0; 10 | while (sum < number) {// here i said run while till sum is less then number the user entered 11 | sum++;// every time while runs sum gets a plus one so that while does not enter in a 12 | // infinite loop 13 | } 14 | System.out.println(sum);// and here you can see as soon as the value of sum hit equal to number 15 | // sum 0 && age <= 12) { // now here i wanted to check the age two times if it statisfied both the times 31 | // then only i wanted to print that the user is a kid. So i used (&) which is 32 | // called and so if the age is greater then 0 and less then 12 or equal to 12 it 33 | // will print the user is a kid 34 | System.out.println(name + " " + "is a kid"); 35 | } else if (age >= 13 && age < 18) {// same here if age is greater then 12 means from 13 to less then 18 means 17 36 | // (13-17) it will print the user is a teen 37 | System.out.println(name + " " + "is a teen"); 38 | } else if (age >= 18 && age < 60) {// if the age is 18 or greater 18 and is less then 60 means (18-59) the user 39 | // is an adult 40 | System.out.println(name + " " + "is a adult"); 41 | } else {// and else any age beyond 59 means from 60 till he/she dies he/she is a senior 42 | // citizen 43 | System.out.println(name + " " + "is a senior citizen"); 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /03Day3/Array/Array.java: -------------------------------------------------------------------------------- 1 | package DAY3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Array { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int size = in.nextInt();// input a number that will define the size of our array 9 | int[] array = new int[size];// this is how you create array in java 10 | for (int i = 0; i < array.length; i++) {// we run for loop from 0 to the size of the array and use .length 11 | // function here .length function is used to find the size of an array 12 | array[i] = in.nextInt();// now in every for iteration we take a value and assign it to the respective 13 | // address of the array 14 | } 15 | for (int i = 0; i < array.length; i++) { 16 | System.out.println(array[i]);// and here we print out that assigned values 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /03Day3/Array/Array2.java: -------------------------------------------------------------------------------- 1 | package DAY3; 2 | 3 | import java.util.Arrays;//imported array class 4 | import java.util.Scanner; 5 | 6 | public class Array2 { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int size = in.nextInt(); 10 | int array[] = new int[size]; 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt(); 13 | } 14 | System.out.println(Arrays.toString(array));// this is a inbuilt function in array class that helps us to print 15 | // the array in a clean manner EX:-[12, 32, 1] 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /03Day3/Array/Subarray.java: -------------------------------------------------------------------------------- 1 | package DAY3; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Subarray { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int size = in.nextInt(); 10 | int array[] = new int[size]; 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt(); 13 | } 14 | System.out.println(Arrays.toString(array)); 15 | int[] subArray = Arrays.copyOfRange(array, 0, 2);// this function is used to create subarray from an aray that 16 | // already exists ND BELOW is the out put 17 | System.out.println(Arrays.toString(subArray)); 18 | //[1, 2, 3, 4] 19 | // [1, 2] 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /03Day3/String-functions/String.java: -------------------------------------------------------------------------------- 1 | package DAY3; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Srting { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | String test = in.nextLine();// input test String 9 | String test1 = test.toLowerCase();// this function is used to convert all the characters in your test string to 10 | // lower case if any in upper case 11 | System.out.println(test1);// printing the lower cased string test1 12 | String test2 = test.toUpperCase();// this function is used to convert all of your lower cased characters if any 13 | // into upper case 14 | System.out.println(test2);// printing upper cased String 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /03Day3/String-functions/String2.java: -------------------------------------------------------------------------------- 1 | package DAY3; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class String2 { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | String test = in.nextLine(); 10 | char array[] = test.toCharArray();// here i converted the string into char array 11 | System.out.println(Arrays.toString(array));// and if we print it for example the string was test so the result 12 | // would be [t, e, s, t] 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /03Day3/Task/Q1.java: -------------------------------------------------------------------------------- 1 | The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value. 2 | 3 | Example: 4 | public class CompareToExample { 5 | public static void main(String args[]) { 6 | String str1 = "String method tutorial"; 7 | String str2 = "compareTo method example"; 8 | String str3 = "String method tutorial"; 9 | 10 | int var1 = str1.compareTo( str2 ); 11 | System.out.println("str1 & str2 comparison: "+var1); 12 | 13 | int var2 = str1.compareTo( str3 ); 14 | System.out.println("str1 & str3 comparison: "+var2); 15 | 16 | int var3 = str2.compareTo("compareTo method example"); 17 | System.out.println("str2 & string argument comparison: "+var3); 18 | } 19 | } 20 | 21 | // output for above code : 22 | // str1 & str2 comparison: -16 23 | // str1 & str3 comparison: 0 24 | // str2 & string argument comparison: 0 25 | 26 | you cana also use import.java.util.Scanner to input a string and then use the function compareT0() with syntax: string1.compareTo()("Just a string other or another string"); 27 | 28 | -------------------------------------------------------------------------------- /03Day3/Task/Q2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Staircase 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner scan= new Scanner(System.in); 7 | Sysytem.out.println("Enter the height of Staircase: "); 8 | int height = scan.nextInt(); //Input for height of staircase 9 | 10 | int c=height-1; 11 | for(int i=0; i1; j--) 17 | { 18 | // printing spaces 19 | System.out.print(" "); 20 | } 21 | 22 | // inner loop to handle number of columns 23 | // values changing acc. to outer loop 24 | for (int j=0; j<=i; j++ ) 25 | { 26 | // printing stars 27 | System.out.print("* "); 28 | } 29 | 30 | // ending line after each row 31 | System.out.println(); 32 | } 33 | } 34 | 35 | // Driver Function 36 | public static void main(String args[]) 37 | { 38 | int n = 5; 39 | printTriagle(n); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /03Day3/Task/pattern1.java: -------------------------------------------------------------------------------- 1 | package staircase; 2 | 3 | import java.util.Scanner; 4 | 5 | public class pattern1 { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int num = in.nextInt(); 9 | int c = num - 1; 10 | for (int i = 0; i < num; i++) { 11 | for (int j = 0; j < num; j++) { 12 | if (j < c) { 13 | System.out.print(" "); 14 | } else { 15 | System.out.print("#"); 16 | } 17 | } 18 | System.out.println(" "); 19 | c = c - 1; 20 | } 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /03Day3/Task/pattern2.java: -------------------------------------------------------------------------------- 1 | package staircase; 2 | 3 | import java.util.Scanner; 4 | 5 | public class pattern2 { 6 | public static void main(String[] args) { 7 | Scanner in = new Scanner(System.in); 8 | int num = in.nextInt(); 9 | int c = num - 1; 10 | for (int i = 0; i < num; i++) { 11 | for (int j = 0; j < num; j++) { 12 | if (j < c) { 13 | System.out.print(" "); 14 | } else { 15 | System.out.print("# "); 16 | } 17 | } 18 | System.out.println(" "); 19 | c = c - 1; 20 | } 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /03Day3/Task/task.txt: -------------------------------------------------------------------------------- 1 | Q1) Task one would be to study about the string compare function and try to impliment it 2 | Q2) Task to will be that you have to genrate a staircase patter by taking (n) as input and where n=lenght of the stair case 3 | for EX:- n=6 so # 4 | ## 5 | ### 6 | #### 7 | ##### 8 | ###### 9 | 10 | 11 | 12 | 13 | Q3) Task to will be that you have to genrate a staircase patter by taking (n) as input and where n=lenght of the stair case 14 | for EX:- n=6 so 15 | 16 | # 17 | # # 18 | # # # 19 | # # # # 20 | # # # # # 21 | # # # # # # 22 | -------------------------------------------------------------------------------- /04Day4/Sorting-algos/Bubblesort.java: -------------------------------------------------------------------------------- 1 | package DAY4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Bubblesort { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int size = in.nextInt(); 10 | int array[] = new int[size]; 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt(); 13 | } 14 | sort(array); 15 | print(array); 16 | } 17 | public static void sort(int array[]) { 18 | int n = array.length; 19 | for (int i = 0; i < n - 1; i++) 20 | for (int j = 0; j < n - i - 1; j++) 21 | if (array[j] > array[j + 1]) { 22 | // swap array[j+1] and array[i] 23 | int temp = array[j]; 24 | array[j] = array[j + 1]; 25 | array[j + 1] = temp; 26 | } 27 | } 28 | public static void print(int array[]) { 29 | System.out.println(Arrays.toString(array)); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /04Day4/Sorting-algos/Insertionsort.java: -------------------------------------------------------------------------------- 1 | package DAY4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Insertionsort { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in);// As usual called the scanner class 9 | int size = in.nextInt();// used the object created from the scanner class 10 | int array[] = new int[size];// initialized the array size 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt();// taken values inside the array 13 | } 14 | sort(array);// this function will sort the array 15 | printarray(array);// this function will print the sorted array 16 | } 17 | public static void sort(int array[]) { 18 | for (int i = 0; i < array.length; ++i) { 19 | int key = array[i]; 20 | int j = i - 1; 21 | 22 | // now here we are moving the elements one step ahead who are greater than key 23 | while (j >= 0 && array[j] > key) { 24 | array[j + 1] = array[j]; 25 | j = j - 1; 26 | } 27 | array[j + 1] = key;// and finally we have our sorted array 28 | } 29 | } 30 | public static void printarray(int array[]) { 31 | System.out.println(Arrays.toString(array));// again here we called the array class utility 32 | // i personally believe that before going ahead you should master arrays and for 33 | // that you need to study some sorting algorithms thats why today we will only 34 | // learn about some sorting algorithms 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /04Day4/Sorting-algos/Mergesort.java: -------------------------------------------------------------------------------- 1 | package DAY4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Mergesort { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int size = in.nextInt(); 10 | int array[] = new int[size]; 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt(); 13 | } 14 | Mergesort ob = new Mergesort();// called the main class via object creation like we use to do in Scanner class 15 | // for ex Scanner in = new SCanner(System.in) 16 | ob.sort(array, 0, array.length - 1); 17 | print(array); 18 | } 19 | public static void merge(int array[], int big, int mid, int end) { 20 | int l = mid - big + 1; 21 | int r = end - mid; 22 | 23 | int LeftArray[] = new int[l];// created two array left array and right array as a sub array of our main array 24 | int RightArray[] = new int[r]; 25 | 26 | for (int i = 0; i < l; ++i) 27 | LeftArray[i] = array[big + i]; 28 | 29 | for (int j = 0; j < r; ++j) 30 | RightArray[j] = array[mid + 1 + j]; 31 | 32 | int i = 0, j = 0; 33 | int k = big; 34 | while (i < l && j < r) { 35 | if (LeftArray[i] <= RightArray[j]) { 36 | array[k] = LeftArray[i]; 37 | i++; 38 | } else { 39 | array[k] = RightArray[j]; 40 | j++; 41 | } 42 | k++; 43 | } 44 | while (i < l) { 45 | array[k] = LeftArray[i]; 46 | i++; 47 | k++; 48 | } 49 | 50 | while (j < r) { 51 | array[k] = RightArray[j]; 52 | j++; 53 | k++; 54 | } 55 | } 56 | public static void sort(int array[], int big, int end) { 57 | if (big < end) { 58 | int mid = (big + end) / 2; 59 | sort(array, big, mid); 60 | sort(array, mid + 1, end); // here we are sorting the array 61 | merge(array, big, mid, end); 62 | } 63 | 64 | } 65 | public static void print(int array[]) { 66 | System.out.println(Arrays.toString(array));// here we called the array class utility to print array 67 | } 68 | } 69 | 70 | -------------------------------------------------------------------------------- /04Day4/Task/Solution-of-Q4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | public class Sorting{ 5 | public static void main(String[] args) 6 | { 7 | Scanner scan= new Scanner(System.in); 8 | int size = scan.nextInt(); 9 | int array[] = new int[size]; 10 | for(inti=0; i= 0 && array[j] > key) { 34 | array[j + 1] = array[j]; 35 | j = j - 1; 36 | } 37 | array[j + 1] = key;// and finally we have our sorted array 38 | } 39 | System.out.println(Arrays.toString(array)); 40 | } 41 | 42 | public static void Bubblesort(int array[]) //function for bubble sort 43 | { 44 | int n = array.length; 45 | for (int i = 0; i < n - 1; i++) 46 | for (int j = 0; j < n - i - 1; j++) 47 | if (array[j] > array[j + 1]) { 48 | // swap array[j+1] and array[i] 49 | int temp = array[j]; 50 | array[j] = array[j + 1]; 51 | array[j + 1] = temp; 52 | } 53 | System.out.println(Arrays.toString(array)); 54 | } 55 | 56 | public static void Mergesort(int array[], int big,int mid,int end) 57 | { 58 | int l = mid - big + 1; 59 | int r = end - mid; 60 | 61 | int LeftArray[] = new int[l];// created two array left array and right array as a sub array of our main array 62 | int RightArray[] = new int[r]; 63 | 64 | for (int i = 0; i < l; ++i) 65 | LeftArray[i] = array[big + i]; 66 | 67 | for (int j = 0; j < r; ++j) 68 | RightArray[j] = array[mid + 1 + j]; 69 | 70 | int i = 0, j = 0; 71 | int k = big; 72 | while (i < l && j < r) { 73 | if (LeftArray[i] <= RightArray[j]) { 74 | array[k] = LeftArray[i]; 75 | i++; 76 | } else { 77 | array[k] = RightArray[j]; 78 | j++; 79 | } 80 | k++; 81 | } 82 | while (i < l) { 83 | array[k] = LeftArray[i]; 84 | i++; 85 | k++; 86 | } 87 | 88 | while (j < r) { 89 | array[k] = RightArray[j]; 90 | j++; 91 | k++; 92 | } 93 | }if (big < end) { 94 | int mid = (big + end) / 2; 95 | sort(array, big, mid); 96 | sort(array, mid + 1, end); // here we are sorting the array 97 | merge(array, big, mid, end); 98 | } 99 | System.out.println(Arrays.toString(array)); 100 | } 101 | } 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /04Day4/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Write a switch case program for all the three sorting algos learned todat (insertion sort , bubble sort and merge sort)! 2 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/CreateAFile/File1.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File1 { 7 | public static void main(String[] args) { 8 | try { 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | /* 11 | * If file gets created then the createNewFile() method would return true or if 12 | * the file is already present it would return false 13 | */ 14 | boolean fvar = file.createNewFile(); 15 | if (fvar) { 16 | System.out.println("File has been created successfully"); 17 | } else { 18 | System.out.println("File already present at the specified location"); 19 | } 20 | } catch (IOException e) { 21 | System.out.println("File not created"); 22 | e.printStackTrace(); 23 | } 24 | 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/Delete/File13.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | 5 | public class File13 { 6 | public static void main(String[] args) { 7 | try { 8 | // Specify the file name and path 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\copyfile.txt"); 10 | /* 11 | * the delete() method returns true if the file is deleted successfully else it 12 | * returns false 13 | */ 14 | if (file.delete()) { 15 | System.out.println(file.getName() + " is deleted!"); 16 | } else { 17 | System.out.println("Delete failed: File didn't delete"); 18 | } 19 | } catch (Exception e) { 20 | System.out.println("Exception occurred"); 21 | e.printStackTrace(); 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/FileCopy/File10.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | 8 | public class File10 { 9 | public static void main(String[] args) { 10 | FileInputStream instream = null; 11 | FileOutputStream outstream = null; 12 | 13 | try { 14 | File infile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 15 | File outfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\copyfile.txt"); 16 | 17 | instream = new FileInputStream(infile); 18 | outstream = new FileOutputStream(outfile); 19 | 20 | byte[] buffer = new byte[1024]; 21 | 22 | int length; 23 | /* 24 | * copying the contents from input stream to output stream using read and write 25 | * methods 26 | */ 27 | while ((length = instream.read(buffer)) > 0) { 28 | outstream.write(buffer, 0, length); 29 | } 30 | 31 | // Closing the input/output file streams 32 | instream.close(); 33 | outstream.close(); 34 | 35 | System.out.println("File copied successfully!!"); 36 | 37 | } catch (IOException ioe) { 38 | ioe.printStackTrace(); 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/FileRecord/File11.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.text.SimpleDateFormat; 5 | 6 | public class File11 { 7 | public static void main(String[] args) { 8 | // Specify the file path and name 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 11 | System.out.println("Last Modified Date: " + sdf.format(file.lastModified())); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/Fileconvertion/File7.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File7 { 7 | public static void main(String[] args) { 8 | File myfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | // making the file read only 10 | boolean flag = myfile.setReadOnly(); 11 | if (flag == true) { 12 | System.out.println("File successfully converted to Read only mode!!"); 13 | } else { 14 | System.out.println("Unsuccessful Operation!!"); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/Fileconvertion/File8.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File8 { 7 | public static void main(String[] args) { 8 | File myfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | if (myfile.canWrite()) { 10 | System.out.println("File is writable."); 11 | } else { 12 | System.out.println("File is read only."); 13 | } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/Fileconvertion/File9.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File9 { 7 | public static void main(String[] args) { 8 | 9 | File myfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | // changing the file mode to writable 11 | myfile.setWritable(true); 12 | if (myfile.canWrite()) { 13 | System.out.println("File is again writable."); 14 | } else { 15 | System.out.println("File is still read only."); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/ReadAFile/File5.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | public class File5 { 8 | public static void main(String[] args) { 9 | 10 | BufferedReader br = null; 11 | BufferedReader br2 = null; 12 | try { 13 | br = new BufferedReader(new FileReader("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt")); 14 | 15 | // One way of reading the file 16 | System.out.println("Reading the file using readLine() method:"); 17 | String contentLine = br.readLine(); 18 | while (contentLine != null) { 19 | System.out.println(contentLine); 20 | contentLine = br.readLine(); 21 | } 22 | 23 | br2 = new BufferedReader(new FileReader("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY6\\newfile.txt")); 24 | 25 | // Second way of reading the file 26 | System.out.println("Reading the file using read() method(2):"); 27 | int num = 0; 28 | char ch; 29 | while ((num = br2.read()) != -1) { 30 | ch = (char) num; 31 | System.out.print(ch); 32 | } 33 | 34 | } catch (IOException ioe) { 35 | ioe.printStackTrace(); 36 | } finally { 37 | try { 38 | if (br != null) 39 | br.close(); 40 | if (br2 != null) 41 | br2.close(); 42 | } catch (IOException ioe) { 43 | System.out.println("Error in closing the BufferedReader"); 44 | } 45 | } 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/ReadAFile/File6.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.*; 4 | 5 | public class File6 { 6 | public static void main(String[] args) { 7 | // Specify the path of the file here 8 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | BufferedInputStream bis = null; 10 | FileInputStream fis = null; 11 | 12 | try { 13 | // FileInputStream to read the file 14 | fis = new FileInputStream(file); 15 | 16 | /* 17 | * Passed the FileInputStream to BufferedInputStream For Fast read using the 18 | * buffer array. 19 | */ 20 | bis = new BufferedInputStream(fis); 21 | 22 | /* 23 | * available() method of BufferedInputStream returns 0 when there are no more 24 | * bytes present in the file to be read 25 | */ 26 | while (bis.available() > 0) { 27 | System.out.print((char) bis.read()); 28 | } 29 | 30 | } catch (FileNotFoundException fnfe) { 31 | System.out.println("The specified file not found" + fnfe); 32 | } catch (IOException ioe) { 33 | System.out.println("I/O Exception: " + ioe); 34 | } finally { 35 | try { 36 | if (bis != null && fis != null) { 37 | fis.close(); 38 | bis.close(); 39 | } 40 | } catch (IOException ioe) { 41 | System.out.println("Error in InputStream close(): " + ioe); 42 | } 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/WriteAFile/File2.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileWriter; 6 | import java.io.IOException; 7 | import java.util.Scanner; 8 | 9 | public class File2 { 10 | public static void main(String[] args) { 11 | BufferedWriter bw = null; 12 | Scanner in = new Scanner(System.in); 13 | System.out.println("Enter what you want to write in your file"); 14 | try { 15 | String mycontent = in.nextLine(); 16 | // Specify the file name and path here 17 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 18 | 19 | /* 20 | * This logic will make sure that the file gets created if it is not present at 21 | * the specified location 22 | */ 23 | if (!file.exists()) { 24 | file.createNewFile(); 25 | } 26 | 27 | FileWriter fw = new FileWriter(file); 28 | bw = new BufferedWriter(fw); 29 | bw.write(mycontent); 30 | System.out.println("File written Successfully"); 31 | 32 | } catch (IOException ioe) { 33 | ioe.printStackTrace(); 34 | } finally { 35 | try { 36 | if (bw != null) 37 | bw.close(); 38 | } catch (Exception ex) { 39 | System.out.println("Error in closing the BufferedWriter" + ex); 40 | } 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/WriteAFile/File3.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.util.Scanner; 7 | 8 | public class File3 { 9 | public static void main(String[] args) { 10 | FileOutputStream fos = null; 11 | File file; 12 | Scanner in = new Scanner(System.in); 13 | System.out.println("Enter what do you want to write in the file"); 14 | String mycontent = in.nextLine(); 15 | try { 16 | // Specify the file path here 17 | file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 18 | fos = new FileOutputStream(file); 19 | 20 | /* 21 | * This logic will check whether the file exists or not. If the file is not 22 | * found at the specified location it would create a new file 23 | */ 24 | if (!file.exists()) { 25 | file.createNewFile(); 26 | } 27 | 28 | /* 29 | * String content cannot be directly written into a file. It needs to be 30 | * converted into bytes 31 | */ 32 | byte[] bytesArray = mycontent.getBytes(); 33 | 34 | fos.write(bytesArray); 35 | fos.flush(); 36 | System.out.println("File Written Successfully"); 37 | } catch (IOException ioe) { 38 | ioe.printStackTrace(); 39 | } finally { 40 | try { 41 | if (fos != null) { 42 | fos.close(); 43 | } 44 | } catch (IOException ioe) { 45 | System.out.println("Error in closing the Stream"); 46 | } 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/checkfile/File4.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File4 { 7 | public static void main(String[] args) { 8 | // Provide the complete file path here 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | 11 | if (file.isHidden()) { 12 | System.out.println("The specified file is hidden"); 13 | } else { 14 | System.out.println("The specified file is not hidden"); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /05DAY5/Fileoperations/rename/File12.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | 5 | public class File12 { 6 | public static void main(String[] args) { 7 | // Old File 8 | File oldfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | // New File 10 | File newfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\renamedfile.txt"); 11 | /* 12 | * renameTo() return boolean value It return true if rename operation is 13 | * successful 14 | */ 15 | boolean flag = oldfile.renameTo(newfile); 16 | if (flag) { 17 | System.out.println("File renamed successfully"); 18 | } else { 19 | System.out.println("Rename operation failed"); 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /05DAY5/Task/Task.txt: -------------------------------------------------------------------------------- 1 | todays task would be go through all of the source code and leanr and understand more about file operations 2 | -------------------------------------------------------------------------------- /06Day6/Task/Solution-Q1.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | import java.io.PrintWriter; 6 | import java.io.Reader; 7 | import java.util.Scanner; 8 | 9 | public class Files { 10 | 11 | public static void main(String args[]){ 12 | 13 | System.out.print("Enter Text: "); 14 | Scanner scan = new Scanner(System.in); 15 | String text = scan.nextLine(); 16 | FileWriter fWriter = null; 17 | BufferedWriter writer = null; 18 | try { 19 | fWriter = new FileWriter("newfile.txt"); 20 | writer = new BufferedWriter(fWriter); 21 | writer.write(text); 22 | writer.newLine(); 23 | writer.close(); 24 | System.err.println("Your input of " + text.length() + " characters was saved."); 25 | } catch (Exception e) { 26 | System.out.println("Error!"); 27 | } 28 | 29 | Scanner in= new Scanner(System.in); 30 | str pass= in.nextInt(); //input password 31 | 32 | // Specify the path of the file here 33 | File file = new File("D:\\JAVA\\newfile.txt"); 34 | BufferedInputStream bis = null; 35 | FileInputStream fis = null; 36 | 37 | try { 38 | // FileInputStream to read the file 39 | fis = new FileInputStream(file); 40 | 41 | /* 42 | * Passed the FileInputStream to BufferedInputStream For Fast read using the 43 | * buffer array. 44 | */ 45 | bis = new BufferedInputStream(fis); 46 | 47 | /* 48 | * available() method of BufferedInputStream returns 0 when there are no more 49 | * bytes present in the file to be read 50 | */ 51 | while (bis.available() > 0) { 52 | System.out.print((char) bis.read()); 53 | } 54 | 55 | } catch (FileNotFoundException fnfe) { 56 | System.out.println("The specified file not found" + fnfe); 57 | } catch (IOException ioe) { 58 | System.out.println("I/O Exception: " + ioe); 59 | } finally { 60 | try { 61 | if (bis != null && fis != null) { 62 | fis.close(); 63 | bis.close(); 64 | } 65 | } catch (IOException ioe) { 66 | System.out.println("Error in InputStream close(): " + ioe); 67 | } 68 | } 69 | str Pass2= in.nextInt(); 70 | if(pass==Pass2) 71 | { 72 | try { 73 | String mycontent = in.nextLine(); 74 | // Specify the file name and path here 75 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 76 | 77 | /* 78 | * This logic will make sure that the file gets created if it is not present at 79 | * the specified location 80 | */ 81 | if (!file.exists()) { 82 | file.createNewFile(); 83 | } 84 | 85 | FileWriter fw = new FileWriter(file); 86 | bw = new BufferedWriter(fw); 87 | bw.write(mycontent); 88 | System.out.println("File written Successfully"); 89 | 90 | } catch (IOException ioe) { 91 | ioe.printStackTrace(); 92 | } finally { 93 | try { 94 | if (bw != null) 95 | bw.close(); 96 | } catch (Exception ex) { 97 | System.out.println("Error in closing the BufferedWriter" + ex); 98 | } 99 | } 100 | } 101 | else 102 | { System.out.println("Wrong Password"); 103 | } 104 | } 105 | } 106 | 107 | -------------------------------------------------------------------------------- /06Day6/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Todays task would be to write a program that takes input from a user and stores that into a file and then asks for a password from user (after this convert that file to read only mode) 2 | now if the user waants to write into the file again he must put the password correctly and if the enterd password is correct then only convert the file to writable mode again and then again let the user enter values into the file. And this process countinues. 3 | -------------------------------------------------------------------------------- /06Day6/class-oops/Classobj.java: -------------------------------------------------------------------------------- 1 | package DAY6; 2 | 3 | import java.util.Scanner; 4 | 5 | class demo {// created a demo class that holds a constructor that will take your name and 6 | // age and print them according to the printing statement. 7 | private String name; 8 | private int age; 9 | 10 | demo() { 11 | Scanner in = new Scanner(System.in); 12 | System.out.println("Enter your name"); 13 | name = in.nextLine(); 14 | System.out.println("Enter your age"); 15 | age = in.nextInt(); 16 | System.out.println("Good morning :" + name + " you are :" + age + " this old"); 17 | } 18 | } 19 | 20 | public class Classobj { 21 | public static void main(String[] args) { 22 | demo obj = new demo(); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /06Day6/stack-queue/Quueue.java: -------------------------------------------------------------------------------- 1 | package DAY6; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class Quueue { 7 | public static void main(String[] args) { 8 | Queue q = new LinkedList<>(); 9 | 10 | // Adds elements {0, 1, 2, 3, 4} to queue 11 | for (int i = 0; i < 5; i++) 12 | q.add(i); 13 | 14 | // Display contents of the queue. 15 | System.out.println("Elements of queue-" + q); 16 | 17 | // To remove the head of queue. 18 | int removedele = q.remove(); 19 | System.out.println("removed element-" + removedele); 20 | 21 | System.out.println(q); 22 | 23 | // To view the head of queue 24 | int head = q.peek(); 25 | System.out.println("head of queue-" + head); 26 | 27 | // Rest all methods of collection interface, 28 | // Like size and contains can be used with this 29 | // implementation. 30 | int size = q.size(); 31 | System.out.println("Size of queue-" + size); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /06Day6/stack-queue/Sttack.java: -------------------------------------------------------------------------------- 1 | package DAY6; 2 | 3 | import java.util.Stack; 4 | 5 | public class Sttack { 6 | static void stack_push(Stack stack) { 7 | for (int i = 0; i < 5; i++) { 8 | stack.push(i); 9 | } 10 | } 11 | 12 | // Popping element from the top of the stack 13 | static void stack_pop(Stack stack) { 14 | System.out.println("Pop :"); 15 | 16 | for (int i = 0; i < 5; i++) { 17 | Integer y = (Integer) stack.pop(); 18 | System.out.println(y); 19 | } 20 | } 21 | 22 | // Displaying element on the top of the stack 23 | static void stack_peek(Stack stack) { 24 | Integer element = (Integer) stack.peek(); 25 | System.out.println("Element on stack top : " + element); 26 | } 27 | 28 | // Searching element in the stack 29 | static void stack_search(Stack stack, int element) { 30 | Integer pos = (Integer) stack.search(element); 31 | 32 | if (pos == -1) 33 | System.out.println("Element not found"); 34 | else 35 | System.out.println("Element is found at position " + pos); 36 | } 37 | 38 | public static void main(String[] args) { 39 | Stack stack = new Stack(); 40 | 41 | stack_push(stack); 42 | stack_pop(stack); 43 | stack_push(stack); 44 | stack_peek(stack); 45 | stack_search(stack, 2); 46 | stack_search(stack, 6); 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /07Day7/Task/Inheritance.java: -------------------------------------------------------------------------------- 1 | //Java program to illustrate the 2 | // concept of inheritance 3 | 4 | // base class 5 | class Bicycle 6 | { 7 | // the Bicycle class has two fields 8 | public int gear; 9 | public int speed; 10 | 11 | // the Bicycle class has one constructor 12 | public Bicycle(int gear, int speed) 13 | { 14 | this.gear = gear; 15 | this.speed = speed; 16 | } 17 | // the Bicycle class has three methods 18 | public void applyBrake(int decrement) 19 | { 20 | speed -= decrement; 21 | } 22 | 23 | public void speedUp(int increment) 24 | { 25 | speed += increment; 26 | } 27 | 28 | // toString() method to print info of Bicycle 29 | public String toString() 30 | { 31 | return("No of gears are "+gear 32 | +"\n" 33 | + "speed of bicycle is "+speed); 34 | } 35 | } 36 | // derived class 37 | class MountainBike extends Bicycle 38 | { 39 | 40 | // the MountainBike subclass adds one more field 41 | public int seatHeight; 42 | 43 | // the MountainBike subclass has one constructor 44 | public MountainBike(int gear,int speed, 45 | int startHeight) 46 | { 47 | // invoking base-class(Bicycle) constructor 48 | super(gear, speed); 49 | seatHeight = startHeight; 50 | } 51 | 52 | // the MountainBike subclass adds one more method 53 | public void setHeight(int newValue) 54 | { 55 | seatHeight = newValue; 56 | } 57 | 58 | // overriding toString() method 59 | // of Bicycle to print more info 60 | @Override 61 | public String toString() 62 | { 63 | return (super.toString()+ 64 | "\nseat height is "+seatHeight); 65 | } 66 | 67 | } 68 | 69 | // driver class 70 | public class Test 71 | { 72 | public static void main(String args[]) 73 | { 74 | 75 | MountainBike mb = new MountainBike(3, 100, 25); 76 | System.out.println(mb.toString()); 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /07Day7/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Todays task would be to understand and try to impliment the working of inheritance 2 | -------------------------------------------------------------------------------- /07Day7/inheritance/EX1/err.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class demo {// here if you see i created a class and made a counstructor inside that takes a 6 | // number as ID 7 | public int ID; 8 | 9 | demo() { 10 | Scanner in = new Scanner(System.in); 11 | ID = in.nextInt(); 12 | } 13 | } 14 | 15 | class demo1 extends demo {// now i created another class and made it inherit demo class and how i did this 16 | // is using extends key word yes extends keyword is used to make classes inherit 17 | // classes 18 | void error() { 19 | System.out.println(ID); 20 | } 21 | } 22 | 23 | public class err { 24 | public static void main(String[] args) { 25 | demo1 obj = new demo1();// here i created an object(obj) of demo1 class 26 | 27 | obj.error();// now with the help of obj i called demo1 counstructor but before that it runs 28 | // demo counstructor takes ID and then runs demo1 and prints the ID. 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /07Day7/inheritance/EX2/Game.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class player {// here i created player class which has name fees and add variables defined in 6 | // it 7 | private String name;// i did not want to use this in any furthre class so i made it as private 8 | private int fees;// same with fees 9 | protected int add = 0;// i want this to be accesable for different inherited classes so i made it 10 | // protected 11 | 12 | player() {// now i created a player counstructor which take input from you 13 | 14 | Scanner in = new Scanner(System.in); 15 | System.out.println("Enter fees deposited"); 16 | int fees = in.nextInt(); 17 | if (fees == 1000) { 18 | System.out.println("Enter Player name"); 19 | Scanner in1 = new Scanner(System.in); 20 | name = in1.nextLine(); 21 | add = 1;// variable to check that you deposited the desired amount of fees or not and 22 | // use this as that reference 23 | } else { 24 | add = 2; 25 | } 26 | } 27 | 28 | } 29 | 30 | class hockeyplayer extends player {// inherits class player same with all the classes below 31 | void needofmaterial() { 32 | if (add == 1) { 33 | System.out.println("Congratulation you got addmission in our accademy"); 34 | System.out.println("Bring your own Hockey stick for practice from tommorow"); 35 | } else { 36 | System.out.println("Sorry you did not submited the sufficient ammount"); 37 | } 38 | 39 | } 40 | } 41 | 42 | class cricketplayer extends player { 43 | void needofmaterial() { 44 | if (add == 1) { 45 | System.out.println("Congratulation you got addmission in our accademy"); 46 | System.out.println("Bring your own L-guard for practice from tommorow"); 47 | } else { 48 | System.out.println("Sorry you did not submited the sufficient ammount"); 49 | } 50 | } 51 | } 52 | 53 | class footballplayer extends player { 54 | void needofmaterial() { 55 | if (add == 1) { 56 | System.out.println("Congratulation you got addmission in our accademy"); 57 | System.out.println("Bring your own pair of football boots for practice from tommorow"); 58 | } else { 59 | System.out.println("Sorry you did not submited the sufficient ammount"); 60 | } 61 | } 62 | } 63 | 64 | public class Game { 65 | public static void main(String[] args) { 66 | Scanner in = new Scanner(System.in); 67 | System.out.println("Choose the sport you play"); 68 | System.out.println("1) Hockey(Rs 1000 per month to be deposited in advance)"); 69 | System.out.println("2) Cricket(Rs 1000 per month to be deposited in advance)"); 70 | System.out.println("3) Football(Rs 1000 per month to be deposited in advance)");// according to switch case 71 | // calls different classes 72 | int sport = in.nextInt(); 73 | switch (sport) { 74 | case 1: { 75 | hockeyplayer obj = new hockeyplayer(); 76 | obj.needofmaterial(); 77 | break; 78 | } 79 | case 2: { 80 | cricketplayer obj = new cricketplayer(); 81 | obj.needofmaterial(); 82 | break; 83 | } 84 | case 3: { 85 | footballplayer obj = new footballplayer(); 86 | obj.needofmaterial(); 87 | break; 88 | } 89 | default: 90 | throw new IllegalArgumentException("Unexpected value: " + sport); 91 | } 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /07Day7/inheritance/EX3/Call.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class trunkcall { 6 | public int duration; 7 | public int charges; 8 | 9 | trunkcall() { 10 | System.out.println("Enter Duration of call: "); 11 | Scanner in = new Scanner(System.in); 12 | duration = in.nextInt(); 13 | } 14 | 15 | void charges(int rate) { 16 | charges = duration * rate; 17 | System.out.println("Charges are: " + charges); 18 | } 19 | } 20 | 21 | class ordinarytrunkcall extends trunkcall { 22 | public int rateofordinary; 23 | 24 | ordinarytrunkcall() { 25 | System.out.println("Enter rate of ordinary trunk call as per hour: "); 26 | Scanner in = new Scanner(System.in); 27 | rateofordinary = in.nextInt(); 28 | charges(rateofordinary); 29 | } 30 | } 31 | 32 | class urgenttrunkcall extends trunkcall { 33 | public int rateofurgent; 34 | 35 | urgenttrunkcall() { 36 | System.out.println("Enter rate of ordinary trunk call as per hour: "); 37 | Scanner in = new Scanner(System.in); 38 | rateofurgent = in.nextInt(); 39 | charges(rateofurgent); 40 | } 41 | } 42 | 43 | public class Call { 44 | public static void main(String[] args) { 45 | trunkcall obj; 46 | Scanner in = new Scanner(System.in); 47 | System.out.println("Enter your type: 1:"); 48 | System.out.println("1) ordinary trunk call"); 49 | System.out.println("2) urgent trunk call"); 50 | int i = in.nextInt(); 51 | 52 | switch (i) { 53 | case 1: 54 | System.out.println("It was an Ordinary trunk call"); 55 | obj = new ordinarytrunkcall(); 56 | break; 57 | case 2: 58 | System.out.println("It was an urgent trunk call"); 59 | obj = new urgenttrunkcall(); 60 | break; 61 | default: 62 | System.out.println("It was not a trunk call"); // default case is good for user understanding 63 | } 64 | 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /07Day7/inheritance/EX4/Employee.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class worker { 6 | protected String departmen1 = "devops"; 7 | protected String department2 = "big data"; 8 | public int ID; 9 | protected int Salary; 10 | public String name; 11 | public String department; 12 | 13 | worker() { 14 | Scanner in = new Scanner(System.in); 15 | System.out.println("Enter your name "); 16 | name = in.nextLine(); 17 | System.out.println("Enetr your worker Id"); 18 | ID = in.nextInt(); 19 | System.out.println("Enter your department name"); 20 | department = in.nextLine(); 21 | if (department.isEmpty()) { 22 | department = in.nextLine().toLowerCase(); 23 | } 24 | } 25 | } 26 | 27 | class salarydecide extends worker { 28 | void salaryfinal() { 29 | if (department.matches(departmen1)) { 30 | Salary = 10000 * 15 / 100 + 10000; 31 | System.out.println("you salary after increment is" + " " + Salary); 32 | } else if (department.matches(department2)) { 33 | Salary = 10000 * 10 / 100 + 10000; 34 | System.out.println("you salary after increment is" + " " + Salary); 35 | } else { 36 | System.out.println("sorry we don't have any department of this name"); 37 | } 38 | } 39 | } 40 | 41 | public class Employee { 42 | public static void main(String[] args) { 43 | salarydecide obj = new salarydecide(); 44 | obj.salaryfinal(); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /07Day7/inheritance/EX5/Dailyyworker.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class labour { 6 | public String name; 7 | public int salaryrate; 8 | 9 | labour() { 10 | Scanner in = new Scanner(System.in); 11 | System.out.println("Enter name:"); 12 | name = in.nextLine(); 13 | System.out.println("Enter salaryrate:"); 14 | salaryrate = in.nextInt(); 15 | } 16 | 17 | void compay(int hrs) { 18 | int salary = hrs * salaryrate; 19 | System.out.println("Slary is:" + salary); 20 | } 21 | } 22 | 23 | class dailyworker extends labour { 24 | dailyworker() { 25 | System.out.println("Daily workers will be paid according to 40 hours a week"); 26 | } 27 | } 28 | 29 | class salariedworker extends labour { 30 | salariedworker() { 31 | System.out.println("Salaried labour will be paid according to no of days he/she works"); 32 | } 33 | } 34 | 35 | public class Dailyyworker { 36 | public static void main(String[] args) { 37 | labour w; 38 | w = new dailyworker(); 39 | w.compay(40); 40 | w = new salariedworker(); 41 | System.out.println("Enter no of days salaried labour worked: "); 42 | Scanner in = new Scanner(System.in); 43 | int days = in.nextInt(); 44 | w.compay(days); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /08Day8/Interface/EX1/Inter1.java: -------------------------------------------------------------------------------- 1 | package DAY8; 2 | 3 | import java.util.Scanner; 4 | 5 | interface test { // defining interface 6 | void square(int number); // method is by default abstract 7 | } 8 | 9 | class arithmetic implements test // implementing interface 10 | { 11 | public int number; 12 | 13 | public void square(int number) // abstract method 14 | { 15 | System.out.println("Square of number is: " + number * number); 16 | } 17 | } 18 | 19 | class totest { 20 | arithmetic obj = new arithmetic(); 21 | 22 | totest() // defining constructor 23 | { 24 | Scanner in = new Scanner(System.in); 25 | System.out.println("Enter number: "); 26 | int num = in.nextInt(); 27 | 28 | obj.square(num); 29 | } 30 | } 31 | 32 | public class Inter1 { 33 | public static void main(String[] args) { 34 | totest obj1 = new totest(); 35 | 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /08Day8/Interface/EX2/Inter2.java: -------------------------------------------------------------------------------- 1 | package DAY8; 2 | 3 | import java.util.Scanner; 4 | 5 | interface num1 // defining interface 6 | { 7 | void method1(); // by default, abstract function 8 | 9 | void method2(); 10 | } 11 | 12 | class MyClass implements num1 // implementing interface 13 | { 14 | public void method1() // defining abstract function 15 | { 16 | System.out.println("method 1 defines addition function"); 17 | Scanner in = new Scanner(System.in); // making object for scanner 18 | System.out.println("Enter two numbers num1 and num2 for addition:"); 19 | int num1 = in.nextInt(); 20 | int num2 = in.nextInt(); 21 | int sum = num1 + num2; 22 | System.out.println("Sum is:" + sum); 23 | } 24 | 25 | public void method2() // defining abstract function 26 | { 27 | System.out.println("method 1 defines subtraction function"); 28 | Scanner in = new Scanner(System.in); 29 | System.out.println("Enter two numbers num1 and num2 for subtraction:"); 30 | int num1 = in.nextInt(); 31 | int num2 = in.nextInt(); 32 | int subtraction = num1 - num2; 33 | System.out.println("Subtraction is:" + subtraction); 34 | } 35 | } 36 | 37 | public class Inter2 { 38 | public static void main(String[] args) { 39 | MyClass obj = new MyClass(); 40 | obj.method1(); 41 | obj.method2(); 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /08Day8/Interface/EX3/Inter3.java: -------------------------------------------------------------------------------- 1 | package DAY8; 2 | 3 | import java.util.Scanner; 4 | 5 | interface demo { 6 | Scanner in = new Scanner(System.in); // making object of scanner class 7 | int number = in.nextInt(); 8 | 9 | void functions(); // abstract functions 10 | } 11 | 12 | class myclass1 implements demo // implementing interface demo 13 | { 14 | public void functions() // defining abstract function 15 | { 16 | // number++; will show error 17 | int square = number * number; 18 | System.out.println("Square is: " + square); 19 | } 20 | } 21 | 22 | public class Inter3 implements demo { 23 | public void functions() // defining functions 24 | { 25 | System.out.println("Enter number upto which we want to compare:"); 26 | Scanner in = new Scanner(System.in); 27 | int num = in.nextInt(); 28 | // number++; will show error 29 | System.out.println("Sum is" + number + num); 30 | } 31 | 32 | public static void main(String[] args) { 33 | myclass1 obj = new myclass1(); 34 | obj.functions(); 35 | Inter3 obj1 = new Inter3(); 36 | obj1.functions(); 37 | // functions(); 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /08Day8/Interface/EX4/Inter4.java: -------------------------------------------------------------------------------- 1 | package DAY8; 2 | 3 | import java.util.Scanner; 4 | 5 | interface demooverriding { 6 | void division(); // abstract methods 7 | 8 | default void modules() // default methods which can be overridden 9 | { 10 | System.out.println("in parent interface and in module method"); 11 | } 12 | } 13 | 14 | interface extendeddemo extends demooverriding // extending another interface 15 | { 16 | default void modules() // overriding default method of parent interface 17 | { 18 | System.out.println("in sub-interface and in module method"); 19 | } 20 | } 21 | 22 | public class Inter4 implements extendeddemo { 23 | public void division() { 24 | System.out.println("in division method"); 25 | } 26 | 27 | public static void main(String[] args) { 28 | Inter4 obj = new Inter4(); // making object because static to non-static can't be called 29 | System.out.println("Calling division function\n"); 30 | obj.division(); 31 | System.out.println("Calling module function\n"); 32 | obj.modules(); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /08Day8/Task/Interface1.java: -------------------------------------------------------------------------------- 1 | // Java program to demonstrate working of 2 | // interface. 3 | import java.io.*; 4 | 5 | // A simple interface 6 | interface In1 7 | { 8 | // public, static and final 9 | final int a = 10; 10 | 11 | // public and abstract 12 | void display(); 13 | } 14 | // A class that implements the interface. 15 | class TestClass implements In1 16 | { 17 | // Implementing the capabilities of 18 | // interface. 19 | public void display() 20 | { 21 | System.out.println("Geek"); 22 | } 23 | 24 | // Driver Code 25 | public static void main (String[] args) 26 | { 27 | TestClass t = new TestClass(); 28 | t.display(); 29 | System.out.println(a); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /08Day8/Task/Interface2.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | interface Vehicle { 4 | 5 | // all are the abstract methods. 6 | void changeGear(int a); 7 | void speedUp(int a); 8 | void applyBrakes(int a); 9 | } 10 | class Bicycle implements Vehicle{ 11 | 12 | int speed; 13 | int gear; 14 | 15 | // to change gear 16 | @Override 17 | public void changeGear(int newGear){ 18 | 19 | gear = newGear; 20 | } 21 | 22 | // to increase speed 23 | @Override 24 | public void speedUp(int increment){ 25 | 26 | speed = speed + increment; 27 | } 28 | // to decrease speed 29 | @Override 30 | public void applyBrakes(int decrement){ 31 | 32 | speed = speed - decrement; 33 | } 34 | 35 | public void printStates() { 36 | System.out.println("speed: " + speed 37 | + " gear: " + gear); 38 | } 39 | } 40 | class Bike implements Vehicle { 41 | 42 | int speed; 43 | int gear; 44 | 45 | // to change gear 46 | @Override 47 | public void changeGear(int newGear){ 48 | 49 | gear = newGear; 50 | } 51 | // to increase speed 52 | @Override 53 | public void speedUp(int increment){ 54 | 55 | speed = speed + increment; 56 | } 57 | 58 | // to decrease speed 59 | @Override 60 | public void applyBrakes(int decrement){ 61 | 62 | speed = speed - decrement; 63 | } 64 | 65 | public void printStates() { 66 | System.out.println("speed: " + speed 67 | + " gear: " + gear); 68 | } 69 | 70 | } 71 | class GFG { 72 | 73 | public static void main (String[] args) { 74 | 75 | // creating an inatance of Bicycle 76 | // doing some operations 77 | Bicycle bicycle = new Bicycle(); 78 | bicycle.changeGear(2); 79 | bicycle.speedUp(3); 80 | bicycle.applyBrakes(1); 81 | 82 | System.out.println("Bicycle present state :"); 83 | bicycle.printStates(); 84 | 85 | // creating instance of the bike. 86 | Bike bike = new Bike(); 87 | bike.changeGear(1); 88 | bike.speedUp(4); 89 | bike.applyBrakes(3); 90 | 91 | System.out.println("Bike present state :"); 92 | bike.printStates(); 93 | } 94 | } 95 | 96 | -------------------------------------------------------------------------------- /08Day8/Task/Task.txt: -------------------------------------------------------------------------------- 1 | todays task would be go through alll the files and study about interface 2 | -------------------------------------------------------------------------------- /09Day9/Task/Screenshot (75).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dotQuestionmark/30DaysOfJava/12f31b26e99d6157a9f70c4bf41a53b592154f61/09Day9/Task/Screenshot (75).png -------------------------------------------------------------------------------- /09Day9/Task/Seating.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class seatingarr { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | // ask for number of inputs 7 | int x = in.nextInt(); 8 | // for loop 9 | for (int i = 0; i < x; i++) { 10 | // input the number of seat 11 | int seat = in.nextInt(); 12 | // calculating the facing seat number 13 | seat = seat + 2 * (6 - (seat - 1) % 12) - 1; 14 | // checking the seat type 15 | if (seat % 6 < 2) 16 | System.out.println(seat + " " + "WS"); 17 | else if (seat % 6 == 2 || seat % 6 == 5) 18 | System.out.println(seat + " " + "MS"); 19 | else 20 | System.out.println(seat + " " + "AS"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /09Day9/Task/seatingarr.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class seatingarr { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | // ask for number of inputs 7 | int x = in.nextInt(); 8 | // for loop 9 | for (int i = 0; i < x; i++) { 10 | // input the number of seat 11 | int seat = in.nextInt(); 12 | // calculating the facing seat number 13 | seat = seat + 2 * (6 - (seat - 1) % 12) - 1; 14 | // checking the seat type 15 | if (seat % 6 < 2) 16 | System.out.println(seat + " " + "WS"); 17 | else if (seat % 6 == 2 || seat % 6 == 5) 18 | System.out.println(seat + " " + "MS"); 19 | else 20 | System.out.println(seat + " " + "AS"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Collections/Collection1.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Iterator; 6 | 7 | public class Collection1 { 8 | public static void main(String[] args) { 9 | ArrayList obj = new ArrayList(); // making object of array-list 10 | obj.add(3); 11 | obj.add(5); 12 | obj.add(2); 13 | Iterator it = obj.iterator(); // using iterator to operate on a particular value as we don't have index in 14 | // collections 15 | while (it.hasNext()) { 16 | System.out.println(it.next()); // using next method of collection class 17 | } 18 | ArrayList newone = new ArrayList(); 19 | newone = (ArrayList) obj.clone(); // making duplicate objects using clone method 20 | System.out.println(newone); 21 | Collections.reverse(obj); 22 | System.out.println(obj); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Collections/Collection2.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | import java.util.HashMap; 4 | 5 | public class Collection2 { 6 | public static void main(String[] args) { 7 | HashMap obj = new HashMap<>(); // making object of HashMap 8 | obj.put(1, "Harshil"); // Adding values in HashMap 9 | obj.put(2, "Bhardwaj"); 10 | obj.put(3, "COOkie"); 11 | System.out.println(obj.containsKey(3)); // checking whether HashMap contains a particular key or not 12 | System.out.println(obj.containsValue("Harshil")); // checking whether HashMap contains a particular value or not 13 | System.out.println(obj.keySet()); // to get all the keys from the HashMap 14 | System.out.println(obj); 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Collections/Collection3.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | import java.util.HashSet; 4 | 5 | public class Collection3 { 6 | public static void main(String[] args) { 7 | HashSet obj = new HashSet<>(); // making object of HashSet 8 | obj.add(3); 9 | obj.add(2); // Adding values to HashSet 10 | obj.add(10); 11 | obj.add(1); 12 | HashSet newone = new HashSet(); 13 | newone = (HashSet) obj.clone(); // cloning a object in another new object of HashSet 14 | System.out.println(newone); 15 | //clearing all elements 16 | newone.clear(); 17 | System.out.println( 18 | "so once we are done with this After clearing everything newone hashset will look like: " + newone); 19 | System.out.println(obj.contains(3)); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Threads/Thread1.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | class Hi extends Thread { 4 | public void run() { 5 | for (Integer i = 0; i < 3; i++) { 6 | System.out.println("Hi"); 7 | } 8 | } 9 | } 10 | 11 | class hello extends Thread { 12 | public void run() { 13 | for (Integer i = 0; i < 3; i++) { 14 | System.out.println("Hello"); 15 | } 16 | } 17 | } 18 | 19 | class threadingdemo { 20 | threadingdemo() { 21 | Hi obj1 = new Hi(); 22 | hello obj2 = new hello(); 23 | obj1.start(); 24 | 25 | obj2.start(); 26 | } 27 | } 28 | 29 | //Implementing runnable interface 30 | class multi implements Runnable { 31 | public void run() { 32 | System.out.println("Threading is done i am runnable interface"); 33 | } 34 | } 35 | 36 | class threadingusingrunnableinterface { 37 | threadingusingrunnableinterface() { 38 | multi m = new multi(); 39 | Thread t1 = new Thread(m); 40 | t1.start(); 41 | } 42 | } 43 | 44 | public class Thread1 { 45 | public static void main(String[] args) { 46 | threadingdemo obj = new threadingdemo(); 47 | threadingusingrunnableinterface obj1 = new threadingusingrunnableinterface(); 48 | 49 | } 50 | 51 | } 52 | 53 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Threads/Thread2.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | class even extends Thread { 4 | // thread for printing even no 5 | public void run() { 6 | for (Integer i = 1; i < 10; i++) { 7 | if (i % 2 == 0) { 8 | System.out.println(i + "is even"); 9 | } 10 | } 11 | } 12 | } 13 | 14 | class odd extends Thread { 15 | // thread for printing odd number 16 | public void run() { 17 | for (Integer i = 1; i < 10; i++) { 18 | if (i % 2 != 0) { 19 | System.out.println(i + "is odd"); 20 | } 21 | } 22 | } 23 | } 24 | 25 | class test { 26 | test() throws InterruptedException { 27 | even obj1 = new even(); 28 | odd obj2 = new odd(); 29 | obj1.start(); 30 | obj1.join(); // joining to main thread 31 | obj2.start(); 32 | obj2.join(); 33 | } 34 | } 35 | 36 | public class Thread2 { 37 | public static void main(String[] args) throws InterruptedException { 38 | test obj = new test(); 39 | 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Threads/Thread3.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | class Counter1 { 4 | Integer count = 0; 5 | 6 | public synchronized void increament() { 7 | count++; 8 | } 9 | } 10 | 11 | public class Thread3 { 12 | public static void main(String[] args) { 13 | Counter1 obj = new Counter1(); 14 | // Runnable obj1=new Runnable(); 15 | Thread t1 = new Thread(new Runnable() // implementing runnable for thread 1 16 | { 17 | 18 | public void run() { 19 | 20 | for (Integer i = 0; i < 1000; i++) { 21 | obj.increament(); 22 | } 23 | 24 | } 25 | }); 26 | Thread t2 = new Thread(new Runnable() // implementing runnable for thread 2 27 | { 28 | public void run() { 29 | for (Integer i = 0; i < 1000; i++) { 30 | obj.increament(); 31 | } 32 | } 33 | }); 34 | t1.start(); 35 | t1.setName("Thread1"); 36 | System.out.println(t1.getName()); 37 | t1.run(); 38 | t2.start(); 39 | t1.setName("Thread2"); 40 | System.out.println(t1.getName()); 41 | t2.run(); 42 | System.out.println(obj.count); 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /09Day9/Threads-Collections/Threads/Thread4.java: -------------------------------------------------------------------------------- 1 | package DAY9; 2 | 3 | class que implements Runnable { 4 | public synchronized void run() { 5 | System.out.println(Thread.currentThread().getName()); 6 | System.out.println("Threading is done" + Thread.currentThread().getPriority()); 7 | } 8 | } 9 | 10 | public class Thread4 { 11 | public static void main(String[] args) { 12 | que m = new que(); 13 | Thread t1 = new Thread(m); 14 | Thread t2 = new Thread(m); 15 | Thread t3 = new Thread(m); 16 | Thread t4 = new Thread(m); 17 | Thread t5 = new Thread(m); 18 | t1.start(); 19 | t1.setPriority(5); // setting priority 20 | t2.start(); 21 | t2.setPriority(10); 22 | t3.start(); 23 | t3.setPriority(2); 24 | t4.start(); 25 | t4.setPriority(3); 26 | t5.start(); 27 | t5.setPriority(8); 28 | System.out.println(t1.isAlive()); // checking whether the thread is alive or not 29 | System.out.println(t2.isAlive()); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /10Day10/Regex/Regex1.java: -------------------------------------------------------------------------------- 1 | package DAY10; 2 | 3 | import java.util.regex.Pattern; 4 | import java.util.Scanner; 5 | import java.util.regex.Matcher; 6 | 7 | public class Regex1 { 8 | public static void main(String[] args) { 9 | Scanner sc = new Scanner(System.in); 10 | while (true) { 11 | System.out.println("Enter regex pattern:"); 12 | Pattern pattern = Pattern.compile(sc.nextLine()); 13 | System.out.println("Enter text:"); 14 | Matcher matcher = pattern.matcher(sc.nextLine()); 15 | boolean found = false; 16 | while (matcher.find()) { 17 | System.out.println("I found the text " + matcher.group() + " starting at index " + matcher.start() 18 | + " and ending at index " + matcher.end()); 19 | found = true; 20 | } 21 | if (!found) { 22 | System.out.println("No match found."); 23 | } 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /10Day10/Regex/Regex2.java: -------------------------------------------------------------------------------- 1 | /*Create a regular expression that accepts 10 digit numeric characters 2 | starting with 7, 8 or 9 only.*/ 3 | package DAY10; 4 | 5 | import java.util.regex.*; 6 | 7 | public class Regex2 { 8 | public static void main(String args[]) { 9 | System.out.println("by character classes and quantifiers ..."); 10 | System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));// true 11 | System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));// true 12 | 13 | System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));// false (11 characters) 14 | System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));// false (starts from 6) 15 | System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));// true 16 | 17 | System.out.println("by metacharacters ..."); 18 | System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));// true 19 | System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));// false (starts from 3) 20 | 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /10Day10/Regex/Regex3.java: -------------------------------------------------------------------------------- 1 | /*Create a regular expression that accepts alphanumeric characters only. 2 | Its length must be six characters long only.*/ 3 | 4 | package DAY10; 5 | 6 | import java.util.regex.*; 7 | 8 | public class Regex3 { 9 | public static void main(String args[]) { 10 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));// true 11 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));// false (more than 6 char) 12 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));// true 13 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));// false ($ is not matched) 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /10Day10/Regex/check.java: -------------------------------------------------------------------------------- 1 | package pattern; 2 | 3 | import java.util.Scanner; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | public class check { 8 | 9 | public static void main(String[] args) { 10 | 11 | String regex = "\\b(\\w+)(?:\\W+\\1\\b)+"; 12 | Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 13 | 14 | Scanner in = new Scanner(System.in); 15 | int numSentences = Integer.parseInt(in.nextLine()); 16 | 17 | while (numSentences-- > 0) { 18 | String input = in.nextLine(); 19 | 20 | Matcher m = p.matcher(input); 21 | 22 | // Check for subsequences of input that match the compiled pattern 23 | while (m.find()) { 24 | input = input.replaceAll(m.group(), m.group(1)); 25 | } 26 | 27 | // Prints the modified sentence. 28 | System.out.println(input); 29 | } 30 | 31 | in.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /10Day10/Regex/regex.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.Matcher; 2 | import java.util.regex.Pattern; 3 | import java.util.Scanner; 4 | 5 | class Solution{ 6 | 7 | public static void main(String[] args){ 8 | Scanner in = new Scanner(System.in); 9 | while(in.hasNext()){ 10 | String IP = in.next(); 11 | System.out.println(IP.matches(new MyRegex().pattern)); 12 | } 13 | 14 | } 15 | } 16 | 17 | class MyRegex{ 18 | String num ="([01]?\\d{1,2}|2[0-4]\\d|25[0-5])"; 19 | String pattern=num+"."+num+"."+num+"."+num; 20 | } 21 | -------------------------------------------------------------------------------- /10Day10/Task/Q1.java: -------------------------------------------------------------------------------- 1 | class Rectangle{ //OVERLOADING CLASS 2 | 3 | public static void printArea(int x,int y){ 4 | System.out.println(x*y); 5 | } 6 | public static void printArea(int x){ //overloading function 1 7 | System.out.println(x*x); 8 | } 9 | public static void printArea(int x,double y){ //overloading function 2 10 | System.out.println(x*y); 11 | } 12 | public static void printArea(double x){ //overloading function 3 13 | System.out.println(x*x); 14 | } 15 | 16 | public static void main(String[] args){ //main fucntion for implementation 17 | printArea(2,4); 18 | printArea(2,5.1); 19 | printArea(10); 20 | printArea(2.3); 21 | } 22 | } 23 | 24 | class Animals{ //CLASS FOR OVERRIDING 25 | public void sound(){ 26 | System.out.println("This is parent class."); 27 | } 28 | } 29 | class Dogs extends Animals{ //inherited class 1 30 | public void sound(){ 31 | System.out.println("Dogs bark"); 32 | } 33 | } 34 | class Cats extends Animals{ //inherited class 2 35 | public void sound(){ 36 | System.out.println("Cats meow"); 37 | } 38 | } 39 | class Monkeys extends Animals{ //inherited class 3 40 | public void sound(){ 41 | System.out.println("Monkeys whoop."); 42 | } 43 | } 44 | class m{ //driver class with main functin 45 | public static void main(String[] args){ 46 | Animals d = new Dogs(); 47 | Animals c = new Cats(); 48 | Animals m = new Monkeys(); 49 | d.sound(); 50 | c.sound(); 51 | m.sound(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /10Day10/Task/Q2.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.*; 2 | public class RegexExample1{ 3 | public static void main(String args[]){ 4 | //1st way 5 | Pattern p = Pattern.compile(".s");//. represents single character 6 | Matcher m = p.matcher("as"); 7 | boolean b = m.matches(); 8 | 9 | //2nd way 10 | boolean b2=Pattern.compile(".s").matcher("as").matches(); 11 | 12 | //3rd way 13 | boolean b3 = Pattern.matches(".s", "as"); 14 | 15 | System.out.println(b+" "+b2+" "+b3); 16 | }} 17 | -------------------------------------------------------------------------------- /10Day10/Task/Task.txt: -------------------------------------------------------------------------------- 1 | 2 | Todays task would be to write a source code that has overloading and overriding as well in a same code and the concept is on you write that code with your creativity 3 | and your second task would be to qrite a creative using regex . 4 | -------------------------------------------------------------------------------- /10Day10/methods/Overloading/Overloading1.java: -------------------------------------------------------------------------------- 1 | package DAY10; 2 | 3 | class DisplayOverloading { 4 | public void disp(char c) {// here you can see that i have made two methods with same name but with 5 | // different parameters 6 | System.out.println(c); 7 | } 8 | 9 | public void disp(char c, int num) { 10 | System.out.println(c + " " + num); 11 | } 12 | } 13 | 14 | public class Overloading1 { 15 | public static void main(String args[]) { 16 | DisplayOverloading obj = new DisplayOverloading(); 17 | obj.disp('a');// and when i called them they work fine thats what method overloading is all 18 | // about 19 | obj.disp('a', 10); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /10Day10/methods/Overloading/Overloading2.java: -------------------------------------------------------------------------------- 1 | package DAY10; 2 | 3 | class DisplayOverloading3 { 4 | public void disp(char c, int num) {// they have same name and same parameters 5 | System.out.println("I’m the first definition of method disp"); 6 | } 7 | 8 | public void disp(int num, char c) { 9 | System.out.println("I’m the second definition of method disp"); 10 | } 11 | } 12 | 13 | public class Overloading2 { 14 | public static void main(String args[]) { 15 | DisplayOverloading3 obj = new DisplayOverloading3(); 16 | obj.disp('x', 51);// and when i call them both of them run and thats also method-overloading so 17 | // don't Confuse that you can't create a method that have a same name and same 18 | // parameters 19 | obj.disp(52, 'y'); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /10Day10/methods/Overriding/Overrriding1.java: -------------------------------------------------------------------------------- 1 | package DAY10; 2 | 3 | class test { 4 | // Overridden method 5 | public void test() { 6 | System.out.println("robot is testing"); 7 | } 8 | } 9 | 10 | public class Overrriding1 extends test { 11 | public void test() { 12 | System.out.println("Boy is testing");// so as this method overrides so this method will work only 13 | } 14 | 15 | public static void main(String args[]) { 16 | Overrriding1 obj = new Overrriding1(); 17 | // This will call the child class version of test() 18 | obj.test();// so now you guys would be thinking that what is the use of overriding hmm nice 19 | // question but if i answer this to you will eventually forget as i did when my 20 | // mentor told me the need of overriding so one of your task for to day will be 21 | // to find what overriding is and how it helps 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /11Day11/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Todays task would be to study about exception handling and its advantages 2 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep1.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep1 { 4 | public static void main(String[] args) { 5 | int a = 100; 6 | int b = 0; 7 | int data; 8 | try { 9 | data = a / b; // may throw exception 10 | } 11 | // handling the exception 12 | catch (Exception e) { 13 | // resolving the exception in catch block 14 | System.out.println(a / (b + 2)); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep2.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep2 { 4 | public static void main(String[] args) { 5 | try { 6 | int array[] = { 1, 3, 5, 7 }; 7 | System.out.println(array[10]); // may throw exception 8 | } 9 | // handling the array exception 10 | catch (ArrayIndexOutOfBoundsException e) { 11 | System.out.println(e); 12 | } 13 | System.out.println("The index size shold be 4"); 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep3.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep3 { 4 | public static void main(String[] args) {// multiple catch block 5 | try { 6 | int a[] = new int[5]; 7 | a[5] = 30 / 0; 8 | } catch (ArithmeticException e) { 9 | System.out.println("Arithmetic Exception occurs"); 10 | } catch (ArrayIndexOutOfBoundsException e) { 11 | System.out.println("ArrayIndexOutOfBounds Exception occurs"); 12 | } catch (Exception e) { 13 | System.out.println("Parent Exception occurs"); 14 | } 15 | System.out.println("The following statement represent the catch block activated"); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep4.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep4 { 4 | public static void main(String[] args) { 5 | try { 6 | int a[] = new int[5]; 7 | 8 | System.out.println(a[10]); 9 | } catch (ArithmeticException e) { 10 | System.out.println("Arithmetic Exception occurs"); 11 | } catch (ArrayIndexOutOfBoundsException e) { 12 | System.out.println("ArrayIndexOutOfBounds Exception occurs"); 13 | } catch (Exception e) { 14 | System.out.println("Parent Exception occurs"); 15 | } 16 | System.out.println("rest of the code"); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep5.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep5 { 4 | public static void main(String[] args) { 5 | 6 | try { 7 | try { 8 | System.out.println("going to divide"); 9 | int b = 100 / 0; 10 | } catch (ArithmeticException e) { 11 | System.out.println(e);// here cannot be divided by zero exception wil run 12 | } 13 | 14 | try { 15 | int a[] = new int[5]; 16 | a[5] = 4;// here array out of bounds exception will run 17 | } catch (ArrayIndexOutOfBoundsException e) { 18 | System.out.println(e); 19 | } 20 | } catch (Exception e) { 21 | System.out.println("handeled"); 22 | } 23 | 24 | System.out.println("This is how a try catch block flows"); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep6.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep6 { 4 | public static void main(String[] args) { 5 | try { 6 | int data = 25 / 0; 7 | System.out.println(data); 8 | } catch (ArithmeticException e) { 9 | System.out.println(e); 10 | } finally { 11 | System.out.println("finally block is always executed");// this shows what if you always want a piece of code 12 | // to run then for that we have finally block which 13 | // always run 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep7.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Exep7 { 6 | static void validate() { 7 | Scanner in = new Scanner(System.in); 8 | System.out.println("Enter your age"); 9 | int age = in.nextInt(); 10 | if (age < 18) 11 | throw new ArithmeticException("not valid, you are still under-age ");// Throw exception is used when you 12 | // wanna check some conditions 13 | else 14 | System.out.println("welcome to vote"); 15 | } 16 | 17 | public static void main(String[] args) { 18 | validate(); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /11Day11/try-catch-throw/Exep8.java: -------------------------------------------------------------------------------- 1 | package DAY11; 2 | 3 | public class Exep8 { 4 | public void finalize() { 5 | System.out.println("finalize called"); 6 | } 7 | 8 | public static void main(String[] args) { 9 | Exep8 f1 = new Exep8(); 10 | Exep8 f2 = new Exep8(); 11 | f1 = null; 12 | f2 = null; 13 | f1.finalize(); 14 | f2.finalize(); 15 | System.gc(); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/CreateAFile/File1.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File1 { 7 | public static void main(String[] args) { 8 | try { 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | /* 11 | * If file gets created then the createNewFile() method would return true or if 12 | * the file is already present it would return false 13 | */ 14 | boolean fvar = file.createNewFile(); 15 | if (fvar) { 16 | System.out.println("File has been created successfully"); 17 | } else { 18 | System.out.println("File already present at the specified location"); 19 | } 20 | } catch (IOException e) { 21 | System.out.println("File not created"); 22 | e.printStackTrace(); 23 | } 24 | 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/Delete/File13.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | 5 | public class File13 { 6 | public static void main(String[] args) { 7 | try { 8 | // Specify the file name and path 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\copyfile.txt"); 10 | /* 11 | * the delete() method returns true if the file is deleted successfully else it 12 | * returns false 13 | */ 14 | if (file.delete()) { 15 | System.out.println(file.getName() + " is deleted!"); 16 | } else { 17 | System.out.println("Delete failed: File didn't delete"); 18 | } 19 | } catch (Exception e) { 20 | System.out.println("Exception occurred"); 21 | e.printStackTrace(); 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/FileCopy/File10.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | 8 | public class File10 { 9 | public static void main(String[] args) { 10 | FileInputStream instream = null; 11 | FileOutputStream outstream = null; 12 | 13 | try { 14 | File infile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 15 | File outfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\copyfile.txt"); 16 | 17 | instream = new FileInputStream(infile); 18 | outstream = new FileOutputStream(outfile); 19 | 20 | byte[] buffer = new byte[1024]; 21 | 22 | int length; 23 | /* 24 | * copying the contents from input stream to output stream using read and write 25 | * methods 26 | */ 27 | while ((length = instream.read(buffer)) > 0) { 28 | outstream.write(buffer, 0, length); 29 | } 30 | 31 | // Closing the input/output file streams 32 | instream.close(); 33 | outstream.close(); 34 | 35 | System.out.println("File copied successfully!!"); 36 | 37 | } catch (IOException ioe) { 38 | ioe.printStackTrace(); 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/FileRecord/File11.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.text.SimpleDateFormat; 5 | 6 | public class File11 { 7 | public static void main(String[] args) { 8 | // Specify the file path and name 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 11 | System.out.println("Last Modified Date: " + sdf.format(file.lastModified())); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/Fileconvertion/File7.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File7 { 7 | public static void main(String[] args) { 8 | File myfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | // making the file read only 10 | boolean flag = myfile.setReadOnly(); 11 | if (flag == true) { 12 | System.out.println("File successfully converted to Read only mode!!"); 13 | } else { 14 | System.out.println("Unsuccessful Operation!!"); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/Fileconvertion/File8.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File8 { 7 | public static void main(String[] args) { 8 | File myfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | if (myfile.canWrite()) { 10 | System.out.println("File is writable."); 11 | } else { 12 | System.out.println("File is read only."); 13 | } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/Fileconvertion/File9.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File9 { 7 | public static void main(String[] args) { 8 | 9 | File myfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | // changing the file mode to writable 11 | myfile.setWritable(true); 12 | if (myfile.canWrite()) { 13 | System.out.println("File is again writable."); 14 | } else { 15 | System.out.println("File is still read only."); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/ReadAFile/File5.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | public class File5 { 8 | public static void main(String[] args) { 9 | 10 | BufferedReader br = null; 11 | BufferedReader br2 = null; 12 | try { 13 | br = new BufferedReader(new FileReader("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt")); 14 | 15 | // One way of reading the file 16 | System.out.println("Reading the file using readLine() method:"); 17 | String contentLine = br.readLine(); 18 | while (contentLine != null) { 19 | System.out.println(contentLine); 20 | contentLine = br.readLine(); 21 | } 22 | 23 | br2 = new BufferedReader(new FileReader("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY6\\newfile.txt")); 24 | 25 | // Second way of reading the file 26 | System.out.println("Reading the file using read() method(2):"); 27 | int num = 0; 28 | char ch; 29 | while ((num = br2.read()) != -1) { 30 | ch = (char) num; 31 | System.out.print(ch); 32 | } 33 | 34 | } catch (IOException ioe) { 35 | ioe.printStackTrace(); 36 | } finally { 37 | try { 38 | if (br != null) 39 | br.close(); 40 | if (br2 != null) 41 | br2.close(); 42 | } catch (IOException ioe) { 43 | System.out.println("Error in closing the BufferedReader"); 44 | } 45 | } 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/ReadAFile/File6.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.*; 4 | 5 | public class File6 { 6 | public static void main(String[] args) { 7 | // Specify the path of the file here 8 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | BufferedInputStream bis = null; 10 | FileInputStream fis = null; 11 | 12 | try { 13 | // FileInputStream to read the file 14 | fis = new FileInputStream(file); 15 | 16 | /* 17 | * Passed the FileInputStream to BufferedInputStream For Fast read using the 18 | * buffer array. 19 | */ 20 | bis = new BufferedInputStream(fis); 21 | 22 | /* 23 | * available() method of BufferedInputStream returns 0 when there are no more 24 | * bytes present in the file to be read 25 | */ 26 | while (bis.available() > 0) { 27 | System.out.print((char) bis.read()); 28 | } 29 | 30 | } catch (FileNotFoundException fnfe) { 31 | System.out.println("The specified file not found" + fnfe); 32 | } catch (IOException ioe) { 33 | System.out.println("I/O Exception: " + ioe); 34 | } finally { 35 | try { 36 | if (bis != null && fis != null) { 37 | fis.close(); 38 | bis.close(); 39 | } 40 | } catch (IOException ioe) { 41 | System.out.println("Error in InputStream close(): " + ioe); 42 | } 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/WriteAFile/File2.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.FileWriter; 6 | import java.io.IOException; 7 | import java.util.Scanner; 8 | 9 | public class File2 { 10 | public static void main(String[] args) { 11 | BufferedWriter bw = null; 12 | Scanner in = new Scanner(System.in); 13 | System.out.println("Enter what you want to write in your file"); 14 | try { 15 | String mycontent = in.nextLine(); 16 | // Specify the file name and path here 17 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 18 | 19 | /* 20 | * This logic will make sure that the file gets created if it is not present at 21 | * the specified location 22 | */ 23 | if (!file.exists()) { 24 | file.createNewFile(); 25 | } 26 | 27 | FileWriter fw = new FileWriter(file); 28 | bw = new BufferedWriter(fw); 29 | bw.write(mycontent); 30 | System.out.println("File written Successfully"); 31 | 32 | } catch (IOException ioe) { 33 | ioe.printStackTrace(); 34 | } finally { 35 | try { 36 | if (bw != null) 37 | bw.close(); 38 | } catch (Exception ex) { 39 | System.out.println("Error in closing the BufferedWriter" + ex); 40 | } 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/WriteAFile/File3.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.util.Scanner; 7 | 8 | public class File3 { 9 | public static void main(String[] args) { 10 | FileOutputStream fos = null; 11 | File file; 12 | Scanner in = new Scanner(System.in); 13 | System.out.println("Enter what do you want to write in the file"); 14 | String mycontent = in.nextLine(); 15 | try { 16 | // Specify the file path here 17 | file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 18 | fos = new FileOutputStream(file); 19 | 20 | /* 21 | * This logic will check whether the file exists or not. If the file is not 22 | * found at the specified location it would create a new file 23 | */ 24 | if (!file.exists()) { 25 | file.createNewFile(); 26 | } 27 | 28 | /* 29 | * String content cannot be directly written into a file. It needs to be 30 | * converted into bytes 31 | */ 32 | byte[] bytesArray = mycontent.getBytes(); 33 | 34 | fos.write(bytesArray); 35 | fos.flush(); 36 | System.out.println("File Written Successfully"); 37 | } catch (IOException ioe) { 38 | ioe.printStackTrace(); 39 | } finally { 40 | try { 41 | if (fos != null) { 42 | fos.close(); 43 | } 44 | } catch (IOException ioe) { 45 | System.out.println("Error in closing the Stream"); 46 | } 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/checkfile/File4.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class File4 { 7 | public static void main(String[] args) { 8 | // Provide the complete file path here 9 | File file = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 10 | 11 | if (file.isHidden()) { 12 | System.out.println("The specified file is hidden"); 13 | } else { 14 | System.out.println("The specified file is not hidden"); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Fileoperations/rename/File12.java: -------------------------------------------------------------------------------- 1 | package DAY5; 2 | 3 | import java.io.File; 4 | 5 | public class File12 { 6 | public static void main(String[] args) { 7 | // Old File 8 | File oldfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\newfile.txt"); 9 | // New File 10 | File newfile = new File("G:\\HACKERRANK\\JAVAFORDUMMIES\\src\\DAY5\\renamedfile.txt"); 11 | /* 12 | * renameTo() return boolean value It return true if rename operation is 13 | * successful 14 | */ 15 | boolean flag = oldfile.renameTo(newfile); 16 | if (flag) { 17 | System.out.println("File renamed successfully"); 18 | } else { 19 | System.out.println("Rename operation failed"); 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Regex/Regex1.java: -------------------------------------------------------------------------------- 1 | package DAY10; 2 | 3 | import java.util.regex.Pattern; 4 | import java.util.Scanner; 5 | import java.util.regex.Matcher; 6 | 7 | public class Regex1 { 8 | public static void main(String[] args) { 9 | Scanner sc = new Scanner(System.in); 10 | while (true) { 11 | System.out.println("Enter regex pattern:"); 12 | Pattern pattern = Pattern.compile(sc.nextLine()); 13 | System.out.println("Enter text:"); 14 | Matcher matcher = pattern.matcher(sc.nextLine()); 15 | boolean found = false; 16 | while (matcher.find()) { 17 | System.out.println("I found the text " + matcher.group() + " starting at index " + matcher.start() 18 | + " and ending at index " + matcher.end()); 19 | found = true; 20 | } 21 | if (!found) { 22 | System.out.println("No match found."); 23 | } 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Regex/Regex2.java: -------------------------------------------------------------------------------- 1 | /*Create a regular expression that accepts 10 digit numeric characters 2 | starting with 7, 8 or 9 only.*/ 3 | package DAY10; 4 | 5 | import java.util.regex.*; 6 | 7 | public class Regex2 { 8 | public static void main(String args[]) { 9 | System.out.println("by character classes and quantifiers ..."); 10 | System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));// true 11 | System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));// true 12 | 13 | System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));// false (11 characters) 14 | System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));// false (starts from 6) 15 | System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));// true 16 | 17 | System.out.println("by metacharacters ..."); 18 | System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));// true 19 | System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));// false (starts from 3) 20 | 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Regex/Regex3.java: -------------------------------------------------------------------------------- 1 | /*Create a regular expression that accepts alphanumeric characters only. 2 | Its length must be six characters long only.*/ 3 | 4 | package DAY10; 5 | 6 | import java.util.regex.*; 7 | 8 | public class Regex3 { 9 | public static void main(String args[]) { 10 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));// true 11 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));// false (more than 6 char) 12 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));// true 13 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));// false ($ is not matched) 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Regex/check.java: -------------------------------------------------------------------------------- 1 | package pattern; 2 | 3 | import java.util.Scanner; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | public class check { 8 | 9 | public static void main(String[] args) { 10 | 11 | String regex = "\\b(\\w+)(?:\\W+\\1\\b)+"; 12 | Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 13 | 14 | Scanner in = new Scanner(System.in); 15 | int numSentences = Integer.parseInt(in.nextLine()); 16 | 17 | while (numSentences-- > 0) { 18 | String input = in.nextLine(); 19 | 20 | Matcher m = p.matcher(input); 21 | 22 | // Check for subsequences of input that match the compiled pattern 23 | while (m.find()) { 24 | input = input.replaceAll(m.group(), m.group(1)); 25 | } 26 | 27 | // Prints the modified sentence. 28 | System.out.println(input); 29 | } 30 | 31 | in.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Regex/regex.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.Matcher; 2 | import java.util.regex.Pattern; 3 | import java.util.Scanner; 4 | 5 | class Solution{ 6 | 7 | public static void main(String[] args){ 8 | Scanner in = new Scanner(System.in); 9 | while(in.hasNext()){ 10 | String IP = in.next(); 11 | System.out.println(IP.matches(new MyRegex().pattern)); 12 | } 13 | 14 | } 15 | } 16 | 17 | class MyRegex{ 18 | String num ="([01]?\\d{1,2}|2[0-4]\\d|25[0-5])"; 19 | String pattern=num+"."+num+"."+num+"."+num; 20 | } 21 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Sorting-algos/Bubblesort.java: -------------------------------------------------------------------------------- 1 | package DAY4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Bubblesort { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int size = in.nextInt(); 10 | int array[] = new int[size]; 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt(); 13 | } 14 | sort(array); 15 | print(array); 16 | } 17 | public static void sort(int array[]) { 18 | int n = array.length; 19 | for (int i = 0; i < n - 1; i++) 20 | for (int j = 0; j < n - i - 1; j++) 21 | if (array[j] > array[j + 1]) { 22 | // swap array[j+1] and array[i] 23 | int temp = array[j]; 24 | array[j] = array[j + 1]; 25 | array[j + 1] = temp; 26 | } 27 | } 28 | public static void print(int array[]) { 29 | System.out.println(Arrays.toString(array)); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Sorting-algos/Insertionsort.java: -------------------------------------------------------------------------------- 1 | package DAY4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Insertionsort { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in);// As usual called the scanner class 9 | int size = in.nextInt();// used the object created from the scanner class 10 | int array[] = new int[size];// initialized the array size 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt();// taken values inside the array 13 | } 14 | sort(array);// this function will sort the array 15 | printarray(array);// this function will print the sorted array 16 | } 17 | public static void sort(int array[]) { 18 | for (int i = 0; i < array.length; ++i) { 19 | int key = array[i]; 20 | int j = i - 1; 21 | 22 | // now here we are moving the elements one step ahead who are greater than key 23 | while (j >= 0 && array[j] > key) { 24 | array[j + 1] = array[j]; 25 | j = j - 1; 26 | } 27 | array[j + 1] = key;// and finally we have our sorted array 28 | } 29 | } 30 | public static void printarray(int array[]) { 31 | System.out.println(Arrays.toString(array));// again here we called the array class utility 32 | // i personally believe that before going ahead you should master arrays and for 33 | // that you need to study some sorting algorithms thats why today we will only 34 | // learn about some sorting algorithms 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Sorting-algos/Mergesort.java: -------------------------------------------------------------------------------- 1 | package DAY4; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Mergesort { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | int size = in.nextInt(); 10 | int array[] = new int[size]; 11 | for (int i = 0; i < array.length; i++) { 12 | array[i] = in.nextInt(); 13 | } 14 | Mergesort ob = new Mergesort();// called the main class via object creation like we use to do in Scanner class 15 | // for ex Scanner in = new SCanner(System.in) 16 | ob.sort(array, 0, array.length - 1); 17 | print(array); 18 | } 19 | public static void merge(int array[], int big, int mid, int end) { 20 | int l = mid - big + 1; 21 | int r = end - mid; 22 | 23 | int LeftArray[] = new int[l];// created two array left array and right array as a sub array of our main array 24 | int RightArray[] = new int[r]; 25 | 26 | for (int i = 0; i < l; ++i) 27 | LeftArray[i] = array[big + i]; 28 | 29 | for (int j = 0; j < r; ++j) 30 | RightArray[j] = array[mid + 1 + j]; 31 | 32 | int i = 0, j = 0; 33 | int k = big; 34 | while (i < l && j < r) { 35 | if (LeftArray[i] <= RightArray[j]) { 36 | array[k] = LeftArray[i]; 37 | i++; 38 | } else { 39 | array[k] = RightArray[j]; 40 | j++; 41 | } 42 | k++; 43 | } 44 | while (i < l) { 45 | array[k] = LeftArray[i]; 46 | i++; 47 | k++; 48 | } 49 | 50 | while (j < r) { 51 | array[k] = RightArray[j]; 52 | j++; 53 | k++; 54 | } 55 | } 56 | public static void sort(int array[], int big, int end) { 57 | if (big < end) { 58 | int mid = (big + end) / 2; 59 | sort(array, big, mid); 60 | sort(array, mid + 1, end); // here we are sorting the array 61 | merge(array, big, mid, end); 62 | } 63 | 64 | } 65 | public static void print(int array[]) { 66 | System.out.println(Arrays.toString(array));// here we called the array class utility to print array 67 | } 68 | } 69 | 70 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/Text.txt: -------------------------------------------------------------------------------- 1 | ubmit all your code here with name task 1 ,,, and so on 2 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/inheritance/EX1/err.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class demo {// here if you see i created a class and made a counstructor inside that takes a 6 | // number as ID 7 | public int ID; 8 | 9 | demo() { 10 | Scanner in = new Scanner(System.in); 11 | ID = in.nextInt(); 12 | } 13 | } 14 | 15 | class demo1 extends demo {// now i created another class and made it inherit demo class and how i did this 16 | // is using extends key word yes extends keyword is used to make classes inherit 17 | // classes 18 | void error() { 19 | System.out.println(ID); 20 | } 21 | } 22 | 23 | public class err { 24 | public static void main(String[] args) { 25 | demo1 obj = new demo1();// here i created an object(obj) of demo1 class 26 | 27 | obj.error();// now with the help of obj i called demo1 counstructor but before that it runs 28 | // demo counstructor takes ID and then runs demo1 and prints the ID. 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/inheritance/EX2/Game.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class player {// here i created player class which has name fees and add variables defined in 6 | // it 7 | private String name;// i did not want to use this in any furthre class so i made it as private 8 | private int fees;// same with fees 9 | protected int add = 0;// i want this to be accesable for different inherited classes so i made it 10 | // protected 11 | 12 | player() {// now i created a player counstructor which take input from you 13 | 14 | Scanner in = new Scanner(System.in); 15 | System.out.println("Enter fees deposited"); 16 | int fees = in.nextInt(); 17 | if (fees == 1000) { 18 | System.out.println("Enter Player name"); 19 | Scanner in1 = new Scanner(System.in); 20 | name = in1.nextLine(); 21 | add = 1;// variable to check that you deposited the desired amount of fees or not and 22 | // use this as that reference 23 | } else { 24 | add = 2; 25 | } 26 | } 27 | 28 | } 29 | 30 | class hockeyplayer extends player {// inherits class player same with all the classes below 31 | void needofmaterial() { 32 | if (add == 1) { 33 | System.out.println("Congratulation you got addmission in our accademy"); 34 | System.out.println("Bring your own Hockey stick for practice from tommorow"); 35 | } else { 36 | System.out.println("Sorry you did not submited the sufficient ammount"); 37 | } 38 | 39 | } 40 | } 41 | 42 | class cricketplayer extends player { 43 | void needofmaterial() { 44 | if (add == 1) { 45 | System.out.println("Congratulation you got addmission in our accademy"); 46 | System.out.println("Bring your own L-guard for practice from tommorow"); 47 | } else { 48 | System.out.println("Sorry you did not submited the sufficient ammount"); 49 | } 50 | } 51 | } 52 | 53 | class footballplayer extends player { 54 | void needofmaterial() { 55 | if (add == 1) { 56 | System.out.println("Congratulation you got addmission in our accademy"); 57 | System.out.println("Bring your own pair of football boots for practice from tommorow"); 58 | } else { 59 | System.out.println("Sorry you did not submited the sufficient ammount"); 60 | } 61 | } 62 | } 63 | 64 | public class Game { 65 | public static void main(String[] args) { 66 | Scanner in = new Scanner(System.in); 67 | System.out.println("Choose the sport you play"); 68 | System.out.println("1) Hockey(Rs 1000 per month to be deposited in advance)"); 69 | System.out.println("2) Cricket(Rs 1000 per month to be deposited in advance)"); 70 | System.out.println("3) Football(Rs 1000 per month to be deposited in advance)");// according to switch case 71 | // calls different classes 72 | int sport = in.nextInt(); 73 | switch (sport) { 74 | case 1: { 75 | hockeyplayer obj = new hockeyplayer(); 76 | obj.needofmaterial(); 77 | break; 78 | } 79 | case 2: { 80 | cricketplayer obj = new cricketplayer(); 81 | obj.needofmaterial(); 82 | break; 83 | } 84 | case 3: { 85 | footballplayer obj = new footballplayer(); 86 | obj.needofmaterial(); 87 | break; 88 | } 89 | default: 90 | throw new IllegalArgumentException("Unexpected value: " + sport); 91 | } 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/inheritance/EX3/Call.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class trunkcall { 6 | public int duration; 7 | public int charges; 8 | 9 | trunkcall() { 10 | System.out.println("Enter Duration of call: "); 11 | Scanner in = new Scanner(System.in); 12 | duration = in.nextInt(); 13 | } 14 | 15 | void charges(int rate) { 16 | charges = duration * rate; 17 | System.out.println("Charges are: " + charges); 18 | } 19 | } 20 | 21 | class ordinarytrunkcall extends trunkcall { 22 | public int rateofordinary; 23 | 24 | ordinarytrunkcall() { 25 | System.out.println("Enter rate of ordinary trunk call as per hour: "); 26 | Scanner in = new Scanner(System.in); 27 | rateofordinary = in.nextInt(); 28 | charges(rateofordinary); 29 | } 30 | } 31 | 32 | class urgenttrunkcall extends trunkcall { 33 | public int rateofurgent; 34 | 35 | urgenttrunkcall() { 36 | System.out.println("Enter rate of ordinary trunk call as per hour: "); 37 | Scanner in = new Scanner(System.in); 38 | rateofurgent = in.nextInt(); 39 | charges(rateofurgent); 40 | } 41 | } 42 | 43 | public class Call { 44 | public static void main(String[] args) { 45 | trunkcall obj; 46 | Scanner in = new Scanner(System.in); 47 | System.out.println("Enter your type: 1:"); 48 | System.out.println("1) ordinary trunk call"); 49 | System.out.println("2) urgent trunk call"); 50 | int i = in.nextInt(); 51 | 52 | switch (i) { 53 | case 1: 54 | System.out.println("It was an Ordinary trunk call"); 55 | obj = new ordinarytrunkcall(); 56 | break; 57 | case 2: 58 | System.out.println("It was an urgent trunk call"); 59 | obj = new urgenttrunkcall(); 60 | break; 61 | default: 62 | System.out.println("It was not a trunk call"); // default case is good for user understanding 63 | } 64 | 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/inheritance/EX4/Employee.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class worker { 6 | protected String departmen1 = "devops"; 7 | protected String department2 = "big data"; 8 | public int ID; 9 | protected int Salary; 10 | public String name; 11 | public String department; 12 | 13 | worker() { 14 | Scanner in = new Scanner(System.in); 15 | System.out.println("Enter your name "); 16 | name = in.nextLine(); 17 | System.out.println("Enetr your worker Id"); 18 | ID = in.nextInt(); 19 | System.out.println("Enter your department name"); 20 | department = in.nextLine(); 21 | if (department.isEmpty()) { 22 | department = in.nextLine().toLowerCase(); 23 | } 24 | } 25 | } 26 | 27 | class salarydecide extends worker { 28 | void salaryfinal() { 29 | if (department.matches(departmen1)) { 30 | Salary = 10000 * 15 / 100 + 10000; 31 | System.out.println("you salary after increment is" + " " + Salary); 32 | } else if (department.matches(department2)) { 33 | Salary = 10000 * 10 / 100 + 10000; 34 | System.out.println("you salary after increment is" + " " + Salary); 35 | } else { 36 | System.out.println("sorry we don't have any department of this name"); 37 | } 38 | } 39 | } 40 | 41 | public class Employee { 42 | public static void main(String[] args) { 43 | salarydecide obj = new salarydecide(); 44 | obj.salaryfinal(); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/inheritance/EX5/Dailyyworker.java: -------------------------------------------------------------------------------- 1 | package DAY7; 2 | 3 | import java.util.Scanner; 4 | 5 | class labour { 6 | public String name; 7 | public int salaryrate; 8 | 9 | labour() { 10 | Scanner in = new Scanner(System.in); 11 | System.out.println("Enter name:"); 12 | name = in.nextLine(); 13 | System.out.println("Enter salaryrate:"); 14 | salaryrate = in.nextInt(); 15 | } 16 | 17 | void compay(int hrs) { 18 | int salary = hrs * salaryrate; 19 | System.out.println("Slary is:" + salary); 20 | } 21 | } 22 | 23 | class dailyworker extends labour { 24 | dailyworker() { 25 | System.out.println("Daily workers will be paid according to 40 hours a week"); 26 | } 27 | } 28 | 29 | class salariedworker extends labour { 30 | salariedworker() { 31 | System.out.println("Salaried labour will be paid according to no of days he/she works"); 32 | } 33 | } 34 | 35 | public class Dailyyworker { 36 | public static void main(String[] args) { 37 | labour w; 38 | w = new dailyworker(); 39 | w.compay(40); 40 | w = new salariedworker(); 41 | System.out.println("Enter no of days salaried labour worked: "); 42 | Scanner in = new Scanner(System.in); 43 | int days = in.nextInt(); 44 | w.compay(days); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/stack-queue/Quueue.java: -------------------------------------------------------------------------------- 1 | package DAY6; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class Quueue { 7 | public static void main(String[] args) { 8 | Queue q = new LinkedList<>(); 9 | 10 | // Adds elements {0, 1, 2, 3, 4} to queue 11 | for (int i = 0; i < 5; i++) 12 | q.add(i); 13 | 14 | // Display contents of the queue. 15 | System.out.println("Elements of queue-" + q); 16 | 17 | // To remove the head of queue. 18 | int removedele = q.remove(); 19 | System.out.println("removed element-" + removedele); 20 | 21 | System.out.println(q); 22 | 23 | // To view the head of queue 24 | int head = q.peek(); 25 | System.out.println("head of queue-" + head); 26 | 27 | // Rest all methods of collection interface, 28 | // Like size and contains can be used with this 29 | // implementation. 30 | int size = q.size(); 31 | System.out.println("Size of queue-" + size); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /12Day12-13Day13/SOLUTION/stack-queue/Sttack.java: -------------------------------------------------------------------------------- 1 | package DAY6; 2 | 3 | import java.util.Stack; 4 | 5 | public class Sttack { 6 | static void stack_push(Stack stack) { 7 | for (int i = 0; i < 5; i++) { 8 | stack.push(i); 9 | } 10 | } 11 | 12 | // Popping element from the top of the stack 13 | static void stack_pop(Stack stack) { 14 | System.out.println("Pop :"); 15 | 16 | for (int i = 0; i < 5; i++) { 17 | Integer y = (Integer) stack.pop(); 18 | System.out.println(y); 19 | } 20 | } 21 | 22 | // Displaying element on the top of the stack 23 | static void stack_peek(Stack stack) { 24 | Integer element = (Integer) stack.peek(); 25 | System.out.println("Element on stack top : " + element); 26 | } 27 | 28 | // Searching element in the stack 29 | static void stack_search(Stack stack, int element) { 30 | Integer pos = (Integer) stack.search(element); 31 | 32 | if (pos == -1) 33 | System.out.println("Element not found"); 34 | else 35 | System.out.println("Element is found at position " + pos); 36 | } 37 | 38 | public static void main(String[] args) { 39 | Stack stack = new Stack(); 40 | 41 | stack_push(stack); 42 | stack_pop(stack); 43 | stack_push(stack); 44 | stack_peek(stack); 45 | stack_search(stack, 2); 46 | stack_search(stack, 6); 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task1.txt: -------------------------------------------------------------------------------- 1 | Task one is that you have to write a code that works as username and password field where 2 | USERNAME cannot be started with a space or numbers or any special charaters, it must start from a-z and then it can contain number but cannot contain any special characters 3 | PASSWORD length must be greater then 6 4 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task2.txt: -------------------------------------------------------------------------------- 1 | Task two is to take number of inputs and then store that into a file after completion make that file as read only mode. 2 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task3.txt: -------------------------------------------------------------------------------- 1 | Task 3 write a code for inheritacne where class A is inherited by class B and class C then b and c are inherited by Class D 2 | 3 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task4.txt: -------------------------------------------------------------------------------- 1 | Task 4 is Write a code that have two methods with same name and same parameters and perform overriding 2 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task5.txt: -------------------------------------------------------------------------------- 1 | Write the code for sorting algos that you have practised in this session so far 2 | 3 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task6.txt: -------------------------------------------------------------------------------- 1 | write a code that diveds array into subarray 2 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task7.txt: -------------------------------------------------------------------------------- 1 | Write a code for stack oerations 2 | 3 | -------------------------------------------------------------------------------- /12Day12-13Day13/TASK/Task8.txt: -------------------------------------------------------------------------------- 1 | Write code for Queue operations 2 | -------------------------------------------------------------------------------- /12Day12/SOLUTION/Solution3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io,*; 4 | 5 | class B 6 | { 7 | public void print_one() 8 | { 9 | System.out.println("Class B"); 10 | } 11 | } 12 | 13 | class C 14 | { 15 | public void print_two() 16 | { 17 | System.out.println("Class C"); 18 | } 19 | } 20 | class A extends B,C 21 | { 22 | public void print_three() 23 | { 24 | System.out.println("Class A"); 25 | } 26 | } 27 | class D extends B,c 28 | { 29 | public void print_four() 30 | { 31 | System.ot.println("Class D"); 32 | } 33 | } 34 | public class main 35 | { 36 | public static void main(String[] args) 37 | { 38 | A a= new A(); 39 | a.print_three(); 40 | D d= new D(); 41 | d.print_four(); 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /12Day12/SOLUTION/Solution4.java: -------------------------------------------------------------------------------- 1 | //MWTHOD ONE FOR OVERRIDING 2 | 3 | class MyBaseClass{ 4 | protected void disp() 5 | { 6 | System.out.println("Parent class method"); 7 | } 8 | } 9 | class MyChildClass extends MyBaseClass{ 10 | public void disp(){ 11 | System.out.println("Child class method"); 12 | } 13 | public static void main( String args[]) { 14 | MyChildClass obj = new MyChildClass(); 15 | obj.disp(); 16 | } 17 | } 18 | 19 | //METHOD TWO FOR OVERRIDING 20 | 21 | class ABC{ 22 | public void myMethod() 23 | { 24 | System.out.println("Overridden method"); 25 | } 26 | } 27 | class Demo extends ABC{ 28 | public void myMethod(){ 29 | //This will call the myMethod() of parent class 30 | super.myMethod(); 31 | System.out.println("Overriding method"); 32 | } 33 | public static void main( String args[]) { 34 | Demo obj = new Demo(); 35 | obj.myMethod(); 36 | } 37 | }class ABC{ 38 | public void myMethod() 39 | { 40 | System.out.println("Overridden method"); 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /12Day12/SOLUTION/Solution6.java: -------------------------------------------------------------------------------- 1 | class GFG 2 | { 3 | 4 | // Function to divide an array into k 5 | // parts such that the sum of difference 6 | // of every element with the maximum element 7 | // of that part is minimum 8 | static int divideArray(int arr[], int n, int k) 9 | { 10 | // Dp to store the values 11 | int dp[][] = new int[500][500]; 12 | int i, j; 13 | 14 | for(i = 0; i < 500; i++) 15 | for(j = 0; j < 500; j++) 16 | dp[i][j] = 0; 17 | 18 | k -= 1; 19 | // Fill up the dp table 20 | for (i = n - 1; i >= 0; i--) 21 | { 22 | for (j = 0; j <= k; j++) 23 | { 24 | 25 | // Intitilize maximum value 26 | dp[i][j] = Integer.MAX_VALUE; 27 | 28 | // Max element and the sum 29 | int max_ = -1, sum = 0; 30 | // Run a loop from i to n 31 | for (int l = i; l < n; l++) 32 | { 33 | // Find the maximum number 34 | // from i to l and the sum 35 | // from i to l 36 | max_ = Math.max(max_, arr[l]); 37 | sum += arr[l]; 38 | 39 | // Find the sum of difference 40 | // of every element with the 41 | // maximum element 42 | int diff = (l - i + 1) * max_ - sum; 43 | 44 | // If the array can be divided 45 | if (j > 0) 46 | dp[i][j] = Math.min(dp[i][j], diff + 47 | dp[l + 1][j - 1]); 48 | else 49 | dp[i][j] = diff; 50 | } 51 | } 52 | } 53 | // Returns the minimum sum 54 | // in K parts 55 | return dp[0][k]; 56 | } 57 | 58 | // Driver code 59 | public static void main (String[] args) 60 | { 61 | int arr[] = { 2, 9, 5, 4, 8, 3, 6 }; 62 | int n = arr.length; 63 | int k = 2; 64 | 65 | System.out.println(divideArray(arr, n, k)); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /12Day12/SOLUTION/Solution7.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Stack 4 | { 5 | private int arr[]; 6 | private int top; 7 | private int capacity; 8 | 9 | // Constructor to initialize stack 10 | Stack(int size) 11 | { 12 | arr = new int[size]; 13 | capacity = size; 14 | top = -1; 15 | } 16 | 17 | // Utility function to add an element x in the stack 18 | public void push(int x) 19 | { 20 | if (isFull()) 21 | { 22 | System.out.println("OverFlow\nProgram Terminated\n"); 23 | System.exit(1); 24 | } 25 | 26 | System.out.println("Inserting " + x); 27 | arr[++top] = x; 28 | } 29 | 30 | // Utility function to pop top element from the stack 31 | public int pop() 32 | { 33 | // check for stack underflow 34 | if (isEmpty()) 35 | { 36 | System.out.println("UnderFlow\nProgram Terminated"); 37 | System.exit(1); 38 | } 39 | System.out.println("Removing " + peek()); 40 | 41 | // decrease stack size by 1 and (optionally) return the popped element 42 | return arr[top--]; 43 | } 44 | 45 | // Utility function to return top element in a stack 46 | public int peek() 47 | { 48 | if (!isEmpty()) 49 | return arr[top]; 50 | else 51 | System.exit(1); 52 | 53 | return -1; 54 | } 55 | 56 | // Utility function to return the size of the stack 57 | public int size() 58 | { 59 | return top + 1; 60 | } 61 | 62 | // Utility function to check if the stack is empty or not 63 | public Boolean isEmpty() 64 | { 65 | return top == -1; // or return size() == 0; 66 | } 67 | 68 | // Utility function to check if the stack is full or not 69 | public Boolean isFull() 70 | { 71 | return top == capacity - 1; // or return size() == capacity; 72 | } 73 | public static void main (String[] args) 74 | { 75 | Stack stack = new Stack(3); 76 | 77 | stack.push(1); // Inserting 1 in the stack 78 | stack.push(2); // Inserting 2 in the stack 79 | 80 | stack.pop(); // removing the top 2 81 | stack.pop(); // removing the top 1 82 | 83 | stack.push(3); // Inserting 3 in the stack 84 | System.out.println("Top element is: " + stack.peek()); 85 | System.out.println("Stack size is " + stack.size()); 86 | 87 | stack.pop(); // removing the top 3 88 | 89 | // check if stack is empty 90 | if (stack.isEmpty()) 91 | System.out.println("Stack Is Empty"); 92 | else 93 | System.out.println("Stack Is Not Empty"); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /14Day14/Solution/Soluton.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class String{ 3 | public static void main(String[] args) { 4 | Scanner input = new Scanner(System.in); 5 | String name = ""; 6 | 7 | System.out.println("Please enter your name: "); 8 | name = input.nextLine(); 9 | 10 | name= name.toLowerCase(); 11 | 12 | try{ 13 | if(!name.find()){ // If string contains any number/symbols etc... 14 | throw new Exception("Not a perfect String"); 15 | } 16 | 17 | 18 | //This will not be executed if exception occurs 19 | System.out.println("This will not be executed if exception occurs"); 20 | 21 | }catch(Exception e){ 22 | System.out.println(e.toString()); 23 | } 24 | System.out.println("Exception handling"); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /14Day14/Solution/text.txt: -------------------------------------------------------------------------------- 1 | push your solutions heree 2 | -------------------------------------------------------------------------------- /14Day14/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Write a exception handeling program for only taking a string As an input 2 | -------------------------------------------------------------------------------- /15Day15/Interface/EX1.java: -------------------------------------------------------------------------------- 1 | // Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract. 2 | // Also, the variables declared in an interface are public, static & final by default. We will cover this in detail, later in this guide. 3 | // interface are used for full abstraction. 4 | // Since methods in interfaces do not have body, they have to be implemented by the class before you can access them. 5 | // Also, java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class. 6 | 7 | 8 | interface EX1 9 | { 10 | // All the methods are public abstract by default 11 | // As you see they have no body 12 | 13 | public void method1(); 14 | public void method2(); 15 | } 16 | -------------------------------------------------------------------------------- /15Day15/Interface/EX2.java: -------------------------------------------------------------------------------- 1 | interface inter 2 | { 3 | // compiler will treat them as: 4 | // public abstract void method1(); 5 | // public abstract void method2(); 6 | 7 | public void method1(); 8 | public void method2(); 9 | } 10 | class EX2 implements inter 11 | { 12 | // This class must have to implement both the abstract methods 13 | // else you will get compilation error 14 | 15 | public void method1() 16 | { 17 | System.out.println("implementation of method1"); 18 | } 19 | public void method2() 20 | { 21 | System.out.println("implementation of method2"); 22 | } 23 | public static void main(String arg[]) 24 | { 25 | inter obj = new EX2(); 26 | obj.method1(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /15Day15/Interface/EX3.java: -------------------------------------------------------------------------------- 1 | // class implements interface where an interface extends another interface 2 | 3 | interface inter{ 4 | public void method1(); 5 | } 6 | interface Interface extends inter { 7 | public void method2(); 8 | } 9 | public class EX3 implements Interface{ 10 | // Even though this class is only implementing the 11 | // interface (Interface), it has to implement all the methods 12 | // of inter as well because the interface (Interface) extends inter 13 | 14 | public void method1(){ 15 | System.out.println("method1"); 16 | } 17 | public void method2(){ 18 | System.out.println("method2"); 19 | } 20 | public static void main(String args[]){ 21 | Interface obj = new EX3(); 22 | obj.method2(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /15Day15/Interface/EX4.java: -------------------------------------------------------------------------------- 1 | // A class cannot implement two interfaces that have methods with same name but different return type. 2 | interface A 3 | { 4 | public void aaa(); 5 | } 6 | interface B 7 | { 8 | public int aaa(); 9 | } 10 | 11 | class EX4 implements A,B 12 | { 13 | 14 | public void aaa() // error 15 | { 16 | } 17 | public int aaa() // error 18 | { 19 | } 20 | public static void main(String args[]) 21 | { 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /15Day15/Interface/EX5.java: -------------------------------------------------------------------------------- 1 | // Variable names conflicts can be resolved by interface name. 2 | 3 | interface A 4 | { 5 | int x=10; 6 | } 7 | interface B 8 | { 9 | int x=100; 10 | } 11 | class EX5 implements A,B 12 | { 13 | public static void Main(String args[]) 14 | { 15 | // reference to x is ambiguous both variables are x 16 | // so we are using interface name to resolve the 17 | // variable 18 | 19 | System.out.println(x); 20 | System.out.println(A.x); 21 | System.out.println(B.x); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /15Day15/Task/Task.txt: -------------------------------------------------------------------------------- 1 | go through the files and understand the functioning of interface in java 2 | -------------------------------------------------------------------------------- /16Day16/SOLUTION/Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | interface Vehicle { 4 | 5 | // all are the abstract methods. 6 | void changeGear(int a); 7 | void speedUp(int a); 8 | void applyBrakes(int a); 9 | } 10 | class Bicycle implements Vehicle{ 11 | 12 | int speed; 13 | int gear; 14 | 15 | // to change gear 16 | @Override 17 | public void changeGear(int newGear){ 18 | 19 | gear = newGear; 20 | } 21 | 22 | // to increase speed 23 | @Override 24 | public void speedUp(int increment){ 25 | 26 | speed = speed + increment; 27 | } 28 | 29 | // to decrease speed 30 | @Override 31 | public void applyBrakes(int decrement){ 32 | 33 | speed = speed - decrement; 34 | } 35 | 36 | public void printStates() { 37 | System.out.println("speed: " + speed 38 | + " gear: " + gear); 39 | } 40 | } 41 | class Bike implements Vehicle { 42 | 43 | int speed; 44 | int gear; 45 | 46 | // to change gear 47 | @Override 48 | public void changeGear(int newGear){ 49 | 50 | gear = newGear; 51 | } 52 | 53 | // to increase speed 54 | @Override 55 | public void speedUp(int increment){ 56 | 57 | speed = speed + increment; 58 | } 59 | 60 | // to decrease speed 61 | @Override 62 | public void applyBrakes(int decrement){ 63 | 64 | speed = speed - decrement; 65 | } 66 | 67 | public void printStates() { 68 | System.out.println("speed: " + speed 69 | + " gear: " + gear); 70 | } 71 | 72 | } 73 | class GFG { 74 | 75 | public static void main (String[] args) { 76 | 77 | // creating an inatance of Bicycle 78 | // doing some operations 79 | Bicycle bicycle = new Bicycle(); 80 | bicycle.changeGear(2); 81 | bicycle.speedUp(3); 82 | bicycle.applyBrakes(1); 83 | 84 | System.out.println("Bicycle present state :"); 85 | bicycle.printStates(); 86 | 87 | // creating instance of the bike. 88 | Bike bike = new Bike(); 89 | bike.changeGear(1); 90 | bike.speedUp(4); 91 | bike.applyBrakes(3); 92 | 93 | System.out.println("Bike present state :"); 94 | bike.printStates(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /16Day16/SOLUTION/text.txt: -------------------------------------------------------------------------------- 1 | push your code solution here in this folder 2 | -------------------------------------------------------------------------------- /16Day16/TASK/task.txt: -------------------------------------------------------------------------------- 1 | todays task would be tp use interface in your java program and write a program using interface 2 | -------------------------------------------------------------------------------- /17Day17/SOLUTION/Radix-sort.java: -------------------------------------------------------------------------------- 1 | // Radix sort Java implementation 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class Radix { 6 | 7 | // A utility function to get maximum value in arr[] 8 | static int getMax(int arr[], int n) 9 | { 10 | int mx = arr[0]; 11 | for (int i = 1; i < n; i++) 12 | if (arr[i] > mx) 13 | mx = arr[i]; 14 | return mx; 15 | } 16 | // A function to do counting sort of arr[] according to 17 | // the digit represented by exp. 18 | static void countSort(int arr[], int n, int exp) 19 | { 20 | int output[] = new int[n]; // output array 21 | int i; 22 | int count[] = new int[10]; 23 | Arrays.fill(count,0); 24 | 25 | // Store count of occurrences in count[] 26 | for (i = 0; i < n; i++) 27 | count[ (arr[i]/exp)%10 ]++; 28 | 29 | // Change count[i] so that count[i] now contains 30 | // actual position of this digit in output[] 31 | for (i = 1; i < 10; i++) 32 | count[i] += count[i - 1]; 33 | // Build the output array 34 | for (i = n - 1; i >= 0; i--) 35 | { 36 | output[count[ (arr[i]/exp)%10 ] - 1] = arr[i]; 37 | count[ (arr[i]/exp)%10 ]--; 38 | } 39 | 40 | // Copy the output array to arr[], so that arr[] now 41 | // contains sorted numbers according to curent digit 42 | for (i = 0; i < n; i++) 43 | arr[i] = output[i]; 44 | } 45 | // The main function to that sorts arr[] of size n using 46 | // Radix Sort 47 | static void radixsort(int arr[], int n) 48 | { 49 | // Find the maximum number to know number of digits 50 | int m = getMax(arr, n); 51 | 52 | // Do counting sort for every digit. Note that instead 53 | // of passing digit number, exp is passed. exp is 10^i 54 | // where i is current digit number 55 | for (int exp = 1; m/exp > 0; exp *= 10) 56 | countSort(arr, n, exp); 57 | } 58 | // A utility function to print an array 59 | static void print(int arr[], int n) 60 | { 61 | for (int i=0; i b) ? a : b;// remember we did this on the first week this a normal if else statement so 7 | // don't get confused 8 | } 9 | 10 | // Returns the maximum value that can 11 | // be put in a knapsack of capacity W 12 | static int knapSack(int W, int wt[], int val[], int n) { 13 | // Base Case 14 | if (n == 0 || W == 0)// we will be 15 | return 0; 16 | 17 | // If weight of the nth item is more 18 | // than Knapsack capacity W, then 19 | // this item cannot be included in the optimal solution 20 | if (wt[n - 1] > W) 21 | return knapSack(W, wt, val, n - 1); 22 | 23 | // Return the maximum of two cases: 24 | // (1) nth item included 25 | // (2) not included 26 | else 27 | return max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1), knapSack(W, wt, val, n - 1)); 28 | } 29 | 30 | public static void main(String[] args) { 31 | int val[] = new int[] { 160, 18, 130 }; 32 | int wt[] = new int[] { 13, 20, 36 }; 33 | int W = 50; 34 | int n = val.length; 35 | System.out.println(knapSack(W, wt, val, n)); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /21Day21/SOL/Solution.txt: -------------------------------------------------------------------------------- 1 | Advantages of trees 2 | Trees reflect structural relationships in the data. Trees are used to represent hierarchies. Trees provide an efficient insertion and searching. Trees are very flexible data, allowing to move subtrees around with minumum effort. 3 | 4 | By using graphs you can create any social network site. As persons represent to vertices while connection between them is edge. Graphs are easy to implement by using list or array. You have this flexibility to choose represention according to your requirement. 5 | -------------------------------------------------------------------------------- /21Day21/SOL/text.txt: -------------------------------------------------------------------------------- 1 | if you have any doubts in previous tasks or understanding any code please create an issue on git hub i will definetly revert back to you with in 6 hours 2 | and upload your todas task here 3 | -------------------------------------------------------------------------------- /21Day21/TASK/Task.txt: -------------------------------------------------------------------------------- 1 | you might be wondering why trees and graphs but if you see these are more oftenly used while solving problem statements in the competiontion at google level 2 | so your task would be go through them understand there basic functioning and we will countinue to understand and learn how to utilize them more efficiently 3 | 4 | 1) after going through todays code upload a paragraph describing the benifits about trees and graph utility in the sol forlder 5 | 6 | -------------------------------------------------------------------------------- /21Day21/Tree-graphs/Tree.java: -------------------------------------------------------------------------------- 1 | package DAY21; 2 | 3 | import java.util.Arrays; 4 | import java.util.SortedSet; 5 | import java.util.TreeSet; 6 | 7 | // An Example of TreeSet 8 | // As we know, the property of Set implementation ensures that the tree shall not contain any duplicates 9 | // when storing the data element in a tree. In contrast to other set implementations, 10 | // the TreeSet guarantees that the data elements stored will be sorted by default according to the natural 11 | // ordering of the elements. Let's have a glimpse of its usage with the help of a simple example. 12 | 13 | public class Tree { 14 | public static void main(String[] args) { 15 | Integer[] nums = { 2, 4, 1, 6, 3, 7, 9, 5 }; 16 | SortedSet tree = new TreeSet<>(Arrays.asList(nums)); 17 | 18 | // Print first and last element 19 | System.out.println(tree.first()); 20 | System.out.println(tree.last()); 21 | 22 | printAll(tree); 23 | // False. Set does not allow duplicates, 24 | // so this will not be added. 25 | System.out.println(tree.add(1)); 26 | 27 | // But, this will be added because 11 is not a duplicate 28 | System.out.println(tree.add(11)); 29 | printAll(tree); 30 | 31 | printAll(tree.headSet(7)); 32 | } 33 | 34 | public static void printAll(SortedSet tree) { 35 | for (int s : tree) { 36 | System.out.println(s); 37 | } 38 | System.out.println(); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /21Day21/Tree-graphs/Tree2.java: -------------------------------------------------------------------------------- 1 | package DAY21; 2 | 3 | import java.util.Map; 4 | import java.util.TreeMap; 5 | 6 | // An Example of TreeMap 7 | // A key, value pair class much like another Map implementation, called a HashMap. 8 | // Apart from storing its data elements in a red-black tree structure, TreeMap maintains an order in the keys stored. 9 | // Therefore, when we iterate over the keys, we find that they are naturally ordered. Let us see with the help of a simple example. 10 | 11 | public class Tree2 { 12 | public static void main(String[] args) { 13 | TreeMap treeMap = new TreeMap<>(); 14 | 15 | treeMap.put("Paradise Lost", 23.56); 16 | treeMap.put("Golden Treasury", 12.47); 17 | treeMap.put("Moon and the Sixpence", 65.28); 18 | treeMap.put("Holinshed", 7.68); 19 | treeMap.put("Ancient Mariner", 45.36); 20 | 21 | printAll(treeMap); 22 | 23 | // Keys cannot be duplicates. This will not be stored. 24 | treeMap.put("Paradise Lost", 23.56); 25 | printAll(treeMap); 26 | 27 | // Values may be duplicates. This will be stored. 28 | treeMap.put("Paradise Regained", 23.56); 29 | printAll(treeMap); 30 | } 31 | 32 | public static void printAll(TreeMap treeMap) { 33 | for (Map.Entry et : treeMap.entrySet()) { 34 | System.out.println(et.getKey() + ": " + et.getValue()); 35 | } 36 | System.out.println(); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /22Day22/N-QUEENS/info.txt: -------------------------------------------------------------------------------- 1 | This problem is to find an arrangement of N queens on a chess board, such that no queen can attack any other queens on the board. 2 | 3 | The chess queens can attack in any direction as horizontal, vertical, horizontal and diagonal way. 4 | 5 | A binary matrix is used to display the positions of N Queens, where no queens can attack other queens. 6 | 7 | Input and Output 8 | Input: 9 | The size of a chess board. Generally, it is 8. as (8 x 8 is the size of a normal chess board.) 10 | Output: 11 | The matrix that represents in which row and column the N Queens can be placed. 12 | If the solution does not exist, it will return false. 13 | 14 | 1 0 0 0 0 0 0 0 15 | 0 0 0 0 0 0 1 0 16 | 0 0 0 0 1 0 0 0 17 | 0 0 0 0 0 0 0 1 18 | 0 1 0 0 0 0 0 0 19 | 0 0 0 1 0 0 0 0 20 | 0 0 0 0 0 1 0 0 21 | 0 0 1 0 0 0 0 0 22 | 23 | In this output, the value 1 indicates the correct place for the queens. 24 | The 0 denotes the blank spaces on the chess board. 25 | Algorithm 26 | isValid(board, row, col) 27 | 28 | Input: The chess board, row and the column of the board. 29 | Output: True when placing a queen in row and place position is a valid or not. 30 | 31 | Begin 32 | if there is a queen at the left of current col, then 33 | return false 34 | if there is a queen at the left upper diagonal, then 35 | return false 36 | if there is a queen at the left lower diagonal, then 37 | return false; 38 | return true //otherwise it is valid place 39 | End 40 | solveNQueen(board, col) 41 | 42 | Input: The chess board, the col where the queen is trying to be placed. 43 | 44 | Output: The position matrix where queens are placed. 45 | 46 | Begin 47 | if all columns are filled, then 48 | return true 49 | for each row of the board, do 50 | if isValid(board, i, col), then 51 | set queen at place (i, col) in the board 52 | if solveNQueen(board, col+1) = true, then 53 | return true 54 | otherwise remove queen from place (i, col) from board. 55 | done 56 | return false 57 | End 58 | -------------------------------------------------------------------------------- /22Day22/SOL/text.txt: -------------------------------------------------------------------------------- 1 | upload your code here i will be uploading the solution here genrate and issue if you face any problem 2 | -------------------------------------------------------------------------------- /22Day22/TASK/task.txt: -------------------------------------------------------------------------------- 1 | The algo and theory is provided you all have to write n queen code i will be uploading the code tommorow so you all have time to understand and practice n queens it will inhance you thinking skills 2 | -------------------------------------------------------------------------------- /23DAY23/N-QUEENS/text.txt: -------------------------------------------------------------------------------- 1 | i thught befor going on we all should be on the same page and n queens problem makes your java fundementals clear so for that as many of you were facing problem in that here is a code from geeks for geeks 2 | go through it try to understand the work flow tommorow i will upload a detailed source code that will make you understanding about n queens algorith more easy 3 | -------------------------------------------------------------------------------- /23DAY23/SOL/text.txt: -------------------------------------------------------------------------------- 1 | Please utilise this folder to submit all you solutions to the problems given before 2 | -------------------------------------------------------------------------------- /23DAY23/TASK/Task.txt: -------------------------------------------------------------------------------- 1 | Todays task will be just to go through the source code that i picked from geeks for geeks so that before i upload my detailed code on nqueens you all should be on the same page to understand what actually is happenig here you all can also refer the text that i shared on DAY22 2 | -------------------------------------------------------------------------------- /24Day24/SOL/8-Queens.java: -------------------------------------------------------------------------------- 1 | // This program solves the classic "8 queens" problem using recursive 2 | // backtracking. 3 | 4 | import java.util.*; 5 | public class Queens { 6 | public static void main(String[] args) { 7 | giveIntro(); 8 | Scanner console = new Scanner(System.in); 9 | System.out.print("What size board do you want to use? "); 10 | int size = console.nextInt(); 11 | System.out.println(); 12 | Board b = new Board(size); 13 | solve(b); 14 | } 15 | // post: explains program to user 16 | public static void giveIntro() { 17 | System.out.println("This program solves the classic '8 queens'"); 18 | System.out.println("problem, placing queens on a chessboard so"); 19 | System.out.println("that no two queens threaten each other."); 20 | System.out.println(); 21 | } 22 | // pre : queens have been safely placed in columns 1 through (col - 1) 23 | // post: recursively searchs the board for a solution starting at col, 24 | // returning true iff such a solution occurs and storing the 25 | // solution in board if it exists 26 | public static boolean explore(Board b, int col) { 27 | if (col > b.size()) 28 | return true; 29 | else { 30 | for (int row = 1; row <= b.size(); row++) 31 | if (b.safe(row, col)) { 32 | b.place(row, col); 33 | for (int i = 1; i < col; i++) 34 | System.out.print(" "); 35 | System.out.println("Q in row " + row + " of col " + col); 36 | if (explore(b, col + 1)) 37 | return true; 38 | b.remove(row, col); 39 | } 40 | return false; 41 | } 42 | } 43 | 44 | // post: searches for a solution to the 8 queens problem with this 45 | // board, reporting result. 46 | public static void solve(Board solution) { 47 | if (!explore(solution, 1)) 48 | System.out.println("No solution."); 49 | else { 50 | System.out.println("One solution is as follows:"); 51 | solution.print(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /24Day24/SOL/text.txt: -------------------------------------------------------------------------------- 1 | upload your solution here 2 | 3 | 4 | -------------------------------------------------------------------------------- /24Day24/TASk/task.txt: -------------------------------------------------------------------------------- 1 | I tried to make it as simple as i could and i hope that you will try to write the solution to the eight queens problem on your own and push that in sol folder 2 | -------------------------------------------------------------------------------- /24Day24/src/Board.java: -------------------------------------------------------------------------------- 1 | package algos; 2 | 3 | public class Board { 4 | public static final char BOARD_CHAR = '.'; 5 | public static final char QUEEN_CHAR = 'X'; 6 | public char[][] board; 7 | 8 | public Board() { 9 | board = new char[8][8]; 10 | } 11 | 12 | public void displayBoard() {// function to display the board 13 | for (int i = 0; i < 8; i++) { 14 | for (int c = 0; c < 8; c++) { 15 | System.out.print(board[i][c] + " "); 16 | } 17 | System.out.println(); 18 | System.out.println(); 19 | } 20 | } 21 | 22 | public void fillboard()// fill the empty board with dots so that we can keep a check on queens that we 23 | // are placing 24 | { 25 | for (int i = 0; i < 8; i++) { 26 | for (int c = 0; c < 8; c++) { 27 | board[i][c] = BOARD_CHAR; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /24Day24/src/EightQueens.java: -------------------------------------------------------------------------------- 1 | package algos; 2 | 3 | public class EightQueens { 4 | public static void main(String[] args) { 5 | Algo run = new Algo(); 6 | run.PlaceQueens(); 7 | } 8 | 9 | } 10 | 11 | -------------------------------------------------------------------------------- /25Day25/Eight-Queens/Board.java: -------------------------------------------------------------------------------- 1 | package algos; 2 | 3 | public class Board { 4 | public static final char BOARD_CHAR = '.'; 5 | public static final char QUEEN_CHAR = 'X'; 6 | public char[][] board; 7 | 8 | public Board() { 9 | board = new char[8][8]; 10 | } 11 | 12 | public void displayBoard() {// function to display the board 13 | for (int i = 0; i < 8; i++) { 14 | for (int c = 0; c < 8; c++) { 15 | System.out.print(board[i][c] + " "); 16 | } 17 | System.out.println(); 18 | System.out.println(); 19 | } 20 | } 21 | 22 | public void fillboard()// fill the empty board with dots so that we can keep a check on queens that we 23 | // are placing 24 | { 25 | for (int i = 0; i < 8; i++) { 26 | for (int c = 0; c < 8; c++) { 27 | board[i][c] = BOARD_CHAR; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /25Day25/Eight-Queens/EightQueens.java: -------------------------------------------------------------------------------- 1 | package algos; 2 | 3 | public class EightQueens { 4 | public static void main(String[] args) { 5 | Algo run = new Algo(); 6 | run.PlaceQueens(); 7 | } 8 | 9 | } 10 | 11 | -------------------------------------------------------------------------------- /25Day25/Factorial/Factorial.java: -------------------------------------------------------------------------------- 1 | package factorial; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Factorial { 6 | 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | System.out.print("Please enter the number whoes factorial you want to calculate "); 10 | int n = Integer.parseInt(scanner.nextLine()); 11 | scanner.close(); 12 | System.out.println("The factorial of given number by Method1 is: " + n + " is " + Method1(n)); 13 | System.out.println("The factorial of given number by Method2 is: " + n + " is " + Method2(n)); 14 | System.out.println("The factorial of given number by Recursion is: " + n + " is " + Byrecursion(n)); 15 | } 16 | 17 | private static int Byrecursion(int n) {// This method is used to find the factorial by the help of recursion 18 | if (n == 1) 19 | return 1; 20 | 21 | return n * Byrecursion(n - 1); 22 | } 23 | 24 | private static int Method2(int n) {// Method one to find a factorial of the given number 25 | int factorial = n; 26 | for (int i = 1; i < n; i++) { 27 | factorial *= i; 28 | } 29 | 30 | return factorial; 31 | } 32 | 33 | private static int Method1(int n) {// Method two find the factorial of the givaen number 34 | int factorial = 1; 35 | for (int i = n; i >= 1; i--) { 36 | factorial *= i; 37 | } 38 | 39 | return factorial; 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /25Day25/Fibonacci/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package fibonacii; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Fibonacci { 6 | 7 | public static void main(String[] args) { 8 | System.out.println("Fibonacci by loops"); 9 | System.out.println("Enter two number to make a sequence"); 10 | Scanner in = new Scanner(System.in); 11 | int number1 = in.nextInt(); 12 | int number2 = in.nextInt(); 13 | 14 | System.out.println(number1); 15 | System.out.println(number2); 16 | 17 | for (int i = 0; i <= 15; i++) { 18 | int next = number1 + number2; 19 | number1 = number2; 20 | number2 = next; 21 | 22 | System.out.println(next); 23 | } 24 | 25 | System.out.println(""); 26 | System.out.println("Fibonacci recursive solution with return value"); 27 | 28 | System.out.println(0); 29 | for (int i = 0; i <= 15; i++) { 30 | System.out.println(RecursiveSolution(i)); 31 | } 32 | } 33 | 34 | private static int RecursiveSolution(int n) {// method for recursive solution 35 | if (n <= 1) 36 | return 1; 37 | 38 | return RecursiveSolution(n - 2) + RecursiveSolution(n - 1); 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /25Day25/FizzBuzz/Fizzbuzz.java: -------------------------------------------------------------------------------- 1 | package fizzbuzz; 2 | 3 | public class Fizzbuzz { 4 | public static void main(String[] args) { 5 | System.out.println("For Loop solution"); 6 | 7 | String text; 8 | for (int i = 100; i >= 1; i--) { 9 | if (i % 15 == 0) 10 | text = "FizzBuzz"; 11 | else if (i % 3 == 0) 12 | text = "Fizz"; 13 | else if (i % 5 == 0) 14 | text = "Buzz"; 15 | else 16 | text = Integer.toString(i); 17 | 18 | System.out.println(text); 19 | } 20 | 21 | System.out.println(""); 22 | System.out.println("Recursive solution"); 23 | RecursiveSolution(1); 24 | } 25 | 26 | private static void RecursiveSolution(int n) { 27 | String text = ""; 28 | if (n > 100) 29 | return; 30 | 31 | if (n % 15 == 0) 32 | text = "FizzBuzz"; 33 | else if (n % 3 == 0) 34 | text = "Fizz"; 35 | else if (n % 5 == 0) 36 | text = "Buzz"; 37 | else 38 | text = Integer.toString(n); 39 | 40 | System.out.println(text); 41 | RecursiveSolution(n + 1); 42 | } 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /25Day25/Knight-Tour/Board.java: -------------------------------------------------------------------------------- 1 | package knighttour; 2 | 3 | public class Board { 4 | public static final int SIZE = 8; 5 | public static final char BOARD_SYMBOL = '.'; 6 | public static final char MOVE_SYMBOL = 'X'; 7 | private int[] horizontal; 8 | private int[] vertical; 9 | public static char[][] chessBoard; 10 | 11 | public Board() { 12 | horizontal = new int[] { 2, 1, -1, -2, -2, -1, 1, 2 }; 13 | vertical = new int[] { -1, -2, -2, -1, 1, 2, 2, 1 }; 14 | chessBoard = new char[SIZE][SIZE]; 15 | } 16 | 17 | public int getHorizontal(int i) { 18 | return this.horizontal[i]; 19 | } 20 | 21 | public int getVertical(int i) { 22 | return vertical[i]; 23 | } 24 | 25 | public void DisplayBoard() { 26 | System.out.print(" "); 27 | for (int i = 65; i < SIZE + 65; i++) 28 | System.out.print((char) i); 29 | 30 | System.out.println(""); 31 | 32 | for (int i = 0; i < SIZE; i++) { 33 | System.out.print(i + 1 + " "); 34 | for (int j = 0; j < SIZE; j++) { 35 | System.out.print(chessBoard[i][j]); 36 | } 37 | System.out.println(""); 38 | } 39 | } 40 | 41 | public void FillBoard(int curH, int curV) { 42 | for (int i = 0; i < SIZE; i++) { 43 | for (int j = 0; j < SIZE; j++) { 44 | chessBoard[i][j] = BOARD_SYMBOL; 45 | } 46 | } 47 | chessBoard[curH][curV] = MOVE_SYMBOL; 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /25Day25/Knight-Tour/Knight.java: -------------------------------------------------------------------------------- 1 | package knighttour; 2 | 3 | import java.util.Random; 4 | 5 | public class Knight { 6 | private static Random rand = new Random(); 7 | private int curH; 8 | private int curV; 9 | private Board board; 10 | private boolean isFullTour = false; 11 | private boolean isEndTour = false; 12 | private static int bestScore = 0; // stores the most number of moves made in one tour so far 13 | private static int tourCount = 0; // counts all incomplete tours 14 | private int moveCounter; 15 | 16 | public Knight() { 17 | board = new Board(); 18 | } 19 | 20 | private void getTourResults() { 21 | isEndTour = true; 22 | if (moveCounter == Board.SIZE * Board.SIZE) { 23 | isFullTour = true; 24 | board.DisplayBoard(); 25 | System.out.println( 26 | "SUCCESS. The Tour #" + tourCount + " was a complete tour!\n" + "The best socre is: " + bestScore); 27 | return; 28 | } 29 | 30 | if (moveCounter == bestScore) { 31 | board.DisplayBoard(); 32 | System.out.println("No success. The best score is still " + bestScore 33 | + " moves reached again on this Tour #" + tourCount + "\n"); 34 | } else if (moveCounter > bestScore) { 35 | bestScore = moveCounter; 36 | board.DisplayBoard(); 37 | System.out.println( 38 | "No success, but getting closer! " + bestScore + " moves reached on Tour #" + tourCount + "\n"); 39 | } 40 | } 41 | 42 | public void move() { 43 | while (!isFullTour) { 44 | curH = rand.nextInt(Board.SIZE); 45 | curV = rand.nextInt(Board.SIZE); 46 | moveCounter = 1; 47 | board.FillBoard(curH, curV); 48 | isEndTour = false; 49 | 50 | do { 51 | if (!isMoving()) 52 | getTourResults(); 53 | } while (!isEndTour); 54 | } 55 | } 56 | 57 | public boolean isMoving() { 58 | int[] movesTaken = { 0, 0, 0, 0, 0, 0, 0, 0 }; 59 | boolean allMovesUsed = false; 60 | 61 | while (!allMovesUsed) { 62 | int i = rand.nextInt(8); 63 | if (movesTaken[i] == 0) { 64 | int hMove = curH + board.getHorizontal(i); 65 | int vMove = curV + board.getVertical(i); 66 | if (hMove >= 0 && hMove < Board.SIZE && vMove >= 0 && vMove < Board.SIZE 67 | && Board.chessBoard[hMove][vMove] == Board.BOARD_SYMBOL) { 68 | curH = hMove; 69 | curV = vMove; 70 | Board.chessBoard[curH][curV] = Board.MOVE_SYMBOL; 71 | moveCounter++; 72 | return true; 73 | } 74 | 75 | movesTaken[i] = 1; 76 | } 77 | 78 | int c = 0; 79 | for (int moves : movesTaken) { 80 | c += moves; 81 | } 82 | 83 | if (c == 8) 84 | allMovesUsed = true; 85 | } 86 | 87 | tourCount++; 88 | return false; 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /25Day25/Knight-Tour/KnightTour.java: -------------------------------------------------------------------------------- 1 | package knighttour; 2 | 3 | public class KnightTour { 4 | public static void main(String[] args) { 5 | Knight knight = new Knight(); 6 | knight.move(); 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /25Day25/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Todays task wolud be to analize these algorithms and try to understand whats happening in them 2 | -------------------------------------------------------------------------------- /26Day26/Problems/Problem1.txt: -------------------------------------------------------------------------------- 1 | You are conducting a contest at your college. This contest consists of two problems and n participants. You know the problem that a candidate will solve during the contest. 2 | 3 | You provide a balloon to a participant after he or she solves a problem. There are only green and purple-colored balloons available in a market. Each problem must have a balloon associated with it as a prize 4 | for solving that specific problem. You can distribute balloons to each participant by performing the following operation: 5 | 6 | 1) Use green-colored balloons for the first problem and purple-colored balloons for the second problem 7 | 2) Use purple-colored balloons for the first problem and green-colored balloons for the second problem 8 | You are given the cost of each balloon and problems that each participant solve. Your task is to print the minimum price that you have to pay while purchasing balloons. 9 | -------------------------------------------------------------------------------- /26Day26/Problems/Problem2.txt: -------------------------------------------------------------------------------- 1 | Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. 2 | Minimum dimension of the picture can be L x L, where L is the length of the side of square. 3 | 4 | Now Roy has N photos of various dimensions. 5 | Dimension of a photo is denoted as W x H 6 | where W - width of the photo and H - Height of the photo 7 | 8 | When any photo is uploaded following events may occur: 9 | 10 | [1] If any of the width or height is less than L, user is prompted to upload another one. Print "UPLOAD ANOTHER" in this case. 11 | [2] If width and height, both are large enough and 12 | (a) if the photo is already square then it is accepted. Print "ACCEPTED" in this case. 13 | (b) else user is prompted to crop it. Print "CROP IT" in this case. 14 | 15 | (quotes are only for clarification) 16 | 17 | Given L, N, W and H as input, print appropriate text as output. 18 | -------------------------------------------------------------------------------- /26Day26/Problems/Problem3.txt: -------------------------------------------------------------------------------- 1 | You are required to enter a word that consists of x and y that denote the number of Zs and Os respectively. The input word is considered similar to word zoo if 2 * x = y. 2 | 3 | Determine if the entered word is similar to word zoo. 4 | 5 | For example, words such as zzoooo and zzzoooooo are similar to word zoo but not the words such as zzooo and zzzooooo. 6 | -------------------------------------------------------------------------------- /26Day26/Problems/Problem4.txt: -------------------------------------------------------------------------------- 1 | You are given a rectangle of length a and width b. You are required to determine a circle that contains the maximum circumference that fits inside the rectangle. This type of circle is called a big circle. Your task is to determine the maximum number of big circles that can fit inside the rectangle. 2 | -------------------------------------------------------------------------------- /26Day26/Problems/Problem5.txt: -------------------------------------------------------------------------------- 1 | Gary likes to solve problems of array, but he doesn't like problems that involve queries. His school teacher gave him an assignment full of problems but one of them involve queries. Can you help Gary in solving that problem so that he can go and play with his friends? The problem is : 2 | 3 | Given an Array A consisting of N positive integers, you have to answer Q queries on it. Queries can be of the two types: * 1 X Y - Update X at location Y in the array. * 2 L R - Print the sum of range [L, R], i.e. Both L and R are inclusive. 4 | 5 | Note:- Array indexing is from 0. 6 | Input: 7 | 8 | The first line contains two space separated integers N(Length of Array) and Q(Queries). 9 | Next Line contains N space separated integers denoting array A. 10 | Next Q Line contains 3 space separated integers for each line, as described above 11 | -------------------------------------------------------------------------------- /26Day26/SOL/SOL.txt: -------------------------------------------------------------------------------- 1 | upload your solutions here 2 | 3 | -------------------------------------------------------------------------------- /27Day27/Regex/Regex1.java: -------------------------------------------------------------------------------- 1 | package DAY10; 2 | 3 | import java.util.regex.Pattern; 4 | import java.util.Scanner; 5 | import java.util.regex.Matcher; 6 | 7 | public class Regex1 { 8 | public static void main(String[] args) { 9 | Scanner sc = new Scanner(System.in); 10 | while (true) { 11 | System.out.println("Enter regex pattern:"); 12 | Pattern pattern = Pattern.compile(sc.nextLine()); 13 | System.out.println("Enter text:"); 14 | Matcher matcher = pattern.matcher(sc.nextLine()); 15 | boolean found = false; 16 | while (matcher.find()) { 17 | System.out.println("I found the text " + matcher.group() + " starting at index " + matcher.start() 18 | + " and ending at index " + matcher.end()); 19 | found = true; 20 | } 21 | if (!found) { 22 | System.out.println("No match found."); 23 | } 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /27Day27/Regex/Regex2.java: -------------------------------------------------------------------------------- 1 | /*Create a regular expression that accepts 10 digit numeric characters 2 | starting with 7, 8 or 9 only.*/ 3 | package DAY10; 4 | 5 | import java.util.regex.*; 6 | 7 | public class Regex2 { 8 | public static void main(String args[]) { 9 | System.out.println("by character classes and quantifiers ..."); 10 | System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));// true 11 | System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));// true 12 | 13 | System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));// false (11 characters) 14 | System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));// false (starts from 6) 15 | System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));// true 16 | 17 | System.out.println("by metacharacters ..."); 18 | System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));// true 19 | System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));// false (starts from 3) 20 | 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /27Day27/Regex/Regex3.java: -------------------------------------------------------------------------------- 1 | /*Create a regular expression that accepts alphanumeric characters only. 2 | Its length must be six characters long only.*/ 3 | 4 | package DAY10; 5 | 6 | import java.util.regex.*; 7 | 8 | public class Regex3 { 9 | public static void main(String args[]) { 10 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));// true 11 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));// false (more than 6 char) 12 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));// true 13 | System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));// false ($ is not matched) 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /27Day27/Regex/check.java: -------------------------------------------------------------------------------- 1 | package pattern; 2 | 3 | import java.util.Scanner; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | public class check { 8 | 9 | public static void main(String[] args) { 10 | 11 | String regex = "\\b(\\w+)(?:\\W+\\1\\b)+"; 12 | Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 13 | 14 | Scanner in = new Scanner(System.in); 15 | int numSentences = Integer.parseInt(in.nextLine()); 16 | 17 | while (numSentences-- > 0) { 18 | String input = in.nextLine(); 19 | 20 | Matcher m = p.matcher(input); 21 | 22 | // Check for subsequences of input that match the compiled pattern 23 | while (m.find()) { 24 | input = input.replaceAll(m.group(), m.group(1)); 25 | } 26 | 27 | // Prints the modified sentence. 28 | System.out.println(input); 29 | } 30 | 31 | in.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /27Day27/Regex/regex.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.Matcher; 2 | import java.util.regex.Pattern; 3 | import java.util.Scanner; 4 | 5 | class Solution{ 6 | 7 | public static void main(String[] args){ 8 | Scanner in = new Scanner(System.in); 9 | while(in.hasNext()){ 10 | String IP = in.next(); 11 | System.out.println(IP.matches(new MyRegex().pattern)); 12 | } 13 | 14 | } 15 | } 16 | 17 | class MyRegex{ 18 | String num ="([01]?\\d{1,2}|2[0-4]\\d|25[0-5])"; 19 | String pattern=num+"."+num+"."+num+"."+num; 20 | } 21 | -------------------------------------------------------------------------------- /27Day27/SOl/sol.txt: -------------------------------------------------------------------------------- 1 | upload your prvios tasks here 2 | -------------------------------------------------------------------------------- /27Day27/Task/Task.txt: -------------------------------------------------------------------------------- 1 | These last few days have been hard so lets go slow today we almost finished pretty much everything 2 | 1) go through the given examples and understand what is regex 3 | 2) it helps you to manipulate strings and patterns hint from me 4 | -------------------------------------------------------------------------------- /28Day28/JFrame/Button/Button.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | 5 | public class Button { 6 | public static void main(String[] args) { 7 | JFrame f = new JFrame("Button Example");// creating instance of JFrame 8 | 9 | JButton b = new JButton("Button");// creating instance of JButton 10 | b.setBounds(200, 200, 100, 100);// x axis, y axis, width, height 11 | 12 | f.add(b);// adding button in JFrame 13 | 14 | f.setSize(500, 500);// 400 width and 500 height 15 | f.setLayout(null);// using no layout managers 16 | f.setVisible(true);// making the frame visible 17 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /28Day28/JFrame/Button/Buttonphoto.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | 5 | public class Buttonphoto { 6 | public static void main(String[] args) { 7 | Buttonqw();// called button method 8 | } 9 | 10 | static void Buttonqw() { 11 | JFrame f = new JFrame("Button Example");// creates frame instance 12 | JButton b = new JButton(new ImageIcon("C:\\Users\\hp\\Pictures\\Saved Pictures\\ss.jpg"));// path to the pic 13 | b.setBounds(100, 100, 500, 500);// x-axis,y-axis,width,length 14 | f.add(b);// add button in frame 15 | f.setSize(300, 400);// setsize for the frame 16 | f.setLayout(null);// set layout instance 17 | f.setVisible(true);// visible instance for frame 18 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// exits automatically 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /28Day28/JFrame/Label/Label.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | 5 | public class Label { 6 | public static void main(String[] args) { 7 | JFrame f = new JFrame("Label Example");// adding instance to the frame 8 | JLabel l1, l2;// defineing labels 9 | l1 = new JLabel("First Label.");// adding instance to the labels 10 | l1.setBounds(50, 50, 100, 30);// x-axis,y-axis,width,length(label 1) 11 | l2 = new JLabel("Second Label.");// adding instance to label 2 12 | l2.setBounds(50, 100, 100, 20);// x-axis,y-axis,width,length(label 2) 13 | f.add(l1);// adding label in the frame 14 | f.add(l2);// adding label int frame 15 | f.setSize(300, 300);// setting the size of the frame 16 | f.setLayout(null);// null instance declaration 17 | f.setVisible(true);// setting instance to visible 18 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /28Day28/JFrame/Label/Label2.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | package walkthrough; 4 | 5 | import javax.swing.*; 6 | import java.awt.*; 7 | import java.awt.event.*; 8 | 9 | public class Label2 extends Frame implements ActionListener { 10 | JTextField tf;// added text field 11 | JLabel l;// added label 12 | JButton b;// added button 13 | private ActionEvent e;// created an event e 14 | 15 | Label2() {// counstructor for class 16 | JFrame f = new JFrame("IP Finder"): 17 | tf = new JTextField();// new text field instance created 18 | tf.setBounds(50, 50, 150, 20);// postition nad dimension creatred 19 | l = new JLabel();// same another label 20 | l.setBounds(50, 100, 250, 20);// with its axis and dimension 21 | b = new JButton("Find IP");// button instance created 22 | b.setBounds(50, 150, 95, 30);// with its axis and dimension 23 | b.addActionListener(this);// actionlistner class that extended used with the help of this key word 24 | f.add(b);// added button in the frame 25 | f.add(tf);// added the textfield in the frame 26 | f.add(l);// added a label in the frame 27 | f.setSize(400, 400);// defining the size of frame 28 | f.setLayout(null);// setting layout instance 29 | f.setVisible(true);// setting instance to visible 30 | actionPerformed(e);// called the method action performed 31 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 32 | } 33 | 34 | public void actionPerformed(ActionEvent e) { 35 | try { 36 | String host = tf.getText(); 37 | String ip = java.net.InetAddress.getByName(host).getHostAddress(); 38 | l.setText("IP of " + host + " is: " + ip); 39 | } catch (Exception ex) { 40 | System.out.println(ex); 41 | } 42 | } 43 | 44 | public static void main(String[] args) { 45 | Label2 obj = new Label2();// created and object for label2 class 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /28Day28/JFrame/TextArea/Textarea1.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | 5 | public class Textarea1 { 6 | 7 | Textarea1() {// creating counstrucctor 8 | JFrame f = new JFrame("note pad");// adding frame instance 9 | JTextArea area = new JTextArea();// creating the instance for text area 10 | area.setBounds(10, 30, 200, 200);// position and dimension of text area 11 | f.add(area);// adding text area in the frame 12 | f.setSize(300, 300);// setting the frame size 13 | f.setLayout(null);// set layout instance to null 14 | f.setVisible(true);// setting the instance to visible 15 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 | } 17 | 18 | public static void main(String[] args) { 19 | Textarea1 ob = new Textarea1();// called the class instructor through object initiation 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /28Day28/JFrame/TextArea/Textarea2.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | import java.awt.event.*; 5 | 6 | public class Textarea2 implements ActionListener { 7 | JLabel l1, l2; 8 | JTextArea area; 9 | JButton b; 10 | private ActionEvent e; 11 | 12 | Textarea2() { 13 | JFrame f = new JFrame("Character counter");// this instance for frame 14 | l1 = new JLabel();// same 15 | l1.setBounds(50, 25, 100, 30);// same 16 | l2 = new JLabel();// same 17 | l2.setBounds(160, 25, 100, 30);// same 18 | area = new JTextArea();// instance for text area 19 | area.setBounds(20, 75, 250, 200);// setting the position and the size 20 | b = new JButton("Count Words");// createing button instance 21 | b.setBounds(100, 300, 120, 30);// definig the axis and dimension for the wordas 22 | b.addActionListener(this);// using the extended class action listner 23 | f.add(l1);// adding the label in the frame 24 | f.add(l2);// adding the label in the frame 25 | f.add(area);// adding the textarea int the frame 26 | f.add(b);// addint the button 27 | f.setSize(450, 450);// setting the size of frame size 28 | f.setLayout(null);// seting layout instcance to null 29 | f.setVisible(true);// setting the instance to visible 30 | actionPerformed(e);// calling the method action performed 31 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 32 | } 33 | 34 | public void actionPerformed(ActionEvent e) { 35 | String text = area.getText();// get the text in the given text area 36 | String words[] = text.split("\\s");// spliting texts thropugh space tou count number of wordz 37 | l1.setText("Words: " + words.length);// printing out the number of words 38 | l2.setText("Characters: " + text.length());// printing out the number of characters 39 | } 40 | 41 | public static void main(String[] args) { 42 | Textarea2 ob = new Textarea2();// class counstructor calling through objects 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /28Day28/JFrame/TextField/Textfield1.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | 5 | public class Textfiled1 { 6 | public static void main(String[] args) { 7 | JFrame f = new JFrame("TextField Example"); 8 | JTextField t1, t2; 9 | t1 = new JTextField(); 10 | t1.setBounds(50, 100, 200, 30); 11 | t2 = new JTextField(); 12 | t2.setBounds(50, 150, 200, 30); 13 | f.add(t1); 14 | f.add(t2); 15 | f.setSize(400, 400); 16 | f.setLayout(null); 17 | f.setVisible(true); 18 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 | } 20 | } 21 | //here if you see first we created an instance for the frame that we will be using and then created and instance for two text fields and gave them there position and size and then added them in the frame 22 | // then we decided the size for the frame and then set the layout instance to null and visible instance to true 23 | -------------------------------------------------------------------------------- /28Day28/JFrame/TextField/Textfield2.java: -------------------------------------------------------------------------------- 1 | package walkthrough; 2 | 3 | import javax.swing.*; 4 | import java.awt.event.*; 5 | 6 | class abc implements ActionListener { 7 | JTextField tf1, tf2, tf3; 8 | JButton b1, b2; 9 | private ActionEvent e; 10 | 11 | abc() { 12 | JFrame f = new JFrame("calculator"); 13 | tf1 = new JTextField(); 14 | tf1.setBounds(50, 50, 150, 20); 15 | tf2 = new JTextField(); 16 | tf2.setBounds(50, 100, 150, 20); 17 | tf3 = new JTextField(); 18 | tf3.setBounds(50, 150, 150, 20); 19 | tf3.setEditable(false); 20 | b1 = new JButton("+"); 21 | b1.setBounds(50, 200, 50, 50); 22 | b2 = new JButton("-"); 23 | b2.setBounds(120, 200, 50, 50); 24 | b1.addActionListener(this); 25 | b2.addActionListener(this); 26 | f.add(tf1); 27 | f.add(tf2); 28 | f.add(tf3); 29 | f.add(b1); 30 | f.add(b2); 31 | f.setSize(300, 300); 32 | f.setLayout(null); 33 | f.setVisible(true); 34 | actionPerformed(e); 35 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 36 | } 37 | 38 | public void actionPerformed(ActionEvent e) { 39 | String s1 = tf1.getText(); 40 | String s2 = tf2.getText(); 41 | int a = Integer.parseInt(s1); 42 | int b = Integer.parseInt(s2); 43 | int c = 0; 44 | if (e.getSource() == b1) { 45 | c = a + b; 46 | } else if (e.getSource() == b2) { 47 | c = a - b; 48 | } 49 | String result = String.valueOf(c); 50 | tf3.setText(result); 51 | } 52 | 53 | } 54 | 55 | public class Textfiled2 { 56 | public static void main(String[] args) { 57 | abc obj = new abc(); 58 | 59 | } 60 | } 61 | // everthing is same but here in axtensiion to action listner we added and created a method for addition and substraction 62 | -------------------------------------------------------------------------------- /28Day28/SOL/sol.txt: -------------------------------------------------------------------------------- 1 | upload your jframe code here 2 | 3 | -------------------------------------------------------------------------------- /28Day28/Task/Task.txt: -------------------------------------------------------------------------------- 1 | understand the basic functioning of JFrame in java 2 | 3 | write a program for a simple calculator using jframe 4 | -------------------------------------------------------------------------------- /29Day29/SOL/SOL.txt: -------------------------------------------------------------------------------- 1 | upload your previous work here 2 | -------------------------------------------------------------------------------- /29Day29/Servelet/calserv/cal.java: -------------------------------------------------------------------------------- 1 | package calculator; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.ServletContext; 7 | import javax.servlet.annotation.WebServlet; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | public class cal extends HttpServlet { 13 | protected void service(HttpServletRequest req, HttpServletResponse res) throws IOException { 14 | PrintWriter obj=res.getWriter(); 15 | int i=Integer.parseInt(req.getParameter("num1")); 16 | int j=Integer.parseInt(req.getParameter("num2")); 17 | String operation=req.getParameter("op"); 18 | if(operation.equals("Addition")) 19 | { 20 | obj.println(i+j); 21 | } 22 | else if(operation.equals("Subtraction")) 23 | { 24 | obj.println(i-j); 25 | } 26 | else if(operation.equals("Multiplication")) 27 | { 28 | obj.println(i*j); 29 | } 30 | else if(operation.equals("Division")) 31 | { 32 | obj.println(i/j); 33 | } 34 | else { 35 | obj.println("not matching any operation");} 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /29Day29/Servelet/calserv/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 |
9 | Enter first number:
10 | Enter Second number:
11 | 17 | 18 |
19 | 20 | 21 | -------------------------------------------------------------------------------- /29Day29/Servelet/calserv/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | servletlabexperiment 8 | 9 | ques4 10 | calculator.cal 11 | 12 | 13 | ques4 14 | /calculating 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /29Day29/Servelet/q1/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 |
9 | Enter first number:
10 | Enter Second number:
11 | Enter third number:
12 | 13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /29Day29/Servelet/q1/servelet.java: -------------------------------------------------------------------------------- 1 | package demoapp; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.ServletContext; 7 | import javax.servlet.annotation.WebServlet; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | public class servelet extends HttpServlet { 12 | protected void service(HttpServletRequest req,HttpServletResponse res) throws IOException { 13 | PrintWriter obj=res.getWriter(); 14 | String values[]=req.getParameterValues("num1"); //array is used to get multiple values 15 | int addition=0; 16 | for(int i=0;i 2 | 3 | servlet_config_and_context 4 | 5 | servelet 6 | demoapp.servelet 7 | 8 | 9 | servelet 10 | /add 11 | 12 | 13 | -------------------------------------------------------------------------------- /29Day29/Servelet/q2/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 | 9 |
10 | Enter first number :
11 | Enter second number :
12 | 13 | 14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /29Day29/Servelet/q2/multigetandpost.java: -------------------------------------------------------------------------------- 1 | package get_post_method; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.http.HttpServlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | public class multigetandpost extends HttpServlet { 11 | public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException // throwing Input Output 12 | // exception 13 | { 14 | int i = Integer.parseInt(req.getParameter("num1")); // converting string to Int 15 | int j = Integer.parseInt(req.getParameter("num2")); 16 | int k = i * j; 17 | PrintWriter out = res.getWriter(); 18 | out.println("Result is: " + k); 19 | } 20 | 21 | public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException // doGet method 22 | { 23 | int i = Integer.parseInt(req.getParameter("num1")); 24 | int j = Integer.parseInt(req.getParameter("num2")); 25 | int k = i * j; 26 | PrintWriter out = res.getWriter(); 27 | out.println("Result is: " + k); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /29Day29/Servelet/q2/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | newservlet 5 | get_post_method.multigetandpost 6 | 7 | 8 | newservlet 9 | /multiply 10 | 11 | 12 | -------------------------------------------------------------------------------- /29Day29/Servelet/q3/Post.java: -------------------------------------------------------------------------------- 1 | package demo1app; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.GenericServlet; 7 | import javax.servlet.ServletRequest; 8 | import javax.servlet.ServletResponse; 9 | 10 | public class Post extends GenericServlet { 11 | 12 | public void service(ServletRequest req, ServletResponse res) throws IOException { 13 | res.setContentType("text/html"); 14 | PrintWriter obj = res.getWriter(); 15 | java.util.Date date = new java.util.Date(); // used java.util.date header file 16 | obj.println("

" + "Current Date & Time: " + date.toString() + "

"); 17 | obj.close(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /29Day29/Servelet/q3/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | newservelet 5 | 6 | ques2 7 | demo1app.Post 8 | 9 | 10 | ques2 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /29Day29/Servelet/q4/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 |
9 | "Enter first number: "
10 | "Enter second number: "
11 | 12 |
13 | 14 | 15 | ~ 16 | ~ 17 | 18 | -------------------------------------------------------------------------------- /29Day29/Servelet/q4/q3.java: -------------------------------------------------------------------------------- 1 | package que3; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.http.Cookie; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | public class q3 extends HttpServlet { 12 | public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { 13 | int i = Integer.parseInt(req.getParameter("num1")); 14 | int j = Integer.parseInt(req.getParameter("num2")); 15 | int k = i + j; 16 | PrintWriter obj = res.getWriter(); 17 | obj.println("Sum is: " + k); 18 | Cookie cookie = new Cookie("k", k + ""); // making new cookie so that value can be passed through one servlet to 19 | // another 20 | res.addCookie(cookie); 21 | res.sendRedirect("sq"); // calling square servlet 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /29Day29/Servelet/q4/supperclass.java: -------------------------------------------------------------------------------- 1 | package que3; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.http.Cookie; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | public class supperclass extends HttpServlet { 12 | public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { 13 | int k = 0; 14 | Cookie cookies[] = req.getCookies(); // recieving cookies from first servlet 15 | for (Cookie c : cookies) { 16 | if (c.getName().equals("k")) { 17 | k = Integer.parseInt(c.getValue()); 18 | } 19 | } 20 | k = k * k; 21 | PrintWriter obj = res.getWriter(); 22 | obj.println("Square is: " + k); 23 | 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /29Day29/Servelet/q4/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | httpsession 8 | que3.q3 9 | 10 | 11 | httpsession 12 | /add 13 | 14 | 15 | session 16 | que3.supperclass 17 | 18 | 19 | session 20 | /sq 21 | 22 | 23 | -------------------------------------------------------------------------------- /29Day29/Task/Task.txt: -------------------------------------------------------------------------------- 1 | just go through and understand the functioning of Servelet interface 2 | -------------------------------------------------------------------------------- /30Day30/JDBC/connection/Conn.java: -------------------------------------------------------------------------------- 1 | package jdbc; 2 | 3 | import java.sql.*; 4 | 5 | public class Conn { 6 | public static void main(String args[]) throws Exception { 7 | Class.forName("com.mysql.jdbc.Driver"); 8 | Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/write your schema name here", "root", "write your password here"); 9 | Statement stmt = con.createStatement(); 10 | stmt.executeUpdate("create table employe(empid int(10), name varchar(20), salary int(10))"); 11 | con.close(); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /30Day30/JDBC/insertion/insert.java: -------------------------------------------------------------------------------- 1 | package jdbc; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.Statement; 6 | import java.util.Scanner; 7 | 8 | public class insert { 9 | public static void main(String[] args) throws Exception { 10 | Class.forName("com.mysql.jdbc.Driver"); 11 | Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/write your schema name here", "root", "write your password here"); 12 | Statement stmt = con.createStatement(); 13 | Scanner in = new Scanner(System.in); 14 | stmt.executeUpdate("INSERT INTO `employe` VALUES (500068802,'harshil bhardwaj',1000000)"); 15 | con.close(); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /30Day30/SOL/SOL.txt: -------------------------------------------------------------------------------- 1 | Upload your previous work here 2 | -------------------------------------------------------------------------------- /30Day30/Task/Task.txt: -------------------------------------------------------------------------------- 1 | Thats all i can help if in future you guys face any problem in java just raise an issue on my git hub i will reply with 5 hours 2 | and now start exploring the fields and the api that java concists 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Open for Contribution 2 | 3 | 1. Fork the repository and then clone it. For cloning command is: 4 | ``` 5 | $ git clone "https://github.com//30DaysOfJava" 6 | ``` 7 | 8 | 3. Do changes and stage them. 9 | ``` 10 | $ git add 11 | ``` 12 | 13 | 4. Commit you changes with a commit message. 14 | ``` 15 | $ git commit -m "" 16 | ``` 17 | 18 | 5. Push changes to your forked repository 19 | ``` 20 | $ git push -u origin branchname 21 | ``` 22 | 6. Create a pull request to the upstream repository. 23 | 24 | ## Synchronize forked repository with Main repository (Needed if creating a branch) 25 | 26 | 1. Create upstream as our repository 27 | ``` 28 | $ git remote add main "https://github.com/dotQuestionmark/30DaysOfJava" 29 | ``` 30 | 31 | 2. Fetch changes from main repository 32 | ``` 33 | $ git fetch main 34 | ``` 35 | 36 | 3. Merge changes after being fetched 37 | ``` 38 | $ git merge main/master 39 | ``` 40 | 41 | 5. Push changes to your forked repository 42 | ``` 43 | $ git push -f origin master 44 | ``` 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-for-dummies 2 | 3 | Keep the track of tutorial files uploaded daywise and learn java 4 | 5 | Contributions guidelines can be found [here.](CONTRIBUTING.md) 6 | 7 | ## Setting up JDK 8 | 9 | ``` 10 | https://www.oracle.com/java/technologies/javase-jdk13-downloads.html 11 | ``` 12 | 13 | 1) visit this site and download the latest version of jdk 14 | 2) Second go to your system enviorment settings and copy paste the path of your jdk folder till bin there 15 | 3) to check run java command on command promt if it runs you are good to goo 16 | 17 | ## Setting up an IDE (ECLIPSE) 18 | 19 | I personally prefer ECPLISE IDE 20 | ``` 21 | https://www.eclipse.org/downloads/ 22 | ``` 23 | 1) visit this website and download the ide for free 24 | 2) while downloading choose java devloper option that would be sufficient for beginner level 25 | 3) After downloading go to new and select new project (JAVA) 26 | 4) Create a new java class with a specific pakage name 27 | 5) start coding you are good to go 28 | 29 | ## Java ? 30 | 31 | Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. 32 | 33 | ### Stable release: Java SE 14 / March 17, 2020; 34 | 35 | All the projects are licenced under [GNU GENERAL PUBLIC LICENSE](LICENSE) 36 | --------------------------------------------------------------------------------