Tuesday, June 20, 2017

Secent method implementation using perl language


#Md. Alamgir Hossain
#Dept. of Computer Science & Engineering
#Jessore University of Science & Technology
#Secent method implementation us   ing perl language......


#--------------------The given function ---------------------------
sub secentMethod
{
($x)=@_;
#return (($x*$x)-(5*$x)+2);
#return (2*$x-3-cos($x));
#return (($x*$x*$x) - (21*$x)+3500);
#return ($x*$x-5*$x+2);
#return ($x*$x)-(4*$x)-10;
return ($x*$x*$x)-(4*$x)-9;

}
$x0,$x1,$l1,$l2,$r,$f1,$f2,$f3,$iteration=5,$cnt=1;

#--------------------Finding a and b----------------------

    for($k=-20;$k<=$iteration;$k++){
        if(&secentMethod($k)>0){
            if(&secentMethod($k+1)<0){
                $x0=$k;
                $x1=$k+1;

                print "a = ";
                print ($x0);
                print " and F(x) is positive. b = ";
                print ($x1);
                print ", and F(x) is Negative \n\n";
                last;
            }
        }
        if(&secentMethod($k)<0){
            if(secentMethod($k+1)>0){
                $x0=$k;
                $x1=$k+1;
                print "a = ";
                print ($x0);
                print " and F(x) is Negative. b = ";

                print ($x1);
                print " and F(x) is Positive \n\n";
                last;
            }
        }


}

print "---------------------------Iteraation part-------------------------------\n\n";

$l1=   $x0;
$l2=   $x1;

#------Finding root after one by one iteration---------------

    for($i=1;$i<=$iteration;$i++){
              $first=(&secentMethod($l2)*($l2-$l1));
              $second=(&secentMethod($l2)-&secentMethod($l1));
              $l3=$l2-($first/$second);
              #$l3=$l2- ((&secentMethod($l2)*($l2-$l1))/(&secentMethod($l2)-&secentMethod($l1)));
              #$pr=abs(($l3-$l2)/($l3));
              print "After iteration ";
              print ($i);
              print " The root is : ";
              print ($l3);
              print "\n";
              $l1=$l2;
              $l2=$l3;
  }
  #-----------------------------Approximate root or Ans root-----------------------------

  print "\nApproximate root is : ";
  print ($l3);
  print "\n";
 
exit;

Sunday, June 18, 2017

How to open a weblink from java jbutton with eclipse

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddLink extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddLink frame = new AddLink();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AddLink() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 671, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        JButton btnGoToThe = new JButton("Go to the Link");
        btnGoToThe.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               
                try {
                   
                    String myurl = "https://alamgirhossainjust.blogspot.com/";
                   
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(myurl));
                   
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }
        });
        btnGoToThe.setBounds(137, 109, 184, 49);
        contentPane.add(btnGoToThe);
    }

}

How to open a pdf or txt file from java with eclipse

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OpenPdf extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OpenPdf frame = new OpenPdf();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public OpenPdf() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 636, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        JButton btnOpenPdf = new JButton("Open Pdf");
        btnOpenPdf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               
                try {
                   
                    Desktop.getDesktop().open(new java.io.File("File/sc.pdf"));
                   
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        });
        btnOpenPdf.setBounds(35, 31, 108, 23);
        contentPane.add(btnOpenPdf);
       
        JButton btnOpenText = new JButton("Open Text");
        btnOpenText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               
                try {
                   
                    Desktop.getDesktop().open(new java.io.File("File/scp.txt"));
                   
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
               
            }
        });
        btnOpenText.setBounds(197, 31, 89, 23);
        contentPane.add(btnOpenText);
    }

}

Friday, June 16, 2017

How to connect sqlite database from java jframe

import java.sql.*;


import javax.swing.JOptionPane;

