Bài tập & code Java (Sưu tầm và update thường xuyên)

Class Product with properties are ID – int, Name – String, quantity – int, Price – float
Methods are:
- void createProduct() : enter the information of product from the keyboard
- void displayProduct() : display product’s information as format:

id name quantity price cost

- float getCost() : return the cost for a product (price * quantity)
- void sortProduct(Product p[]) : sort product in array p base on cost (price * quantity) in acsending
- Product findProduct(int quantity, Product p[]) : return the first product that has the quantity is greater than the number you have entered.
- In main:
+ Create an array product with n elements, n is get from the keyboard
+ Enter the information for all elements of array p
+ Display all products in ascending of cost
+ Enter a whole number from the keyboard, display the information of the first product that has the quantity is greater than this number.

PHP:
import java.util.Scanner;
public class Product {
    int pID;
    String pName;
    int pQuanty;
    float pPrice;
  
    // Nhap thong tin cho san pham
    void createProduct(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Id: ");
        pID = sc.nextInt();
        System.out.println("Name: ");
        pName = sc.next();
        System.out.println("Quantity: ");
        pQuanty = sc.nextInt();
        System.out.println("Price: ");
        pPrice = sc.nextFloat();
    }
  
    // Hien thi san pham
    void displayProduct(){
        System.out.println(pID + "\t" + pName + "\t" + pQuanty + "\t" + pPrice + "\t" + getCost());
    }
  
    // Tra ve chi phi cua san pham
    float getCost(){
        return pQuanty * pPrice;
    }
  
    // Sap xep san pham theo chi phi
    void sortProduct(Product p[]){
        for(int i = 0; i < p.length - 1; i++){
            for(int j = i + 1; j < p.length; j++){
                if(p[j].getCost() < p[i].getCost()){
                    Product temp = p[i];
                    p[i] = p[j];
                    p[j] = temp;
                }
            }
        }
    }
  
    // Tra ve san pham dau tien co so luong lon hon so nhap vao
    Product findProduct(int quanty, Product p[]){
        for(int i = 0; i < p.length; i++){
            if(p[i].pQuanty > quanty){
                return p[i];
            }
        }
        return null;
    }
  
    public static void main(String[] args) {
        Product pro = new Product();
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the number elements of the array: ");
        int n = sc.nextInt();
        Product[] p = new Product[n];
      
        // Nhap thong tin cho cac phan tu mang
        for(int i = 0; i < n; i++){
            p[i] = new Product();
            System.out.println("Please enter the information of the product " + (i + 1) + ":");
            p[i].createProduct();
        }
      
        // Sap xep tat ca san pham theo thu tu tang dan cua chi phi
        pro.sortProduct(p);
      
        // Hien thi tat ca san pham theo thu tu tang dan cua chi phi
        System.out.println("Id\tName\tQuanty\tPrice\tCost");
        for(int i = 0; i < n; i++){
            p[i].displayProduct();
        }
      
        // Hien thi thong tin san pham dau tien co so luong lon hon so nhap vao
        System.out.println("Please enter any number to find quantity: ");
        int quanty = sc.nextInt();
        System.out.println("The first product have quantity more than " + quanty + " is:");
        pro.findProduct(quanty, p).displayProduct();
    }
}
 
Create a class Average to allow to enter 5 float from the keyboard. Print the average value of them with 3 digits in decimal part.

PHP:
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
    float a,b,c,d,e,f;
        Scanner sc=new Scanner(System.in);
    System.out.println(" Please enter 5 float from the keyboard : ");
    a=sc.nextFloat();
    b=sc.nextFloat();
    c=sc.nextFloat();
    d=sc.nextFloat();
    e=sc.nextFloat();
    f=((a+b+c+d+e)/3);
    System.out.printf("Print %.3f",f);
}
}




Cách 2: dùng mảng ( array ):

PHP:
import java.util.Scanner;

public class Average {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float[] inputNumbers = new float[5];
        float sum = 0;
        for(int i = 0; i < 5; i++){
            System.out.println("Number " + (i + 1) + " is: ");
            inputNumbers[i] = sc.nextFloat();
            sum += inputNumbers[i];
        }
        System.out.printf("The average of 5 number you have entered is: %.3f" , sum/5);
    }
}
 
Viết chương trình tìm số nhỏ nhất trong số các số được nhập vào từ bàn phím. Cho biết số nguyên đầu tiên nhập vào sẽ là số các con số được nhập.


PHP:
import java.util.Scanner;

public class Abcd {
    
        public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the amount of input numbers: ");
        int n = sc.nextInt();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            System.out.println("Please enter the number " + (i + 1) + " : ");
            arr[i] = sc.nextInt();
        }
        int min = arr[0];
        for (int i = 0; i < n; i++) {
            if(arr[i] < min){
                min = arr[i];
            }
        }
        System.out.println("The minimum of the input numbers is: " + min);

        }
}
 
baijava.jpg



