Monday, 5 June 2017

JAVA PROGRAMMING TYPE 4 DRIVER PROGRAM FOR DEVELOPING LOGIN APP IN ECLIPSE

/**JAVA PROGRAMMING
TYPE 4 DRIVER PROGRAM 
FOR DEVELOPING LOGIN APP*/


package jdbcProject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginApp {

public static void main(String[] args) {
Scanner sc=null;
String user=null,pass=null;
Connection con=null;
ResultSet rs=null;
Statement st=null;
String query=null;
int count=0;
try{
sc=new Scanner(System.in);
if(sc!=null){
System.out.println("enter username:");
user=sc.nextLine();
System.out.println("enter passowrd:");
pass=sc.nextLine();
}
user= ""+user+"";
pass= ""+pass+"";
Class.forName("oracle.jdbc.driver.OracleDriver");

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","mahesh","mahesh");
if(con!=null)
st=con.createStatement();
System.out.println(query);
if(st!=null)
rs=st.executeQuery(query);
if(rs!=null){
if(rs.next())
count=rs.getInt(1);
System.out.println(count);
}
if(count==0)
System.out.println("invalid credentials");
else
System.out.println("valid credentials");


}
catch(SQLException se){
se.printStackTrace();
System.out.println("recoed insertion failed");
}
catch(ClassNotFoundException cnf){
System.out.println("record insertion failed");
}
catch(Exception e){
System.out.println("recoed insertion failed");
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
}
catch(SQLException se){
se.printStackTrace();
}
try{
if(st!=null)
st.close();
}
catch(SQLException se){
se.printStackTrace();
}
try{
if(con!=null)
con.close();
}
catch(SQLException se){
se.printStackTrace();
}
try{
if(sc!=null)
sc.close();
}
catch(Exception se){
se.printStackTrace();
}
}

}

}


Wednesday, 15 March 2017

JAVA PROGRAM TO DISPLAY PYRAMID IN REVERSE

// JAVA PROGRAM TO DISPLAY PYRAMID IN REVERSE


package com.javapragramming.blogspot.mahesh;
class Sample{
public static void main(String[ ] ar){

for(int i=1;i<=5;i++){
    for(int j=1;j<=5;j++){
if(i>j){
   System.out.print(" ");
 
    }
else{
  System.out.print(" "+"*");

   }
}

System.out.println(" ");
}

}
}



Friday, 24 February 2017

EXPLANATION OF PUBLIC STATIC VOID MAIN(STRING[ ] AR)

// EXPLANATION OF PUBLIC STATIC VOID MAIN(STRING[ ] AR)


PUBLIC: The main method should be declared as public, so that the JVM can access the main method from any location.

STATIC: The main method should be declared as static, so that the JVM cam invoke the main method directly by using class name.

VOID: The caller of the main method is JVM and the JVM does not expect any value from the main method, therefore the main method should not return any value to JVM hence we specify the return type as void.

MAIN( ): main is the name of the method, a valid java identifier, following the java coding conventions and the name is fixed.

STRING[ ]: It is used for storing command line arguments.

The name of the string array can be any valid java identifier.

RULE: If a declaration contains multiple modifiers, then we can specify them in any sequence.
static public void main(String[ ] abc)
public static void main(String[ ] abc)


//EXPLAIN SYSTEM.OUT.PRINTLN( ):

  • System is a predefined class available in java.lang package.
  • Out is a reference variable of PrintStream class declared as static in System class.
  • println() is predefined method available in PrintStream. PrintStream is a predefined class available in java.io package.


Out is a reference variable declared as static in System class and therefore it can be accessed directly by using class name(System.out).
System.out will provide an object of PrintStream class. using which we can access the methods of PrintStream class and therefore we write System.out.println().

Sunday, 19 February 2017

OPERATIONS ON ARRAYS USING METHOD INVOCATION TECHNIQUE

/** JAVA PROGRAM TO FIND SUM OF ARRAY ELEMENTS
      TO FIND BIGGEST NUMBER OF ARRAY
      TO FIND SMALLEST NUMBER OF ARRAY
      USING METHOD INVOCATION */

