Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Môn Java

Uploaded by

Thủy Lê
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Môn Java

Uploaded by

Thủy Lê
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

In ra màn hình 10 số nguyên tố đầu tiên

package nto;
public class Nto {
public static void main(String[] args) {
int k=0,n,dem,j=0;
while(j<10)
{
k++;
dem=0;
for(n=2;n<=k;n++)
{
if(k%n==0)
{
dem++;
}
}
if(dem==1)
{
System.out.print(k+" ");
j++;
}
}
}
}
In ra màn hình các số <100 và chia hết cho 3,7
package bai02;
public class Bai02 {
public static void main(String[] args) {
int k,n;
for (k=1;k<100;k++)
{
if((k%3==0)&&(k%7==0))
System.out.print(k+" ");
}
}
}
Cho trước số tự nhiên N bất kì. In ra màn hình tất cả các USNT khác nhau của N.
package usnt;
public class USNT {
public static void main(String[] args) {
int k,n,dem,N=1027;
for(k=1;k<=N;k++)
{
dem=0;
for(n=2;n<=k;n++)
{
if(k%n==0)
{
dem++;
}
}
if(dem==1 & N%k==0)
{
System.out.print(k+" ");
}
}
}
}
Cho 1 dãy số tự nhiên, viết chương trình sắp xếp dãy này theo thứ tự giảm dần.
package sxgd;
public class SXGD {
public static void main(String[] args) {
int []a={3,1,7,0,10};
int N=5,k,j,temp;
for(k=0;k<N-1;k++)
{
for(j=k;j<N;j++)
{
if(a[k]<a[j])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for(k=0;k<N;k++)
System.out.print(a[k]+" ");
}
}
Cho 1 dãy các số tự nhiên, tìm và in ra 1 giá trị min của dãy này và tất cả các chỉ số ứng với gtri
min này.
package minmang;
public class Minmang {
public static void main(String[] args) {
int []a={3,1,7,0,10};
int N=5,k,min;
min = a[0];
for(k=0;k<N;k++)
if(min>a[k])
min=a[k];
System.out.println("Gia tri nho nhat cua day la: "+min);
System.out.print("Vi tri cua so co gia tri min la: ");
for (k=0;k<N;k++)
if(min==a[k])
System.out.print(k+" ");
}
}
Bài làm nhóm
package ketnoicsdl;
import java.sql.*;
public class KetnoiCSDL {
public static void main(String[] args) {
Connection connec = null;
try
{
//ket noi Java voi CSDL SQL
String userName = "sa";
String password = "123456";
String url =
"jdbc:sqlserver://localhost:1433;databaseName=Demo;encrypt=true;trustServerCertificate=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connec = java.sql.DriverManager.getConnection(url,userName,password);
System.out.println("Da ket noi CSDL");
//Tao 1 doi tuong trong Statement
java.sql.Statement st = connec.createStatement();
//Khai bao 1 chuoi SQL
String Lenhsql ="";
//Update du lieu
//Lenhsql = "update Products set Name='Gao nep' where ID=888";
//st.executeUpdate(Lenhsql);
//Them moi 1 record
Lenhsql = "insert into Products(ID, Name, Unit) values ('777', 'May tinh xach tay', 'Chiec')";
st.executeUpdate(Lenhsql);
/*
Lenhsql = "insert into Products(ID, Name, Unit) values ('222', 'Gao tam thom', 'Yen')";
st.executeUpdate(Lenhsql);
Lenhsql = "insert into Products(ID, Name, Unit) values ('333', 'Dua', 'Qua')";
st.executeUpdate(Lenhsql);
Lenhsql = "insert into Products(ID, Name, Unit) values ('444', 'Trai cay', 'Kg')";
st.executeUpdate(Lenhsql);
Lenhsql = "insert into Products(ID, Name, Unit) values ('555', 'Thuy san', 'Tan')";
st.executeUpdate(Lenhsql);
Lenhsql = "insert into Products(ID, Name, Unit) values ('666', 'Sach', 'Quyen')";
st.executeUpdate(Lenhsql);*/
Lenhsql = "delete from Products where ID=111";
st.executeUpdate(Lenhsql);
//Select du lieu
Lenhsql = "select * from Products";
ResultSet rs = st.executeQuery(Lenhsql);
while(rs.next())
{
System.out.print("ProductID la: "+rs.getString(1)+" ");
System.out.print("NameID la: "+rs.getString(2)+" ");
System.out.print("UnitID la: "+rs.getString(3)+" ");
System.out.println();
}
}
catch (Exception e)
{
System.err.println("Khong ket noi duoc do: " +e.getMessage());
}
finally
{
if (connec != null)
{
try
{
connec.close();
System.out.println("Dong ket noi");
}
catch (Exception e) {}
}
}
}
}

Lenhsql = "update Products set Name='Gao nep' where ID=888";


st.executeUpdate(Lenhsql);

Lenhsql = "insert into Products(ID, Name, Unit) values ('777', 'May tinh xach tay', 'Chiec')";
st.executeUpdate(Lenhsql);

Lenhsql = "delete from Products where ID=111";


st.executeUpdate(Lenhsql);
Bài TH3: CalculatorForm
package main1;
public class Main1 {
public static void main(String[] args) {
CalculatorForm form = new CalculatorForm();
form.setVisible(true);
}
}
______________________________________________________________________________
import javax.swing.JOptionPane;
_________________________________________________________________________
int number1, number2;
try
{
number1 = Integer.parseInt(this.firstNumberText.getText());
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Bad first
number","Errorr",JOptionPane.ERROR_MESSAGE);
return;
}
try
{
number2 = Integer.parseInt(this.secondNumberText.getText());
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Bad first
number","Errorr",JOptionPane.ERROR_MESSAGE);
return;
}
int answer = number1 + number2;
this.answerLabel.setText("Dap an la: "+answer);

Bài TH4: Giải PT bậc hai


import javax.swing.JOptionPane;
_______________________________________________________________________
double a,b,c,d;
try
{
a = Double.parseDouble(jTxtA.getText());
b = Double.parseDouble(jTxtB.getText());
c = Double.parseDouble(jTxtC.getText());
if(a!=0)
{
d = b*b-4*a*c;
if(d<0)
{
jTextArea1.setText("Phuong trinh vo nghiem");
}
if(d==0)
{
jTextArea1.setText("""
Phuong trinh co nghiem kep:
x = """ + String.valueOf(round(-0.5*b/a,4)));
}
if(d>0)
{
jTextArea1.setText("Phuong trinh co 2 nghiem phan biet :\n x1 =
"+String.valueOf(round(0.5*(-b-Math.sqrt(d))/a,4))+"\n x2 = "+String.valueOf(round(0.5*(-
b+Math.sqrt(d))/a,4)));
}
}
else
{
if(b!=0)
{
jTextArea1.setText("Phuong trinh co 1 nghiem \n x = " + String.valueOf(round(-c/b,4)));
}
else
{
if(c!=0)
{
jTextArea1.setText("Phuong trinh vo nghiem");
}
else
{
jTextArea1.setText("Phuong trinh vo so nghiem");
}
}
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(rootPane,"Loi: "+e.getMessage()+"\n Moi ban nhap
lai!","Thong bao",WIDTH);
}

public double round(double value, int fractionDigits)


{
double d = Math.pow(10,fractionDigits);
return(Math.round(value*d)/d);
}
Bài TH5: Bảng máy tính

double firstDouble;
double secondDouble;
double totalDouble;
int plusClick;
int minusClick;
int multiplyClick;
int deviceClick;
int decimalClick;
_____________________________________________________________
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
display.setText("");
decimalClick=0;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton1.getText());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton2.getText());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton3.getText());
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton4.getText());
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton5.getText());
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton6.getText());
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton7.getText());
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton8.getText());
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton9.getText());
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton13.getText());
}
private void decimalActionPerformed(java.awt.event.ActionEvent evt) {
if(decimalClick==0)
{
display.setText(display.getText()+decimal.getText());
decimalClick=1;
}
}
private void plusActionPerformed(java.awt.event.ActionEvent evt) {
firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
display.setText("");
plusClick=1;
decimalClick=0;
}
private void minusActionPerformed(java.awt.event.ActionEvent evt) {
firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
display.setText("");
minusClick=1;
decimalClick=0;
}
private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {
firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
display.setText("");
multiplyClick=1;
decimalClick=0;
}
private void deviceActionPerformed(java.awt.event.ActionEvent evt) {
firstDouble=(Double.parseDouble(String.valueOf(display.getText())));
display.setText("");
deviceClick=1;
decimalClick=0;
}
private void equalsActionPerformed(java.awt.event.ActionEvent evt) {
secondDouble=(Double.parseDouble(String.valueOf(display.getText())));
if(plusClick>0)
{
totalDouble=firstDouble+secondDouble;
display.setText(String.valueOf(totalDouble));
firstDouble=0;
secondDouble=0;
plusClick=0;
}
if(minusClick>0)
{
totalDouble=firstDouble-secondDouble;
display.setText(String.valueOf(totalDouble));
firstDouble=0;
secondDouble=0;
minusClick=0;
}
if(multiplyClick>0)
{
totalDouble=firstDouble*secondDouble;
display.setText(String.valueOf(totalDouble));
firstDouble=0;
secondDouble=0;
multiplyClick=0;
}
if(deviceClick>0)
{
totalDouble=firstDouble/secondDouble;
display.setText(String.valueOf(totalDouble));
firstDouble=0;
secondDouble=0;
deviceClick=0;
}
}
Bài TH6: Quản lý sinh viên
import java.io.*;
class sv
{
int masv;
String hoten;
int tuoi;
float dtb;
void nhap()throws IOException
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Nhap vao ma sinh vien:");
masv=Integer.parseInt(buf.readLine());
System.out.println("Nhap ten sinh vien:");
hoten=buf.readLine();
System.out.println("Nhap tuoi cua sinh vien:");
tuoi=Integer.parseInt(buf.readLine());
System.out.println("Nhap DTB cua sinh vien:");
dtb=Float.parseFloat(buf.readLine());
}
void xuat()
{
System.out.println(masv+" "+hoten+" "+tuoi+" "+dtb);
}
}
class dssv
{
int n;
sv ds[];
void nhapds()throws IOException
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("**Nhap danh sach sinh vien**");
System.out.println("Nhap so sinh vien: ");
n=Integer.parseInt(buf.readLine());
ds=new sv[n];
for(int i=0;i<n;i++)
{
System.out.println("Nhap thong tin cho sinh vien thu "+(i+1));
sv x=new sv();
x.nhap();
ds[i]=x;
}
}
void xuatds()
{
System.out.println("***Danh sach sinh vien***");
for(int i=0;i<n;i++)
{
System.out.print((i+1)+" ");
ds[i].xuat();
}
}
void tuoi25()
{
int t=0;
System.out.println("***Danh sach sv co tuoi lon hon 25***");
for(int i=0;i<n;i++)
if(ds[i].tuoi>25)
{
System.out.print((++t)+" ");
ds[i].xuat();
}
}
void svkha()
{
int dem=0;
System.out.println("***Danh sach sinh vien dat loai kha***");
for(int i=0;i<n;i++)
if(ds[i].dtb>7.0)
{
dem++;
System.out.print((i+1)+" ");
ds[i].xuat();
}
System.out.println("Tong so sinh vien dat loai kha: "+dem);
}
void sapxep()
{
sv tg= new sv();
System.out.println("***DSSV sap xep theo ma SV***");
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(ds[i].masv>ds[j].masv)
{
tg=ds[i];
ds[i]=ds[j];
ds[j]=tg;
}
for(int i=0;i<n;i++)
{
System.out.print((i+1)+" ");
ds[i].xuat();
}
}
public static void main(String arg[])throws IOException
{
dssv x=new dssv();
x.nhapds();
x.xuatds();
x.tuoi25();
x.svkha();
x.sapxep();
}}
Bài TH: Giải phương trình bậc 1
package ptb1;
public class Ptb1 {
public static void main(String[] args) {
Giaiptb1 form = new Giaiptb1();
form.setVisible(true);
}
}

import javax.swing.JOptionPane;

double a,b,x;
try
{
a = Double.parseDouble(jTxtA.getText());
b = Double.parseDouble(jTxtB.getText());
if(a==0)
{
if(b==0)
{
jTextArea1.setText("Phương trình này có vô số nghiệm");
}
else
{
jTextArea1.setText("Phương trình vô nghiệm");
}
}
else
{
x = (double) -b/a;
jTextArea1.setText("Phương trình có nghiệm x = "+ String.valueOf(-b/a));
}
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(rootPane,"Lỗi: "+e.getMessage()+"\n Mời bạn nhập
lại!","Thông báo",WIDTH);
}

You might also like