PHP:
public class Abcd {

public static void main(String[] args) {
        float a = 0, b = 0;
        for (float i = 3; i < 1000000; i += 4) {
            a += 4/i;
        }
       
        for (float i = 5; i < 1000000; i += 4) {
            b += 4/i;
        }
       
        System.out.printf("The value of Pi: %.10f", (4 + (b - a)));
    }
}


Hơi thủ công, ai có cách nào hay hơn ko??
 
Viết chương trình nhập 2 số và hiển thị kết quả của các phép toán dưới đây(sử dụng các toán tử bit)
Số 1 & Số 2:
Số 1 | Số 2:
~ Số 1: Số 2:
(~ Số 1) & (~ Số 2):
(~ Số 1) | (~ Số 2):
Số 1 >> Số 2:
Số 1 << Số 2:
Số 2 >> Số 1:
Số 2 << Số 1:

PHP:
import java.util.Scanner;
public class Operator {
/*Số 1 & Số 2:
Số 1 | Số 2:
~ Số 1: Số 2:
(~ Số 1) & (~ Số 2):
(~ Số 1) | (~ Số 2):
Số 1 >> Số 2:
Số 1 << Số 2:
Số 2 >> Số 1:
Số 2 << Số 1:*/
    public static void main(String[] args) {
        int a,b;
        System.out.println(" Nhap 2 so :");
        Scanner input=new Scanner(System.in);
        System.out.println("so 1: ");
        a=input.nextInt();
        System.out.println("so 2: ");
        b=input.nextInt();
        System.out.printf("\n %d & %d : %d",a,b,a&b);
        System.out.printf("\n %d | %d : %d",a,b,a|b);
        System.out.printf("\n ~%d : %d ",a,~a);
        System.out.printf("\n ~%d : %d ",b,~b);
        System.out.printf("\n (~ %d) & (~ %d)",a,b,(~a&~b));
        System.out.printf("\n (~ %d) | (~ %d)",a,b,(~a|~b));
        System.out.printf("\nSo %d >> So %d : %d ",a,b,a>>b);
        System.out.printf("\nSo %d << So %d : %d ",a,b,a<<b);
        System.out.printf("\nSo %d >> So %d : %d",b,a,b>>a);
        System.out.printf("\nSo %d << So %d : %d\n",b,a,b<<a);
 }
}



@bạn gì hỏi về login : hỏi thế thì vô cùng quá bạn ơi.
 
à tớ muốn tìm 1 ví dụ về quyền login trong java ây ( log vào thì có quyền admin, hoặc quyền mem.. hoặc..)
 
hi các pro! Có ai biết lam game kim cương bằng ngôn ngữ Java ko?em đang phai làm bài tập lớn về đề tài này. Mà em chưa biết làm thế nào. ai pro giúp em với
 
Các bác có ai có code game drop four ko?
Bọn mình phải làm bài này
mà chả biết code ethees nào
 
Login phân quyền admin nằm trong JAVA GUI APPICATION rồi mà! đây là Java Console mà
 
Ồ, topic này hữu dụng đấy! Mong bạn chủ topic tiếp tục phát huy nhé, nếu mình thấy hay mình sẽ stick lên chú ý ^^
 
Bạn có thể giúp mình bài tập này với được không?
1.Viết chương trình tính tổng các số nguyên tố trong đoạn (a,b). Với a<b
2.Viết chương trình tính tổng các số nguyên tố <= 100

Mình mới học mà thầy ra những câu thế này không làm nổi luôn.
 
Đây là mấy bài đơn giản, chỉ có học cách viết chứ đã phải thuật toán cao siêu gì đâu!

import java.io.*;
/**
*
* @author LW
*/
public class Main {
public static void main(String[] args) throws IOException {
int a, b;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("a :");
a = Integer.parseInt(in.readLine());
System.out.println("b :");
b = Integer.parseInt(in.readLine());

System.out.println("cau 1: " + sum1(a, b));
System.out.println("cau 2: " + sum2());
}

public static boolean isPrimeNumber(int num){
for(int i = 2; i < num /2; i++){
if(i % 2 == 0)
return false;
}
return true;
}

// in [a, b]
public static int sum1(int a, int b){
int s = 0;
for(int i = a; i <= b; i++)
if(isPrimeNumber(i))
s += i;
return s;
}
// in [0, 100]
public static int sum2(){
int s = 0;
for(int i = 0; i <= 100; i++)
if(isPrimeNumber(i))
s += i;
return s;
}
}

//đoạn [a, b] hay khoảng (a, b)?
 
à, hàm kiểm tra nguyên tố chưa kiểm tra điều kiện <=1, tự thêm nhé
 
Mình cũng mới học xong Core java, bạn nào có bài tập khó cứ pm mình giải thử ^^
 
Đây là chương trình được viết ở dạng socket server mô phỏng quản lý điểm sinh viên, máy client nhập điểm hoặt thên sửa xóa chuyển lên server sử lý và được lưu vào csdl sql server 2008 và trả lai kêt quả cho client....
 
Back
Top