class ArrayDemo3{
public static void main(String[] ar){
ArrayDemo3 ad= new ArrayDemo3();
int arr[]={10,201,3,44,5,66,7,568,94};

int s=ad.sum(arr);
System.out.println("sum of all array elements is:"+s);
ArrayDemo3 ad2= new ArrayDemo3();
int y=ad2.min(arr);
System.out.println("the min number of array is"+y);
ArrayDemo3 ad3= new ArrayDemo3();
int z=ad3.max(arr);
System.out.println("the max number if array is"+z);

}
int sum(int[] f){
int s=0;
for(int d:f){
s=s+d;

}
return s;
}
int min(int[] g){
int y=g[0];
for(int i=0;i<g.length;i++) {
if(y>g[i]){
y=g[i];
}
}
return y;
}

int max(int[] h){
int z=h[0];
for(int i=0;i<h.length;i++){
if(z<h[i]){
z=h[i];
}
}
return z;
}

}

OUTPUT:





CLICK HERE FOR NORMAL PROCEDURE OF ARRAYS

Friday, 17 February 2017

JAVA PROGRAM FOR ARITHMETIC OPERATIONS USING METHOD INVOCATION

// JAVA PROGRAM FOR ARITHMETIC OPERATIONS USING METHOD INVOCATION

import java.io.*;
class Calculations{
public static void main(String[] ar)
throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter your choice 1.add 2.sub 3. mul 4. div");
int ch= Integer.parseInt(br.readLine());

switch(ch){
case 1: Calculations ad= new Calculations();
ad.add();
break;

case 2: System.out.println("enter x value:");
int x= Integer.parseInt(br.readLine());
System.out.println("enter y value:");
int y= Integer.parseInt(br.readLine());
Calculations sb= new Calculations();
sb.sub(x,y);
break;

case 3: Calculations mu= new Calculations();
int res= mu.mul();
System.out.println("the multiplication of given numbers is:"+res);
break;

case 4: Calculations di= new Calculations();
System.out.println("enter a value:");
int a= Integer.parseInt(br.readLine());
System.out.println("enter b value:");
int b= Integer.parseInt(br.readLine());
int result= di.div(a,b);
System.out.println("the division of given numbers is:"+result);
break;
default: System.out.println("choose correct option");
}
}

void add()
throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter x value:");
int x= Integer.parseInt(br.readLine());
System.out.println("enter y value:");
int y= Integer.parseInt(br.readLine());
int z=x+y;
System.out.println("sum of"+x+"and"+y+"is:"+z);
}

void sub(int m,int n)
throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int z=m-n;
System.out.println("sub:"+z);
}

int mul()
throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter x value:");
int x= Integer.parseInt(br.readLine());
System.out.println("enter y value:");
int y= Integer.parseInt(br.readLine());
int z=x*y;
return z;
}

int div(int m,int n)
throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int z= m/n;
return z;
}
}

OUTPUT:


Sunday, 12 February 2017

JAVA PROGRAM USING ENCAPSULATION TECHNIQUE

// JAVA PROGRAM USING ENCAPSULATION TECHNIQUE

class Student{
int rollno;
double marks;
String name;

void display(){
System.out.println("rollno:"+ rollno);
System.out.println("marks:"+ marks);
System.out.println("name:"+ name);
}

public static void main(String[] ar){
System.out.println("student information");
Student st=new Student();
st.display();
}
}

OUTPUT;
rollno:0
marks:0.0
name:null




when the above java program Student.java is compiled, the compiler will verify whether the java code available in the program is valid or not, if valid the compiler will generate the .class file. The .class file generated by the compiler will be provided to the JVM for execution.

The execution of the java program will be done by the JVM and it begins from the main(). Initially the java stack will be empty. the JVM will begin execution of the program by calling main(). When a method is called for execution then that method will be pushed into the java stack and then begins the execution.

Student st= new Student();

The above statement will create an object of Student class. Creating an object means allocating memory for the instance variables in heap memory.

A class can contain any number of methods, but the JVM can call only main() for execution. If we want other methods to be executed, then it is the responsibility of the programmer to call the other method which has to be executed.

st.display();

The above statement will call display() for execution. When the display() is called for execution, the display() will be pushed into the java stack and then begins the execution. When a method is completely executed then that method will be popped(removed) from the java stack.



