By GokiSoft.com| 15:29 21/09/2022|
Java Basic

Java basic- Test 60 phút - tổng hợp kiến thức java căn bản

1. Tạo 1 lớp giao diện IInfor chứa hàm showInfor

2. Tạo lớp đối tượng People gồm các thuộc tính tên, tuổi, địa chỉ kế thừa từ lớp IInfor

- Tạo hàm input

Tạo lớp Car gồm các thuộc tính tên và màu -> kế thừa từ lớp IInfor

- Tạo hàm input

Viết code cho các lớp trên.

3. Trọng phương thức main của lớp Test tạo 2 đối tượng People và Car. 

- tạo phương thức như sau.

public static void showInfor(ArrayList<IInfor> a) -> hàm này hiển thị thông tin tất cả đối tượng.

Viết chương trình và sử dụng hàm showInfor trong phương thức main.


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

5

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

Trương Công Vinh [T1907A]
Trương Công Vinh

2020-03-11 10:09:57



/*
 * 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 Bt522;
import java.util.ArrayList;
/**
 *
 * @author DELL
 */
public class Test {
    public static void main(String[] args) {
        People P =new People();
        Car C = new Car();
        ArrayList<IInfor> a = new ArrayList<>();
        System.out.println("Car : ");
        C.input();
        a.add(C);
        System.out.println("People : ");
        P.input();
        a.add(P);
        showInfor(a);
    }
    public static void showInfor(ArrayList<IInfor> a){
        for (IInfor iInfor : a) {
            iInfor.showInfor();
        }
    }
}



/*
 * 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 Bt522;

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class Car implements IInfor{
 String name,color;   

    public Car() {
    }

    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

 
 public void input(){
     Scanner scan = new Scanner(System.in);
     System.out.println("Name : ");
     name = scan.nextLine();
     System.out.println("Color : ");
     color = scan.nextLine();
 }
 
 
    @Override
    public void showInfor() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Car{" + "name=" + name + ", color=" + color + '}';
    }
}



/*
 * 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 Bt522;

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class People implements IInfor{
   String name , address;
   int age;

    public People() {
    }

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

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
   
    
    
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Name : ");
        name = scan.nextLine();
        System.out.println("Age : ");
        age = Integer.parseInt(scan.nextLine());
        System.out.println("Address : ");
        address = scan .nextLine();
    }
    
    
    @Override
    public void showInfor() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "People{" + "name=" + name + ", address=" + address + ", age=" + age + '}';
    }
    
}



/*
 * 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 Bt522;

/**
 *
 * @author DELL
 */
public interface IInfor {
    public void showInfor();
}