[r]
(1)Bài 1)
import java.util.Scanner;
public class GiaiPTB2 {
float a,b,c; double delta; float x1,x2;
boolean conghiem = false;
/////////////////////////////////////////////////////////////////////////////////////// void Nhap()
{
Scanner scn = new Scanner(System.in); System.out.print("Moi nhap a: "); a = scn.nextFloat();
System.out.print("Moi nhap b: "); b = scn.nextFloat();
System.out.print("Moi nhap c: "); c = scn.nextFloat();
}
/////////////////////////////////////////////////////////////////////////////////////// double TinhDelta()
{
return (b*b - 4*a*c); }
/////////////////////////////////////////////////////////////////////////////////////// void Giai() //Ax^2 + Bx + C =
{
if(a==0)//phương trình bậc {
if(b==0) {
if(c==0)
System.out.println("Phương trình vơ số nghiệm."); else //c!=0
System.out.println("Phương trình vơ nghiệm."); }
else //b!=0 //Bx = -C {
x1 = x2 = -c/b; conghiem = true; }
(2)}
// truong hop a!=0 //Phương trình bậc hai delta = TinhDelta(); if(delta <0)
System.out.println("Phương trình vơ nghiệm."); else
{
if(delta == 0) {
x1 = x2 = -b/(2*a); conghiem = true; }
else {
x1 = (float)((-b + Math.sqrt(delta))/(2*a)); x2 = (float)((-b - Math.sqrt(delta))/(2*a)); conghiem = true;
} } }
/////////////////////////////////////////////////////////////////////////////////////// void Xuat()
{
if(conghiem == true) {
System.out.println("Nghiệm x1=" + x1 + ", x2=" + x2); }
}
/////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) {
GiaiPTB2 gpt = new GiaiPTB2(); gpt.Nhap();
gpt.Giai(); gpt.Xuat(); }
(3)Bài 2)
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GiaiPTB2_GD extends Frame implements ActionListener
{
Label lbNhapA = new Label("Nhập a:");
TextField tfNhapA = new TextField();
Label lbNhapB = new Label("Nhập b:");
TextField tfNhapB = new TextField();
Label lbNhapC = new Label("Nhập c:");
TextField tfNhapC = new TextField();
Label lbNghiem = new Label("Nghiệm:");
TextField tfNghiem = new TextField();
Button btnXoa = new Button("Xóa");
(4)Button btnThoat = new Button("Thoát");
public GiaiPTB2_GD()
{
this.setTitle("Giải PT bậc 2");
this.setLayout(null);
lbNhapA.setBounds(40,50,50,30);
this.add(lbNhapA);
tfNhapA.setBounds(100,50,150,30);
this.add(tfNhapA);
lbNhapB.setBounds(40,90,50,30);
this.add(lbNhapB);
tfNhapB.setBounds(100,90,150,30);
this.add(tfNhapB);
lbNhapC.setBounds(40,130,50,30);
this.add(lbNhapC);
tfNhapC.setBounds(100,130,150,30);
this.add(tfNhapC);
lbNghiem.setBounds(40,170,50,30);
this.add(lbNghiem);
tfNghiem.setBounds(100,170,150,30);
this.add(tfNghiem);
(5)btnXoa.setBounds(40, 210,60,30);
this.add(btnXoa);
btnGiai.setBounds(110, 210,60,30);
this.add(btnGiai);
btnThoat.setBounds(180, 210,60,30);
btnThoat.addActionListener(this);
this.add(btnThoat);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
GiaiPTB2_GD f = new GiaiPTB2_GD();
f.setBounds(350,100,300,350);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}