TO KNOW ABOUT OOPS CONCEPTS CHECK HERE........



Thursday, 9 February 2017

JAVA PROGRAM TO CHECK ARMSTRONG NUMBER

/* JAVA PROGRAM TO CHECK ARMSTRONG NUMBER
    ENTERED FROM KEYBOARD */


import java.io.*;
class ArmstrongNumber{
public static void main(String[] ar)
throws IOException{

BufferedReader br= new BufferedReader(
new InputStreamReader(System.in));
System.out.print("enter the number to check armstrong number:");
int n=Integer.parseInt(br.readLine());
int a=0,k=0,temp;
temp=n;
while(n>0){
a=n%10;
n=n/10;
k=k+(a*a*a);
}

if(temp==k){
System.out.println("the number is armstrong number");
}

else{
System.out.println("the number is not armstrong number");
}
     }
}


OUTPUT:


this is the java program output for Armstrong number.

Tuesday, 7 February 2017

OPERATIONS ON STRINGS

// OPERATIONS ON STRINGS


class StringDemo2{
public static void main(String[] args){
String str=new String("hello");
System.out.println(str+1+2+3);
System.out.println(1+str+2+3);
System.out.println(1+2+str+3);
System.out.println(1+2+3+str);
}
}

Out put:
hello123
1hello23
3hello3
6hello

JAVA PROGRAM TO DO OPERATIONS ON SWITCH CASE

//JAVA PROGRAM TO DO OPERATIONS ON SWITCH CASE



import java.io.*;
class SwitchDemo{
public static void main(String[] ar)
throws IOException{
BufferedReader br= new BufferedReader(
         new InputStreamReader(System.in));
System.out.println("enter your choice:");
String str= br.readLine();
int ch= Integer.parseInt(str);


switch(ch){
case 1: System.out.println("first choice:");
break;
case 2: System.out.println("second choice:");
break;
case 3: System.out.println("third choice:");
break;
case 4: System.out.println("fourth choice:");
break;
default: System.out.println("wrong choice, choose correct choice:");
break;
}
    }
}

JAVA PROGRAM FOR ARITHMETIC OPERATIONS


// JAVA PROGRAM FOR ARITHMETIC OPERATIONS


class Test{
public static void main(String[] ar){
int a=7;
int b=++a;
int c=b--;
int d=a++ + --b - --c;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}

JAVA PROGRAM TO CHECK GIVEN STRING IS PALINDROME OR NOT

/* JAVA PROGRAM TO CHECK GIVEN STRING IS
    PALINDROME OR NOT */


class PalindromeDemo{
public static void main(String[] args){
String s1=new String("madam");
String reverse="";
for(int i=s1.length()-1;i>=0;i--){
reverse=reverse+s1.charAt(i);
}
if(s1.equals(reverse)){
System.out.println("the given string is palindrome");
}
else{
System.out.println("the given string is not palindrome");
}
}
}

JAVA PROGRAM TO DISPLAY ARRAY ELEMENTS IN MATRIX FORM IN TWO WAYS ADDITION, SUBTRACTION AND MULTIPLICATION

/** JAVA PROGRAM TO DISPLAY ARRAY ELEMENTS 
      IN MATRIX FORM IN TWO WAYS 
      ADDITION, SUBTRACTION AND MULTIPLICATION */



class MatrixDemo{
public static void main(String[] args){
int a[][]={{1,2},{3,4}};
int b[][]={{3,1},{2,1}};
int c[][]=new int[2][2];

// DISPLAYING ARRAY ARRAY ELEMENTS IN MATRIX FROM USING FOR LOOP

for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
      System.out.println();
}
System.out.println();
System.out.println();


// DISPLAYING ARRAY ARRAY ELEMENTS IN MATRIX FROM USING FOR EACH LOOP

for(int[] x:b){
for(int y:x){
System.out.print(y+" ");
}
      System.out.println();
}

System.out.println("addition of two arrays:");

for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
      System.out.print(a[i][j]+b[i][j]+" ");
}
      System.out.println();
}

System.out.println("subtraction of two arrays:");

for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
      System.out.print(a[i][j]-b[i][j]+" ");
}
      System.out.println(" ");
}