public class ConnectionClass
{
    Connection conn = null;
    public static Connection dbconnect()
    {
    try {
      Class.forName("org.sqlite.JDBC");
      Connection conn = DriverManager.getConnection("jdbc:sqlite:F:\\New folder\\New folder\\JavaProgram\\SchoolMnagementSystem\\SchoolManagementSystem.sqlite");
      //JOptionPane.showMessageDialog(null, "Connected");
      return conn;
    } catch ( Exception e )
    {
      JOptionPane.showMessageDialog(null, e);
      return null;
    }
  }
}

Simple calculator source code using java jframe

///Md. Alamgir Hossain
////Dept. of Computer Science & Engineering
///Jessore University of Scoence & Technology

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

public class Main_Frame extends JFrame {

    private JPanel contentPane;
    private JTextField txtdisplay;

    /**
     * Launch the application.
     */
   
    double firstnum;
    double secondnum;
    double res;
    String operation;
    String ans;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main_Frame frame = new Main_Frame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main_Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        setLocationRelativeTo(null);
        setBounds(100, 100, 231, 282);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        txtdisplay = new JTextField();
        txtdisplay.setFont(new Font("Monotype Corsiva", Font.PLAIN, 15));
        txtdisplay.setHorizontalAlignment(SwingConstants.RIGHT);
        txtdisplay.setBounds(10, 11, 210, 29);
        contentPane.add(txtdisplay);
        txtdisplay.setColumns(10);
       
