Posts

Showing posts with the label jj

AWT Program to display the given input on button click

Image
This is a program which will accept the input from the keyboard and on clicking the button, it will show the given input. This program is compiled in Eclipse. This is a basic program in Java using AWT where a single TextField, Label and a Button is used. Screenshot The Code: package dProBuk; import java.awt.Button; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class SimpleLayoutAWT implements ActionListener { TextField t; Label l; Button b; public SimpleLayoutAWT() { // TODO Auto-generated constructor stub Frame f=new Frame("Simple Frame"); t=new TextField("Enter the Name"); l=new Label(""); b=new Button("Show !"); f.setLayout(new GridLayout(3,1)); f.setVisible(true); f.setSize(500,200); f.add(t); f.add(b); ...

A simple program to check anagrams in java

Image
This is a simple program in Java. This program can be used to check if the two given strings are anagram. Two given strings are said to be anagrams when they have equal length and same characters are present in both the Strings. An example of anagram strings is SILENT and LISTEN ! Screenshot The Code: import java.util.*; public class Anagram { public static boolean isAnagram(String s1,String s2) { if(s1.length()!=s2.length()) { return false; } s1=sortCharacters(s1); s2=sortCharacters(s2); return s1.equals(s2); } public static String sortCharacters(String str) { char[] c=str.toLowerCase().toCharArray(); Arrays.sort(c); return String.valueOf(c); } public static void main(String arg[]) { String s1,s2; Scanner sc=new Scanner(System.in); System.out.println("enter 2 strings"); s1=sc.nextLine(); s2=sc.nextLine(); if(isAnagram(s1,s2)) System.out.println(s1+"is an anagram"+s2); else System.out.println(s1+"is...

Creating a Calculator panel using AWT in Java

Image
The Real power of java comes into the picture in AWT and Applet in java. We can create a popup window and program it according to our use, just like real life applications in the computers. It is a simple design of a calculator using AWT and Swing. Screenshots: Calculator panel in ubuntu Calculator panel in Windows The Code: import java.util.*; import java.awt.*; import javax.swing.*; class calculator extends Frame { Panel number,text_box,operation,buttons; JFrame cal; /*Creating and object for JFrame*/ /*Creating Buttons and TextBox reference variables*/ Button one,two,three,four,five,six,seven,eight,nine,zero,dot,plus,minus,mul,div,percentage,factorial,log,Ln,brac1,brac2; TextField box; public calculator() { /*Configuring frame of the window and Heading, Layout, close button action*/ cal = new JFrame(); cal.setTitle("CALCULATOR"); /*Title of the window*/ cal.setSize(500,500); /*size of the window in pixels*/ cal.setLayout(new F...

Simple animation using jQuery

Image
This is a post in which the demonstration of various types of animations in jQuery is shown. In this, when the button is clicked, the div scales it self then inside the div, the font also scales it's size. Demo Start Animation jQuery Animation The Code <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button#bDemo").click(function(){ $("div#divDemo").animate({left:"170px",height:"190px",width:"400px",opacity:"0.5"},"slow"); $("div#divDemo").animate({fontSize:"5em"},"slow"); }); }); </script> <style> #bDemo{ color: #fff !important; background-color: #2196F3 !important; } #divDemo{ border:1px solid #555; border-radius:3px; color:white; background:#555; height:20px; width:120px; position:relative; } </style> <button id=...

Simple equation solver in JS

Image
This post will let you create a simple equation solver. This program is designed using JS and HTML. This equation solver can be used to solve addition, division, subtraction or multiplication between two numbers. Screenshot Things this code will do Create a Form Take Input from the user A button when click, calls a function Function determines which type of operation and appends the Answer. The Code <!--The CSS For the Page--> <style> #ans { position: relative; text-align: center; top: 200; } .form { position: relative; text-align: -webkit-center; padding: 110; } .form input[type="text"] { padding: 10 10 2 10; border-bottom: 2px; border-style: solid; border-left: none; border-top: none; border-right: none; } .form input:focus,button:focus{ outline: 0; } .form button { padding: 2; background-color: white; border-bottom-color: black; border-style: solid; border-left: none; border-right:...

Getting Form Data through Java Script

Image
This is a simple code snippet in which you will be able to retrieve data from the form using JS. This is a basic Js stuff, which you should know how to use. Basically in this code snippet, there in a HTML form element which has 2 text type input box and a button which calls the JS function. Screenshot The JS Function has a Form Element which takes the values from the input elements and then appends it inside a paragraph and shows ! The Code <!--The CSS For the Page--> <style> #pclass { position: relative; text-align: center; top: 300; } .form { position: relative; text-align: -webkit-center; padding: 110; } .form input[type="text"] { padding: 10 10 2 10; border-bottom: 2px; border-style: solid; border-left: none; border-top: none; border-right: none; } .form button { padding: 5; background-color: white; border-left: none; border-right: none; border-top: none } </style> <!--The CSS Ends--...

Program to find strings are "anagram" or not

Image
                                 This is a program which uses java concept to find out the given strings are anagram or not. Well, anagram is a concept in which two strings will be having same alphabets but the spelling would be different. This program uses String class to declare objects and the methods used are accordingly to check the strings. Look at the sample output and the code given below. Sample Output: Program Code: import java.util.*; class AnagramEx { public static void main(String[] args)throws Exception { Scanner sc=new Scanner(System.in); System.out.println("Enter first string:"); String s1=sc.nextLine(); System.out.println("Enter second string:"); String s2=sc.nextLine(); Boolean flag=true; int i,j; i=s1.length(); j=s2.length(); if(i==j) { for (int k=0;k<i ;k++ ) { for(int l=0;l<i;l++) { if(s1.charAt(k)==s2.charAt(l)) {...

How multitasking or multi-threading is working in Java

Image
Java supports the multi tasking concept using time sharing concept of CPU. It switch from one task to another task in the midway. We can control the switching time using sleep or wait method. It's a totally random process, we cannot predict the order. Screenshot/Output: Using the try block ( sleep method) Without using the try block ( sleep method) The Code: class Demo1 extends Thread { public void run() { for(int i=0;i<10;i++) { System.out.println("Thread 1 "+i); /*try { Thread.sleep(200); } catch(InterruptedException e) { System.out.println(e); }*/ } } } class Demo2 extends Thread { synchronized public void run() { for(int i=0;i<20;i++) { System.out.println("\t.\t Thread 2 "+i); /*try { Thread.sleep(100); } catch(InterruptedException e) { System.out.println(e); }*/ } } } class Demo3 extends Thread { public void run() { for(int i=0;...

64 Bit Decimal to Binary Converter In Java

Image
Hey, Have you ever think about the simple basic things, the Binary. Yeah , whatever we do in any digital electronics device all are processed and stored in memory in binary. Its a basic. The conversion of decimal to binary.... Yeah, we all know about it. Its just division and reminder.. and repeat.. But how we efficiently do that... And we have limitation.. Of-course we have.. Even computers have.. A 64 bit binary in Decimal is around 10 to the power 19... yeah very large enough... Now we convert Decimal to Binary.. lets check the limitation... Screenshot / Output Showing the bit length and the binary equivalent of the decimal input The Code: import java.util.*; public class binary { public static void main(String arg[]) { System.out.print("Give the decimal Number: "); Scanner s = new Scanner(System.in); long n = s.nextLong(); long a[] = new long[64]; int i,j,k; for(i = 0; i<64; i++) { a[63-i] = (n%2); n=n/2; } fo...

Solve Determinant of N x N Matrix in Java

Image
This is a Java program which can be used to find the Determinant of any Matrix. This program involves transforming the given N x N matrix to a Upper Triangle form. Screenshot[Demo] The Code package dProBuk; import java.util.Scanner; public class Det { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s= new Scanner(System.in); int n; System.out.println("Give the value of N for [N x N]: "); n=s.nextInt(); double a[][]=new double [n][n]; //Storing Values in the Array ! for (int i=0;i<n;i++) for (int j=0;j<n;j++) a[i][j]=s.nextInt(); // Conversion of matrix to upper triangular for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) if(j>i) { double ratio = a[j][i]/a[i][i]; for(int k = 0; k < n; k++) a[j][k] -= ratio * a[i][k]; } } double det = 1.0;...

Binary Search using Java

Image
It is a technique to search an element in sorted array. In this type of search half of the array is ignored just after one comparison. Each time required element is compared with middle element of the array and range is decided . Best Case Performance :  O(1) Average Case Performance : O(log n) Worst Case Performance : O(log n) Demo Output: The Program import java.util.Scanner; public class BinarySearch1 { public static void main(String args[]) { int i, first=0, last, mid, no, element; Scanner s = new Scanner(System.in); System.out.println("\n How many elements:"); no= s.nextInt(); int arr[]= new int[no]; System.out.println("\n Enter the elements:"); for (i = 0; i < no; i++) arr[i] = s.nextInt(); System.out.println("\n Enter the element to be searched:"); element = s.nextInt(); last = no - 1; mid = (first + last)/2; while( first <= last ) { if ...

User Defined Packages In Java

Image
This post will demonstrate the creation of user defined packages and use them. Basically we will use a Math class and an IO class. These two classes will be included in a package sample . Math class will perform all basic add, mul, sub, div operations. On the other side, the IO class will handle with input and output operations. Screenshot[Final Output] The Code Section Firstly creating the Math.java file package sample; public class Math { public int add(int a, int b)   {     return a+b;   }   public int mul(int a, int b)   {     return a*b;   }   public int sub(int a, int b)   {     return a-b;   }   public int div(int a, int b)   {     return a/b;   } } Secondly creating the IO.java file package sample; import java.util.Scanner; public class IO {   Scanner s;   public IO()   {     s= new Scanner(System.in);   } ...

User Defined Exceptions in Java

Image
In this post you will learn to create your own, user defined exceptions. This program will handle unchecked exceptions, i.e those exceptions which cannot be checked by the compiler, or the run time exceptions. In this program there is a simple class AgeMargin in which when the age is less then 18 or greater then 24 generates an AgeException. Screenshots[Demo] The Code: import java.util.Scanner; class AgeException extends Exception {  public String toString()  {    return "AgeException Caught!";  } } public class AgeMargin {  public static void main(String []args)  {    int age;    Scanner s=new Scanner(System.in);    while(true)    {     try     {      System.out.println("Enter The Age: "); age=s.nextInt(); if(age>=18 && age<=24) { System.out.println("The age is w...

Shuffle the characters of a string in Java

Image
This is a program which accepts a string using the BufferdReader and InputStreamReader class, and then shuffle the characters present inside the string and generate a new string which is displayed in the console. Basically the characters of the string are stored in a Character List using the for each loop. Then the characters in that list is randomly removed and appended in a StringBuilder object output. Screenshots [Demo] The Code: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; class Brain { public static void main(String []as)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter The String: "); String input=br.readLine(); List<Character> characters = new ArrayList<Character>(); for(char c:input.toCharArray()){ ...

Read Input form HTML Form Element using JS

Image
This post will help you to fetch the value from any input tag in the FORM Element. The below code fetches the value of the text field when submit button is clicked and displays an JS alert counting the number of characters of the input string. Screenshots: The Code: <script type="text/javascript"> function callMe() { var nam=document.getElementById("myText").value; alert("Your name is "+nam+" and it has "+nam.length+" Chars"); } </script> <form onsubmit="callMe()"> <input type="text" placeholder="Enter your name!" id="myText"/><br/><br/> <input type="submit" value="Submit"> </form> Found any suggestions ? Comment them here !

Shuffle an Integer Array In Java

Image
This program can be used to shuffle an integer array using java. Basically user gives an integer array then this array is converted to an arraylist of integer type. Then that array list is passed to a predefined function shuffle() from the collection class ! Screenshot: The Code import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Shuffle { public static void main(String args[]) { List<Integer> dataList = new ArrayList<Integer>(); Scanner s=new Scanner(System.in); int n[]=new int[4]; for(int i=0;i<4;i++) { n[i]=s.nextInt(); dataList.add(n[i]); } /*Alternate Method: * for(int i=0;i<4;i++) dataList.add(s.nextInt()); */ Collections.shuffle(dataList); int[] num = new int[dataList.size()]; for (int i = 0; i < dataList.size(); i++) { num[i] = dataList.get(i); } System.out.println("Shuffled Array is : "); for (int i = 0; i < n...

Selection Sort Using Java

Image
This is a simple program to illustrate how to use selection sort for sorting integers using java code. Selection sort is an in-place sorting technique with time complexity of O(n2). Its time complexity remains same in all the 3 cases (i.e. best, average and worst) .This type of sorting is preferred where memory is limited. Screenshots: Program: import java.util.*; class Sorting { public static void selectionSort(int arr[], int size) { int i, j, pos=0,temp=0; for(i=0;i<size-1;i++) { pos=i; for (j = i+1; j < size; j++) { if (arr[j] < arr[pos]) pos = j; } // Swapping the elements temp = arr[i]; arr[i] = arr[pos]; arr[pos]= temp; } } public sta...

Simple Password Generator Using JS

Image
This is a simple password generator using javascript. This script generates random passwords of length 6. These code snippet can be used for those registration systems where user is assigned with an generated password for the first time. This password is based on 6 String sets which shuffle their index and generate a password ! Screenshots[Demo]: The Code: <script> function gene() { var set1='abcdefghi0',set2='jklmnopqu2',set3='ABCDEFG34s'; var set4='HIJtuv569w',set5='xJKL!@#$7',set6='8&_MNOPQRS'; var set7='TUVWXYZ012'; var pas=set1.charAt(Math.floor((Math.random() * 10) + 1))+ set2.charAt(Math.floor((Math.random() * 10) + 1))+ set3.charAt(Math.floor((Math.random() * 10) + 1))+ set4.charAt(Math.floor((Math.random() * 10) + 1))+ set5.charAt(Math.floor((Math.random() * 10) + 1))+ set6.charAt(Math.floor((Math.random() * 10) + 1))+ set7.charAt(Math.floor((Math.random() * 10) + 1)); prompt(...

Check whether a point lies within a given circle using java

Image
                          Hello guys, this a very interesting yet simple concept. Here what we are going to learn is to check whether a point lies inside a given circle or not. Here we will use BufferedReader  to take inputs and compare between the two inputs. The result value we will display in the output. Sample Output: Program Code: import java.io.*; class Circle { double x, y; double radius; Circle(double x1,double y1, double radius) { x=x1; y=y1; this.radius=radius; } void check(double x2, double y2) { double d1=Math.sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2)); if (radius>d1) { System.out.println("the point lies inside circle with "+x+","+y+" as centre"); } else System.out.println("the point doesnt lie inside the circle with "+x+","+y+" as centre and "+radius+" as radius"); } public static void main(String[] args) throws Exception { BufferedReader br=new BufferedRea...

Countdown Timer In Java Using Threading

Image
This is a simple Countdown program in Java which is done using threading. In this program the console is periodically cleared with a Console Object. The Basic design is to make the thread sleep for 1 sec till the give time is reached. The hours and Mins along with seconds are at first converted to seconds and a loop does the rest of the work ! Screenshot [Demo] The Code: import java.io.Console; import java.util.Scanner; public class timer { public static final char ESC = 27; public static void main(String as[])throws Exception { Scanner s=new Scanner(System.in); String tim; int hh,mm=0,ss=0; int sec; Console c = System.console(); if (c == null) { System.err.println("no console"); System.exit(1); } System.out.println("Enter In this format: \"HH:MM:SS\""); tim=s.next(); hh=Integer.parseInt(tim.substring(0,2)); mm=Integer.parseInt(tim.substring(3,5)); ss=Integer.parseInt(tim.substring(6,8)); sec=hh*3600+mm*60+ss; ...