Tải bản đầy đủ (.pdf) (5 trang)

thực hành java võ tấn dũng votandung

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (88.38 KB, 5 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

<b>Bài 1) </b>



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 = 0


{


if(a==0)//phương trình bậc nhất
{


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;
}


</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=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();
}


</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

<b>Bài 2) </b>




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");


</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=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);


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=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);


}


</div>

<!--links-->

×