Thursday, 23 November 2017

Bresenham's line drawing algorithm program in Java

/*
    Bresenham's Line Drawing Algorithm
    Created By : Pirate
*/

import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.*;
public class Line extends JFrame implements Runnable{
int a,b,x,y,p=0,q=0;

public static void main(String[] args) {
                 System.out.println("*** Bresenham Line Algorithm ***");
                 System.out.println("\nEnter the starting point of line");
                 Scanner scan = new Scanner(System.in);
                 Line line = new Line();
                 line.a = scan.nextInt();
                 line. b = scan.nextInt();
                 System.out.println("\nEnter the ending point of line");
                 line.x = scan.nextInt();
                 line.y = scan.nextInt();
 Thread t= new Thread(line);
 t.start();
 scan.close();
}
public Line(){
                 this.pack();
                 this.setVisible(true);
                 this.setTitle("Wave The World");
                 this.setSize(400, 400);
                 this.setContentPane(getContentPane());
                 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
        public void paint(Graphics g) {
                 super.paint(g);
                 g.drawLine(a, b, p, q);
        }

public void putPixel(int x,int y){
p=x;
q=y;
repaint();
}

public void run(){
      Bre_Line();
}

public void Bre_Line(){
int x1=a;
                int y1=b;
                int dx, dy;
                x2=x;
                y2=y;
dx=x2-x1;

if(dx < 0){
dx = dx*(-1);
}

dy=y2-y1;

if(dy < 0){
dy= dy*(-1);
}
int temp;
if(dx<dy){
temp = dx;
dx = dy;
dy = temp;
}

                int p=2*dy-dx;
                i=dx;
                while(i>0){
             try{
                Thread.sleep(100);
             }catch(Exception e){
                                    System.out.println(e.getMessage());
             }
                         putPixel(x1,y1);
                         if(p<0)
                         {
                                   x1=x1+1;
                                   p=p+2*dy;
                 }
                 else
                {
                           x1=x1+1;
                   y1=y1+1;
                   p=p+2*dy-2*dx;
                }
            i--;
    }
      }
}

Output :









Enjoy :)

Monday, 13 November 2017

How to get value from comma separated string in oracle

Hi Friends,

Today I'm here with a new program. The problem is that we have given a string with comma separated values i.e 'wave,the,world,oracle,program,' and we have to extract values from the string.


So the input value will be,

Input : 'wave,the,world,oracle,program,'

and output will be like this,
Output :
wave
the
world
oracle
program

Let's start, we are going to use following Oracle builtin function in the program.








length()

translate()
substr()
instr()

So first we will understand the working of these functions.



  • length() : The Oracle length() function returns the length of the specified string.

          i.e. select length('wave the world') from dual;
          Result : 14


  • translate() : The Oracle translate() function replaces a sequence of character in a string with another set of characters. It replaces a single character at a time. i.e it will replace the first character in the string_to_replace with the first character in the replacement_string. The it will replace the second character and so on.

          i.e select translate('wave the world','e ','AS') from dual;
          Result : wavASthASworld


  • substr() : The Oralce substr() function allows you to extract a substring from a string.
          i.e select substr('wave the world',0,4) from dual;
          Result : wave







  • instr() : The function returns the location of a substring in a string.
          i.e select instr('wave the world','the') from dual;
          Result : 6

Now we will create a pl/sql block to do the job by using all these functions.



declare
    comma varchar2(20);
    st varchar2(50):='wave,the,world,oracle,program,';
    st_temp varchar2(20);
begin
    comma:=length(st) -length(translate(st,'A,','A'));
    for i in 0.. comma - 1 loop
        st_temp := substr(st,0,instr(st,',')-1);
        dbms_output.put_line(st_temp);
        st:= substr(st,instr(st,',') + 1);
    end loop;
end;



When will you run this pl/sql block you will get the desired output.








Output :
wave
the
world
oracle
program



How this code works? 
First we count the no of comm in the string and run a for loop. Everytime we cut the string that is printed and loop contine run till the all values from string printed.

Share your thoughts in comment section.

Enjoy :) 


Thursday, 2 November 2017

Program to implement Insertion Sort in C

/*
   Insertion Sort
   Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
void insertion(int items[],int n);
void main(){
    int items[500],n,i;

    printf("*** Insertion Sort ***\n");
    printf("\nEnter the number of elements: ");
    scanf("%d",&n);

    printf("\nEnter the %d elements: \n",n);
    for(i = 0; i < n; i++){
        scanf("%d",&items[i]);
    }

    printf("\n\nBefore Sorting:\n");
    for(i = 0; i < n; i++){
        printf("%d\t",items[i]);
    }

    insertion(items,n);

    printf("\n\nAfter Sorting:\n");
    for(i = 0; i < n; i++){
        printf("%d\t",items[i]);
    }
    getch();
}

void insertion(int items[],int n){
    int i,j,temp;
    for(i=0;i<n;i++){
        temp = items[i];
        for(j = i-1; j >= 0; j--){
            if(temp<items[j]){
                items[j+1]=items[j];
            }
            else{
                break;
            }
        }
        items[j+1] = temp;
    }

}






Output :


Program to implement Bubble Sort in C

/*
   Bubble Sort Algorithm
   Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
void bubble(int items[],int n);
void main(){
    int items[500],n,i;

    printf("*** Bubble Sort ***\n");

    printf("Enter the number of elements: ");
    scanf("%d",&n);

    printf("\nEnter the elements: \n");
    for(i = 0; i < n; i++){
        scanf("%d",&items[i]);
    }

    printf("\n\nBefore Sorting:\n");
    for(i = 0; i < n; i++){
        printf("%d\t",items[i]);
    }
    bubble(items,n);

    printf("\n\nAfter Sorting:\n");
    for(i = 0; i < n; i++){
        printf("%d\t",items[i]);
    }
    getch();
}
void bubble(int items[],int n){
    int i,j,temp;
    for(i = 0; i < n; i++){
        for(j = 0; j < n-1; j++){
            if(items[j] > items[j+1]){
                temp = items[j];
                items[j] = items[j+1];
                items[j+1] = temp;
            }
        }
    }

}






Output :