Bài tập java cơ bản, bài tập java , bài giảng java, những bài tập java cơ bản, 1 số bài tập java cơ bản, bài tập java có lời giải ,bài tập java cơ bảncác dạng bài tập java cơ bảnnhững bài tập java cơ bảnmột số bài tập java cơ bản1 số bài tập java cơ bản42 bài tập java cơ bảnbài tập java cơ bản có lời giảicác bài tập java cơ bản co loi giaicac bai tap java co ban co loi giaicode bài tập java cơ bản
Trang 11 Giải phương trình bậc nhất : ax+b=0
2 Phương trình bậc hai : ax2 + bx + c=0
3 Tìm số trung gian của 3 số a,b,c
4 Viết chương trình tính tiền cho bài toán KaraOke
+ Giờ bắt đầu : a (int) + Giờ kết thúc : b (int) + Nếu nhỏ hơn 18h : 45000đ/1h, lớn hơn 18h : 60000đ/1h
5 Nhập vào tháng, năm bất kỳ In ra số ngày tương ứng với tháng, năm đó.
Cấu trúc lặp :
6 Viết chương trình tính :
S=1+1/2+1/3+ +1/n
class myclass
{
public void tinhS(int x)
{
int S=0;
for(int i=1;i<=x;i++) S+=1/x;
System.out.println(“S = “+S);
}
public static void main(string [] args)
{
myclass dt=new myclass();
dt.tinhS(20);
}
}
7 Viết chương trình tính :
S=15-1+1/2-1/3!+ +(-1)n 1/n!
class myclass
{
public tinhS(int n)
{
int S=15, a=1;
for(int i=1;i<n;i++) {
if(i%2==0) x=1;
else x=-1;
a*=i;
S+=x/a;
Trang 2} System.out.println(“S= “+S); }
public static void main(string [] args) {
myclass dt=new myclass(); dt.tinhS(20);
}
}
8 Viết chương trình tính :
S=1+1/3!+1/5!+… +1/(2n-1)! class myclass
{
public void tinhS(int n)
{
int S=1, a=1;
for(int i=1;i<2n;i+2) {
S+=1/a;
a*=i*(i+1);
} System.out.println(“S = “+S); }
public static void main(string [] args) {
myclass dt = new myclass(); dt,tinhS(29);
}
}
= 2*4*6*….*n(n chẵn)
class myclass
{
public tinhN(int n)
{
int N;
for(int i= n%2+2;i<=n;i+=2) N*=i;
System.out.println(“N!!=”+N); }
public static void main(string [] args) {
myclass dt=new myclass();
Trang 3}
}
10 Tính tổng và tích các chữ số của một số nguyên dương m cho trước
(Ví dụ : m=234=> S=2+3+4=9, P=2*3*4=24) class myclass
{
public void tongvatich(int n)
{
int S, P;
do {
S+=n%10;
P*=n%10;
n/=10;
}while(n!=0);
System.out.println(“\nTong S=”+S+”\nTich P=”+P);
}
public static void main(string [] args)
{
myclass dt= new myclass();
dt.tongvatich(294);
}
}
11 Nhập một số và kiểm tra có phải nguyên tố không?
12 Kiểm tra số P có phải là số chính phương không?
13 Kiểm tra số M có phải là số đối xứng không?
public class myclass1
{
public void doixung(int so)
{
int x,y,=0,n,a=0;
x=y=n=so;
String s="";
while(so>0)
{
i++;
so/=10;
}
if(i%2==1)a=1;
for(int =1;j<=i/2+a;++)
{
x=x/10;
}
Trang 4for(int =1;j<=i/2;j++)
{
s+=y%10;
y/=10;
}
if(x==Integer.parseInt(s))
System.out.println("So "+n+" la so doi xung");
else
System.out.println("So "+n+" la so khong doi xung");
}
public static void main(String s[])
{
myclass1 dt=new myclass1();
dt.doixung(123321);
}
}
14 In ra các số nguyên tố nhỏ hơn hoặc bằng số nguyên dương n cho trước
public class songuyento
{
public boolean nguyento(int n)
{
if(n<2)return false;
if(n==2)return true;
for(int =2;i<=Math.sqrt(n);i++)
{
if(n%i==0)return false;
}return true;
}
public static void main(String s[])
{
songuyento dt=new songuyento();
for(int =1;i<=92;i++)
if(dt.nguyento(i))
System.out.println(i);
}
}
15 In ra các số hoàn hảo nhỏ hơn 1000
( Ví dụ : 6=1+2+3, 28=1+2+4+7+14)
public class Sohoanhao {
public boolean shh(int n)
{
int sum=0;
for(int =1;sum<=n&&i<n;++)
Trang 5if(n%i==0)sum+=i
if(sum==n)return true;
}return false;
}
public static void main(String args[])
{
Sohoanhao dt=new Sohoanhao();
for(int =1;i<1000;i++)
{
if(dt.shh(i))
System.out.println(i);
}
}
}
16 In ra n chữ số Fibonaci đầu tiên
public class Fibonaci {
public int Fibonaci(int n)
{
if(n==1||n==2)return 1;
else return Fibonaci(n-1)+Fibonaci(n-2);
}
public static void main(String s[])
{
Fibonaci dt=new Fibonaci();
for(int =1;i<=20;i++)
{
System.out.println(dt.Fibonaci(i));
}
}
}
17 Kiểm tra số K có thuộc dãy Fibonaci hay không? import java.util.*;
public class kiemtraFibonaci {
public int Fibonaci(int n)
{
if(n==1||n==2)return 1;
return Fibonaci(n-1)+Fibonaci(n-2);
}
public static void main(String args[])
{
kiemtraFibonaci dt=new kiemtraFibonaci();
int s=1,K;
Scanner x=new Scanner(System.in);
Trang 6System.out.println("Nhập K:");
K=x.nextInt();
while(true)
{
if(K==dt.Fibonaci(s))
{ System.out.println(K+" thuộc Fibonaci");
break;
} if(K<s)
{ System.out.println(K+" không thuộc dãy Fibonaci");
break;
}
s++;
}
}
}
18 Tìm ước chung lớn nhất và bội chung nhỏ nhất của 2 số a và b import java.util.*;
public class UCLN {
public int UCLN(int x,int y)
{
if(x==0||y==0)return 0;
if(x==y)return x;
if(x>y)return UCLN(x-y,y);
return UCLN(x,y-x);
}
public static void main(String args[])
{
UCLN dt=new UCLN();
Scanner x= new Scanner(System.in);
int a,b;
System.out.println("Nhập a: ");
a=x.nextInt();
System.out.println("Nhập b: ");
b=x.nextInt();
System.out.println("Ước Chung lớn nhất : "+dt.UCLN(a,b));
System.out.println("Bội Chung nhỏ nhất : "+a* /dt.UCLN(a, ));
}
}
4.1 Giải phưong trình bậc nhất :
import java.awt.*;
import java.awt.event.*;
public class Giaiptbn extends Frame implements ActionListener
{
Label giai, nhapa, nhapb,inkq;
TextField a,b,kq;
Button tinh,reset,thoat;
Trang 7Panel pn,pn1,pn2,pn3,pn4,pn5;
public void GUI()
{
giai=new Label("Giải Phương Trình Bậc Nhất ax+b=0"); nhapa=new Label("Nhập a");
nhapb=new Label("Nhập b");
inkq=new Label("Kết quả");
a=new TextField("");
b=new TextField("");
kq=new TextField("");
tinh=new Button("Tính");
reset=new Button("Reset");
thoat=new Button("Thoát");
tinh.addActionListener(this);
reset.addActionListener(this);
thoat.addActionListener(this);
pn=new Panel(new GridLayout(5,1));
pn1=new Panel(new FlowLayout());
pn2=new Panel(new GridLayout(1,2));
pn3=new Panel(new GridLayout(1,2));
pn4=new Panel(new GridLayout(1,2));
pn5=new Panel(new GridLayout(1,3));
pn1.add(giai);
pn2.add(nhapa);
pn2.add(a);
pn3.add(nhapb);
pn3.add(b);
pn4.add(inkq);
pn4.add(kq);
pn5.add(tinh);
pn5.add(reset);
pn5.add(thoat);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
pn.add(pn5);
add(pn);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tinh)
{
int x=Integer.parseInt(a.getText());
int y=Integer.parseInt(b.getText());
if(x==0) kq.setText("Phương trình vô nghiệm");
else
{
if(y==0) kq.setText("Phương trình có vô số nghiệm"); else kq.setText(Float.toString((float)-y/x));
Trang 8}
if(e.getSource()==reset)
{
a.setText(" ");
b.setText(" ");
kq.setText(" ");
}
if(e.getSource()==thoat)
{
System.exit(0);
}
}
public Giaiptbn(String st)
{
super(st);
GUI();
}
public static void main(String [] args)
{
new Giaiptbn("GIẢI PHƯƠNG TRÌNH BẬC NHẤT AX+B=0"); }
}
4.2 Minh họa các phép toán :
import java.awt.*;
import java.awt.event.*;
public class Cacpheptoan extends Frame implements ActionListener {
Label pheptoan,nhapa,nhapb,inkq;
TextField a,b,kq;
Button cong,tru,nhan,chia,thoat,reset,ketqua;
Panel pn,pn1,pn2,pn3,pn4,pn5,pn6;
public void GUI()
{
pheptoan=new Label("Các phép toán");
nhapa=new Label("Nhập a");
nhapb=new Label("Nhập b");
inkq=new Label("Kết quả");
a=new TextField("");
b=new TextField("");
kq=new TextField("");
cong=new Button("Cộng");
tru=new Button("Trừ");
nhan=new Button("Nhân");
chia=new Button("Chia");
reset=new Button("Reset");
thoat=new Button("Exit");
cong.addActionListener(this);
tru.addActionListener(this);
Trang 9chia.addActionListener(this);
reset.addActionListener(this);
thoat.addActionListener(this);
pn=new Panel(new GridLayout(6,1));
pn1=new Panel(new GridLayout(1,2));
pn2=new Panel(new GridLayout(1,2));
pn3=new Panel(new GridLayout(1,2));
pn4=new Panel(new GridLayout(1,2));
pn5=new Panel(new FlowLayout());
pn6=new Panel(new FlowLayout());
pn1.add(pheptoan);
pn2.add(nhapa);
pn2.add(a);
pn3.add(nhapb);
pn3.add(b);
pn4.add(inkq);
pn4.add(kq);
pn5.add(cong);
pn5.add(tru);
pn5.add(nhan);
pn5.add(chia);
pn6.add(thoat);
pn6.add(reset);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
pn.add(pn5);
pn.add(pn6);
add(pn);
setSize(500,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int x=Integer.parseInt(a.getText());
int y=Integer.parseInt(b.getText());
if(e.getSource()==thoat)
{
System.exit(0);
}
if(e.getSource()==cong)
{
kq.setText(Float.toString((float)x+y)); }
if(e.getSource()==tru)
{
kq.setText(Float.toString((float)x-y)); }
if(e.getSource()==nhan)
{
Trang 10kq.setText(Float.toString((float)x y));
}
if(e.getSource()==chia)
{
if(y==0)kq.setText("Vô nghiệm");
else kq.setText(Float.toString((float)x/y));
}
if(e.getSource()==reset)
{
a.setText(" ");
b.setText(" ");
kq.setText(" ");
}
}
public Cacpheptoan(String st)
{
super(st);
GUI();
}
public static void main(String [] args)
{
new Cacpheptoan("Các PHÉP TOÁN");
}
}
4.3 In các số nguyên tố nhỏ hơn hoặc bằng số n cho trước :
import java.awt.*;
import java.awt.event.*;
public class Songuyento extends Frame implements ActionListener
{
Label lb,nhapn,kq;
TextField n,kqua;
Button tim,reset,thoat;
Panel pn,pn1,pn2,pn3,pn4;
public void GUI()
{
lb=new Label("Các số nguyên tố <=n");
nhapn=new Label("Nhập n");
kq=new Label("Kết quả");
n=new TextField("");
kqua=new TextField("");
tim=new Button("Tìm");
reset=new Button("Reset");
thoat=new Button("Exit");
tim.addActionListener(this);
reset.addActionListener(this);
thoat.addActionListener(this);
pn=new Panel(new GridLayout(4,1));
pn1=new Panel(new FlowLayout());
pn2=new Panel(new GridLayout(1,2));
pn3=new Panel(new GridLayout(1,2));
pn4=new Panel(new FlowLayout());
Trang 11pn2.add(nhapn);
pn2.add(n);
pn3.add(kq);
pn3.add(kqua);
pn4.add(tim);
pn4.add(reset);
pn4.add(thoat);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
add(pn);
setSize(500,300);
setVisible(true);
}
private int songuyento(int a)
{
for(int =2;i<=Math.sqrt(a);i++)
{
if(a%i==0) return 0;
}
return 1;
}
public void actionPerformed(ActionEvent e)
{
String s="";
int x=Integer.parseInt(n.getText());
if(e.getSource()==thoat)
{
System.exit(0);
}
if(e.getSource()==tim)
{
if(x<2)kqua.setText("Không có số nguyên tố nào");
else
{
for(int =2;i<=x;++)
{
if(songuyento(i)==1) s+=Integer.toString((int)i)+" "; }
kqua.setText(s);
}
}
if(e.getSource()==reset)
{
n.setText("");
kqua.setText("");
}
}
public Songuyento(String st)
{
Trang 12GUI();
}
public static void main(String []args)
{
new Songuyento("SỐ NGUYÊN TỐ");
}
}
4.4Kiểm tra một số có thuộc dãy Fibonaci hay không?
import java.awt.*;
import java.awt.event.*;
public class Fibonaci extends Frame implements ActionListener
{
Label lb,nhapa,kqua;
TextField a,kq;
Button ok,reset,thoat;
Panel pn,pn1,pn2,pn3,pn4;
public void GUI()
{
lb=new Label("Kiểm tra a có thuộc dãy Fibonaci");
nhapa=new Label("Nhập a");
kqua=new Label("Kết quả");
a=new TextField("",20);
kq=new TextField("",20);
ok=new Button("OK");
reset=new Button("Reset");
thoat=new Button("Exit");
ok.addActionListener(this);
reset.addActionListener(this);
thoat.addActionListener(this);
pn=new Panel(new GridLayout(4,1));
pn1=new Panel(new FlowLayout());
pn2=new Panel(new FlowLayout());
pn3=new Panel(new FlowLayout());
pn4=new Panel(new FlowLayout());
pn1.add(lb);
pn2.add(nhapa);
pn2.add(a);
pn3.add(kqua);
pn3.add(kq);
pn4.add(ok);
pn4.add(reset);
pn4.add(thoat);
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
add(pn);
setSize(500,300);
Trang 13}
public void actionPerformed(ActionEvent e)
{
int n=Integer.parseInt(a.getText());
if(e.getSource()==thoat)
{
System.exit(0);
}
if(e.getSource()==reset)
{
a.setText("");
kq.setText("");
}
if(e.getSource()==ok)
{
if(n<1)kq.setText(n+" không thuộc dãy Fibonaci"); else
{
int f,=1;
do {
f=fibonaci(i);
if(n==f
{
kq.setText(n+" thuộc dãy Fibonaci"); break;
} if(n<f
{
kq.setText(n+" không thuộc dãy Fibonaci"); break;
}
i++;
}while(true);
}
}
}
private int fibonaci(int x)
{
if(x==1||x==2) return 1;
else return fibonaci(x-1)+fibonaci(x-2);
}
public Fibonaci(String st)
{
super(st);
GUI();
}
public static void main(String []args)
{
new Fibonaci("Fibonaci");
}
}
Trang 144.5Mô tả máy tính điên tử cá nhân
4.6Đổi màu nền :
‘BaiTap Mang A[n]
import java.util.*;
public class mangA
{
public int tong(int m[],int n)
{
int sum=0;
for(int =0;i<n;++)
{
if(m[i]%2==1)sum+=m[i];
}return sum;
}
public void timK(int m[],int n,int k)
{
for(int =0;i<n;++)
{
if(m[i]==k)
{
System.out.println("Vi tri cua "+k+" la: "+(i+1)); break;
}
}
}
public void sapxep(int m[],int n)
{
int tam;
for(int =0;i<n-1;i++)
for(int =i <n;++)
if(m[i]>m[j])
{
tam=m[i];
m[i]=m[j];
m[j]=tam;
}
System.out.println("Mang A[]");
for(int =0;i<n;++)
System.out.println("A["+(i+1)+"] = "+m[i]);
}
public void chen(int m[], int n,int p)
{
m[n]=p;n++;
sapxep(m,n);
}
public static void main(String args[])
{
mangA dt=new mangA();
Scanner x=new Scanner(System.in);
int n,A[]=new int[100],k,p;
Trang 15System.out.println("Nhap chiu dai mang: n = ");
n=x.nextInt();
for(int =0;i<n;++)
{
System.out.println("A["+(i+1)+"] = ");
A[i]=x.nextInt();
}
System.out.println("Tong cac so le trong mang = "+dt.tong(A,n)); System.out.println("Nhap so can tim: ");
k=x.nextInt();
dt.timK(A, , );
dt.sapxep(A, );
System.out.println("Nhap so can chen: ");
p=x.nextInt();
dt.chen(A, , );
}
}
Mang B[m][n]
import java.util.*;
public class mangB
{
static int B[][]=new int[100][100],X[]=new int[100],n,m;
public void tich()
{
int =1;
for(int =0;i<n;++)
{
if(B[0][i]%3==0)t*=B[0][i];
}
if(t==1)System.out.println("Khong co so bo ba nao");
else System.out.println("Tich cac so bo ba: "+t);
}
public void mang1chieu()
{
for(int =0;i<n;++)
X[i]=B[0][i];
for(int =0;i<n;++)
for(int =0;j<m;++)
{
if(X[i]<B[j][i])
X[i]=B[j][i];
}
for(int =0;i<n;++)
System.out.println("X["+(i+1)+"] = "+X[i]);
}
public void xoasole()
{
for(int =0;i<n;++)
if(X[i]%2==1)
{
n ;
Trang 16for(int =i <n;++)
X[j]=X[j+1];
break;
}
for(int =0;i<n;++)
System.out.println("X["+(i+1)+"] = "+X[i]);
}
public void sapxep()
{
int tam;
for(int =0;i<n-1;i++)
for(int =i <n;++)
{
if(X[i]<X[j]) {
tam=X[i];
X[i]=X[j];
X[j]=tam; }
}
for(int =0;i<n;++)
System.out.println("X["+(i+1)+"] = "+X[i]);
}
public static void main(String args[])
{
mangB dt= new mangB();
Scanner y=new Scanner(System.in);
System.out.println("Nhap m,n: ");
m=y.nextInt();
n=y.nextInt();
System.out.println("Nhap mang B[][]");
for(int =0;i<m;++)
for(int =0;j<n;++)
{
System.out.println("B["+(i+1)+"]["+(j+1)+"] = ");
B[i][j]=y.nextInt();
}
dt.tich();
dt.mang1chieu();
dt.xoasole();
dt.sapxep();
}
}