Bitcoin

Bitcoin
Bitcoin

Recursion

The repeated application of a recursive procedure or definition. It is the process by which a class or a method calls itself.
The base case returns a value without making any subsequent recursive calls. It does this for one or more special input values for which the function can be evaluated without recursion

For factorial(), the base case is n = 1. The reduction step is the central part of a recursive function.


public class FactorialImplements{
         public static void main(String args[]){
         int i,fact=1;
         int number=5;//It is the number to calculate factorial.
         for(i=1;i<=number;i++){
              fact=fact*i;
         }
        System.out.println("Factorial of "+number+" is: "+fact);
}

In fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0 and 1. There are two ways to write the fibonacci series program in javaFibonacci Serieswithout using recursion.

public class FibonacciSeries {
  // recursive declaration of method fibonacci
  public static long fibonacci(long number) {
    if ((number == 0) || (number == 1)) // base cases
      return number;
    else
      // recursion step
      return fibonacci(number - 1) + fibonacci(number - 2);
  }

  public static void main(String[] args) {
    for (int counter = 0; counter <= 10; counter++)
      System.out.printf("Fibonacci of %d is: %d\n", counter, fibonacci(counter));
  }
}

Implementation of queue

A Queue is a data structure that allows the first entry to be the first out. It uses the concept of first in first out.

import java.util.LinkedList;
import java.util.Queue; 
import java.util.Scanner;
public class Queue_Ex1 {
       Scanner scan;
       Queue<Integer> queue; 
       int n; 
       void insert() { 
              scan = new Scanner(System.in);  
              queue = new LinkedList<Integer>(); 
              System.out.println("Integer Queue - Insert & Delete");
              System.out.println("\nEnter 'n' value :"); 
              n = scan.nextInt(); 
              System.out.println("Enter the elements"); 
              for(int i=0; i<n; i++) {
                    queue.add(scan.nextInt()); } 
              } 
     void delete() { 
             System.out.println("\nThe Queue"); 
             while(!queue.isEmpty()) { 
                   System.out.println(queue.poll()); 
             }
     }
}
class MainClass {
     public static void main(String args[]) { 
           Queue_Ex1 obj = new Queue_Ex1(); 
           obj.insert(); obj.delete();; 
     } 
}

Problem Description: How to implement stack ?

A stack is a data structure that uses the concept of last in first out (LIFO). It follows the concept of plate stack because the first entry in a plate stack is the last to be retrieved.

Solution
Following example shows how to implement stack by creating user defined push() method for entering elements and pop() method for retrieving elements from the stack.


public class MyStack { 
          private int maxSize; 
          private long[] stackArray; 
          private int top; 
          public MyStack(int s) { 
                    maxSize = s; 
                    stackArray = new long[maxSize]; 
                    top = -1; 
         }
         public void push(long j) {
                   stackArray[++top] = j; 
         } 
         public long pop() { 
                   return stackArray[top--]; 
         } 
         public long peek() { 
                   return stackArray[top]; 
         }
        public boolean isEmpty() { 
                   return (top == -1);
        } 
        public boolean isFull() { 
                   return (top == maxSize - 1); 
        } 
        public static void main(String[] args) { 
                  MyStack theStack = new MyStack(10); 
                  theStack.push(10); 
                  theStack.push(20); 
                  theStack.push(30); 
                  theStack.push(40); 
                  theStack.push(50); 
                  while (!theStack.isEmpty()) { 
                            long value = theStack.pop(); 
                            System.out.print(value); 
                            System.out.print(" "); 
                  } 
                 System.out.println(""); 
        } 
}



A Spin Color cube using java3D

package Java3D;

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;
public class SpinCube {
    public SpinCube(){
        SimpleUniverse uni = new SimpleUniverse();
        uni.getViewingPlatform().setNominalViewingTransform();
        BranchGroup group = createSceneGraph();
       
        uni.addBranchGraph(group);
    }

    private BranchGroup createSceneGraph() {
        BranchGroup branch = new BranchGroup();
        TransformGroup trans = new TransformGroup();
        trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        branch.addChild(trans);
        ColorCube cube = new ColorCube(0.4);
        trans.addChild(cube);
        Alpha spin = new Alpha(-1,1000);
        RotationInterpolator rotate = new RotationInterpolator(spin,trans);
        rotate.setSchedulingBounds(new BoundingSphere(new Point3d(),1000.0));
        trans.addChild(rotate);
        branch.compile();
        return branch;
    }
    public static void main(String[] args){
        new SpinCube();
    }
}

A Splash Screen which loads a login page by implementing runnables

package Execute;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JWindow;
import javax.swing.JLabel;
import javax.swing.ImageIcon; 
import javax.swing.JProgressBar; 
import javax.swing.Box; 
import javax.swing.border.Border;

public final class Splash implements Runnable{
    JWindow win;
    Toolkit kit=Toolkit.getDefaultToolkit();
    Image img;
    JLabel lab;
    ImageIcon ico;
    JProgressBar pr;
    Box b=Box.createVerticalBox();

