Posts

Showing posts with the label Java Programs

Java Program - Infinite for loop

class InfiniteForLoop { public static void main(String[] args) { /* * Its perfectely legal to skip any of the 3 parts of the for loop. * Below given for loop will run infinite times. */ for (;;) System.out.println("Hello"); /* * To terminate this program press ctrl + c in the console. */ } }    Output: Hello Hello Hello Hello .. ..

Java Program - Remove all elements from arrayList

import java.util.ArrayList; class RemoveAllElementsOfArrayListExample { public static void main(String[] args) { //create an ArrayList object ArrayList arrayList = new ArrayList(); //Add elements to Arraylist arrayList.add("1"); arrayList.add("2"); arrayList.add("3"); System.out.println("Size of ArrayList before removing elements : " + arrayList.size()); /* To remove all elements from the ArrayList use void clear() method. */ arrayList.clear(); System.out.println("Size of ArrayList after removing elements : " + arrayList.size()); } } Output: Size of ArrayList before removing elements : 3 Size of ArrayList after removing elements : 0

Simple Login Validation and Session with Database - Jsp and Servlet

Image
Simple Login Validation and Session with Database - JSP and Servlet Example ========================================================

Java Database Connection Class-JDBC

Java Database Connection Class-JDBC-Easier In order to connect to a database it is required that the connection url, username, password.So without specifying it in the connection class we can specify it in a new properties file.This technique will reduce the recompilation of the jdbc class whenever a database change or credentials change needed.Because we can just change it in the configuration without changing the content of jdbc connection class. So First Create a properties file by renaming the extension of text file to .properties and add the contents as mentioned below and save it to the source folder. db.properties --------------------------------------- driver= your driver name url= your url name username= your database username password= your database password ---------------------------------------------------- JdbcConnector.class --------------------------------------- package com.anandhu.jdbc; import java.io.FileInputStream; import ...

JavaSE Calculator

Image
Click Here To Download Source Folder Click Here to Download Application Output ------------------    

Java Object Oriented-Data Encapsulation

Encapsulation.java ..................................... package encapsulation; public class Encapsulation {     private String userid="abc123";     private final int  password=2798;     /*public void function(){         System.out.println("UserID = "+userid+" and Password = "+password);     }*/ } class Stranger{     public Stranger(){         Encapsulation obj=new Encapsulation();         //System.out.println(obj.password);//password cannot be accessed         //System.out.println(obj.userid);//userid cannot be accessed             } } Main.java ........................... package encapsulation; public class Main {     public static void main(String[] args) {     Stran...

Java Object Oriented-Classes and Objects

Mobile.java .......................... package myjava; public class Mobile {   String mobname;     public Mobile(String mobname) {         this.mobname = mobname;         this.calling();         this.messaging();     }   public void calling(){       System.out.println(mobname+" used for Calling");   }   public void messaging(){       System.out.println(mobname+" used for Messaging");   }   } Main.java ............................ package myjava; public class Main {     public static void main(String[] args) {         Mobile Nokia=new Mobile("Nokia");//object of mobile class         Mobile Samsung=new Mobile("Samsung");//object of mobile class     ...

Java Program-Palindrome or Not (Without Using Built in Functions)

Java Program-Palindrome or Not (Without Using Built in Functions)

Java Program-Prime Checking

Simple Java Program-Prime Checking

Java Program-Fibonacci Series

Simple Java Program-Fibonacci Series

Java Program-Length of a string

Java Program-Length of a string

Java Program-Leap Year Checking

Simple Java Program-Leap Year Checking

Java Program-Even or Odd

Simple Java Program-Even or Odd import java.util.Scanner; public class EvenOdd {     public static void main(String[] args) {         Scanner reader = new Scanner(System.in);         System.out.print("Enter a number: ");         int num = reader.nextInt();         if(num % 2 == 0)             System.out.println(num + " is even");         else             System.out.println(num + " is odd");     } } Output Enter a number: 3 is odd

Java Program-Sum of two numbers

 Simple Java Program-Sum of two numbers

Java Programs-Hello World

Simple Java Programs-Hello World Create a java file named "HelloWorld" and enter this code