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)

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

2024-04-26 13:51:37

#Bai2.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.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) {
        //B2
        System.out.print("Nhap max: ");
        int max = scan.nextInt();

        int a = 0, b = 1, next;

        System.out.print("Day Fibonacci chia het cho 2 va 3: ");
        while (a < max) {
            if (a % 2 == 0 && a % 3 == 0) {
                System.out.print(a + " ");
            }
            next = a + b;
            a = b;
            b = next;
        }
    }
}


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

2024-04-26 13:50:18

#Bai1.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.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) {
        //B1
        int n;
        n = Integer.parseInt(scan.nextLine());
        
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                if (j == i) {
                    System.out.print("* ");
                } else {
                    System.out.print(" ");
                }
                if (i == 1) {
                    for (int k = 1; k <= n; k++) {
                        System.out.print("*.");
                    }
                }
            }
            System.out.println();
        }
    }
}


Phuc
Phuc

2024-04-26 13:24:48


#Bai1.java


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



/**
 *
 * @author anhphuc
 */
public class Bai1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int n = 10 ; 
        while(n > 1 ){
            for(int i = n ; i > 1 ; i--){
            System.out.print(" ");
            }
            System.out.println("*");
            n--;
        }
        for(int i = 1 ; i <=10 ; i++){
            System.out.print("*");
        }
    }
    
}


#Bai2.java


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

import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Bai2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n  = sc.nextInt();
        if(n < 46368){
            System.out.println("Khong co so nao ");
        } 
        else{
            System.out.println( "46368"+ "");
        int s1 = 28657;
        int s2 = 46368;
            while(true ){
                int tam = s1 ; 
                s1 = s2 ;
                s2 = tam + s2 ;
                if(s2 > n)break;
                else{
                    if(s2 % 6 == 0 ){
                        System.out.println(s2 + " ");
                    }
                }
            }
        }
    }
}



#Bai3.java


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

import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Bai3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap chuoi thu nhat : ");
        String s1 = sc.nextLine();
        System.out.println("Nhap chuoi thu hai : ");
        String s2 = sc.nextLine();
        String kq = "";
        int min = Math.min(s1.length(), s2.length());
        int max = Math.max(s1.length(), s2.length());
        for(int i = 0 ; i < min;i++){
            kq += s1.charAt(i);
            kq += s2.charAt(i);
        }
        if(max != min){
            for(int i = min ;i < max ; i++){
                if(s1.length() > s2.length())kq += s1.charAt(i);
                else kq += s2.charAt(i);
            }
        }
        System.out.println("Chuoi ket qua la : " + kq);
    }
    
}


#Bai4.java


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

import java.math.BigInteger;
import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Bai4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap so nguyen lon thu nhat : ");
        BigInteger a = new BigInteger(sc.nextLine());
        System.out.println("Nhap so nguyen lon thu hai : ");
        BigInteger b = new BigInteger(sc.nextLine());
        System.out.println("Tong hai so nguyen lon la :" + a.add(b));
    }
    
}


#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.mycompany.lesson08;

import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Cat implements IInput{
    String name;
    int age ; 

    @Override
    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap thong tin cho meo ");
        System.out.println("Nhap ten : ");
        this.name = sc.nextLine();
        System.out.println("Nhap tuoi : ");
        this.age = Integer.parseInt(sc.nextLine());
    }
    @Override
    public String toString(){
        return "Ten meo : " + name + "\n" + 
                "Tuoi : " + age + "\n"
                ;
    }
    
    public void display(){
        System.out.println(this);
    }
    
}


#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.mycompany.lesson08;

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


#Main.java


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

import java.util.ArrayList;
import java.util.Scanner;
import javax.xml.transform.OutputKeys;

/**
 *
 * @author anhphuc
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<IInput> list = new ArrayList<>();
        System.out.println("Nhap so phan tu cua mang : ");
        int n = Integer.parseInt(sc.nextLine());
        for(int i = 0 ; i < n ; i++){
        showMenu();
        int chon = Integer.parseInt(sc.nextLine());
        switch(chon){
            case 1 :
                Student st = new Student();
                st.input();
                list.add(st);
                break;
            case 2 :
                Vehicle v = new Vehicle();
                v.input();
                list.add(v);
                break;
            case 3 : 
                Tiger t = new Tiger();
                t.input();
                list.add(t);
                break;
            case 4 : 
                Cat c = new Cat();
                c.input();
                list.add(c);
                break;
            case 5 : 
                System.out.println("Thoat thanh cong !");
                System.exit(0);
                break;
             default:
                 System.out.println("Yeu cau khong hop le vui lonh nhap lai ");
                 break;
        }
            System.out.println("----------------------------------------");
            System.out.println("Cac phan tu cua mang la : ");
    }
        for(IInput i : list){
                System.out.println(i);
            }
    }
    public static void showMenu(){
            System.out.println("Chon them doi tuong ");
            System.out.println("1.Student");
            System.out.println("2.Vehicle");
            System.out.println("3.Tiger");
            System.out.println("4.Cat");
            System.out.println("5.Thoat");
    }
}


#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.mycompany.lesson08;

import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Student implements IInput{
    String name;
    int age ; 

    @Override
    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap thong tin cho hoc sinh ");
        System.out.println("Nhap ten : ");
        this.name = sc.nextLine();
        System.out.println("Nhap tuoi : ");
        this.age = Integer.parseInt(sc.nextLine());
    }
    @Override
    public String toString(){
        return "Ten hoc sinh : " + name + "\n" + 
                "Tuoi hoc sinh : " + age + "\n"
                ;
    }
    
    public void display(){
        System.out.println(this);
    }
    
}


#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.mycompany.lesson08;

import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Tiger implements  IInput{
    String name;
    int age ; 

    @Override
    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap thong tin cho ho  ");
        System.out.println("Nhap ten : ");
        this.name = sc.nextLine();
        System.out.println("Nhap tuoi : ");
        this.age = Integer.parseInt(sc.nextLine());
    }
    @Override
    public String toString(){
        return "Ten ho : " + name + "\n" + 
                "Tuoi : " + age + "\n"
                ;
    }
    
    public void display(){
        System.out.println(this);
    }
    
}


#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.mycompany.lesson08;

import java.util.Scanner;

/**
 *
 * @author anhphuc
 */
public class Vehicle implements IInput{
    String name;
    int age ; 

    @Override
    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Nhap thong tin cho phuong tien  ");
        System.out.println("Nhap ten : ");
        this.name = sc.nextLine();
        System.out.println("Nhap tuoi : ");
        this.age = Integer.parseInt(sc.nextLine());
    }
    @Override
    public String toString(){
        return "Ten phuong tien  : " + name + "\n" + 
                "Tuoi : " + age + "\n"
                ;
    }
    
    public void display(){
        System.out.println(this);
    }
    
}



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

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