        JButton btnNewButton = new JButton("C");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                txtdisplay.setText("");
            }
        });
        btnNewButton.setBounds(10, 62, 45, 23);
        contentPane.add(btnNewButton);
       
        final JButton btn7 = new JButton("7");
        btn7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String your_enter_num = txtdisplay.getText() + btn7.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn7.setBounds(10, 94, 45, 23);
        contentPane.add(btn7);
       
        final JButton btn8 = new JButton("8");
        btn8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String your_enter_num = txtdisplay.getText() + btn8.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn8.setBounds(65, 94, 45, 23);
        contentPane.add(btn8);
       
        final JButton btn9 = new JButton("9");
        btn9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn9.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn9.setBounds(120, 94, 45, 23);
        contentPane.add(btn9);
       
        JButton button_3 = new JButton("*");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                firstnum = Double.parseDouble(txtdisplay.getText());
                txtdisplay.setText("");
                operation = "*";
            }
        });
        button_3.setBounds(175, 94, 45, 23);
        contentPane.add(button_3);
       
        final JButton btn4 = new JButton("4");
        btn4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn4.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn4.setBounds(10, 128, 45, 23);
        contentPane.add(btn4);
       
        final JButton btn5 = new JButton("5");
        btn5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn5.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn5.setBounds(65, 128, 45, 23);
        contentPane.add(btn5);
       
        final JButton btn6 = new JButton("6");
        btn6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn6.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn6.setBounds(120, 128, 45, 23);
        contentPane.add(btn6);
       
        JButton button_7 = new JButton("-");
        button_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                firstnum = Double.parseDouble(txtdisplay.getText());
                txtdisplay.setText("");
                operation = "-";
            }
        });
        button_7.setBounds(175, 128, 45, 23);
        contentPane.add(button_7);
       
        final JButton btn1 = new JButton("1");
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn1.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn1.setBounds(10, 160, 45, 23);
        contentPane.add(btn1);
       
        final JButton btn2 = new JButton("2");
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn2.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn2.setBounds(65, 162, 45, 23);
        contentPane.add(btn2);
       
        final JButton btn3 = new JButton("3");
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn3.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn3.setBounds(120, 162, 45, 23);
        contentPane.add(btn3);
       
        JButton button_11 = new JButton("+");
        button_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                firstnum = Double.parseDouble(txtdisplay.getText());
                txtdisplay.setText("");
                operation = "+";
            }
        });
        button_11.setBounds(175, 162, 45, 23);
        contentPane.add(button_11);
       
        final JButton btn0 = new JButton("0");
        btn0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btn0.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btn0.setBounds(10, 194, 45, 23);
        contentPane.add(btn0);
       
        final JButton btnp = new JButton(".");
        btnp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String your_enter_num = txtdisplay.getText() + btnp.getText();
                txtdisplay.setText(your_enter_num);
            }
        });
        btnp.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        btnp.setBounds(65, 194, 45, 23);
        contentPane.add(btnp);
       
        JButton button_14 = new JButton("+/-");
        button_14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double x= Double.parseDouble(String.valueOf(txtdisplay.getText()));
                x = x * (-1);
                txtdisplay.setText(String.valueOf(x));
            }
        });
        button_14.setFont(new Font("Tahoma", Font.PLAIN, 8));
        button_14.setBounds(120, 196, 45, 23);
        contentPane.add(button_14);
       
        JButton button_15 = new JButton("=");
        button_15.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                secondnum = Double.parseDouble(txtdisplay.getText());
               
                if(operation == "+")
                {
                    res = firstnum + secondnum;
                    ans = String.format("%.0f", res);
                    txtdisplay.setText(ans);
                }
               
                else if(operation == "-")
                {
                    res = firstnum - secondnum;
                    ans = String.format("%.0f", res);
                    txtdisplay.setText(ans);
                }
               
                else if(operation == "*")
                {
                    res = firstnum * secondnum;
                    ans = String.format("%.0f", res);
                    txtdisplay.setText(ans);
                }
               
                else if(operation == "/")
                {
                    res = firstnum / secondnum;
                    ans = String.format("%.0f", res);
                    txtdisplay.setText(ans);
                }
                else if(operation == "%")
                {
                    res = firstnum % secondnum;
                    ans = String.format("%.0f", res);
                    txtdisplay.setText(ans);
                }
            }
        });
        button_15.setBounds(175, 196, 45, 23);
        contentPane.add(button_15);
       
        JButton button_16 = new JButton("%");
        button_16.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                firstnum = Double.parseDouble(txtdisplay.getText());
                txtdisplay.setText("");
                operation = "%";
            }
        });
        button_16.setBounds(65, 62, 45, 23);
        contentPane.add(button_16);
       
        JButton btnBk = new JButton("B");
        btnBk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String back = null;
                if(txtdisplay.getText().length()>0)
                {
                    StringBuilder strbldr = new StringBuilder(txtdisplay.getText());
                    strbldr.deleteCharAt(txtdisplay.getText().length()-1);
                    back = strbldr.toString();
                    txtdisplay.setText(back);
                }
            }
        });
        btnBk.setBounds(120, 62, 45, 23);
        contentPane.add(btnBk);
       
        JButton button_18 = new JButton("/");
        button_18.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                firstnum = Double.parseDouble(txtdisplay.getText());
                txtdisplay.setText("");
                operation = "/";
            }
        });
        button_18.setBounds(175, 62, 45, 23);
        contentPane.add(button_18);
       
        JPanel panel = new JPanel();
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                try {
                    String url ="http://alamgir-hossain.simplesite.com/";
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
                   
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
               
            }
        });
        panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, new Color(102, 0, 0)));
        panel.setBounds(46, 225, 145, 25);
        contentPane.add(panel);
        panel.setLayout(null);
       
        JLabel lblDesignBy = new JLabel("Design by ");
        lblDesignBy.setFont(new Font("Monotype Corsiva", Font.BOLD, 15));
        lblDesignBy.setBounds(6, 3, 133, 20);
        panel.add(lblDesignBy);
        lblDesignBy.setHorizontalAlignment(SwingConstants.CENTER);

    }
}

How to off resize java jframe

In your frame constructor write this code...


this.setresizable(false);

How to set java jframe in the middle screen

In your jframe class constructor  write......

this.setLocationRelativeTo(null);

How to connect weblink from java jframe

///Take a button -->Add event handler-->action-->actionperformed............and paste this code...........  

try {
                    String url ="http://alamgir-hossain.simplesite.com/";
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
                   
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

/////Alamgir Hossain, CSE, JUST

Perl Script Run command with strawberry from notepadpp

NPP_SAVE
cd "$(CURRENT_DIRECTORY)"
C:\strawberry\perl\bin\perl "$(FILE_NAME)"