Xem số n vừa nhập vào có phải là Số Nguyên Tố hay không ?

Một phần của tài liệu Bài tập Lập trình JAVA căn bàn full code (Trang 81 - 93)

- Phân tích số n vừa nhập vào

*** Đáp án xuất file

============================ Và đây là code trong void main :

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Font.*;

public class DoHoaTinhTong_US_SNT_PT extends JFrame {

int tam;

String s1=" ",s2=" ";

JLabel tieude=new JLabel(); JLabel gioithieu=new JLabel(); JLabel lnhapn=new JLabel(); JLabel lketqua=new JLabel();

JTextField nhapn=new JTextField(); JTextField ketqua=new JTextField();

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 81 JRadioButton tong=new JRadioButton("Tong S=1+2+...+n");

JRadioButton uocso=new JRadioButton("Uoc So cua n"); JRadioButton songuyento=new JRadioButton("So nguyen to"); JRadioButton phantich=new JRadioButton("Phan tich");

JButton thoat=new JButton(); JButton thuchien=new JButton(); JButton tieptuc=new JButton();

ButtonGroup group1=new ButtonGroup();

Font font = new Font("Tahoma", Font.BOLD, 14);

public DoHoaTinhTong_US_SNT_PT() {

this.addWindowListener(new Close_this_windowAdapter(this)); this.getContentPane().setLayout(null);

tieude.setBounds(new Rectangle(30,20,350,30));

tieude.setText("Tinh Tong - Uoc So - So Nguyen To - Phan Tich"); tieude.setFont(font); this.getContentPane().add(tieude); gioithieu.setBounds(new Rectangle(150,50,140,30)); gioithieu.setText("www.tamga.tk"); this.getContentPane().add(gioithieu); //---- Nhap n --- www.tamga.tk lnhapn.setBounds(new Rectangle(40,100,100,30)); lnhapn.setText("Nhap n :"); this.getContentPane().add(lnhapn); nhapn.setBounds(new Rectangle(110,100,100,25)); this.getContentPane().add(nhapn);

//---- Ket qua --- www.tamga.tk

lketqua.setBounds(new Rectangle(40,140,100,30)); lketqua.setText("Ket qua :"); this.getContentPane().add(lketqua); ketqua.setBounds(new Rectangle(110,140,100,25)); this.getContentPane().add(ketqua); //---- Radio --- www.tamga.tk tong.setBounds(new Rectangle(230,80,130,30)); this.getContentPane().add(tong); uocso.setBounds(new Rectangle(230,110,100,30)); this.getContentPane().add(uocso); songuyento.setBounds(new Rectangle(230,140,100,30)); this.getContentPane().add(songuyento);

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 82 phantich.setBounds(new Rectangle(230,170,100,30)); this.getContentPane().add(phantich); //---- Button --- www.tamga.tk thuchien.setBounds(new Rectangle(40,200,90,30)); thuchien.setText("Thuc Hien"); this.getContentPane().add(thuchien); tieptuc.setBounds(new Rectangle(145,200,90,30)); tieptuc.setText("Tiep Tuc"); this.getContentPane().add(tieptuc); thoat.setBounds(new Rectangle(250,200,80,30)); thoat.setText("Thoat"); this.getContentPane().add(thoat);

//---- Set mac dinh chon 1 trong 4 --- group1.add(tong);

group1.add(uocso); group1.add(songuyento); group1.add(phantich);

// ---- Phan mac dinh Radio phanradio k =new phanradio(); tong.addItemListener(k);

uocso.addItemListener(k); songuyento.addItemListener(k); phantich.addItemListener(k);

// ---- Phan mac dinh Button phanbutton j=new phanbutton(); thoat.addActionListener(j); thuchien.addActionListener(j); tieptuc.addActionListener(j); }

public class phanradio implements ItemListener {

public void itemStateChanged(ItemEvent e)

{ double a,b; a=Double.parseDouble(nhapn.getText()); String s; if (e.getItem()==tong) { tam=1; } else {

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 83 if (e.getItem()==uocso) { tam=2; } else { if (e.getItem()==songuyento) { tam=3; } else //(e.getItem()==phantich) { tam=4; } } } } }

public class phanbutton implements ActionListener {

public void actionPerformed(ActionEvent h) {

DoHoaTinhTong_US_SNT_PT xx=new DoHoaTinhTong_US_SNT_PT(); if(h.getSource()==thoat) { System.exit(0); xx.dispose(); } if(h.getSource()==tieptuc) { nhapn.setText(""); ketqua.setText(""); } Integer a,b=0,c ; a=Integer.parseInt(nhapn.getText()); String s;

if(h.getSource()==thuchien && tam==1) { for(int i=0;i<=a;i++) b=b+i; s=String.valueOf(b); ketqua.setText(s); }

if(h.getSource()==thuchien && tam==2) { for(int i=1;i<a;i++) { c=a/i; c=a-i*c; if (c == 0) b=i; } s=String.valueOf(b); ketqua.setText(s); }

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 84

if(h.getSource()==thuchien && tam==3)

{

for(int i=2; i<a;i++)

{ c=a/i; c=a-i*c; if(c==0) b=1; } if(b==1) ketqua.setText("False"); else ketqua.setText("True"); }

if(h.getSource()==thuchien && tam==4)

{

for(int i=1; i<a; i++)

{ c=a/i; c=a-i*c; if(c==0) b=i; } c=a/b; s1.valueOf(b); s2.valueOf(c); ketqua.setText(b+"*"+c); } } }

public static void main(String[] args)

{ DoHoaTinhTong_US_SNT_PT f=new DoHoaTinhTong_US_SNT_PT(); f.setSize(400,300);

f.show(); }

public class Close_this_windowAdapter extends WindowAdapter {

DoHoaTinhTong_US_SNT_PT jframe;

Close_this_windowAdapter(DoHoaTinhTong_US_SNT_PT jframe) { this.jframe=jframe; }

public void windowClosing(WindowEvent e) { System.exit(0);

jframe.dispose(); }

} }

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 85

*** Bài 26: Class dùng Switch Case tính - Giải phương trình bậc 1 và bậc 2

*** Đáp án xuất file

============================ Và đây là code trong void main :

import java.util.Scanner; import java.io.*;

public class GiaiPTBac1Bac2_SwitchCase {

public static void main(String[] args) throws IOException {

int n;

Scanner sc = new Scanner(System.in);

System.out.println("Hay nghe TamGa huong dan ne ! "); System.out.println("1. Giai Phuong Trinh Bac 1"); System.out.println("2. Giai Phuong Trinh Bac 2"); System.out.print("Chon so thu tu ma ban muon tinh : "); n = sc.nextInt(); System.out.print("\n"); switch(n) { case 1: GiaiPTBac1.nhapptbac1(); GiaiPTBac1.giaiptbac1(); break; case 2: GiaiPTBac2.nhapptbac2(); GiaiPTBac2.giaiptbac2(); break; default:

System.out.print("Hay nhap lai so tu 1 den 2!"); }

} }

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 86

============================ Và đây là code trong void class GiaiPTBac1 :

import java.io.*;

public class GiaiPTBac1 {

static int a,b ;

//=============== Giai phuong trinh bac 1 =============== public static void nhapptbac1() throws IOException

{

BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); System.out.println("======= Giai Phuong Trinh Bac 1 www.tamga.tk"); System.out.print("Nhap a: "); String s=d.readLine(); a=Integer.parseInt(s); System.out.print("Nhap b: "); s=d.readLine(); b=Integer.parseInt(s); }

public static void giaiptbac1() {

System.out.print("Ket qua PT bac1: "); if(a==0)

System.out.println("Phuong trinh vo nghiem"); else

{

if(b==0)

System.out.println("Phuong trinh vo so nghiem"); else

System.out.println("Phuong trinh co nghiem: " +(-b/a)); }

} }

============================ Và đây là code trong void class GiaiPTBac2 :

import java.io.*; class GiaiPTBac2 {

static double a,b,c ;

//=============== Giai phuong trinh bac 2 =============== public static void nhapptbac2() throws IOException

{

BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); System.out.println("======= Giai Phuong Trinh Bac 2 www.tamga.tk"); System.out.print("Nhap a: "); String s=d.readLine(); a=Double.parseDouble(s); System.out.print("Nhap b: "); s=d.readLine(); b=Double.parseDouble(s);

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 87 System.out.print("Nhap c: ");

s=d.readLine();

c=Double.parseDouble(s); }

public static void giaiptbac2() {

double delta,x1,x2; delta=(b*b)-(4*a*c);

System.out.print("Ket qua PT bac2: "); if (delta<0)

System.out.println("Phuong trinh vo nghiem"); else

{

if (delta==0)

System.out.println("Phuong trinh co 1 nghiem: " +(-b)/(2*a)); else

{ x1=(-b+Math.sqrt(delta))/(2*a); x2=(-b-Math.sqrt(delta))/(2*a);

System.out.println("Phuong trinh co 2 nghiem: "); System.out.println("x1= "+x1+" va "+"x2= "+x2); }

} }

}

*** Bài 27: Class xuất Hình Chữ Nhật Rỗng & Đặc - Với chiều dài và chiều rộng nhập từ bàn phím

*** Đáp án xuất file

============================ Và đây là code trong void main :

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 88 import java.io.*;

public class HinhChuNhatDacRong {

static int a,b,i,j; static String s;

// ---- Nhap ---- www.tamga.tk

public static void nhap() throws IOException

{ BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap Cot [ Row ] : ");

s=d.readLine(); a=Integer.parseInt(s);

System.out.print("Nhap Dong [ Column ] : "); s=d.readLine();

b=Integer.parseInt(s); }

// ---- Xuat hinh chu nhat Rong---- www.tamga.tk public static void xuat1() throws IOException

{ System.out.println("---- Hinh chu nhat Rong"); for (i=0;i<a;++i) { for (j=0;j<b;++j) if (i==0 || j==0 ||i==(a-1) || j==(b-1)) System.out.print("*"); else System.out.print(" "); System.out.print("\n"); } }

// ---- Xuat hinh chu nhat Dac---- www.tamga.tk public static void xuat2() throws IOException

{

System.out.println("---- Hinh chu nhat Dac"); for (int i=1;i<=a;i++)

{ for (int j=1; j<=b; j++) System.out.print("*"); System.out.print("\n"); } }

public static void main(String[] args) throws IOException {

HinhChuNhatDacRong hcn=new HinhChuNhatDacRong (); hcn.nhap(); hcn.xuat1(); hcn.xuat2();

} }

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 89

*** Bài 28: Class xuất Hình Vuông Rỗng & Đặc - Với cạnh nhập từ bàn phím

*** Đáp án xuất file

============================ Và đây là code trong void main :

import java.io.*;

public class HinhVuongDacRong {

static int n,i,j; static String s;

// ---- Nhap ---- www.tamga.tk

public static void nhap() throws IOException {

BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap Canh n = ");

s=d.readLine(); n=Integer.parseInt(s); }

// ---- Xuat hinh vuong Rong---- www.tamga.tk public static void xuat1() throws IOException

{

System.out.println("---- Hinh vuong Rong --- www.tamga.tk"); for (int i=1;i<=n;i++)

{ for (int j=1; j<=2*n; j++) if (i==1 || i==n || j==1 || j==2*n) System.out.print("*"); else System.out.print(" "); System.out.print("\n"); } }

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 90 // ---- Xuat hinh vuong Dac---- www.tamga.tk

public static void xuat2() throws IOException {

System.out.println("---- Hinh vuong Dac --- www.tamga.tk"); for (int i=1;i<=n;i++)

{ for (int j=1; j<=2*n; j++) System.out.print("*"); System.out.print("\n"); } }

public static void main(String[] args) throws IOException {

HinhVuongDacRong hv=new HinhVuongDacRong (); hv.nhap(); hv.xuat1(); hv.xuat2(); }

}

*** Bài 29: Class xuất Hình Chóp Rỗng & Đặc - Với chiều cao nhập từ bàn phím

*** Đáp án xuất file

============================ Và đây là code trong void main :

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 91 import java.io.*;

public class HinhChopDacRong { static int n,i,j;

static String s;

// ---- Nhap ---- www.tamga.tk

public static void nhap() throws IOException {

BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap Chieu Cao n = ");

s=d.readLine(); n=Integer.parseInt(s); }

// ---- Xuat hinh chop Rong---- www.tamga.tk public static void xuat1() throws IOException

{

System.out.println("---- Hinh chop Rong --- www.tamga.tk"); for (int i=1;i<=n;i++)

{ for (int j=1; j<=n-1+i; j++)

if (j==n+1-i || j==n-1+i || i==n) System.out.print("*"); else System.out.print(" "); System.out.print("\n"); } }

// ---- Xuat hinh chop Dac---- www.tamga.tk public static void xuat2() throws IOException

{

System.out.println("---- Hinh chop Dac --- www.tamga.tk"); for (int i=1;i<=n;i++)

{ for (int j=1; j<=n-1+i; j++)

if (j>=n+1-i) System.out.print("*"); else System.out.print(" "); System.out.print("\n"); } }

public static void main(String[] args) throws IOException {

HinhChopDacRong hinhchop=new HinhChopDacRong(); hinhchop.nhap();

hinhchop.xuat1(); hinhchop.xuat2(); }

Tâm Gà – Phone: 01283.98.69.98 Email: tamgaalbum@yahoo.com Site : www.tamga.tk 92

*** Bài 30: Class xuất Tam Giác Vuông tăng dần với n

*** Đáp án xuất file

============================ Và đây là code trong void main :

import java.io.*;

public class TamGiacVuongTangDan {

static int n,i,j; static String s;

// ---- Nhap ---- www.tamga.tk

public static void nhap() throws IOException {

BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap n = "); s=d.readLine(); n=Integer.parseInt(s); } // ---- Xuat ---- www.tamga.tk

public static void xuat() throws IOException {

System.out.println("---- Tam Giac Vuong tang dan theo gia tri n "); for(i=1;i<=n;i++) //tang so dong

{

for(j=1;j<=i;j++) //tang so cot

{ System.out.print( j ); } System.out.print("\n");

} }

public static void main(String[] args) throws IOException

{ TamGiacVuongTangDan tgv=new TamGiacVuongTangDan(); tgv.nhap(); tgv.xuat();

} }

Một phần của tài liệu Bài tập Lập trình JAVA căn bàn full code (Trang 81 - 93)

Tải bản đầy đủ (PDF)

(93 trang)