By GokiSoft.com| 19:21 06/07/2022|
Java Basic

[Source Code] Java Basic- OOP - Interface - Quản lý động vật java - đề tiếng anh - C2108L

Java Basic- OOP - Interface - Quản lý động vật java - đề tiếng anh


#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 bt988;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    //Cau 1:
    static IAnimal[] animalList = new IAnimal[3];
        
    public static void main(String[] args) {
        //Cau 1
        animalList[0] = new Animal();
        animalList[1] = new Cat();
        animalList[2] = new Chicken();
        
        //Cau 2
        inputDataForAnimal();
        
        //Cau 3
        displayDataOfAnimal();
    }
    
    //Cau 2
    static void inputDataForAnimal() {
        System.out.println("Nhap thong tin dong vat: ");
        for (IAnimal animal : animalList) {
            animal.input();
        }
    }
    
    //Cau 3
    static void displayDataOfAnimal() {
        System.out.println("Hien thi thong tin dong vat: ");
        for (IAnimal animal : animalList) {
            animal.display();
        }
    }
}


#IAnimal.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 bt988;

/**
 *
 * @author Diep.Tran
 */
public interface IAnimal {
    void input();
    void display();
}


#Chicken.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 bt988;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Chicken extends Animal{
    int numberOfLeg;

    public Chicken() {
    }

    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap so chan: ");
        numberOfLeg = Integer.parseInt(scan.nextLine());
    }

    @Override
    public String toString() {
        return super.toString() + ", Number of Leg=" + numberOfLeg;
    }
    
    public int getNumberOfLeg() {
        return numberOfLeg;
    }

    public void setNumberOfLeg(int numberOfLeg) {
        this.numberOfLeg = numberOfLeg;
    }
    
    
}


#Cat.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 bt988;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Cat extends Animal{
    String color;

    public Cat() {
    }

    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap mau sac: ");
        color = scan.nextLine();
    }

    @Override
    public String toString() {
        return super.toString() + ", color=" + color;
    }

    public String getColor() {
        return color;
    }

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


#AnimalTest.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 bt988;

import static bt988.Test.animalList;

/**
 *
 * @author Diep.Tran
 */
public class AnimalTest {
    public static void main(String[] args) {
        //Cau 1:
        IAnimal[] animalList = new IAnimal[3];
        
        animalList[0] = new Animal();
        animalList[1] = new Cat();
        animalList[2] = new Chicken();
        
        //Cau 2
        inputDataForAnimal(animalList);
        
        //Cau 3
        displayDataOfAnimal(animalList);
    }
    
    static void inputDataForAnimal(IAnimal[] animalList) {
        System.out.println("Nhap thong tin dong vat: ");
        for (IAnimal animal : animalList) {
            animal.input();
        }
    }
    
    static void displayDataOfAnimal(IAnimal[] animalList) {
        System.out.println("Hien thi thong tin dong vat: ");
        for (IAnimal animal : animalList) {
            animal.display();
        }
    }
}


#Animal.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 bt988;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Animal implements IAnimal{
    String name;
    double weight;

    public Animal() {
    }
    
    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        name = scan.nextLine();
        System.out.println("Nhap trong luong: ");
        weight = Double.parseDouble(scan.nextLine());
    }

    @Override
    public void display() {
        System.out.println(this);
//        System.out.println("Ten: " + name + ", trong luong: " + weight);
    }

    @Override
    public String toString() {
        return "name=" + name + ", weight=" + weight;
    }
    
    public String getName() {
        return name;
    }

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

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
    
}


Tags:



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 đó