Skip to main content

Posts

Showing posts with the label java

Valid Parentheses Question Solution in java

  Valid Parentheses Question Solution in java Code:  import java.util.*; class Test { public static void main(String[] args){ Scanner scn = new Scanner(System.in); String s = scn.next(); System.out.println(isValid(s)); } public static boolean isValid(String s) {        Stack<Character> st = new Stack<>();         for(int i=0;i<s.length();i++){             char ch = s.charAt(i);             if(ch=='('||ch=='['||ch=='{'){                 st.push(ch);             }else if(st.isEmpty()){                 return false;             }else { char top = st.pop();                 if(ch==')'&&top!='('){                     return false;                 } if(ch=='}'&&top!='{'){                     return false;                 } if(ch==']'&&top!='['){                     return false;                 }             }         } return st.isEmpty();     } }

Kadane's Algorithm implementation in java | Largest Sum Contiguous Subarray

 Kadane's Algorithm implementation in java Code::  import java.util.*; class Test { public static void main(String[] args){ int[] arr =  {1,2,3,-2,5}; int maxSum = arr[0]; int currSum = 0; for(int i=0;i<arr.length;i++){ currSum += arr[i]; if(currSum > maxSum){ maxSum = currSum; } if(currSum<0){ currSum = 0; } } System.out.println(maxSum); } } o/p: 9 Largest Sum Contiguous Subarray Brute Force  Code:  import java.util.*; class Test { public static void main(String[] args){ int[] arr =  {1,2,3,-2,5}; int max = Integer.MIN_VALUE; for(int i=0;i<arr.length;i++){ int sum = arr[i]; max = Math.max(sum,max); for(int j=i+1;j<arr.length;j++){ sum = sum + arr[j]; max = Math.max(sum,max); } } System.out.println(max); } }

Sorting Algorithms Implementation In java

  Selection Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; //selection Sort class Test { public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; for(int i=0;i<arr.length;i++){ int minVal = arr[i]; int ind = i; for(int j= i+1;j<arr.length;j++){ if(arr[j] < minVal){ ind = j; minVal = arr[j]; } } //swap with minimum  int temp = arr[i]; arr[i] = minVal; arr[ind] = temp; } for(int i : arr){ System.out.print(i+" "); } } }  Bubble Sort Program in java Code:: import java.util.*; import java.lang.*; import java.io.*; //Bubble Sort class Test { public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; for(int i=0;i<arr.length;i++){ //the last val is at its coorect Position for(int j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ //swap int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp;

Quick Sort Program in java

 Quick Sort Program in java Code:: import java.util.*; import java.lang.*; import java.io.*; //Quick Sort class Test { public static void main(String[] args){ int[] arr = {8,7,3,4,11,13,14,2,17}; quickSort(arr,0,arr.length-1); for(int i : arr){ System.out.print(i+ " "); } } public static void quickSort(int[] arr,int s,int e){ if(s>=e){ return; } int pIndex = partition(arr,s,e); quickSort(arr,s,pIndex-1); quickSort(arr,pIndex+1,e); } public static int partition(int[] arr,int s,int e){ int pivot = arr[e]; int pIndex = s; for(int i=s;i<=e-1;i++){  if(arr[i] <= pivot){ int temp = arr[i]; arr[i] = arr[pIndex]; arr[pIndex] = temp; pIndex++; } } int temp = arr[e]; arr[e] = arr[pIndex]; arr[pIndex] = temp; return pIndex; } }

insertion Sort Program in java

 insertion Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; //Insertion Sort class Test{ public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; insertion(arr); for(int i : arr){ System.out.print(i+" "); } } public static void insertion(int[] arr){ for(int i=1;i<arr.length;i++){ int hole = i; int val = arr[i]; while(hole>0 && arr[hole-1]>val){ arr[hole] = arr[hole-1]; hole--; } arr[hole] = val; } } }

Bubble Sort Program in java

 Bubble Sort Program in java Code:: import java.util.*; import java.lang.*; import java.io.*; //Bubble Sort class Test{ public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; bubble(arr); for(int i : arr){ System.out.print(i+" "); } } public static void bubble(int[] arr){ for(int i=0;i<arr.length;i++){ //the last val is at its correct Position for(int j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ //swap int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } } Optimized code if the right part of the array is sorted code: import java.util.*; import java.lang.*; import java.io.*; //Bubble Sort class Test{ public static void main(String[] args){ int[] arr = {2,7,4,1,5,3}; bubble(arr); for(int i : arr){ System.out.print(i+" "); } } public static void bubble(int[] arr){ for(int i=0;i<arr.length;i++){ int swap = 0; for(int j=0;j<

