By GokiSoft.com| 15:35 21/06/2023|
Java Basic

Examination & TestExa >> Interface trong java >> Java basic BT1140

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.

Liên kết rút gọn:

https://gokisoft.com/1140

Bình luận

avatar
Nguyễn Việt Hoàng [community,AAHN-C2009G]
2021-09-21 13:48:23


#Car.java


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

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class Car implements Info {
    private String name ;
    private String color;
    
    public Car(){
        
    }
    public Car(String name, String color){
        this.name = name;
        this.color = color;
    }
    public String getName(){
        return this.name;
    }
    public String getColor(){
        return this.color;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setColor(String color){
        this.color = color;
    }
    public void input(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter name :");
        String name  = sc.nextLine();
        System.out.println("Enter color :");
        String color = sc.nextLine();
        setName(name);
        setColor(color);
    }
    @Override
      public void showInfo(){
          System.out.println("Name :" + this.getName()
                             + ", Color :" + this.getColor());
      }
}


#Info.java


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

/**
 *
 * @author DELL
 */
public interface Info {

    public void showInfo();
}


#People.java


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

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class People implements Info {

    private String name;
    private int age;
    private String address;

    public People() {

    }

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

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public String getAddress() {
        return this.address;
    }

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

    public void setAge(int age) {
        this.age = age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter name :");
        String name = sc.nextLine();
        System.out.println("Enter Age :");
        int age = sc.nextInt();
        System.out.println("Enter address :");
        String address = sc.nextLine();
        setName(name);
        setAge(age);
        setAddress(address);
    }

    @Override
    public void showInfo() {
        System.out.println("Name :" + this.getName()
                + ", Age :" + this.getAge()
                + ", Address :" + this.getAddress());
    }
;

}


#test.java


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

import java.util.ArrayList;

/**
 *
 * @author DELL
 */
public class test {
    public static void showInfo(ArrayList<Info> a) {
//        ArrayList<Info> = new ArrayList<>();
//        System.out.println(a);
    }
    public static void main(String[] args) {
        
    }
}


avatar
Hoàng Thiện Thanh [community,AAHN-C2009G]
2021-09-21 11:45:02


#Car.java


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

import java.util.Scanner;

/**
 *
 * @author anhoa
 */
public class Car implements IInfor{
    private String name;
    private String color;
    
    public Car(){
        
    }
    
    public Car(String name, String color){
        this.name = name;
        this.color = color;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public void setColor(String color){
        this.color = color;
    }
    
    public void input(){
        System.out.println("Please enter input for new Car: ");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        sc = new Scanner(System.in);
        String color = sc.nextLine();
        
        setName(name);
        setColor(color);
    }
    
    @Override
    public String toString(){
        return "[Name: " + name + ", Color: " + color + "]";
    }
    
    @Override
    public void showInfor(){
        System.out.println("Name: " + this.name);
        System.out.println("Color: " + this.color);
    }
}


#IInfor.java


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

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


#People.java


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

import java.util.Scanner;

/**
 *
 * @author anhoa
 */
public class People implements IInfor{
    private String name;
    private int age;
    private String address;
    
    public People(){
        
    }
    
    public People(String name, int age, String address){
        this.name = name;
        this.age = age;
        this.address = address;
    }
    
    public String getName(){
        return this.name;
    }
    
    public int getAge(){
        return this.age;
    }
    
    public String getAddress(){
        return this.address;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public void setAge(int age){
        this.age = age;
    }
    
    public void setAddress(String address){
        this.address = address;
    }
    
    public void input(){
        System.out.println("Please enter input for new People:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        sc = new Scanner(System.in);
        int age = sc.nextInt();
        sc = new Scanner(System.in);
        String address = sc.nextLine();
        
        //new People(name, age, address);
        setName(name);
        setAge(age);
        setAddress(address);
    }
    
    @Override
    public String toString(){
        return "[Name: " + name + ", Age: " + age + ", Address: " + address + "]";
    }
    
    @Override
    public void showInfor(){
        System.out.println("Name: " + this.name);
        System.out.println("Age: " + this.age);
        System.out.println("Address: " + this.address);
    }
}


#Test30.java


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

import java.util.ArrayList;

/**
 *
 * @author anhoa
 */
public class Test30 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        People entityA = new People();
        Car entityB = new Car();
        
        entityA.input();
        entityB.input();
        ArrayList<IInfor> all = new ArrayList<>();
        all.add(entityA);
        all.add(entityB);
        showInfor(all);
    }
    public static void showInfor(ArrayList<IInfor> a){
        System.out.println(a);
    };
   
}


avatar
Nam20021608 [community]
2021-09-21 10:44:12

Nguyễn Văn Nam C2009G

avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:36:03
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Test {
public static void main(String [] args) {
People people = new People();
Car car = new Car();
ArrayList<People> peoples = new ArrayList<>();
people.input();
peoples.add(people);
ArrayList<Car> cars = new ArrayList<>();
car.input();
cars.add(car);
for(People people1 : peoples) {
System.out.println(people1.showInfor());
}
for(Car car1 : cars) {
System.out.println(car1.showInfor());
}


}

}
avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:35:40



import java.util.Scanner;

public class Car implements IInfor {
    private String tenXe;
    private String mau;

    public Car() {
    }

    public Car(String tenXe, String mau) {
        this.tenXe = tenXe;
        this.mau = mau;
    }

    public Scanner getScanner() {
        return new Scanner(System.in);
    }
    public String getTenXe() {
        return tenXe;
    }

    public void setTenXe(String tenXe) {
        this.tenXe = tenXe;
    }

    public String getMau() {
        return mau;
    }

    public void setMau(String mau) {
        this.mau = mau;
    }
    public void input() {
        System.out.println("Nhap ten xe");
        this.tenXe = getScanner().nextLine();
        System.out.println("Nhap mau");
        this.mau = getScanner().nextLine();
    }

    @Override
    public String showInfor() {
        return String.format("Ten xe: %s, Mau xe: %s",this.tenXe,this.mau);
    }
}


avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:34:40



import java.util.Scanner;

public class People implements IInfor {
    private String ten;
    private int tuoi;
    private String diachi;

    public People() {
    }

    public People(String ten, int tuoi, String diachi) {
        this.ten = ten;
        this.tuoi = tuoi;
        this.diachi = diachi;
    }

    public Scanner getScanner() {
        return new Scanner(System.in);
    }
    public String getTen() {
        return ten;
    }

    public void setTen(String ten) {
        this.ten = ten;
    }

    public int getTuoi() {
        return tuoi;
    }

    public void setTuoi(int tuoi) {
        this.tuoi = tuoi;
    }

    public String getDiachi() {
        return diachi;
    }

    public void setDiachi(String diachi) {
        this.diachi = diachi;
    }

    public void input() {
        System.out.println("Nhap ten:");
        this.ten = getScanner().nextLine();
        System.out.println("Nhap tuoi:");
        this.tuoi = getScanner().nextInt();
        System.out.println("Nhap dia chi");
        this.diachi = getScanner().nextLine();
    }
    @Override
    public String showInfor() {
        return String.format("Ten: %s, tuoi:%d, dia chi: %s",this.ten,this.tuoi,this.diachi);
    }


}


avatar
Hieu Ngo [community,C2009G]
2021-08-31 04:34:31



public interface IInfor {
    String showInfor();
}


avatar
Nguyễn Hùng Anh [community,C2009G]
2021-08-31 03:41:38


#Car.java


package infor;

import java.util.Scanner;

public class Car extends IInfor{
    public String color;

    public Car() {
    }

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

    public String getColor() {
        return color;
    }

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

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

    Scanner scanner = new Scanner(System.in);
    public void input() {
        System.out.println("Enter car's name: ");
        name = scanner.next();
        System.out.println("Enter car's color: ");
        color = scanner.next();
    }

    @Override
    public void showInfor() {
        super.showInfor();
        System.out.println("color = " + color);
    }
}


#IInfor.java


package infor;

public class IInfor {
    public String name;

    public IInfor() {
    }

    public IInfor(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public void showInfor() {
        System.out.println("name = " + name);
    }
}


#People.java


package infor;

import java.util.Scanner;

public class People extends IInfor{
    public Integer age;
    public String address;

    public People() {
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
    Scanner scanner = new Scanner(System.in);
    public void input() {
        System.out.println("Enter person's name: ");
        this.name = scanner.next();
        System.out.println("Enter person's age: ");
        age = scanner.nextInt();
        System.out.println("Enter person's address: ");
        address = scanner.next();
    }

    @Override
    public void showInfor() {
        super.showInfor();
        System.out.println("age = " + age + "\naddress = " + address);
    }
}


#Main.java


import infor.Car;
import infor.People;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String [] args) {
//        System.out.println("haha");
        int c;
        ArrayList<People> peoples = new ArrayList<>();
        ArrayList<Car> cars = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter number of people: ");
        c = scanner.nextInt();
        for (int i = 0; i < c; i++) {
            People people = new People();
            people.input();
            peoples.add(people);
        }
        for (People people: peoples) {
            people.showInfor();
        }
        System.out.println("Enter number of cars: ");
        c = scanner.nextInt();
        for (int i = 0; i < c; i++) {
            Car car = new Car();
            car.input();
            cars.add(car);
        }
        for (Car car: cars) {
            car.showInfor();
        }
    }
}


avatar
GokiSoft.com [Teacher]
2021-07-31 13:03:26


#People.java


/*
 * 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 lesson01.bt1140;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class People implements IInfor{
    String fullname, address;
    int age;

    public People() {
    }

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

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    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;
    }
    
    @Override
    public void showInfor() {
        System.out.printf("\nTen: %s, tuoi: %d, dia chi: %s\n", fullname, age, address);
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        fullname = scan.nextLine();
        
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(scan.nextLine());
        
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
    }
}


#Main.java


/*
 * 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 lesson01.bt1140;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        People people = new People("TRAN VAN A", "HA NOI", 22);
        Car car = new Car("Santafe", "White");
        
        ArrayList<IInfor> list = new ArrayList<>();
        list.add(car);
        list.add(people);
        
        showAllInfor(list);
    }
    
    public static void showAllInfor(ArrayList<IInfor> a) {
        a.forEach(infor -> {
            infor.showInfor();
        });
    }
}


#IInfor.java


/*
 * 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 lesson01.bt1140;

/**
 *
 * @author Diep.Tran
 */
public interface IInfor {
    void showInfor();
}


#Car.java


/*
 * 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 lesson01.bt1140;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
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;
    }
    
    @Override
    public void showInfor() {
        System.out.printf("\nTen: %s, mau: %s\n", name, color);
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        
        System.out.println("Nhap mau: ");
        color = scan.nextLine();
    }
}