intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: Huỳnh Thị Thùy Dương | Ngày: | Loại File: DOC | Số trang:63

307
lượt xem
82
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Nhằm giúp các bạn đang học chuyên ngành Công nghệ thông tin có thêm tài liệu tham khảo trong quá trình học tập và ôn thi, mời các bạn cùng tham khảo nội dung tài liệu "Bài tập thực hành ngôn ngữ lập trình Java" dưới đây. Hy vọng đây là tài liệu tham khảo hữu ích cho các bạn.

Chủ đề:
Lưu

Nội dung Text: Bài tập thực hành ngôn ngữ lập trình Java

  1. BÀI TẬP THỰC HÀNH NGÔN NGỮ LẬP TRÌNH JAVA Viết chương trình cho mỗi yêu cầu sau đây: 1. In họ và tên của chính mình ra màn hình.ok 2. Tính diện tích hình thang có đáy lớn bằng 7, đáy nhỏ bằng 5,  chiều cao bằng 6. S=(dl+dn)/2*dc.       Public class tdtht       {         public static void main(String[] args)         {            int dn=5;            int dl=7;            int dc=6;            float s=(dn+dl)/2*dc;            System.out.println(“dien tich hinh thang la: “ +s);          }       }  3. Tính diện tích của hình tròn bán kính bằng 3 public class tdthtron {   public static void main(String[] args)    {       float pi=3.1416;      int r=3;      float s=pi*r*r;      System.out.println(“dien tich hinh tron la:” +s);     } }  4. Tính diện tích của tam giác có ba cạnh lần lượt bằng 5,6,7.      x y 5. Tính giá trị của  x y  với x=1234, y=4321.        public class tinhfx        {             public static void main(String[] args)
  2.             {              int x=1234;            int y=4321;            float fx=(x+y)/(x­y);            System.out.println(“gia tri cua ham so la:” +fx);           }      } 6. Tính giá trị của  x x x  với x=3  Public class tinhham {     public static void main(String[] args)      {           int x=3;         float y=Math.sqrt(x+Math.sqrt(x+Math.sqrt(x)));          System.out.println(“ket qua cua ham so la:” +y);      }  } 3 7. Tính giá trị của  x log 2 ( x 4 2) 3 x 4  với x=6 //Co so e = 2,71828183   Public class tinhham2   {     public static void main(String[] args)     {       int x=6;     //log 2(x^4+2)=log e(x^4+2)/log e(2)       double z=Math.log(x*x*x*x+2)/Math.log(2)       double y=x*x*x+Math.log(x*x*x*x+2)­Math.cbrt(x­4);        8.  543200 giây là bao nhiêu giờ, phút, giây? Ví dụ 3662 giây là 1giờ  1 phút 2 giây.   Public class doi_gio  {    public static void main(String[] args) throws Exception    {     int x=543200;     //doi sang gio
  3.     int gio=x/3600;     Math.floor(gio);     Int phut=(x­gio*3600)/60;     Math.floor(phut);      Int giay=x­gio*3600­phut*60;      System.out.println(“ket qua la: “  +gio+”gio”+phut+”phut”+giay+”giay”);      } } BÀI TẬP THỰC HÀNH SỐ 2 Viết chương trình cho mỗi yêu cầu sau đây: 1. Tính diện tích hình thang có đáy lớn, đáy nhỏ, chiều cao nhập từ  bàn phím. Import java.io.*;  public class tinhdtht {  public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.print(“nhap day lon cua hinh thang: “); int dl=Integer.parseInt(in.readLine()); System.out.print(“nhap day nho cua hinh thang: “); int dn= Integer.parseInt(in.readLine()); System.out.print(“nhap chieu cao cua hinh thang: “); int cc=Integer.parseInt(in.readLine()); double s=(dl+dn)*cc/2; System.out.print(“Dien tich cua hinh thang la: “ +s); } } 2. Tính diện tích của hình tròn bán kính nhập từ bàn phím Import java.io.*; Public class tinhdthtron {  public static void main(String[] args) throws Exception {
  4.  BufferedReader in=new BufferedReader(new  InputStreamReader(System.in); System.out.print(“nhap ban kinh cua hinh tron R= ”); Int r=Integer.parseInt(in.readLine()); Float s=Math.PI*R*R; System.out.println(“dien tich hinh tron la:” +s); } } 3. Tính diện tích của tam giác có độ dài ba cạnh nhập từ bàn phím. Import java.io.*; Public class tinhdthtamgiac { public static void main(String[] args) { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println(“Nhap ba canh cua tam giac:”; System.out.print(“a=”); Int a=Integer.parseInt(in.readLine()); Sytem.out.print(“b=”); Int b=Integer.parseInt(in.readLine()); System.out.print(“c=”); Int c=Integer.parseInt(in.readLine()); Double p=(a+b+c)/2; Double s=Math.sqrt(p*(p­a)*(p­b)*(p­c)); System.out.print(“Dien tich hinh tam giac la: s=” +s); } }  x y 4. Tính giá trị của  x y  với x, y nhập từ bàn phím. Import java.io.*;     public class tinhfx {     public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println(“Nhap cac bien cua ham so”);
  5. System.out.print(“x=”); Int x=Integer.parseInt(in.readLine()); System.out.print(“y=”); Int y=Integer.parseInt(in.readLine()); If ((x­y)==0) System.out.print(“ket qua bi loi, phep chia cho 0”); Else { double fx=(x+y)/(x­y); System.out.print(“Ket qua cua ham so la:” +fx); } } }              int x=1234;            int y=4321;            float fx=(x+y)/(x­y);            System.out.println(“gia tri cua ham so la:” +fx);           }      } 5. Tính giá trị của  x x x  với x nhập từ bàn phím Import java.io.*; Public class tinhhamx {     public static void main(String[] args) throws Exception      {  BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.print(“Nhap x=” ); Int x=Integer.parseInt(in.readLine()); If (x
  6.  } 3 4 Tính giá trị của  x log 2 ( x 2) x 4  với x nhập từ bàn phím 3 6. 7.  Nhập một số nguyên dương n (giây). Viết chương trình tính   n  (giây) là bao nhiêu giờ, phút, giây? Ví dụ 3662 giây là 1giờ 1 phút  2 giây. BÀI TẬP THỰC HÀNH SỐ 3 (if) 1. Viết chương trình giải phương trình bậc nhất /* giai phuong trinh bac 1    ax+b=0   nghiem cua Phuong trinh   x=­b/a;*/ Import java.io.*; Public class gptbac1 { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println(“nhap he so cua phuong trinh”); System.out.print(“a= “); Int a=Integer.parseInt(in.readLine()); System.out.print(“b= “); Int b=Integer.parseInt(in.readLine()); Float x=(float)­b/a; System.out.print(“Nghiem cua Phuong trinh la x= “ +x); } } 2. Viết chương trình giải phương trình bậc hai với a,b,c nhập từ  bàn phím.
  7. /* Thuật giải: 1. Nhận các hệ số a, b, c từ người sử dụng 2. Nếu a = 0 không xét vì không còn là tam thức 3. Ngược lại nếu a # 0 delta = b*b ­ 4*a*c  ­ Nếu delta  0 thì phương trình có hai nghiệm phân biệt:  x1 = (­b ­ sqrt(delta))/(2*a) và  x1 = (­b + sqrt(delta))/(2*a) */ import java.io.*; public class gptbac2 { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println(“nhap cac he so cua phuong trinh”); System.out.print(“a= “); Int a=Integer.parseInt(in.readLine()); System.out.print(“b= “); Int b=Integer.parseInt(in.readLine()); System.out.print(“c= “); Int c=Integer.parseInt(in.readLine()); If (a=0) {System.out.println(“Day la phuong trinh bac nhat”);     If ((b=0)&&(c!=0))    {     System.out.print(“Neu a=0 va b=0 va c!=0 Phuong trinh vo  nghiem”);       If ((b=0)&&(c=0))        System.out.print(“Neu ca a,b,c=0 Phuong trinh vo so nghiem”);       }       else       {   float x=(float)(­b/a);
  8.   System.out.print(“neu a=0 va b!=0 Phuong trinh co mot nghiem x=  “+x);   } } else {  float delta= (float)(b*b­4*a*c); if (delta
  9. + Lay so nam chia module cho 4 neu =0 va chia module cho 100  neu khac 0 thi la nam nhuan.               Import java.io.*;        Public class timnamnhuan        {            public static void main(String[] args) throws Exception            {             BufferedReader in=new BufferedReader(new  InputStreamReader(System.in));             System.out.print(“nhap nam muon tim: “);             Int n=Integer.parseInt(in.readLine());             If ((n%400)=0)             System.out.println(“Day la nam nhuan co 366 ngay”);             Else                    If ((n%4)=0&&(n%100)!=0)                    System.out.print(“Day la nam nhuan co 366 ngay”);                    Else                       System.out.println(“Day khong phai la nam nhuan, co 365  ngay”);               }           }           } 4. Nhập vào 4 số nguyên. Đưa ra số lớn nhất, số bé nhất trong các  số đó. /*thuat toan ­ Nhap vao 4 so: a,b,c,d; ­ a
  10.      System.out.println(“Nhap 4 so nguyen:”);      System.out.print(“a= “);      Int a=Integer.parseInt(in.readLine());             System.out.print(“b= “);      Int b=Integer.parseInt(in.readLine());             System.out.print(“c= “);      Int c=Integer.parseInt(in.readLine());             System.out.print(“d= “);      Int d=Integer.parseInt(in.readLine());      If (a
  11.              if (b
  12. System.out.println(“Nhap toa do cua diem M1”); System.out.print(“X1=”); Int x1=Integer.parseInt(in.readLine()); System.ou.print(“Y1=”); Int y1=Integer.parseInt(in.readLine()); Sytem.out.print(“X2=”); Int x2=Integer.parseInt(in.readLine()); System.out.print(“Y2=”); Int y2=Integer.parseInt(in.readLine()); Float l=Math.sqrt((x2­x1)*(x2­x1)+(y2­y1)*(y2­y1)); If (l==0) System.out.println(“hai diem nay trung nhau”); Else System.out.println(“Khoang cach giua hai diem la L=” +l); } } 7. Nhập vào tọa độ một điểm. Kiểm tra điểm này có nằm trong  hình tròn tâm 0 bán kính bằng 1 hay không. Import java.io.*; Public class tinhkc2d  { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println(“Nhap toa do cua diem M”); Sytem.out.print(“X=”); Int x=Integer.parseInt(in.readLine()); System.ou.print(“Y=”); Int y=Integer.parseInt(in.readLine()); Float l=Math.sqrt(x*x+y*y); If (l
  13. } } 8. Nhập vào một kí tự, thông báo đó là chữ cái, chữ số hay là kí tự  khác.  System.out.println((int)'A'); System.out.println((char)65); 9.Nhap vao ho va ten.  xuat ra man hinh ho va ten khac nhau. Import java.io.*; Public class hoten { public static void main(String[] args) throws Exception { BufferedReader nhapvao=new BufferedReader(new  InputStreamReader(System.in)); String name=new String(); System.out.print(“Nhap vao ho va ten cua hoc sinh:”); Name=nhapvao.readLine(); String Ho=name.substring(0,7); System.out.println(“Ho cua hoc sinh nay la:”+ho); String ten=name.substring(8,12); System.out.println(“Ten cua hoc sinh nay la:”+ten); } } BÀI TẬP THỰC HÀNH SỐ 4 (switch+if) 1. Nhập vào hai số nguyên a, b và dấu của một phép toán: +, ­, *, /.  Tính giá trị khi thực hiện phép toán giữa a và b. import java.io.*; public class pheptoan { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println("Nhap hai so hang:");
  14. System.out.print("a="); int a=Integer.parseInt(in.readLine()); System.out.print("b="); int b=Integer.parseInt(in.readLine()); System.out.print("Nhap dau cua phep toan"); char dau='A'; dau= (char) System.in.read(); switch(dau) { case '+':     System.out.print("Ket qua cua phep toan la Y="+(a+b));    break; case '­':     System.out.print("Ket qua cua phep toan la Y="+(a­b));    break; case '*':     System.out.print("Ket qua cua phep toan la Y="+(a*b));    break; case '/':     System.out.print("Ket qua cua phep toan la Y="+(a/b));    break; default:    System.out.println("Ban nhap sai dau cua phep toan"); } } } 2. Cho biết ngày 1/3/2008 là thứ 7. Nhập vào một ngày trong tháng  3. Thông báo đó là ngày thứ mấy? Import java.io.*; Public class tinhngay { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.print(“Nhap vao mot ngay trong thang 3 date=”); Int date=Integer.parseInt(in.readLine());
  15. Int n=date%7; Switch (n) { case 0:      System.out.println(“Day la thu sau”);      break; case 1:      System.out.println(“Day la thu bay”);      break; case 2:      System.out.println(“Day la chu nhat”);      break; case 3:      System.out.println(“Day la thu hai”);      break; case 4:      System.out.println(“Day la thu ba”);      break; case 5:      System.out.println(“Day la thu tu”);      break; default:      System.out.println(“Day la thu nam”);      break; } } } 3. Nhập vào số lượng điện tiêu thụ trong tháng. Tính số tiền phải  trả biết rằng: 50 khw đầu tiên đơn giá 550đ, 50k tiếp theo đơn  giá 850đ, 100k tiếp theo đơn giá 1200đ, còn lại đơn giá 1500đ. Import java.io.*;
  16. Public class tinhdientt { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.print(“nhap luong dien tieu thu trong thang:”); int kwh=Integer.parseInt(in.readLine()); int n; if (kwh
  17. public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.println(“Nhap do dai ba canh cua tam giac:”); System.out.print(“a=”); Int a=Integer.parseInt(in.readLine()); System.out.ptint(“b=”); Int b=Integer.parseInt(in.readLine()); System.out.print(“c=”); Int c=Integer.parseInt(in.readLine()); If ((a*a==b*b+c*c)||(b*b=a*a+c*c)||(c*c=a*a+b*b)) System.out.println(“Day la tam giac vuong”);      Else if (a==b&&a==c)       System.out.println(“Day la tam giac deu”);      Else if ((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a))       System.out.println(“Day la tam giac can”);      Else       System.out.println(“Day la tam giac thuong”):      }      } BÀI TẬP THỰC HÀNH JAVA SỐ 5 (for) 1. In các số chẵn trong đoạn [1,20] trên một dòng, các số lẻ trong  đoạn đó lên dòng kế tiếp bằng hai cách. Public class inso { public static void main(String[] args) { int i;
  18. for (i=1;i
  19. for (i=a;i
  20. import java.io.*; public class tinhtong1n2 { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader(new  InputStreamReader(System.in)); System.out.print("Nhap n="); int n=Integer.parseInt(in.readLine()); int i,y; y=0; for (i=1;i
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2