1. Trang chủ
  2. » Luận Văn - Báo Cáo

Bài thực hành ngôn ngữ lập trình java

54 441 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 54
Dung lượng 1,28 MB

Nội dung

BÀI THỰC HÀNH NGÔN NGỮ LẬP TRÌNH JAVA private Scanner input; public void nhap { input = new ScannerSystem.in; System.out.println"Nhap cac he so:"; System.out.print"a = "; public static v

Trang 1

BÀI THỰC HÀNH NGÔN NGỮ LẬP TRÌNH JAVA

private Scanner input;

public void nhap() {

input = new Scanner(System.in);

System.out.println("Nhap cac he so:");

System.out.print("a = ");

public static void main(String arg[]) {

Bai1 dt = new Bai1();

dt.nhap();

dt.giai();

}

}

Trang 2

public void nhap() {

Scanner input = new Scanner(System.in);

System.out.println("Nhap cac he so");

System.out.print("a = ");

else {

System.out.println("PT co 2 nghiem phan biet");

System.out.println("x1 = " + ((-b-Math.sqrt(delta))/(2*a))); System.out.println("x2 = " + ((-b+Math.sqrt(delta))/(2*a))); }

}

}

public static void main(String areg[]) {

Bai2 dt = new Bai2();

dt.nhap();

dt.giai();

}

}

Trang 3

3 Tìm số trung gian của 3 số a,b,c:

Scanner input=new Scanner(System.in);

System.out.println("Nhap a b c:");

System.out.print("a= ");

public static void main (String[] args) {

Bai3 stg=new Bai3();

stg.nhap();

stg.tg();

}

}

Trang 4

4 Viết chương trình tính tiền cho bài toán karaoke:

Scanner input =new Scanner(System.in);

System.out.println("Nhap gio bat dau ");

public class Bai5 {

int nam,thang;

public Bai5() {

}

Trang 5

public boolean namnhuan(int nam)

{

if((nam%4==0 && nam%100!=0)||(nam%400==0)) return true;

else return false;

}

public void nhap()

{

Scanner input= new Scanner (System.in);

System.out.println("Nhap nam thang ");

System.out.print("Nam ");

public static void main (String[] args) {

Bai5 temp= new Bai5();

temp.nhap();

temp.inra();

}

}

Trang 6

Scanner input=new Scanner(System.in);

System.out.println("Nhap n");

for(int i=1;i<=n;i++) s+=(float) 1/i;

System.out.println("S= "+s);

}

public static void main (String[] args) {

Bai6 b6=new Bai6();

b6.tinh() ; }

Scanner input =new Scanner(System.in);

System.out.print("Nhap n = ");

Trang 7

public void tinh()

Scanner input =new Scanner(System.in);

System.out.println("Nhap n : ");

for(int i=1;i<2*n;i+=2) s+=1.0f/giaithua(i);

System.out.print("S = " + s);

}

public static void main(String[] str)

{

Trang 8

Scanner input=new Scanner(System.in);

System.out.print("Nhap n= ");

public static void main(String[] args) {

// TODO Auto-generated method stub

Bai9 dt=new Bai9();

dt.nhap();

dt.tinh();

}

}

Trang 9

5 Tính tổng và tích các chữ số của một số nguyên dương m cho trước:

Scanner input=new Scanner(System.in);

System.out.print("Nhap so nguyen duong m= ");

System.out.println("Tong cac chu so cua "+ +" la "+s);

System.out.println("Tich cac chu so cua "+ +" la "+p);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Bai10 dt=new Bai10();

public void nhap() {

Scanner input = new Scanner(System.in);

System.out.print("Nhap so : ");

Trang 10

public static void main(String args[]) {

Bai11 a = new Bai11();

a.nhap();

if (a.check()) System.out.println(n + " la so nguyen to");

else System.out.println(a.get() + " ko phai la so nguyen to");

input=new Scanner(System.in);

System.out.printf("Nhap n= ");

n input.nextInt();

}

public boolean chinhphuong(){

for(int i=0;i<=n;i++) if(i*i==n) return true;

if(dt.chinhphuong()) System.out.println(n+ " la so chinh phuong");

else System.out.println(n+ " khong la so chinh phuong");

}

}

Trang 11

8 Kiểm tra số M có phải là số đối xứng không?

package baocao;

import java.util.Scanner;

