By GokiSoft.com| 13:40 06/04/2020|
Java Basic

Share Code >> Java Basic- Overview - Tổng hợp bài tập ôn luyện java basic - Kiểm tra 60 phút

Java Basic- Overview - Tổng hợp bài tập ôn luyện java basic - Kiểm tra 60 phút


Bài 1: Tính tổng các số nguyên từ 0->N (N được nhập từ bàn phím)


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Bai1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap N: ");
        int n = scan.nextInt();
        int total = 0;
        
        for (int i = 0; i < n; i++) {
            total += i;
        }
        
        System.out.println("Tong: " + total);
    }
}

Bài 2: Nhập vào mảng số nguyên gồm N phần tử (N được nhập từ bàn phím) -> In ra các số chia hết cho 2



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Bai2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap N: ");
        int n = input.nextInt();
        
        int[] t = new int[n];
        
        for (int i = 0; i < n; i++) {
            System.out.format("\nNhap t[%d] = ");
            t[i] = input.nextInt();
        }
        
        for (int i = 0; i < n; i++) {
            if(t[i] % 2 == 0) {
                System.out.println(t[i]);
            }
        }
    }
}

Bài 3 : Tạo lớp đối tượng Book gồm các thuộc tính sau

- tên sách, tác giả, nhà sản xuất, năm sản xuất

-Viết getter/setter và 2 hàm tạo 

- Viết hàm nhập/hàm hiển thị

- Tạo 2 đối tượng Book với 2 hàm tạo tương ứng trong phương thức main của lớp test. Nhập và hiển thị thông tin sách



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class People {
    String name, address;

    public People() {
    }

    public People(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ten: ");
        name = input.nextLine();
        System.out.println("Nhap dia chi: ");
        address = input.nextLine();
    }
    
    public void display() {
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "Name: " + name + ", Address: " + address;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession8;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Student extends People{
    String rollNo;

    public static void main(String[] args) {
//        Student std = new Student();
//        std.input();
        Student std = new Student("R001", "A", "Ha Noi");

        std.display();
    }
    
    public Student() {
    }

    public Student(String rollNo, String name, String address) {
        super(name, address);
        this.rollNo = rollNo;
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    @Override
    public void input() {
        super.input(); //To change body of generated methods, choose Tools | Templates.
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap MSV: ");
        rollNo = input.nextLine();
    }

    @Override
    public String toString() {
        //super.toString() => goi toi phuong thuc toString() cua People
//        String str = super.toString();
//        return str + ", Roll No: " + rollNo;
        return super.toString() + ", Roll No: " + rollNo;
    }
}


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

5

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

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

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