Posts

Showing posts with the label java programming

Servlet Program to Print Today’s Date and Time using refresh header

Simple Java Servlet Program to Print Date and Time using refresh header (Time updates automatically) Code: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class servlet2 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { rsp.setIntHeader("Refresh", 1); rsp.setContentType("text/html"); PrintWriter out = rsp.getWriter(); Date now = new Date(); out.println("<html>"); out.println("<head><title> Time Check </title></head>"); out.println("<body>"); out.println("<p text-align=auto>Current Time: " + now + "</p>"); out.println("</body></html>"); } }

Java program to implement Word Count using Map-Reduce in Hadoop Cluster

Simple Java program to implement Word Count using Map-Reduce in Hadoop Cluster Code: import java.io.IOException; import java.util.StringTokenizer;  import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;   public class wc {     public static class TokenizerMapper     extends Mapper<Object, Text, Text, IntWritable>{   private final static IntWritable one = new IntWritable(1); private Text word = new Text();   public void map(Object key, Text value, Context context                 ) throws IOException, InterruptedException {   StringTokenizer ...

Java program to implement Apriori Algorithm

Simple Java program to implement Apriori Algorithm Code: package apriori; import com.sun.xml.internal.ws.util.StringUtils; import java.util.*; import java.lang.Object; public class Apriori { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Number Of Transactions:"); int n = sc.nextInt(); System.out.println("Enter Minumum Limit:"); int min = sc.nextInt(); sc.nextLine(); ArrayList<Set<String>> aray = new ArrayList<>(); Set<String> list = new HashSet<>(); Set<String> list1= new HashSet<>(); Set<String> list2,list3,list4; System.out.println("Enter Item id saperated by space per transaction"); for(int i=0;i<n;i++){ System.out.println("Entere Item Id included in transaction-"+(i+1)); String tempp =sc.nextLine(); Se...

Java program to check the given string is palindrome or not without using Reverse String

Simple Java program to check the given string is palindrome or not without using Reverse String Code: import java.util.Scanner; class p84{public static void main(String args[]){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); boolean palindrome=true; int j=(s.length()-1); for(int i=0;i<(s.length()/2);i++){ if(s.charAt(i)!=s.charAt(j)){palindrome=false;} j--; } if(palindrome){System.out.println("palindrome");} else{System.out.println("Not palindrome");} }}

Java program to count words starting with capital letters in given line

Simple Java program to count words starting with capital letters in given line Code: import java.util.Scanner; class p83{public static void main(String args[]){ Scanner sc=new Scanner(System.in); String s; System.out.println("Enter a line:"); s=sc.nextLine(); int count=0; for(String str: s.split(" ")) { if(str.charAt(0)>=65 && str.charAt(0)<=90) { count++; } } System.out.println("total number of words start with capital letters are :"+count); }}

Java program to calculate occurrence of consonants and vowels in given line

Simple Java program to calculate the number of consonants and vowels present in given line Code: import java.util.Scanner; class p82{public static void main(String args[]){ String s = new String(); int co=0,cs=0; Scanner sc = new Scanner(System.in); s=sc.nextLine(); for(int i=0;i<s.length();i++){ char c = s.charAt(i); if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){co++;} else if(!(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U') && ((c>=65 && c<=90) || (c>=97 && c<=122))){cs++;} } System.out.println("Vowels: "+co); System.out.println("Consonant: "+cs); }}

Java program to implement concept of Abstract Class

Simple Java program to implement concept of Abstract Class Code: abstract class phone{ void rearcamera(){System.out.println("Capturing from rear camera");} void rflesh(){System.out.println("Capturing from rear camera");} void frontcamera(){System.out.println("Capturing from front camera");} abstract void fflesh(); } class model extends phone { void fflesh(){System.out.println("No front flesh available");} } class abstracte{ public static void main(String arg[]){ model e70 = new model(); e70.rearcamera();e70.rflesh();e70.frontcamera();e70.fflesh(); } }

Java program to implement use of Final Keyword on Class, Method and Variable

Image
Simple Java program to use Final Keyword on following items: Variable Class Method Code: class a{ final int a=5; void setfinala(int x) { a=x; } final void finalmethod() { System.out.println("Final method."); } } class a1 extends a{ void finalmethod() { System.out.println("Final method in extended class."); } } final class b{ } class c extends b{ } class p33{ public static void main(String args[]){ a obj = new a(); a.setfinala(3); } } Note: The program won't run because of error and that error is output of our program.

Java program to implement concept of Inner Class

Simple Java program to implement use of Inner Class Code: class Outer{ private int data=30; class Inner{ void msg(){System.out.println("data is "+data);} } } class innersample{ public static void main(String args[]){ Outer obj=new Outer(); Outer.Inner in=obj.new Inner(); in.msg(); } }

Java program to check palindrome number using recursion

Simple Java program to check whether the number is palindrome or not by using Recursion. Code: import java.util.Scanner; class xyz{ int pd(int i,int j,int[] a){ while(i<j){ if(a[i]==a[j]){i++;j--;pd(i,j,a);} else{return 0;} } return 1; } } class palindrome{ public static void main(String args[]){ int x,i=0; System.out.println("Enter integer: "); Scanner sc = new Scanner(System.in); x=sc.nextInt(); int a[]=new int[10]; while(x!=0){ a[i]=x%10; x=x/10; i++; } xyz obj = new xyz(); if(obj.pd(0,i-1,a)==1){System.out.println("palindrome");} else{System.out.println("Not Palindrome");} } }

Java program to implement patterns

Simple Java program to implement the following pyramid pattern. 1 2 2 3 3 3 4 4 4 4    and 4 3 3 2 2 2 1 1 1 1 Code: public class pattern{ public static void main(String srgs[]){ System.out.println("first pattern"); int a=4; for(int i=1;i<=4;i++){ System.out.println(" "); for(int j=1;j<=i;j++){System.out.print(" " +i);} } System.out.println(" "); System.out.println("Second pattern"); for(int i=4;i>=1;i--){ System.out.println(" "); for(int j=1;j<=i;j++){System.out.print(" " +i);} } }} Si...

Java servlet program to find larger number of given two numbers

Simple Java servlet program which accepts two numbers using the post method and displays the smaller one.  Code: import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class server extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); try (PrintWriter out = response.getWriter()) { int sum; int x,y; String a = request.getParameter("num1"); String b = request.getParameter("num2"); x = Integer.parseInt(a); y = Integer.parseInt(b); if(x>y){ out.println("First number "+x+" is greater"); } else{ out.println("Secnd number "+y+" is greater"); } } } }

Menu driven Java JDBC program with PL/SQL

Create a Menu driven program in JDBC (USE appropriate statement, preparestatement or callable statement object as an when require) Requirements: Display all the Records from the both tables by employee_number Insert Record into the Table. (Hint:- create stored procedure insertdata() in PL/SQL to insert all the field in both tables. Call the procedure from java application using callablestatement) Update The existing record by asking the useremployee_number) (HINT:- Take Updable Resultset) Delete Record by asking the user employee_number)) (HINT:- Take Updable Resultset) Program should handle SQLException and SqlWarning. Code: package aj.pkg13; import com.sun.swing.internal.plaf.basic.resources.basic; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.HashSet; import java.util.Scanner; public class AJ13 { public static void main(String[] args) { Scanner sc ...

Java JDBC program to perform CRUD Operation on table using Updatable resultset

Simple Java JDBC program to perform all CRUD operation on data stored in student table using updable resultset. Code: package aj.pkg12; import java.sql.*; import java.util.*; public class AJ12 { public static void main(String args[])throws Exception{ int i=0 , op=0; Float r; String s = null,str = null; String ch = null; Class.forName("com.mysql.jdbc.Driver"); Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root",""); Scanner sc= new Scanner(System.in); Statement stm = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs = null; do{ System.out.println("Which operation do you want to perform ?\n1.Create Records\n2.Read Records\n3.Update Records\n4.Delete Records"); op = sc.nextInt(); switch(op){ case 1: rs = stm.executeQuery("Select * from student_table"); System.out.println("Enter RollNumber : "); i = sc.nextInt(); sc.nextLine(); System.out.pr...

Java JDBC program to implement CRUD Operation without Updatable Resultset

JDBC program to perform all CRUD operation on data stored in student table, containing the columns for roll_number number, name varchar2(50), and result in percentage without using Updatable resultset Code: package aj.pkg11; import java.sql.*; import java.util.*; public class AJ11 { public static void main(String args[])throws Exception{ int i=0 , op=0; Float r; String s = null,str = null; String ch = null; Class.forName("com.mysql.jdbc.Driver"); Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root",""); Scanner sc= new Scanner(System.in); PreparedStatement pstm = cn.prepareStatement("Insert into student_table values (?,?,?)"); PreparedStatement dstm = cn.prepareStatement("Delete from student_table where Roll_Number = ?"); Statement ustm = cn.createStatement(); Statement stm = cn.createStatement(); do{ System.out.println("Which operation do you want to perform ?\n1.Create Records\n2.Re...

Java program to demonstrate UDP Communication with example of country capital name

Simple java program for UDP Communication where client will send name of country and server will return the capital of that country. Code: Server: package aj.pkg9; import java.io.*; import java.net.*; import java.util.HashMap; import java.util.Map; class Server { public static void main(String args[]) throws Exception { Map<String, String> map = new HashMap<String,String>(); map.put("INDIA", "DELHI"); map.put("NEPAL", "KATHMANDU"); map.put("SHRI-LANKA", "COLUMBO"); map.put("BANGLADESH", "DHAKA"); map.put("CHINA", "BEIJING"); DatagramSocket serverSocket = new DatagramSocket(1111); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String( receivePacket.getData(),0,5); InetAddress IPAddress = receiveP...

Java client server program to read text file using Socket Programming

Simple java network program in which client send a text file to server and server returns number of characters, lines, words and sentences of that file to client. Code: Server: package aj.pkg8; import java.net.*; import java.io.*; public class aj8server { public static void main (String [] args ) throws IOException { ServerSocket serverSocket = new ServerSocket(1111); Socket socket = serverSocket.accept(); File transferFile = new File ("sample.txt"); byte [] bytearray = new byte [(int)transferFile.length()]; FileInputStream fin = new FileInputStream(transferFile); BufferedInputStream bin = new BufferedInputStream(fin); bin.read(bytearray,0,bytearray.length); OutputStream os = socket.getOutputStream(); System.out.println("Sending Files..."); os.write(bytearray,0,bytearray.length); os.flush(); socket.close(); } } Client: package aj.pkg8; import java.net.*; import java.io.*; public class aj8client{ public static void main (String [] args) throws IOException { int ...

Java Program to Implement sorting algorithm using TCP on Server application

Simple Java program to Implement sorting algorithm using TCP on Server application Input: Give input on client side and client should receive sorted output from server and display sorted list on input(client) side. Note: You can implement any sorting algorithm by changing the sorting algorithm code on server side Code: Server: package aj.pkg7; import java.io.*; import java.net.*; import java.util.*; class Server { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(1111); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); DataOutputStream dout=new DataOutputStream(s.getOutputStream()); int r,i=0; int n=din.readInt(); int a[]=new int[n]; System.out.println("data:"); int count=0; for(i=0;i<n;i++) { a[i]=din.readInt(); } int number = a.length; for (int i1 = 0; i1 < number-1; i1++) for (int j1 = 0; j1 < number-i1-1; j1++) if (a[j1] > a[j1+1]) { int temp = a[j1]; a[j1] = a[j1+1]; a[j1+1] = temp; } ...

Java code to implement Math Server with multi client support using socket programming

Simple Java Math Server (Factorial, Fibonacci, Prime, and Palindrome) with multi client support using socket programming Code: Server: package javaapplication7; import java.io.*; import java.net.*; import java.util.Scanner; import java.lang.String; public class MyServer{ public static void main(String args[]){ try{ ServerSocket ss = new ServerSocket(1111); Socket s = ss.accept(); DataInputStream dis = new DataInputStream(s.getInputStream()); DataOutputStream dout=new DataOutputStream(s.getOutputStream()); dout.writeUTF("Math Server"); dout.writeUTF("Enter your option from below:"); dout.writeUTF("1.Factorial of the number"); dout.writeUTF("2.Fibonacci series "); dout.writeUTF("3.Check prime or not"); dout.writeUTF("4.Check the string is palindrome or not"); int option=Integer.parseInt(dis.readUTF()); switch(option){ case 1: { dout.writeUTF("Enter the number for factorial:");dout.flush(); int number=Integer.pars...

Java chat application using TCP protocol for Client and Server

Simple Java chat application using TCP protocol with coeds for Client and Server to send Messages in string format Code: Server Code: package clientserver; import java.io.*; import java.net.*; class server { public static void main(String[] args) throws Exception { ServerSocket sersock = new ServerSocket(3000); Socket sock = sersock.accept( ); BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); OutputStream ostream = sock.getOutputStream(); PrintWriter pwrite = new PrintWriter(ostream, true); InputStream istream = sock.getInputStream(); BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); String receiveMessage, sendMessage; while(true) { if((receiveMessage = receiveRead.readLine()) != null) { System.out.println("Client:"+receiveMessage); } sendMessage = keyRead.readLine(); pwrite.println(sendMessage); pwrite.flush(); } } } Client Code: package clientserver; import java.io.*; import java.net.*; public class client { publ...