Selection Sort Program in java

 Selection Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; selection(arr); for(int i : arr){ System.out.print(i+ " "); } } public static void selection(int[] arr){ for(int i=0;i<arr.length;i++){ int minVal = arr[i]; int ind = i; for(int j= i+1;j<arr.length;j++){ if(arr[j] < minVal){ ind = j; minVal = arr[j]; } } //swap with minimum  int temp = arr[i]; arr[i] = minVal; arr[ind] = temp; } } }

program for finding lcm and hcf of two numbers using modulo operator in euclidean algorithm in java

 hcf of the numbers Code:: import java.util.*; import java.lang.*; import java.io.*; //hcf of two numbers  //by efficient use of modulo operator in euclidean algorithm class Test { public static void main(String[] args){ int num1=36,num2=60,hcf; hcf = getHcf(num1,num2); System.out.println("the hcf : "+hcf); } public static int getHcf(int a,int b){ return b==0 ? a : getHcf(b,a%b); } } LCM of two numbers property  if we have 2 numbers num1,num2 and there lcm is l and hcf is h then num1 * num2 = l * h Code:: import java.util.*; import java.lang.*; import java.io.*; //lcm of two numbers  //by efficient use of modulo operator in euclidean algorithm class Test { public static void main(String[] args){ int num1=36,num2=60,hcf; hcf = getHcf(num1,num2); int lcm = (num1*num2)/hcf; System.out.println("the lcm: "+lcm); } public static int getHcf(int a,int b){ return b==0 ? a : getHcf(b,a%b); } }

Merge Sort Program in java

 Merge Sort Program in java Code :: import java.util.*; import java.lang.*; import java.io.*; class Test { public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; mergeSort(arr); for(int i : arr){ System.out.print(i+" "); } } public static void mergeSort(int[] arr){ int n = arr.length; if(n<2){ return; } int mid = n/2; int[] left = new int[mid]; int[] right = new int[n-mid]; for(int i=0;i<mid;i++){ left[i] = arr[i]; } for(int i=mid;i<n;i++){ right[i-mid] = arr[i]; } mergeSort(left); mergeSort(right); merge(left,right,arr); } public static void merge(int[] left,int[] right,int[] arr){ int i = 0; int j = 0; int k = 0; while(i<left.length && j<right.length){ if(left[i]<right[j]){ arr[k] = left[i]; i++; }else{ arr[k] = right[j]; j++; } k++; } while(i!=left.length){ arr[k] = left[i]; i++; k++; } wh

how to store html form data in mysql database using java

how to store html form data in mysql database using java for explanation watch the video:: Step 1 : Create the peroject step 2 : create home.html in webapp step 3 : home.html home.html ========== <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script> <!-- Popper JS --> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script> <style> #div1 { width: 600p

Transaction Management in JDBC | JDBC Tutorial

  Transaction Management in JDBC For explanation watch video :: Code:: import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class OracleConn { public static void main(String[] args) { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String uname = "system"; String pwd = "tiger"; // take source account number and dest acc number from user Scanner scn = new Scanner(System.in); System.out.println("Enter source acc no: "); long srcAcNo = scn.nextLong(); System.out.println("Enter dest acc no: "); long destAcNo = scn.nextLong(); // take the amt to transfer System.out.println("Enter amt to transfer: "); long amt = scn.nextLong(); try (Connection con = DriverManager.getConnection(url, uname, pwd);  Statement st = con.createStatement();) { // Begin Tx if (con != null) con.set

Batch Processing in JDBC

 Batch Processing in JDBC Code:: for explanation watch video : package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class OracleConn { public static void main(String[] args) { try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "tiger"); Statement st = con.createStatement()) { // add queries to batch ..these queries can belong to same db table or different // db tables but must be non-select Queries st.addBatch("INSERT INTO STUDENT VALUES(4444,'david',55.77,'france')"); st.addBatch("UPDATE  STUDENT SET AVG=AVG+10 WHERE SNO=2"); st.addBatch("DELETE FROM STUDENT WHERE SNO=1"); // execute the batch int result[] = st.executeBatch(); // find sum of the records that are effected int sum = 0; for (int i = 0; i < result.length; ++i)

Session tracking using Http Cookies

 Session tracking using Http Cookies index.html ======== <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <style type="text/css"> div { width: 500px; margin: auto; margin-top: 100px; } </style> </head> <body> <div> <form action="firsturl" method="post"> Name: <br> <input type="text" name="name"> <br> Choose any one: <br> <select name="dish"> <option value="sweet">Sweet</option> <option value="spicy">Spicy</option> </select> <br> <input type="submit" value="submit"> </form> </div> </body> </html> FirstServlet.java ------------------- package com.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servl

How to fetch and display data of html form in servlet?

 How to fetch and display data of html form in servlet? For explanation watch video:: web.xml ======== <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <display-name>ServetRequest</display-name>   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list> </web-app> home.html ========== <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <!-- jQuery

working with auto increment of mysql in JDBC

 working with auto increment of mysql in JDBC For explanation Watch video :: code:: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class AutoIncrementEx { private static final String query = "insert into emp2(ename,eadd) values(?,?)"; public static void main(String[] args) { try(Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); PreparedStatement ps = con.prepareStatement(query);){ ps.setString(1, "raja"); ps.setString(2, "nagpur"); int count = ps.executeUpdate(); if(count!=0) { System.out.println("Record Inserted"); }else { System.out.println("Record Not Inserted"); } }catch(SQLException se) { se.printStackTrace(); }catch(Exception e) { e.printStackTrace(); } } }

Modified Kaprekar Numbers Hackerrank Solution - java

 Modified Kaprekar Numbers Hackerrank Solution - java For explanation watch video : Code:: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     /*      * Complete the 'kaprekarNumbers' function below.      *      * The function accepts following parameters:      *  1. INTEGER p      *  2. INTEGER q      */     public static void kaprekarNumbers(int p, int q) {         ArrayList<Long> al = new ArrayList<>();         for(long i=p;i<=q;i++){             long sq = i*i;             String num = String.valueOf(i);             String s = String.valueOf(sq);             int leftLength = s.length() - num.length();             long leftNum = 0;             if(leftLength == 0){                 lef

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-