k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

7 bài tập về java Have Full

5 posters

Go down

Cool 7 bài tập về java Have Full

Bài gửi by Admin Sat Sep 15, 2012 8:23 pm

Bài 1 : Viết chương trình hiển thị ra màn hình câu : " Hello Java"
Code:

public class Lesson1 {
   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      System.out.println("Hello Java!");

   }
}

Bài 2 : Viết chương trình nhập vào 1 chuỗi ký tự. Đổi thành chữ Hoa và in ra màn hình.
Code:
import java.util.Scanner;
public class Lesson2 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      
      Scanner input = new Scanner(System.in);
      System.out.print("Nhập vào 1 chuỗi ký tự>>");
      String s = input.nextLine();
      System.out.println("In hoa là: "+ s.toUpperCase());

   }

}
Bài 3 : Viết chương trình nhập vào 1 số nguyên. Kiểm tra xem số đó có phải là số nguyên
tố hay không và thông báo ra màn hình.
Code:
import java.util.Scanner;
public class Lesson3 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner input = new Scanner(System.in);
      System.out.print("Nhập vào số nguyên>>");
      int n = input.nextInt();
      boolean a = true;
      if ((n==1) ||(n==0)) a = false;
      for(int i =2; i<n; i++)
      {
         if(n%i ==0) a = false;
      }
      if(a==true) System.out.println(n + " là số nguyên tố");
      else System.out.println(n + " không là số nguyên tố");
   }

}
Bài 4 : Viết chương trình tính tổng của dãy số từ 1 đến n (Với n được nhập từ bàn phím).
Code:
import java.util.Scanner;
public class Lesson4 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner input = new Scanner(System.in);
      System.out.print("Nhập vào số nguyên n:= ");
      int n = input.nextInt();
      int s =0;
      for(int i =1; i<=n; i++)
      {
         s=s+i;
      }
      System.out.println("Tổng từ 1 đến " +n + " là: "+s);
   }

}
Bài 5 : Nhập vào 1 dãy số thực, tính tổng của các số dương trong dãy đó.
Cách 1:

Code:
import java.util.Scanner;
public class Lesson5 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner input = new Scanner(System.in);
      System.out.println("Nhập vào 1 dãy số thực gồm n số");
      System.out.print("n:= ");
      int n = input.nextInt();
      float a[] = new float[100];
      float s =0;
      for(int i =0; i<n; i++)
      {
         System.out.print("a["+ (i+1) +"]:= ");
         a[i] = input.nextFloat();
         if(a[i]>0) s=s+a[i];
      }
      System.out.println("Tổng số thực dương là: "+s);
   }

}
Cách 2:
Code:
import java.util.Scanner;
public class Lesson5v2 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner input = new Scanner(System.in);
      System.out.println("Chương trình tính tổng dãy số thực dương nhập từ bàn phím");
      System.out.println("Lưu ý nhập 0 để dừng!");
      float s =0;
      float n =0;
      do
      {
         System.out.print("Nhập số: ");
         n = input.nextFloat();
         if(n>0) s = s+n;
      }while(n!=0);
      System.out.println("Tổng số thực dương là: "+s);
   }

}

Bài 6 : Đọc 1 file text và hiển thị ra màn hình nội dung file đó.
Cách 1:
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class Lesson6 {

   /**
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
      // TODO Auto-generated method stub
      try {
         FileInputStream fin = new FileInputStream("D:/input.txt");
         Scanner input = new Scanner(fin);
         while(input.hasNext())
         {
            String s = input.nextLine();
            System.out.println(s);
         }
         input.close();
         fin.close();
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         System.out.println("Không tìm thấy file dữ liệu!");
      }
   }
}
Cách 2:
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Lesson6v2 {

   /**
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
      Scanner input1 = new Scanner(System.in);
      System.out.print("Nhập đường dẫn pacth: ");
      String s = input1.nextLine();
      try {
         FileInputStream fin = new FileInputStream(s);
         Scanner input = new Scanner(fin);
         while(input.hasNext())
         {
            String s2 = input.nextLine();
            System.out.println(s2);
         }
         input.close();
         fin.close();
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         System.out.println("Không tìm thấy file dữ liệu!");
      }
      // TODO Auto-generated method stub

   }

}
Bài 7 : Viết chương trình tạo ra 3 thread chạy song song với mục đích là in ra dãy số từ 1
đến 100. Quan sát kết quả và nhận xét.
Code:

public class Lesson7  extends Thread{

   String name;
   public Lesson7(String ten)
   {
      name = ten;
      System.out.println("Thread " + name + " được khởi tạo!");
   }
   public void run()
   {
      for(int i =1; i<=100; i++)
      {
         System.out.println(name + "---" + i);
      }
   }
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Lesson7 t1 = new Lesson7("Thread1");
      Lesson7 t2 = new Lesson7("Thread2");
      Lesson7 t3 = new Lesson7("Thread3");
      t1.start(); t2.start(); t3.start();
      System.out.println("Độ ưu tiên của luồng Thread");
      System.out.println("Ưu tiên của Thread1: " + t1.getPriority());
      System.out.println("Ưu tiên của Thread2: " + t2.getPriority());
      System.out.println("Ưu tiên của Thread3: " + t3.getPriority());
      //System.out.println("Đặt độ ưu tiên của Thread");
      //t1.setPriority(Lesson7.NORM_PRIORITY);
      //t2.setPriority(Lesson7.MIN_PRIORITY);
      //t3.setPriority(Lesson7.MAX_PRIORITY);
      //t1.start(); t2.start(); t3.start();

   }

}

Chúc anh em làm bài tốt
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by shippou777 Sat Sep 15, 2012 8:46 pm

Thank phát.
Bác admin có bài gì ống ống thầy làm hồi sáng post lên nha (bài phương trình bậc 1).
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by Admin Sat Sep 15, 2012 9:40 pm

Không, có bài tự làm nhưng không chạy, bài của Thầy Trọng giữ rồi chưa thấy Post. Nếu muôn xem bài tôi thì nó đây. Đã fix lỗi
Class Server:
Code:
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;


public class Server extends Thread{

   /**
    * @param args
    */
   int a,b;
   int kq;
   float x;
   PipedInputStream inputS;
   PipedOutputStream outputS;
   Scanner input;
   PrintWriter write;
   Server()
   {}
   Server(PipedInputStream Sinput, PipedOutputStream Soutput)
   {
      this.inputS = Sinput;
      this.outputS = Soutput;
      System.out.println("Server được khởi động... ");
   }
   public void send()
   {
      write = new PrintWriter(this.outputS);
      write.println(kq+" "+x);
      write.flush();      
   }
   public void recived()
   {
      input = new Scanner(inputS);
      a = input.nextInt();
      b = input.nextInt();
      
   }
   public void prosing()
   {
      if (a==0)
      {
         if (b==0) { kq = -1; x=0; }
         else { kq = 0; x=0; }
      }
      else { kq = 1; x = -b/a; }
   }
   public void run()
   {
      
      while(true)
      {
         recived();
         prosing();
         send();
      }
   }

}

