Lập trình Android: Giải Phương trình bậc 2 ppt

5 4.9K 51
Lập trình Android: Giải Phương trình bậc 2 ppt

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

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Phương trình bậc 2 Sau đây mình sẽ tạo 1 ứng dụng kinh điển (thường là các bài tập về nhà ở những môn lập trình khác). Đó là ứng dụng tính Phương trình bậc 2 :)) 1/ Tạo 1 Project như sau: Project name: PTbac2 Build Target: Android 2.3.3 Application name: PTbac2 Package name: com.exam.PTbac2 Create Activity: PTbac2 2/ Các bạn tạo giao diện trong file main.xml như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Giai PT bac 2" /> <TextView android:text="A:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:numeric="integer" android:layout_width="220dp" android:id="@+id/a" android:layout_height="wrap_content"> <requestFocus></requestFocus> </EditText> <TextView android:text="B:" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/b" android:numeric="integer" android:layout_width="210dp" android:layout_height="wrap_content"/> <TextView android:text="C:" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/c" android:numeric="integer" android:layout_width="216dp" android:layout_height="wrap_content"/> <Button android:id="@+id/calculate" android:text="Caculate" android:layout_width="wrap_content" android:layout_height="83dp"/> <TextView android:id="@+id/result" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android:layout_height="117dp" android:layout_weight="0.25" android:layout_width="match_parent"/> </LinearLayout> 3/ Tiếp theo các bạn viết code xử lý trong file PTbac2.java như sau: package com.exam.PTbac2; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.*; public class PTbac2 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button) findViewById(R.id.calculate); final TextView result = (TextView) findViewById(R.id.result); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { String sa = ((EditText) findViewById(R.id.a)).getText().toString(); String sb = ((EditText) findViewById(R.id.b)).getText().toString(); String sc = ((EditText) findViewById(R.id.c)).getText().toString(); try { double a = Double.parseDouble(sa); double b = Double.parseDouble(sb); double c = Double.parseDouble(sc); if (a == 0) { result.setText("Phuong trinh bac I: "); if (b == 0) Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN { if (c == 0) result.setText(result.getText() + "PT co' vo so nghiem"); else result.setText(result.getText() + "PT vo nghiem"); } else { result.setText(result.getText() + "x=" + (-c/b)); } } else { double delta = b*b - (4*a*c); if (delta < 0.0) { result.setText("PT vo nghiem \n"); } else if (delta == 0) { result.setText("PT co nghiem kep: " + (-b/(2*a))); } else { double delta_sqrt = Math.sqrt(delta); Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN result.setText("x1 = " + ((b*b + delta_sqrt)/(2 * a)) +"\n" + "x2 = " + ((b*b - delta_sqrt)/(2 * a))); } } } catch (Exception ex) { result.setText(ex.toString()); } } }); } } Cuối cùng các bạn debug và chạy thử sẽ ra kết quả: Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN Mọi ý kiến đóng góp các bạn vui lòng post bài lên forum trang web www.laptrinhdidong.vn . Rất mong nhận đc sự đóng góp ý kiến của các bạn. Mình sẽ cập nhật trang web thường xuyên Lập trình Android – http://laptrinhdidong.vn Page 5 . Tin học – ĐH KHTN Phương trình bậc 2 Sau đây mình sẽ tạo 1 ứng dụng kinh điển (thường là các bài tập về nhà ở những môn lập trình khác). Đó là ứng dụng tính Phương trình bậc 2 :)) 1/ Tạo 1 Project. android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:numeric="integer" android:layout_width=" ;22 0dp" android:id="@+id/a" android:layout_height="wrap_content"> . android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Giai PT bac 2& quot; /> <TextView android:text="A:" android:id="@+id/textView1" android:layout_width="wrap_content"

Ngày đăng: 07/08/2014, 14:20

Tài liệu cùng người dùng

Tài liệu liên quan