    public Splash(String file){
        start();
        win=new JWindow();
        pr=new JProgressBar(0,100);//the animated progressbar
        Border bor=BorderFactory.createEtchedBorder(5, Color.lightGray, Color.yellow);//Border color
        ico=new ImageIcon(file);//adding image using an imageicon class
        lab=new JLabel();//label added to the JWindow
        lab.setIcon(ico);//Sets the icon of the label as the imageicon
        pr.setStringPainted(true);//Setting the progressBar visible
        b.add(lab);
        b.add(pr);
        b.setBorder(bor);//Sets the border of the Box that contains other java objects
        win.add(b);
        win.setSize(300,200);//Sets the size of the JWindow
        win.setLocation(400,200);//set the location of the JWindow to be displayed
        win.setVisible(true);//Set JWindow visible
    }
    public static void main(String[] arg){
        new Splash();
    }
    public void run() {
        try{
            int current=0;
            for(int i=0;i<=100;i++){
                current=i;
                pr.setValue(current);
                Thread.sleep(30);
            }
            if(current == 100){
                home = new Login();//run this when the splash screen reaches hundred
                home.setTitle("THE LOGIN PAGE THAT ENABLES YOU MAKE USE OF THE APPLICATION");
                home.setVisible(true); set the login page true
                win.setVisible(false);//sets the splash screen visibility false
            }
        }
        catch(Exception e){
              e.printStackTrace();
         }
    }
    public void start(){
        Thread tr=new Thread(this);
        tr.start();
    }
    private Login home;
}

A java code to encrypt and decrypt a written text

package encryption;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Sambyte
 */
public class EncryptionWork extends JFrame implements ActionListener{
    JButton encrypt = new JButton("ENCRYPT FILE"), decrypt = new JButton("DECRYPT FILE"), cancel = new JButton("CANCEL"),
                send = new JButton("SEND MESSAGE");
    private static final String ALG = "AES"; //AES is the encryption algorithm
    private static  String Key = "maryhasonecat123";//This is the key used for the encryption
    JTextArea area = new JTextArea();
    public EncryptionWork(){
        add(new JLabel("ENCRYPTION PORTAL"), "North");
        JPanel p = new JPanel();
     
        p.add(encrypt);p.add(decrypt);p.add(cancel);p.add(send);
        send.setEnabled(false);
        encrypt.addActionListener(this);decrypt.addActionListener(this);cancel.addActionListener(this);
        send.addActionListener(this);
        JInternalFrame f = new JInternalFrame();
        f.setLayout(new BorderLayout());
        f.add(p,"North");
     
        f.add(new JScrollPane(area));
        f.setVisible(true);
        add(f);
        setSize(700,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args){
        new EncryptionWork();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == encrypt){
         
                try{
                area.setText(encrypt());
                }catch(Exception ex){ex.printStackTrace();}
         
        }
        else if(e.getSource() == decrypt){
         
                try{
                    String v = decrypt(area.getText().trim());
                area.setText(v);
                }catch(Exception ex){ex.printStackTrace();}
         
        }
        else if(e.getSource() == cancel){
         
        }
        else if(e.getSource() == send){
         
        }
    }
    private static Key generateKey(){
        Key key = new SecretKeySpec(Key.trim().getBytes(), ALG);
        return key;
    }
//This method encrypt what is written in the JTextArea
    public  String encrypt() throws Exception{
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALG);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypt1 = cipher.doFinal(area.getText().trim().getBytes());
        String encryptedValue = new BASE64Encoder().encode(encrypt1);
        return encryptedValue;
    }
//This is the method to decrypt the encrypted file
    public String decrypt(String value) {
        String decrypting = null;
        try{
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALG);
        cipher.init(Cipher.DECRYPT_MODE,key);
        byte[] decrypt1 = new BASE64Decoder().decodeBuffer(value);
        byte[] decrypted = cipher.doFinal(decrypt1);
        decrypting = new String(decrypted);
     
        }catch(Exception e){decrypting = e.getMessage();}
        return decrypting;
    }
 
}

Java code to display 3D spherical ball

//NOTE: You must download java 3D if you IDE does not support it
// From http://download.java.net/media/java3d/builds/release/1.5.1/README-download.html

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

/**
 *
 * @author Sambyte
 */
public class Ball {
    public Ball(){
        SimpleUniverse v=new SimpleUniverse();//To create an object of the virtual environment
        BranchGroup g = new BranchGroup();//The child of the simpleUniverse to hold all siblings
        Sphere s = new Sphere(0.5f);// Object of the sphere with a float as the radius
        g.addChild(s);// adding the sphere object to the branchgroup
        Color3f l = new Color3f(1.8f,0.1f,0.1f);//color parameters of the sphere
        BoundingSphere sh = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
        Vector3f ve= new Vector3f(4.0f,-7.0f,-12.0f);
        DirectionalLight light = new DirectionalLight(l,ve);//direction viewing light in the virtual world
        light.setInfluencingBounds(sh);
        g.addChild(light);
        v.getViewingPlatform().setNominalViewingTransform();
        v.addBranchGraph(g);
    }
    public static void main(String[] args){
        new Ball();
    }
}

Java code to display numbers from 85 to 5 at the interval of 5

public class MultipleOfFive {
    public static void main(String[] args){
        int i = 90;
        do{
            i = i - 5;
            System.out.print(i + " ");
        }while(i  > 5);
    }
}

Facebook