Class Client
Code:


import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;


public class Client extends Thread{
   int a,b;
   int kq;
   float x;
   PipedInputStream inputC;
   PipedOutputStream outputC;
   Scanner input;
   PrintWriter write;
   /**
    * @param args
    */
   public Client()
   {}
   public Client(PipedInputStream Cinput, PipedOutputStream Coutput)
   {
      this.inputC = Cinput;
      this.outputC = Coutput;
      System.out.println("Client được khởi động... ");

   }
   public void inputab()
   {
      input = new Scanner(System.in);
      write = new PrintWriter(System.out);
      write.println("Chương trình giải phương trình bậc nhất!");
      write.print("a:= ");
      write.flush();
      a = input.nextInt();
      write.print("b:= ");
      write.flush();
      b = input.nextInt();
   }
   public void send()
   {
      write = new PrintWriter(this.outputC);
      write.println(a+" "+b);
      write.flush();
   }
   public void look()
   {
      if(kq==-1) System.out.println("Phương trình vô nghiệm");
      if(kq==0) System.out.println("Phương trình có vô số nghiệm!");
      if(kq==1) System.out.println("Phương có nghiệm x:= "+x);
   }
   public void recived()
   {
      input = new Scanner(inputC);
      kq = input.nextInt();
      x = input.nextFloat();
   }
   public void run()
   {

      while(true)
       {
          inputab();
          send();
          recived();
          look();
       }
      
   }
}








Class Main
Code:
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;


public class Main {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      PipedInputStream inputS = new PipedInputStream();
      PipedOutputStream outputS = new PipedOutputStream();
      PipedInputStream inputC =new PipedInputStream();
      PipedOutputStream outputC = new PipedOutputStream();
      try {
         inputS.connect(outputC);
         outputS.connect(inputC);
         Server S = new Server(inputS, outputS);
         Client C = new Client(inputC, outputC);      
         C.start();
         S.start();
         
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

}

Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by pht0991 Sun Sep 16, 2012 11:29 am

oh hay quá
pht0991
pht0991

Posts : 222
Thanked : 3
Gia Nhập 24/02/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by pht0991 Sun Sep 16, 2012 4:43 pm

nhạc nghe ớn quá
pht0991
pht0991

Posts : 222
Thanked : 3
Gia Nhập 24/02/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by pklove1910 Sun Sep 16, 2012 7:44 pm

thằng Thái spam vãi thật. Shocked
Thank bác Tâm!
pklove1910
pklove1910

Posts : 207
Thanked : 3
Gia Nhập 11/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by pht0991 Sun Sep 30, 2012 2:10 pm

pklove1910 đã viết:thằng Thái spam vãi thật. 7 bài tập về java Have Full 554170
Thank bác Tâm!

spam cha may
pht0991
pht0991

Posts : 222
Thanked : 3
Gia Nhập 24/02/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by shippou777 Tue Oct 02, 2012 10:07 am

pht0991 đã viết:
pklove1910 đã viết:thằng Thái spam vãi thật. 7 bài tập về java Have Full 554170
Thank bác Tâm!

spam cha may
Thằng Thái spam quá có người bức xúc rồi 7 bài tập về java Have Full 3884376900
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by catretrang Tue Oct 02, 2012 6:33 pm

thank bác Tâm nha..để vào lớp hôn cái,động viên Very Happy
catretrang
catretrang

Posts : 76
Thanked : 5
Gia Nhập 19/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by Admin Wed Oct 03, 2012 7:28 am

oh my god, tha cho em tongue bounce
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by shippou777 Sun Oct 07, 2012 5:08 pm

Bác Tâm có mấy bài giải thực hành socket share đi.
Môn này ngu quá, éo biết gì.
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by Admin Sun Oct 07, 2012 7:45 pm

Ok, để em up cho mấy bác. Idea
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: 7 bài tập về java Have Full

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết