By GokiSoft.com| 19:23 26/04/2024|
Java Basic

Bài Test - Ôn tập tổng quát Java

Bài 1: Vẽ hình sau -> Nhập số nguyên N từ bàn phím -> Hiển thị hình sau

N = 10

                                        *

                                    *

                                *

                            *

                        *

                    *

                *

            *

        *

    *   *.  *. *. *. *. *. *.  *.  *.  *

Bài 2:

Cho dãy Fibonacci

F(0) = 1

F(1) = 1

F(n) = F(n-1) + F(n-2)

Nhập số max bất kỳ. In tất cả các số fibonacci <= max và chia hết cho 2 và 3

Bài 3:

Nhập vào 2 chuối s1 và s2 -> Thực hiện nối 2 chuỗi s1 và s2 thành chuỗi s theo yêu cầu sau

s1 = 12345

s2 = 67890

s = 1627384950

Bài 4:

Nhập 2 số nguyên lớn. Giá trị có thể là 353498573459734593475934573495734957349573453459

Yêu cầu cộng 2 số nguyên và hiển thị kết quả ra màn hình.

Bài 5:

Tao lớp giao diện IInput gồm 1 phương thức input()

Tạo lớp đối tượng Student, Vehicle, Tiger, Cat. Gồm các thuộc tính tên và age.

Yêu cầu tạo mảng quản lý N đối tượng Student, Vehicle, Tiger, Cat trong class Main. Thực hiện hỏi người dùng nhập N từ bàn phím và tạo N đối tượng trên, Mỗi lần lặp -> Hỏi người dùng tạo Student, Vehicle, Tiger, Cat và thêm vào mảng.

Viết ham

public static void input(List<IInput> list) trong class Main. Thực hiện nhập thông tin cho mảng trên.

Yêu cầu làm đúng theo thứ tự đề bài.

Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đỗ Khắc Thiện
Đỗ Khắc Thiện

2024-04-26 14:00:34

        Scanner s = new Scanner(System.in);

        n = Integer.parseInt(s.nextLine());

        int m = 1,n;

        while (n > 1) {

            for (int j = n; j > 1; j--) {

                System.out.print(" ");


            }

            m++;

            n--;

            System.out.println("*");

            if (n == 1) {

                for (int i = 0; i < m; i++) {

                    System.out.print("*");

                }

            }

        }


NGUYEN MINH VU
NGUYEN MINH VU

2024-04-26 13:56:07

b1: https://onlinegdb.com/lk7Rjpkuz
b2: https://onlinegdb.com/f0ikLZwd3
b3: https://onlinegdb.com/lsTiNLSyA
b4: https://onlinegdb.com/MKM9BSGKd



Đỗ Quốc Dũng
Đỗ Quốc Dũng

2024-04-26 13:55:49


Bai 3
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package TEST_ON_TAP_JAVA;
import java.util.Scanner;
/**
 *
 * @author ASUS
 */
public class Bai3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhap chuoi s1: ");
        String s1 =  scanner.nextLine();
        System.out.println("Nhap chuoi s2: ");
        String s2 = scanner.nextLine();
        String s = "";
        int length = Math.max(s1.length(), s2.length());
        for(int i =0;i<length;i++){
            if (i<s1.length()) {
                s += s1.charAt(i);
            }
            if (i<s2.length()) {
                s += s2.charAt(i);
            }
        }
        System.out.println("Chuoi sau khi noi: "+s);
        scanner.close();
    }
}




Đỗ Quốc Dũng
Đỗ Quốc Dũng

2024-04-26 13:55:28


Bai 1
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package TEST_ON_TAP_JAVA;
import java.util.Scanner;
/**
 *
 * @author ASUS
 */
public class Bai1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhap so nguyen N: ");
        int N = scanner.nextInt();
        for (int i = N; i >= 1; i--) {
            if(i == 1){
                for(int j= N; j >= 1; j--){
                    System.out.print("*  ");
                }
            }
            for (int j = 1; j <= N; j++) {
                if (j == i) {
                    System.out.print("*");
                } else {
                    System.out.print("  "); 
                }
                
            }
            System.out.println(); 
        }
          
          
    }
}




Đỗ Quốc Dũng
Đỗ Quốc Dũng

2024-04-26 13:54:44


Bai 2
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package TEST_ON_TAP_JAVA;
import java.util.Scanner;
/**
 *
 * @author ASUS
 */
