Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, March 3, 2017

Gauss Jordan JAVA

Gauss Jordan JAVA


ini adalah algoritma gauss jordan untuk menyelesaikan persamaan linier
ini adalah class Gauss_Jordan_Elimination

package gausjordan;


import gauss_jordan.*;
import Database.Data;
import java.util.LinkedList;
import java.util.Scanner;


public class Gauss_Jordan_Elimination

{

private static final double EPSILON = 1e-8;



private final int N; // N-by-N system

private double[][] a; // N-by-N+1 augmented matrix



// Gauss-Jordan elimination with partial pivoting

public Gauss_Jordan_Elimination(){
N=0;
}

public Gauss_Jordan_Elimination(double[][] A, double[] b)

{

N = b.length;



// build augmented matrix

a = new double[N][N+N+1];

for (int i = 0; i < N; i++)

for (int j = 0; j < N; j++)

a[i][j] = A[i][j];



// only need if you want to find certificate of infeasibility (or compute inverse)

for (int i = 0; i < N; i++)

a[i][N+i] = 1.0;



for (int i = 0; i < N; i++)

a[i][N+N] = b[i];



solve();



assert check(A, b);

}



private void solve()

{

// Gauss-Jordan elimination

for (int p = 0; p < N; p++)

{

int max = p;

for (int i = p+1; i < N; i++)

{

if (Math.abs(a[i][p]) > Math.abs(a[max][p]))

{

max = i;

}

}



// exchange row p with row max

swap(p, max);



// singular or nearly singular

if (Math.abs(a[p][p]) <= EPSILON)

{

continue;

// throw new RuntimeException("Matrix is singular or nearly singular");

}



// pivot

pivot(p, p);

}

// show();

}



// swap row1 and row2

private void swap(int row1, int row2)

{

double[] temp = a[row1];

a[row1] = a[row2];

a[row2] = temp;

}





// pivot on entry (p, q) using Gauss-Jordan elimination

private void pivot(int p, int q)

{ // everything but row p and column q

for (int i = 0; i < N; i++) {

double alpha = a[i][q] / a[p][q];

for (int j = 0; j <= N+N; j++)

{

if (i != p && j != q) a[i][j] -= alpha * a[p][j];

}

}



// zero out column q

for (int i = 0; i < N; i++)

if (i != p) a[i][q] = 0.0;



// scale row p (ok to go from q+1 to N, but do this for consistency with simplex pivot)

for (int j = 0; j <= N+N; j++)

if (j != q) a[p][j] /= a[p][q];

a[p][q] = 1.0;

}



// extract solution to Ax = b

public double[] primal()

{

double[] x = new double[N];

for (int i = 0; i < N; i++)

{

if (Math.abs(a[i][i]) > EPSILON)

x[i] = a[i][N+N] / a[i][i];

else if (Math.abs(a[i][N+N]) > EPSILON)

return null;

}

return x;

}



// extract solution to yA = 0, yb != 0

public double[] dual()

{

double[] y = new double[N];

for (int i = 0; i < N; i++)

{

if ( (Math.abs(a[i][i]) <= EPSILON) && (Math.abs(a[i][N+N]) > EPSILON) )

{

for (int j = 0; j < N; j++)

y[j] = a[i][N+j];

return y;

}

}

return null;

}



// does the system have a solution?

public boolean isFeasible()

{

return primal() != null;

}



// print the tableaux

private void show()

{

for (int i = 0; i < N; i++)

{

for (int j = 0; j < N; j++)

{

System.out.print(" "+a[i][j]);

}

System.out.print("| ");

for (int j = N; j < N+N; j++)

{

System.out.print(" "+a[i][j]);

}

System.out.print("| "+a[i][N+N]);

}

System.out.println();

}





// check that Ax = b or yA = 0, yb != 0

private boolean check(double[][] A, double[] b)

{



// check that Ax = b

if (isFeasible())

{

double[] x = primal();

for (int i = 0; i < N; i++)

{

double sum = 0.0;

for (int j = 0; j < N; j++)

{

sum += A[i][j] * x[j];

}

if (Math.abs(sum - b[i]) > EPSILON)

{

System.out.println("not feasible");

System.out.println(i+" = "+b[i]+", sum = "+sum+" ");

return false;

}

}

return true;

}



// or that yA = 0, yb != 0

else

{

double[] y = dual();

for (int j = 0; j < N; j++)

{

double sum = 0.0;

for (int i = 0; i < N; i++)

{

sum += A[i][j] * y[i];

}

if (Math.abs(sum) > EPSILON)

{

System.out.println("invalid certificate of infeasibility");

System.out.println("sum = "+sum+" ");

return false;

}

}

double sum = 0.0;

for (int i = 0; i < N; i++)

{

sum += y[i] * b[i];

}

if (Math.abs(sum) < EPSILON)

{

System.out.println("invalid certificate of infeasibility");

System.out.println("yb = "+sum+" ");



return false;

}

return true;

}

}





public double[] test(double[][] A, double[] b)

{

Gauss_Jordan_Elimination gaussian = new Gauss_Jordan_Elimination(A, b);

if (gaussian.isFeasible())

{

System.out.println("Solution to Ax = b");

double[] x = gaussian.primal();

for (int i = 0; i < x.length; i++)

{

System.out.println(" "+x[i]+" ");

}
return x;
}

else

{

System.out.println("Certificate of infeasibility");

double[] y = gaussian.dual();

for (int j = 0; j < y.length; j++)

{

System.out.println(" "+y[j]+" ");

}

}

System.out.println();
return null;
}


source codenya bisa download disini...

programnya di open pake netbean ya...tinggal di load aja projectnya pake netbeans.....


Available link for download

Read more »

Sunday, January 29, 2017

How to set combobox value from database in java

How to set combobox value from database in java


-How to get data from database to JComboBox?
-L?y giá tr? t? database Hi?n th? lên comboBox! 
Java 2016
public ArrayList<Clazz> getArrayListLop() {
ArrayList<Clazz> list = new ArrayList();
try {
stmt = conn.prepareStatement("SELECT * FROM project.lop");
rs = stmt.executeQuery();
while (rs.next()) {
Clazz clazz = new Clazz(rs.getString("idlop"), rs.getString("lop"), rs.getString("idkhoa"),
rs.getString("khoahoc"));
list.add(clazz);
// Display comboBox set from databse
comboBox_lop.addItem(rs.getString("lop"));
}

} catch (Exception e) {
e.printStackTrace();
}

return list;
}


Available link for download

Read more »

Sunday, January 15, 2017

How to create jTable right click popup menu Java Swing GUI

How to create jTable right click popup menu Java Swing GUI


jTable right-click popup menu Java Swing GUI - Mouse click
Java 2016
table_6.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e)
{
int r = table_6.rowAtPoint(e.getPoint());
if (r >= 0 && r < table_6.getRowCount()) {
table_6.setRowSelectionInterval(r, r);
} else {
table_6.clearSelection();
}

//row index is found...
int rowindex = table_6.getSelectedRow();
if (rowindex < 0)
return;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
JPopupMenu popup = createYourPopUp(rowindex,table_6);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}

});
jTable right-click popup menu Java Swing GUI - Method
Java 2016
public static JPopupMenu createYourPopUp(int rowindex, JTable table_6)
{
JPopupMenu popup=new JPopupMenu();
JMenuItem edit=new JMenuItem("Edit Details");
JMenuItem delete=new JMenuItem("Delete Details");
edit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Edit scucessfuly");
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Delete scucessfuly");
}
});
popup.add(edit);
popup.add(delete);
return popup;
}