public class Bai13 {

static int ;

private Scanner input;

public void nhap(){

input=new Scanner(System.in);

System.out.print("Nhap n=");

if(dao==n) System.out.println(n+ " la so doi xung");

else System.out.println(n+" Khong phai la so doi xung");

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Bai13 dt=new Bai13();

dt.nhap();

dt.doixung(n);

}

}

Trang 12

9 In ra các số nguyên tố nhỏ hơn hoặc bằng số nguyên dương n cho trước:

package baocao;

import java.util.Scanner;

public class Bai14 {

int ;

private Scanner input;

public void nhap(){

input=new Scanner(System.in);

System.out.print("Nhap n=");

public void in(){

System.out.println("Cac so nguyen to <= " +n+ " la : ");

for(int i=2;i<=n;i++) if(nto(i)) System.out.print(" "+i);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Bai14 dt=new Bai14();

public class Bai15 {

public boolean hh(int n)

{

int sum=0;

for (int i=1;i<n;i++) if(n%i==0) sum+=i;

if (sum==n) return true;

Trang 13

for(int i=1;i<1000;i++) if(hh(i)) System.out.println(i);

}

public static void main(String[] args) {

Bai15 dt=new Bai15();

System.out.println(n " Chu so Fibonaci dau tien la: ");

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

System.out.print(fibo(i)+" ");

Trang 14

12 Kiểm tra số K có thuộc dãy Fibonaci hay không?

package baocao;

import java.util.Scanner;

public class Bai17 {

int ;

private Scanner input;

public void nhap()

{

input=new Scanner(System.in);

System.out.print("Nhap n= ");

if(z==n) System.out.println(n+ " la so FIBONACI");

else System.out.println(n " Khong phai la so FBONACI");

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Bai17 dt=new Bai17();

private Scanner input;

public void nhap()

{

input=new Scanner(System.in);

System.out.println("Nhap 2 so: ");

System.out.print("a= ");

a input.nextInt();

System.out.print("b= ");

Trang 15

}

System.out.println("UCLN cua "+ +" va "+ +" la: "+uc);

System.out.println("BCNN cua "+ +" va "+ +" la: "+(a b)/uc);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Bai18 dt=new Bai18();

dt.nhap();

dt.ucbc();

}

}

Trang 16

CHƯƠNG II: LẬP TRÌNH AWT-SWING

public void GUI() {

lb=new Label("Giai phuong trinh bac nhat");

lb1=new Label("Nhap a");

lb2=new Label("Nhap b");

lb3=new Label("Ket qua");

txta=new TextField();

txtb=new TextField();

txtkq=new TextField();

kq=new Button("Tinh");

reset=new Button("Reset");

thoat=new Button("Thoat");

kq.addActionListener(this);

reset.addActionListener(this);

thoat.addActionListener(this);

pn=new Panel(new GridLayout(3,1));

pn1=new Panel(new GridLayout(1,1));

pn2=new Panel(new GridLayout(3,2));

pn3=new Panel(new GridLayout(1,3));

Trang 17

{

int a=Integer.parseInt(txta.getText());

int b=Integer.parseInt(txtb.getText());

if(a!=0) txtkq.setText(Float.toString((float)-b/a) );

else if(b==0) txtkq.setText("Pt vo so nghiem");

else txtkq.setText("Pt vo nghiem");

public static void main (String[] args) {

new Bai19("Giai pt bac 1");

}

Trang 18

2 Minh họa các phép toán:

lb=new Label("Minh hoa cac phep toan");

lb1=new Label("Nhap a");

lb2=new Label("Nhap b");

lb3=new Label("Ket qua");

txta=new TextField("");

txtb=new TextField("");

txtkq=new TextField("");

cong=new Button("Cong");

tru=new Button("Tru");

nhan=new Button("Nhan");

chia=new Button("Chia");

exit=new Button("Exit");

reset=new Button("Reset");

cong.addActionListener(this);

tru.addActionListener(this);

nhan.addActionListener(this);

chia.addActionListener(this);

exit.addActionListener(this);

reset.addActionListener(this);

pn=new Panel(new GridLayout(4,1));

pn1=new Panel(new GridLayout(1,1));

pn2=new Panel(new GridLayout(3,2));

pn3=new Panel(new FlowLayout(FlowLayout.CENTER));

pn4=new Panel(new FlowLayout(FlowLayout.CENTER));

Trang 19

double a=Double.parseDouble(txta.getText());

double b=Double.parseDouble(txtb.getText());

if (e.getSource()==cong) txtkq.setText(Double.toString(a+b));

if (e.getSource()==tru) txtkq.setText(Double.toString(a-b));

if (e.getSource()==nhan) txtkq.setText(Double.toString(a*b));

if (e.getSource()==chia) txtkq.setText(Double.toString(a/b));

public static void main (String[] args) {

new Bai20("Minh hoa cac phep toan");

}

}

Trang 20

3 In ra các số nguyên tố nhỏ hơn hoặc bằng số n cho trước:

//String chuoi1=new String();

//String chuoi2=new String(" ");

Label lb,lb1,lb2;

TextField txtnhap,txtkq;

Button tim,reset,exit;

Panel pn,pn1,pn2,pn3;

public void GUI1(){

lb=new Label("Cac so nguyen to <=n ");

lb1=new Label("Nhap n:");

lb2=new Label("KQ:");

txtnhap=new TextField();

txtkq=new TextField();

txtkq.setEditable(false);

tim=new Button("Tim");

exit=new Button("Exit");

reset=new Button("Reset");

tim.addActionListener(this);

exit.addActionListener(this);

reset.addActionListener(this);

pn=new Panel(new GridLayout(3,1));

pn1=new Panel(new GridLayout(1,1));

pn2=new Panel(new GridLayout(2,2));

pn3=new Panel(new FlowLayout(FlowLayout.CENTER));

Trang 21

public void actionPerformed(ActionEvent e)

public static void main (String[] args) {

new Bai21("Tim so nguyen to");

}

}

Trang 22

4 Kiểm tra một số có thuộc dãy Fibonaci hay không?

Button ok,reset,exit;

Panel pn=new Panel();

GridBagLayout gb=new GridBagLayout();

GridBagConstraints gbs=new GridBagConstraints();

public Bai22(String st)

{

super(st);

lb=new Label("Kiem tra a co thuoc day Fibonaci");

lb1=new Label("Nhap a:");

lb2=new Label("KQ: ");

txtnhap=new TextField(20);

txtkq=new TextField(20);

ok=new Button("OK");

reset=new Button("Reset");

exit=new Button("Exit");

ok.addActionListener(this);

reset.addActionListener(this);

exit.addActionListener(this);

//pn=new Panel(new GridBagLayout());

gbs.insets=new Insets(2, 2, 2, 2);

gbs.fill=GridBagConstraints.BOTH;

Trang 23

y=z;

Trang 24

}

if(z==n) return true; }

return false;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==exit) System.exit(0);

else if(e.getSource()==reset) {

txtnhap.setText("");

txtkq.setText("");

}

else {

int a=Integer.parseInt(txtnhap.getText());

if(fibo(a)) txtkq.setText(a+" la so FIBONACI");

else txtkq.setText(a+" khong la so FIBONACI");

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Bai22("Kiem tra fibo");

public class Bai23 extends JFrame implements ActionListener{

GridBagLayout gb=new GridBagLayout();

GridBagConstraints gbc=new GridBagConstraints();

final int MAX_INPUT_LENGTH=20;

final int INPUT_MODE=0;

final int RESULT_MODE=1;

final int ERROR_MODE=2;

int displayMode=INPUT_MODE;

boolean ClearOnNextDigit,percent;

double lastnumber=0;

Trang 25

String lastoperator="0";

private JTextField txtkq=new JTextField("0",20);

private JButton jbnButtons[];

JPanel jpn=new JPanel();

public Bai23(String s)

{

super(s);

jbnButtons=new JButton[23];

gbc.insets=new Insets(3, 3, 3, 3);

gbc.fill=GridBagConstraints.BOTH;

for(int i=0;i<=9;i++) jbnButtons[i]= new JButton(String.valueOf(i));

jbnButtons[10]=new JButton("+/-");

jbnButtons[11]=new JButton(".");

jbnButtons[12]=new JButton("=");

jbnButtons[13]=new JButton("/");

jbnButtons[14]=new JButton("*");

jbnButtons[15]=new JButton("-");

jbnButtons[16]=new JButton("+");

jbnButtons[17]=new JButton("sqrt");

jbnButtons[18]=new JButton("1/x");

jbnButtons[19]=new JButton("%");

jbnButtons[20]=new JButton("Backspace");

jbnButtons[21]=new JButton("CE");

jbnButtons[22]=new JButton("C");

Trang 28

for(int i=0;i<=22;i++)

{jpn.add(jbnButtons[i]);

jbnButtons[i].addActionListener(this);

case 0: addDigitToDisplay(i);break;

case 1: addDigitToDisplay(i);break;

case 2: addDigitToDisplay(i);break;

case 3: addDigitToDisplay(i);break;

case 4: addDigitToDisplay(i);break;

case 5: addDigitToDisplay(i);break;

case 6: addDigitToDisplay(i);break;

case 7: addDigitToDisplay(i);break;

case 8: addDigitToDisplay(i);break;

case 9: addDigitToDisplay(i);break;

case 10: processSignChange();break;

case 11: addDecimalPoint();break;

case 12: processEquals();break;

case 13: processOperator("/");break;

case 14: processOperator("*");break;

case 15: processOperator("-");break;

case 16: processOperator("+");break;

case 17: if(displayMode!=ERROR_MODE)

}catch(Exception ex){

displayError("Khong hop le");

displayMode=ERROR_MODE; }

Trang 29

}catch(Exception ex){

displayError("Loi chia 0");

displayMode=ERROR_MODE; }

}catch(Exception ex){

displayError("Khong hop le");

displayMode=ERROR_MODE; }

}break;

case 20: if(displayMode!=ERROR_MODE){

setDisplayString(getDisplayString().substring(0, getDisplayString().length()-1));

if(getDisplayString().length()<1) setDisplayString("0");

}break;

case 21: clearExisting();break;

case 22: clearAll();break; }

} }

void addDigitToDisplay(int digit){

if(ClearOnNextDigit && !lastoperator.equals("0")) setDisplayString(""); String inputString=getDisplayString();

if(inputString.indexOf("0")==0 && inputString.indexOf(".")<0)

Trang 30

void addDecimalPoint(){

displayMode=INPUT_MODE;

//if(ClearOnNextDigit) setDisplayString("");

String inputString =getDisplayString();

if(inputString.indexOf(".")<0) setDisplayString(new

else setDisplayString("-"+input);

} }else if(displayMode==RESULT_MODE)

try{

double result=processLastOperator();

displayResult(result);

lastnumber=result;

Trang 31

}catch(Exception e){}

}else lastnumber=numberInDisplay;

}catch(Exception e){

displayError("Loi chia 0");

}lastoperator="0"; }