public class Bai2 {
    // Hàm tạo dãy Fibonacci
    public static void dayFiBoNaCci(int max) {
        int fib1 = 1;
        int fib2 = 1;
        int fib = 0;

        System.out.println("Cac so Fibonacci <= " + max + " va chia het cho 2 va 3 la:");
        while (fib <= max) {
            if (fib % 2 == 0 && fib % 3 == 0) {
                System.out.print(fib + " ");
            }
            fib = fib1 + fib2;
            fib1 = fib2;
            fib2 = fib;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Nhap so max: ");
        int max = scanner.nextInt();

        dayFiBoNaCci(max);
    }
}




Nguyễn Hùng
Nguyễn Hùng

2024-04-26 13:53:57


#IInput.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

/**
 *
 * @author ADMIN
 */
public interface IInput {
    public void input();
}


#Cat.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Cat implements  IInput{
    String ten;
    int tuoi;
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten");
        ten = scan.nextLine();
        System.out.println("Nhap tuoi");
        tuoi = Integer.parseInt(scan.nextLine());
    }
    
    public String display() {
        return toString();
    }

    @Override
    public String toString() {
        return "Ten: " + ten + ", Tuoi: " + tuoi;
    }
}


#Student.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Student implements IInput{
    String ten;
    int tuoi;

    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten");
        ten = scan.nextLine();
        System.out.println("Nhap tuoi");
        tuoi = Integer.parseInt(scan.nextLine());
    }
    
    public String display() {
        return toString();
    }

    @Override
    public String toString() {
        return "Ten: " + ten + ", Tuoi: " + tuoi;
    }
}


#Tiger.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Tiger implements IInput {
    String ten;
    int tuoi;
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten");
        ten = scan.nextLine();
        System.out.println("Nhap tuoi");
        tuoi = Integer.parseInt(scan.nextLine());
    }
    
    public String display() {
        return toString();
    }

    @Override
    public String toString() {
        return "Ten: " + ten + ", Tuoi: " + tuoi;
    }
}


#Vehicle.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Vehicle implements IInput{
    String ten;
    int tuoi;
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten");
        ten = scan.nextLine();
        System.out.println("Nhap tuoi");
        tuoi = Integer.parseInt(scan.nextLine());
    }
    
    public String display() {
        return toString();
    }

    @Override
    public String toString() {
        return "Ten: " + ten + ", Tuoi: " + tuoi;
    }
}



Nguyễn Hùng
Nguyễn Hùng

2024-04-26 13:53:12

#Bai5.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Project {

    public static Scanner scan = new Scanner(System.in);

    public static void menu() {
        System.out.println("""
                           1. Student
                           2. Vehicle
                           3. Tiger
                           4. Cat
                           """);
    }

    public static void input(List<IInput> list) {
    }

    public static void main(String[] args) {
        //B5
        int n;
        n = Integer.parseInt(scan.nextLine());
        ArrayList<IInput> list = new ArrayList<IInput>();

        for (int i = 0; i < n; i++) {
            int choose;
            menu();
            choose = Integer.parseInt(scan.nextLine());

            switch (choose) {
                case 1:
                    Student student = new Student();
                    student.input();
                    list.add(student);
                    break;
                case 2:
                    Vehicle vehicle = new Vehicle();
                    vehicle.input();
                    list.add(vehicle);
                    break;
                case 3:
                    Tiger tiger = new Tiger();
                    tiger.input();
                    list.add(tiger);
                    break;
                case 4:
                    Cat cat = new Cat();
                    cat.input();
                    list.add(cat);
                    break;
            }
        }

        for (IInput obj : list) {
            System.out.println(obj);
        }
    }
}


Nguyễn Hùng
Nguyễn Hùng

2024-04-26 13:52:44

#Bai4.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Project {
    public static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        //B4
        System.out.print("Nhap so mot: ");
        String str1 = scan.next();
        BigInteger num1 = new BigInteger(str1);

        System.out.print("Nhap so hai: ");
        String str2 = scan.next();
        BigInteger num2 = new BigInteger(str2);

        // Cộng hai số nguyên lớn
        BigInteger sum = num1.add(num2);

        // Hiển thị kết quả
        System.out.println(sum);
    }
}


Nguyễn Hùng
Nguyễn Hùng

2024-04-26 13:52:04

#Bai3.java
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.hung.project;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Project {
    public static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        //B3
        System.out.println("Nhap s1:");
        String s1 = scan.nextLine();
        System.out.println("Nhap s2:");
        String s2 = scan.nextLine();
        String s = "";

        int i = 0, j = 0;
        while (i < s1.length() || j < s2.length()) {
            if (i < s1.length()) {
                s += s1.charAt(i);
                i++;
            }
            if (j < s2.length()) {
                s += s2.charAt(j);
                j++;
            }
        }

        System.out.println(s);
    }
}


NGUYEN MINH VU
NGUYEN MINH VU

2024-04-26 13:51:43

b1: https://onlinegdb.com/lk7Rjpkuz
b2: https://onlinegdb.com/f0ikLZwd3
b3: https://onlinegdb.com/lsTiNLSyA
b4: https://onlinegdb.com/JDGozsBWZ


Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó