Showing posts with label practice. Show all posts
Showing posts with label practice. Show all posts

Sunday, February 5, 2023

Database Access with JSP

 

LAB- 2

Database Access with JSP



//addbook.html

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

      <form action="registerbook.jsp" method="post">

            <table>

                  <tr>

                        <td>ISBN</td>

                        <td><input type="text" name="isbn"></td>

                  </tr>

                  <tr>

                        <td>name</td>

                        <td><input type="text" name="name"></td>

                  </tr>

                  <tr>

                        <td>price</td>

                        <td><input type="text" name="price"></td>

                  </tr>

                  <tr>

                        <td>author</td>

                        <td><input type="text" name="author"></td>

                  </tr>

                  <tr>

                        <td>Submit</td>

                        <td><input type="submit" value="addBook"></td>

                  </tr>

            </table>

      </form>

</body>

</html>

// registerbook.jsp

 

<html>

<body>

<%

String i = request.getParameter("isbn");

String n = request.getParameter("name");

String p = request.getParameter("price");

String a = request.getParameter("author");

%>

 

<h2> 

 

<%@ page import = "java.sql.*" %>

<%

//out.println(n);

 

 String dburl = "jdbc:mysql://localhost:3306/demodb";

 String dbuname = "root";

 String dbpassword = "";

 String dbdriver = "com.mysql.jdbc.Driver";

 

 Class.forName(dbdriver);

 

 Connection con = DriverManager.getConnection(dburl, dbuname, dbpassword);

 

Statement stmt =  con.createStatement();

String sql = "insert into book values ('"+i+"','"+n+"','"+p+"','"+a+"')";

 

stmt.executeUpdate(sql);

out.println("Book added success");

 

%>

</h2>

</body>

</html>


 

Monday, January 9, 2023

An array is called balanced if it's even numbered elements (a[0], a[2], etc.) are even and its odd numbered elements (a[1], a[3],etc.) are Odd. Write a function named is Balanced that accepts an array of integers and returns 1 if the array is balanced otherwise it returns 0.


 ..

/**
* * An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its odd
* * numbered elements (a[1], a[3], etc.) are odd.
* *
* * Write a function named isBalanced that accepts an array of integers and returns 1 if the array is
* * balanced, otherwise it returns 0.
* *
* * Examples: {2, 3, 6, 7} is balanced since a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12}
* * is balanced since a[0], a[2] and a[4] are even, a[1] and a[3] are odd.
* * {7, 15, 2, 3} is not balanced since a[0] is odd.
* * {16, 6, 2, 3} is not balanced since a[1] is even.
* *
* * If you are programming in Java, the function signature is
* * int isBalanced(int[ ] a)
*/

..

public class Balanced {


    public static void main(String[] args) {


        int[] arrayOfNumbers = {2, 3, 6, 7};

          //      int[] arrayOfNumbers = {6, 7, 2, 3, 12};

        //     int[] arrayOfNumbers = {7, 15, 2, 3};

        //     int[] arrayOfNumbers = {16, 6, 2, 3};


        System.out.println(isbalanced(arrayOfNumbers));


    }


    public static int isbalanced(int[] a) {

        int i, j;

        for (i = 0; i < a.length; i = i + 2) {

            if (a[i] % 2 != 0) {

                return 0;

            }

        }


        for (j = 1; j < a.length; j = j + 2) {

            if (a[j] % 2 == 0) {

                return 0;

            }

        }

        return 1;


    }

}



https://github.com/bijay-shrestha/practice/blob/master/src/main/java/com/basic/practice/Balanced.java

/*An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its  odd numbered elements (a[1], a[3], etc.) are odd.  Write a function named isBalanced that accepts an array of integers and returns 1 if the array is balanced, otherwise it returns 0. Examples:  {2, 3, 6, 7} is balanced since a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12} is balanced since a[0], a[2] and a[4] are even, a[1] and a[3] are odd. {7, 15, 2, 3} is not balanced since a[0] is odd. {16, 6, 2, 3} is not balanced since a[1] is even.

If you are programming in Java or C#, the function signature is  

int isBalanced(int[ ] a)

If you are programming in C or C++, the function signature is  

int isBalanced(int a[ ], int len) 

where len is the number of elements in the array.   */


public class HelloWorld {

   public static void main(String []args){

       int[] arr={7, 15, 2, 3} ;

        int result=isbalanced(arr);

        System.out.println("isbalanced->"+result);

         

     }

    public static int isbalanced(int[] a)

