Posts

Showing posts from August, 2012

Ask for more

If anyone is having any trouble in developing the program in any language, is humbly welcome. Post your comments

Property Event-Delegation

import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Color; import java.awt.EventQueue; import java.awt.BorderLayout; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; class BoundFrame extends JFrame {     private JButton button1;     private JButton button2;     private Random random;         public BoundFrame() {         initialize();     }         private void initialize() {         button1 = new JButton("Select Me");         button2 = new JButton("No Select Me");         random = new Random();                 button1.addActionListener(new Butt...

Moving Banner on Window

import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.MouseMotionAdapter; public class MoveBannerFrame extends JFrame {     public MoveBannerFrame() {         BannerLabel lbl = new BannerLabel();                 lbl.setFont(new Font("TimesRoman", Font.BOLD, 32));         lbl.setForeground(Color.RED);                 add(lbl);     }     public static void main(String[] args) {         MoveBannerFrame frame = new MoveBannerFrame();         frame.setTitle("Mouse Motion");         frame.setSize(1000, 600);         frame.setLoc...

Largest palindrome multiple of two three digit numbers

public class LargestPamildromeof3Digits {     public static int reverse(int n) {         int num = 0;         int size = String.valueOf(n).length() - 1;                 while (n > 0) {             num += (n % 10) * (int)Math.pow(10, size--);             n /= 10;         }                 return num;     }     public static boolean isPalindrome(int n) {         if (n == reverse(n))             return true;         else             return false;     }       ...

It's Magic...................

import javax.swing.JOptionPane; public class MagicOf9 {     public static void main(String[] args) {         String msg = "Think and write somewhere n digit number, \n" +             "note that all digits must be unique \nfor e.g. for three" +             " digit number I am assuming 123, \nwhere 1, 2, and 3 are " +             "different and unique digits. \nNow add all the digits in " +             "the number \nfor e.g. in our case it is 1 + 2 + 3. \nThe sum " +             "is 6. \nNow subtract this resultant (i.e. 6 in this example) \n" +             "from the supposed number. \nEnter your any (n - 1) digits like \n" +           ...

Method with variable arguments

class VarArgs {     public static void main(String[] args) {         System.out.println(sum(1));                System.out.println(sum(1, 2));                System.out.println(sum(1, 2, 3));                System.out.println(sum(1, 2, 3, 4));            }         static int sum(int...args) {         int sum = 0;             for (int i = 0; i < args.length; i++)             sum += args[i];                     return sum;     } }

Zeller's Congruence

class ZellersCongruence {     /**     * Calculate day of week using Zeller's Congruence     *     * For the Gregorian calendar     * h =(q + floor(13(m + 1) / 5) + K + floor(K / 4) + floor(J/ 4) + 5J) % 7     *     * For Julian calendar     * h =(q + floor(13(m + 1) / 5) + K + floor(K / 4) + 5 + 6J) % 7     *     * where     * h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ...)     * q is the day of the month     * m is the month (3 = March, 4 = April, 5 = May, ..., 14 = February)     * K the year of the century (year % 100).     * J is the century (actually floor(year/100)) (For example, in 1995 the century would be 19, even though it was the 20th century.)     *     * NOTE: In this algorithm January and February are ...

Fabonnaci series using recursion

class Fibonacci {     static int fib(int n) {         if (n == 0)             return 0;         else if(n == 1)             return 1;         else                return fib(n - 1) + fib(n - 2);     }     public static void main(String[] args) {         for (int i = 0; i <= 10; i++)             System.out.println(fib(i));     } }

Getting System Properties

import java.util.Properties; class SystemProperties {     public static void main(String[] args) {         Properties props = System.getProperties();         props.list(System.out);     } }

Calendar on your console

class PrintCalendar {     public static void main(String[] args) {         int month = 0, year = 0;         try {             month = Integer.parseInt(args[0]);             year = Integer.parseInt(args[1]);             printMonth(year, month);         } catch(Exception e) {             System.out.println("USEAGE: java PrintCalendar <month> <year>");         }     }         public static void printMonth(int year, int month) {         printMonthTitle(year, month);         printMonthBody(year, month);     }         publi...

Prime Palindromes

class PrimePalindrome {     static boolean isPrime(int n) {         for (int j = 2; j <= (int)Math.sqrt(n); j++) {             if (n % j == 0)                 return false;         }         return true;     }         static int reverse(int n) {         int num = 0;                int y = String.valueOf(n).length() - 1;                 while (n > 0) {             int r = n % 10;             num += r * (int)Math.pow(10, y--);             n /= 10; ...

EMIRP

class Emrip {     static boolean isPrime(int n) {         for (int i = 2; i <= (int)Math.sqrt(n); i++)             if (n % i == 0)                 return false;                 return true;     }        static int reverse(int n) {         int y = String.valueOf(n).length() - 1;         int num = 0;                 while (n > 0) {             int r = n % 10;             num += r * (int)Math.pow(10, y--);             n /= 10;         }   ...

Reversing a number using loop

class ReverseNumber {     public static void main(String[] args) {         int number = Integer.parseInt(args[0]);         int temp = number;         int n = 0;         int counter = args[0].length() - 1;                 while (number > 0) {             int rem = number % 10;             //System.out.print(rem);             n += rem * (int)Math.pow(10, counter--);             number = number / 10;         }                 if (n == temp)             System.out.println(n + " is palin...

Reversing a number using String and StringBuffer

class ReverseNumber2 {     public static void main(String[] args) {         StringBuffer number = new StringBuffer(args[0]);         int num = Integer.parseInt(number.reverse().toString());         System.out.println(num);     } }

Guess my Birthday?

import java.util.Scanner; import javax.swing.JOptionPane; class Birthday {     public static void main(String[] args) {         int birthDay = 0;                 Scanner input = new Scanner(System.in);                 // Table 1         int answer = JOptionPane.showConfirmDialog(null, "1  3  5  7\n9 11 13 15\n17 19 21 23\n25 27 29 31");         if (answer == 0)             birthDay += 1;                 // Table 2         answer = JOptionPane.showConfirmDialog(null, "2 3 6 7\n10 11 14 15\n18 19 22 23\n26 27 30 31");         if (answer == 0)          ...

Input/Output using JOptionPane (GUI method)

import javax.swing.JOptionPane; class InputGUI {     public static void main(String[] args) {         byte num1 =             Byte.parseByte(                 JOptionPane.showInputDialog(                     null, "Enter first number", "0")             );         byte num2 =             Byte.parseByte(                 JOptionPane.showInputDialog(                     null, "Enter second number", "10")             );            ...