System.out.println("multiplication of two arrays:");

for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
for(int k=0;k<a[i].length;k++){


       c[i][j]=c[i][j]+a[i][k]*b[k][j];

}
System.out.print(c[i][j]+"  ");
}
      System.out.println();
}
}
}

JAVA PROGRAM TO FIND EVEN OR ODD NUMBER ENTERED FROM KEYBOARD BY THE USER

/* JAVA PROGRAM TO FIND EVEN OR ODD NUMBER
    ENTERED FROM KEYBOARD BY THE USER*/


import java.io.*;
class IfelseDemo2{
public static void main(String[] ar)
throws IOException{
BufferedReader br= new BufferedReader(
new InputStreamReader(System.in));
System.out.println("enter your number:");
String str= br.readLine();
int n= Integer.parseInt(str);
if(n%2==0){
System.out.println("the number "+n+" is even numer");
}
else{
System.out.println("the number "+n+" is odd numer");
}
}
}

JAVA PROGRAM TO FIND EVEN NUMBER OR ODD NUMBER

/* JAVA PROGRAM TO FIND GIVEN NUMBER
    IS EVEN NUMBER OR ODD NUMBER */



class IfelseDemo{
public static void main(String[] ar){
long n=47109;

if(n%2==0){
System.out.println(" the given number "+ n + " is even number");
}
else{
System.out.println(" the given number "+ n + " is odd number");
}
     }
}

JAVA PROGRAM TO FIND BIGGEST NUMBER USING BUFFERED READER

/* JAVA PROGRAM TO FIND BIGGEST NUMBER
AMONG GIVEN 3 NUMBERS USING BUFFEREDREADER*/


import java.io.*;
class Biggest{
public static void main(String[] ar)
throws IOException{
BufferedReader br= new BufferedReader(
new InputStreamReader(System.in));
System.out.println("enter A value:");
String str= br.readLine();
int A= Integer.parseInt(str);

System.out.println("enter B value:");
int B=Integer.parseInt(br.readLine());

System.out.println("enter c value:");
int C=Integer.parseInt(br.readLine());

if(A>B&&A>C){
System.out.println("the biggest number is A:"+A);
}

else if(B>C){
System.out.println("the biggest number is B:"+B);
}

else{
System.out.println("the biggest number is C:"+C);
}
    }
}

JAVA PROGRAM TO COUNT NUMBER OF VOWELS IN GIVEN STRING

/* JAVA PROGRAM TO COUNT
NUMBER OF VOWELS IN GIVEN STRING */




class VowelsCount{
public static void main(String[] args){
int i=0,c=0;
String s1=new String("asia's largest institution is not in hyderabad");
char ch;

for(i=0;i<s1.length();i++){
ch=(s1.charAt(i));

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
c++;
}

}
System.out.println("the total number of vowels in a string"+c);
}
}

JAVA PROGRAM TO FIND THE SUM OF ARRAY ELEMENTS AND BIGGEST AND SMALLEST NUMBER


/** JAVA PROGRAM TO FIND
 SUM OF ALL ARRAY ELEMENTS
 BIGGEST NUMBER
SMALLEST NUMBER */



class ArraySum{
public static void main(String[] ar){
int arr[][]={{1,4,7},{2,5,8},{3,6,9}};
int s=0,c=arr[0][0],b=arr[0][0];


for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println();
System.out.println();
for(int[] x:arr){
for(int y:x){
System.out.print(y+" ");

s=s+y;
}
System.out.println();
}
System.out.println();
System.out.println();
System.out.println("sum of all array elements is="+s);

for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){

if(c<arr[i][j]){

}
else{
c=arr[i][j];
}
}
    }
System.out.println();
System.out.println();

System.out.println("the smallest number of the array is:"+c);

  for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){

if(b<arr[i][j]){
b=arr[i][j];
}

}
    }
System.out.println();
System.out.println();
System.out.println("biggest number of the array is:"+b);


for(int[] x:arr){
for(int y:x){
if(c<y){
}
else{
c=y;
}
}
}

System.out.println("the smallest number of the array is:"+c);

     }
}


CLICK HERE FOR INVOCATION TECHNIQUE