throw (new DivideByZeroException());

result = lastnumber / numberInDisplay;

Trang 32

displayMode = ERROR_MODE;

ClearOnNextDigit = true;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new Bai23("Calculator");

//Bai23 dt=new Bai23("Calculator");

public class Bai24 extends Frame implements ActionListener {

Button gre,blu,red,exit;

public Bai24(String st)

Trang 33

{

gre=new Button("Green");

blu=new Button("Blue");

red=new Button("Red");

exit=new Button("Exit");

gre.addActionListener(this);

blu.addActionListener(this);

red.addActionListener(this);

exit.addActionListener(this);

this.setLayout(new FlowLayout(FlowLayout.CENTER));

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource()==exit) System.exit(0);

if(e.getSource()==gre) this.setBackground(Color.GREEN);

if(e.getSource()==blu) this.setBackground(Color.BLUE);

if(e.getSource()==red) this.setBackground(Color.RED);

Trang 34

Checkbox nam=new Checkbox("Male", cg, true);

Checkbox nu=new Checkbox("Female", cg, false);

List list1 =new List(3,false);

Choice ch1=new Choice();

Button exit=new Button("Exit");

Panel pn=new Panel();

GridBagLayout gb=new GridBagLayout();

GridBagConstraints gbs=new GridBagConstraints();

public Bai25(String st)

{

super(st);

list1.add("MSDOS");

list1.add("WINDOW");

list1.add("LINUX");

list1.add("UNIX");

ch1.add("Dog");

ch1.add("Cat");

ch1.add("chicken");

exit.addActionListener(this);

nam.addItemListener(this);

nu.addItemListener(this);

list1.addItemListener(this);

ch1.addItemListener(this);

gbs.insets=new Insets(8, 2, 8, 2);

gbs.fill=GridBagConstraints.BOTH;

Ngày đăng: 24/07/2016, 11:05

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN

w