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

56 1,8K 7

Đ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 56
Dung lượng 762,78 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

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

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

Scanner input=new Scanner(System.in);

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

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

if(((a<=b)&&(b<=c))||((c<=b)&&(b<=a))) tg=b

else if(((b<=a)&&(a<=c))||((c<=a)&&(a<=b))) tg=a

else tg=c

System.out.println(" So trung gian la "+tg);

}

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

if( > 0 && b<=18) sotien = (b a)*45000;

if( >0 && b>18) sotien = (18-a)*45000 + (b-18)*60000;

Trang 5

5 Nhập vào tháng năm bất kì in ra số ngày ứng với tháng năm đó:

package baocao;

import java.util.Scanner;

public class Bai5 {

int nam,thang;

public Bai5() {

}

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

case 2 : if(namnhuan(nam))

{System.out.println("Thang "+thang+" nam"+nam+" co 29

Trang 6

public static void main (String[] args) {

Bai5 temp= new Bai5();

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.nhapn();

b6.tinh() ;}

}

Trang 7

Scanner input =new Scanner(System.in);

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

Trang 8

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

Trang 9

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

Scanner input=new Scanner(System.in);

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

Trang 10

tam=tam/10;

}

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

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

}

Trang 11

7 Kiểm tra số P có phải là số chính phương không?

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

private Scanner input;

public void nhap(){

input=new Scanner(System.in);

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

n input.nextInt();

Trang 12

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

private Scanner input;

public void nhap(){

input=new Scanner(System.in);

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

n input.nextInt();

}

public boolean nto(int n)

{

if(n==0||n==1) return false;

else for(int i=2;i<n;i++) if(n%i==0) return false;

return true;

}

public void in(){

Trang 13

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;

return false;

}

public void inra()

{

System.out.println ("Cac so hoan hao <1000 la: ");

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

}

public static void main(String[] args) {

Bai15 dt=new Bai15();

dt.inra();

}

}

Trang 14

11 In ra n chữ số Fibonaci đầu tiên:

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

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

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

private Scanner input;

public void nhap()

{

input=new Scanner(System.in);

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

Trang 15

x=y;

y=z;

}

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

}

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

Trang 16

dt.ucbc();

}

}

Trang 17

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 18

if(e.getSource()==kq)

{

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 19

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

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 20

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 21

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 22

if(t==0||t==1) return false;

else for(int i=2;i<=(t/2);i++) if((t%i)==0) return false ;

public static void main (String[] args) {

new Bai21("Tim so nguyen to");

}

}

Trang 23

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

Trang 24

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

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

gbs.fill=GridBagConstraints.BOTH;

Trang 25

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

}

}

Trang 26

5 Mô tả máy tính điện tử cá nhân

package baocao;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

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;

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

Trang 27

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 30

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

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

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

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

case 17: if(displayMode!=ERROR_MODE)

case 18: if(displayMode!=ERROR_MODE){

}break;

case 20: if(displayMode!=ERROR_MODE){

setDisplayString(getDisplayString().substring(0,

Trang 31

void addDigitToDisplay(int digit){

if(ClearOnNextDigit && !lastoperator.equals("0"))

else setDisplayString("-"+input);

}}else if(displayMode==RESULT_MODE)

Trang 32

void processOperator(String op){

if(displayMode!=ERROR_MODE)

Trang 33

if (numberInDisplay == 0)

throw (new DivideByZeroException());

result = lastnumber / numberInDisplay;

public static void main(String[] args) {

// TODO Auto-generated method stub

new Bai23("Calculator");

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

Trang 34

public class Bai24 extends Frame implements ActionListener {

Button gre,blu,red,exit;

public Bai24(String st)

{

super(st);

gre=new Button("Green");

blu=new Button("Blue");

red=new Button("Red");

exit=new Button("Exit");

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 35

public class Bai25 extends Frame implements ActionListener,ItemListener {

Label lb=new Label("The item is display here");

CheckboxGroup cg=new CheckboxGroup();

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

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

gbs.fill=GridBagConstraints.BOTH;

Trang 36

public void actionPerformed(ActionEvent e)

Trang 37

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

public static void main(String[] args) {

// TODO Auto-generated method stub

new Bai25("Tong hop cac item");

Label lTitle,lInputA,lInputB,lKq;

TextField tInputA,tInputB,tKq;

Button Tinh,Reset,Thoat;

public void init(){

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

lInputA = new Label("Nhap a");

lInputB = new Label("Nhap b");

lKq = new Label("Ket qua");

tKq = new TextField();

tInputA = new TextField();

Trang 38

tInputB = new TextField();

Tinh = new Button("Tinh");

Reset = new Button("Reset");

Thoat = new Button("Thoat");

p2.add(lInputA); p2.add(tInputA);

p2.add(lInputB); p2.add(tInputB);

public void actionPerformed(ActionEvent ae){

if(ae.getSource()==Thoat) System.exit(1);

if(ae.getSource()==Tinh){

double a,b;

a=Double.parseDouble(tInputA.getText());

b=Double.parseDouble(tInputB.getText());

if(a==0)

if(b==0) tKq.setText("pt co vo so nghiem");

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

Trang 39

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

Ngày đăng: 17/07/2015, 17:58

TỪ KHÓA LIÊN QUAN

w