Available link for download

Read more »

Sunday, December 4, 2016

Hà m xóa dữ liệu với Jcheckbox trong Jtable Delete checkbox table Java Swing Gui Eclipse

Hà m xóa dữ liệu với Jcheckbox trong Jtable Delete checkbox table Java Swing Gui Eclipse





Hàm xóa d? li?u table khi dùng checkbox
Java 2016
public void deleteSinhVien(JTable table){
boolean tich = false;
Object[] options = { "Yes", "No" };
int n = JOptionPane.showOptionDialog(null, "B?n có mu?n xóa d? li?u này không?", "Confirm to Delete?",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if (n == 0)
// Confirm Delete = Yes
{
for (int i = 0; i <
table.getRowCount(); i++) {
Boolean chkDel = Boolean.valueOf(
table.getValueAt(i, 0).toString()); // Checked
if (chkDel)
// Checked to Delete
{
// Delete Data
String sql = "DELETE FROM project.sinhvien WHERE idsv = ?";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1,
table.getValueAt(i, 1).toString());
stmt.executeUpdate();
tich = true;
} catch (Exception ed) {
JOptionPane.showMessageDialog(null, "Không th? xóa id "
+
table.getValueAt(i, 1).toString() + " vui lòng xem l?i b?ng ?i?m");
tich = false;
}
}
}
if (tich) {
JOptionPane.showMessageDialog(null, "Xóa d? li?u thành công!");
}
}
}


Available link for download

Read more »

Saturday, October 15, 2016

How To Set Java Class Path for Windows7

How To Set Java Class Path for Windows7


1. First download JDK from oracle.com
click on the  bellow link to download JDK.
CLICK HERE TO DOWNLOAD


2. Install JDK on to your PC
3.Set Class path and Path as Shown in the Video.
watch and  enjoy...




a

and you FIXED it..!


Available link for download

Read more »