1. Trang chủ
  2. » Công Nghệ Thông Tin

Bai tap thuc hanh java

60 1.3K 20

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Bài tập tham khảo về kỹ thuật lập trình với ngôn ngữ Java (có code và minh họa kết quả cụ thể)

TH Lập trình Java http://nguoichuprong.com Code by Phubk - 1 - BÀI THỰC HÀNH SỐ 1 Bài 1 : Viết chương trình nhập 1,2 hoặc 3 số nguyên dương. Nếu người dùng nhập một số nguyên a, chương trình sẽ hiển thị diện tích của hình tròn có bán kính là a. Nếu người dùng nhập vào hai số nguyên a và b thì chương trình sẽ hiển thị diện tích hình chữ nhật có chiều dài a, chiều rộng là b. Nếu người dùng nhập vào 3 số nguyên a,b và c thì chương trình trước tiên sẽ kiểm tra các số có phải là 3 cạnh của tam giác không?. Nếu đúng , chương trình sẽ hiển thị diện tích của tam giác có chiều dài 3 cạnh là a,b, c; ngược lại, chương trình sẽ hiển thị thông điệp “ không phải là độ dài ba cạnh của một tam giác”. Code : import java.util.Scanner; public class Bai1 { public static int a = 0, b = 0, c = 0; public void nhap() { Scanner sc = new Scanner(System.in); do { System.out.println("Nhap so nguyen duong a: "); a = sc.nextInt(); } while (a < 0); do { System.out.println("Nhap so nguyen duong b: "); b = sc.nextInt(); } while (b < 0); do { System.out.println("Nhap so nguyen duong c: "); c = sc.nextInt(); } while (c < 0); } public void hinhtron(int a) { float kq = 0; TH Lập trình Java http://nguoichuprong.com Code by Phubk - 2 - kq = (float) (3.14 * a * a); System.out.println("Dien tich hinh tron ban kinh a: " + kq); } public void chunhat(int a, int b) { float dt = 0; dt = a * b; System.out .println("Dien tich hinh chu nhat co do dai hai canh a, b la :" + dt); } public void tamgiac(int a, int b, int c) { if (kiemtra(a, b, c)) { float p = (a + b + c) / 2; double s = Math.sqrt(p * (p - a) * (p - b) * (p - c)); System.out.println("Dien tich tam giac : " + s); } else { System.out.println("Day khong phai do dai 3 canh cua 1 tam giac"); } } public static boolean kiemtra(int a, int b, int c) { if ((a + b > c) && (a + c > b) && (b + c > a)) { return true; } else { return false; } } public static void main(String args[]) { Bai1 b1 = new Bai1(); //Khong nhap gan gia tri = 0; b1.nhap(); if (a != 0 && b == 0 && c == 0) { TH Lập trình Java http://nguoichuprong.com Code by Phubk - 3 - b1.hinhtron(a); } if (a != 0 && b != 0 && c == 0) { b1.chunhat(a, b); } if (a != 0 && b != 0 && c != 0) { b1.tamgiac(a, b, c); } } } Kết quả chạy demo: Code : import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Bai1 extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JTextField tfA, tfB, tfC; JTextArea taResut; JButton btOk, btReset, btExit; JPanel p1, p2; public Bai1(String title) { TH Lập trình Java http://nguoichuprong.com Code by Phubk - 4 - super(title); setLayout(new BorderLayout()); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent w) { System.exit(0); } }); p1 = new JPanel(new FlowLayout()); add(p1, BorderLayout.NORTH); p1.add(new JLabel("So a: ")); tfA = new JTextField(5); p1.add(tfA); p1.add(new JLabel("So b: ")); tfB = new JTextField(5); p1.add(tfB); p1.add(new JLabel("So c: ")); tfC = new JTextField(5); p1.add(tfC); btOk = new JButton(" OK "); btOk.addActionListener(this); p1.add(btOk); btReset = new JButton(" Reset "); btReset.addActionListener(this); p1.add(btReset); btExit = new JButton(" Exit "); btExit.addActionListener(this); p1.add(btExit); p2 = new JPanel(new FlowLayout()); add(p2, BorderLayout.CENTER); TH Lập trình Java http://nguoichuprong.com Code by Phubk - 5 - taResut = new JTextArea(5, 40); taResut.setFont(new Font("Arial", Font.BOLD, 14)); p2.add(taResut); setBounds(100, 100, 550, 200); setVisible(true); } public void actionPerformed(ActionEvent ae) { Object cmd = ae.getSource(); if (cmd == btExit) System.exit(0); if (cmd == btReset) { tfA.setText(""); tfB.setText(""); tfC.setText(""); taResut.setText(""); } if (cmd == btOk) { taResut.setText(""); String a = tfA.getText(); String b = tfB.getText(); String c = tfC.getText(); String msg = ""; if (!a.equals("") && !b.equals("") && !c.equals("")) msg = dientichtamgiac(Integer.parseInt(a), Integer.parseInt(b), Integer.parseInt(c)); else { if (!a.equals("") && !b.equals("")) msg = dientichhcn(Integer.parseInt(a), Integer.parseInt(b)); TH Lập trình Java http://nguoichuprong.com Code by Phubk - 6 - else if (!a.equals("") && !c.equals("")) msg = dientichhcn(Integer.parseInt(a), Integer.parseInt(c)); else if (!b.equals("") && !c.equals("")) msg = dientichhcn(Integer.parseInt(b), Integer.parseInt(c)); else if (!a.equals("")) msg = dientichhinhtron(Integer.parseInt(a)); else if (!b.equals("")) msg = dientichhinhtron(Integer.parseInt(b)); else if (!c.equals("")) msg = dientichhinhtron(Integer.parseInt(c)); } taResut.setText(msg); } } public static String dientichhinhtron(int r) { String str = "Dien tich hinh tron = " + (Math.PI * r * r); return str; } public static String dientichhcn(int a, int b) { String str = "Dien tich hcn = " + (a * b); return str; } public static String dientichtamgiac(int a, int b, int c) { double p, s; String str = ""; if (a + b > c && a + c > b && b + c > a) { p = (a + b + c) / 2.0; s = Math.sqrt(p * (p - a) * (p - b) * (p - c)); TH Lập trình Java http://nguoichuprong.com Code by Phubk - 7 - str = "Dien tich tam giac = " + s; } else str = a + ", " + b + ", " + c + ": khong phai do dai 3 canh cua tam giac"; return str; } public static void main(String args[]) { new Bai1("Thuc hanh Java"); } } Demo chương trình : TH Lập trình Java http://nguoichuprong.com Code by Phubk - 8 - Bài 2: Viết chương trình hiển thị tẩt cả các số n từ 10 đến 1000 có tính chất sau: tổng cảu tất cả các số trong n bằng tích của tất cả các số có trong n. Ví dụ số: 123 có tính chất này, vì 1+2+3=1*2*3. Code : public class Bai2 { public static int n, i = 0; public void hienthi() { for (n = 10; n <= 1000; n++) { if (kiemtra(n)) { System.out.println(n); } } } public static boolean kiemtra(int n) { int tam; int s = 0; int p = 1; while (n > 0) { tam = n % 10; n /= 10; i++; s += tam; p *= tam; TH Lập trình Java http://nguoichuprong.com Code by Phubk - 9 - } if (p == s) { return true; } else { return false; } } public static void main(String args[]) { Bai2 b2 = new Bai2(); b2.hienthi(); } } Kết quả chạy Demo: Code : import java.awt.*; import java.awt.event.*; import javax.swing.*; public class bai2s extends JFrame implements ActionListener { JTextArea taResut; JButton btDisplay, btReset, btExit; JPanel p1, p2; public bai2s(String title) { super(title); TH Lập trình Java http://nguoichuprong.com Code by Phubk - 10 - setLayout(new BorderLayout()); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent w) { System.exit(0); } }); p1 = new JPanel(new FlowLayout()); add(p1, BorderLayout.NORTH); btDisplay = new JButton(" Display "); btDisplay.addActionListener(this); p1.add(btDisplay); btReset = new JButton(" Reset "); btReset.addActionListener(this); p1.add(btReset); btExit = new JButton(" Exit "); btExit.addActionListener(this); p1.add(btExit); p2 = new JPanel(new FlowLayout()); add(p2, BorderLayout.CENTER); taResut = new JTextArea(5, 30); taResut.setLineWrap(true); JScrollPane scroll = new JScrollPane(taResut); p2.add(scroll); setBounds(100, 100, 450, 200); setVisible(true); } public void actionPerformed(ActionEvent ae) { Object cmd = ae.getSource(); if (cmd == btExit) System.exit(0); if (cmd == btReset) { [...]... args[]) { new bai2 s( "Bai 2 - Thuc hanh Java" ); Code by Phubk - 11 - TH Lập trình Java http://nguoichuprong.com } } Kết quả chạy Demo: Bài 3: Viết chương trình nhập số tự nhiên n (n=1,2,…) và kiểm tra biểu diễn nhị phân của nó có đối xứng hay không ? Ví dụ: n=9, biểu diễn nhị phân của nó là 1001 là đối xứng Còn nếu n=10 , biểu diễn nhị phân của nó là 1010 là không đối xứng Code : import java. util.*;... System.out.print(" khong doi xung"); } } public static void main(String args[]) { Bai3 b3 = new Bai3 (); b3.nhap(); System.out.print("So nhi phan: "); b3.dectobin(n); b3.hix(n); } } Code by Phubk - 13 - TH Lập trình Java http://nguoichuprong.com Kết quả chạy Demo: Code : import java. awt.*; import java. awt.event.*; import javax.swing.*; public class bai3 extends JFrame implements ActionListener { JTextField tfInput;... Lập trình Java http://nguoichuprong.com str1 = tf1.getText(); str2 = tf2.getText(); str1.toLowerCase(); str2.toLowerCase(); xuli(); } } } Kết quả chạy demo: BÀI THỰC HÀNH SỐ 3 Code by Phubk - 30 - TH Lập trình Java http://nguoichuprong.com Bài 1: Viết chương trình tính n! với 1 . public static void main(String args[]) { new bai2 s(" ;Bai 2 - Thuc hanh Java& quot;); TH Lập trình Java http://nguoichuprong.com Code by Phubk -. args[]) { Bai2 b2 = new Bai2 (); b2.hienthi(); } } Kết quả chạy Demo: Code : import java. awt.*; import java. awt.event.*; import javax.swing.*;

Ngày đăng: 14/03/2014, 23:45

Xem thêm: Bai tap thuc hanh java

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w