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.