     {int i,j;

      for(i=0;i<a.length;i=i+2)

      {

          if(a[i]%2!=0)

          {

              return 0;

          }

      }

      

      for(j=1;j<a.length;j=j+2)

      {

          if(a[j]%2==0)

          {

              return 0;

          }

      }

      return 1;

   

         }

     }

https://onecompiler.com/java/3wzmwmh46

Question :
A balanced array is defined to be an array where for every value n in the array, -n also is in the array. For example {-2, 3, 2, -3} is a balanced array. So is {-2, 2, 2, 2}. But {-5, 2, -2} is not because 5 is not in the array.
Write a function named isBalanced that returns 1 if its array argument is a balanced array. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isBalanced (int [ ] a);
If you are programming in C or C++, the function signature is


int isBalanced(int a[ ], int len) where len is the number of elements in the array.
Solution :
public static int isBalanced(int [] a)
        {
            int isBalanced = 1;
            for(int i=0;i<a.Length;i++)
            {
                int isValid = 0;
                for(int j=0;j<a.Length;j++)
                {
                    if(a[i]==-a[j])
                    {
                        isValid = 1;
                        break;
                    }
                }
                if (isValid==0)
                    isBalanced = 0;
            }
            return isBalanced;
        }

..

A non-empty array A of length n is called on array of all possibilities if it contains all numbers between 0 and A.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0.

 

/**
* * A non-empty array a of length n is called an array of all possiblities if it contains all numbers between 0
* * and a.length-1 inclusive.
* *
* * Write a method named isAllPossibilities that accepts an integer array and returns 1 if
* * the array is an array of all possiblities, otherwise it returns 0.
* *
* * If you are programming in Java or C#, the function signature is
* * int isAllPossibilities(int[ ] a)
* *
* * If the input array is return
* *
* * {1, 2, 0, 3} 1
* * {3, 2, 1, 0} 1
* * {1, 2, 4, 3} 0 (because 0 not included and 4 is too big)
* * {0, 2, 3} 0 (because 1 is not included)
* * {0} 1
* * {} 0
*/
Question :
A non-empty array a of length n is called an array of all possiblities if it contains all numbers between 0 and a.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isAllPossibilities(int[ ] a)
If you are programming in C or C++, the function signature is
int isAllPossibilities(int a[ ], int len) where len is the number of elements in the array
Examples
If the input array is
return
{1, 2, 0, 3}
1
{3, 2, 1, 0}
1
{1, 2, 4, 3}
0 (because 0 not included and 4 is too big)
{0, 2, 3}
0 (because 1 is not included)
{0}
1
{}
0

public static int allPossibilities(int[] a)
        {
            bool isNice = true, step = false;
            int i, j, x, y;
            for (i = 0; i < a.Length; i++)
            {
                step = false;
                for (j = 0; j < a.Length; j++)
                {
                    if (a[i] == j)
                        step = true;
                }
                if (j == a.Length && step == false)
                {
                    isNice = false;
                    break;
                }
            }
            return isNice == true ? 1 : 0;
        }


_________________________________________

solution is not easy to understand.

for any one interested try this

public static int isAllPossibilities(int[] a){
if(a.length < 1){
return 0;
}
else{
for(int i = 0; i < a.length; i++){
boolean iIsInSide = false;

for(int j : a){
if( i == j){
iIsInSide = true;
break;
}
}

if(!iIsInSide){
return 0;
}
}

return 1;
}
}
_________________________________________

//Java

public class allPossibilitiesDemo { 
    public static int allPossibilities(int[] a)
        {
            Boolean isNice = true, step = false;
            int i, j, x, y;
            for (i = 0; i < a.length; i++)
            {
                step = false;
                for (j = 0; j < a.length; j++)
                {
                    if (a[i] == j)
                        step = true;
                }
                if (j == a.length && step == false)
                {
                    isNice = false;
                    break;
                }
            }
            return isNice == true ? 1 : 0;
        }
    
    public static void main(String[] args) {
        System.out.println(allPossibilities(new int[]{1, 2, 0, 3}));
        System.out.println(allPossibilities(new int[]{3, 2, 1, 0}));
        System.out.println(allPossibilities(new int[]{1, 2, 4, 3}));
        System.out.println(allPossibilities(new int[]{0, 2, 3}));
        System.out.println(allPossibilities(new int[]{0}));
        System.out.println(allPossibilities(new int[]{}));
    }
}

_________________________________________

https://github.com/sachinkeshav/JavaExam/blob/master/src/com/knight/exam/java/allPossibilities/AllPossibilitiesArray.java

https://gist.github.com/bijay-shrestha/cb13986207bc1a9f2bb2ccef94fa6070

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...