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

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

Question1:                                                                                                                          

Create an interface named as IAnimal with the below method:

            void  input(): input data from keyboard

            void display(): display information

Question2:                                                                                                                            

Create a class named as Animal which implements the IAnimal interface, and have attributes:

            private String name;

            private double weight;

+ Create two constructors, in which

-       Default constructor

-       Constructor with arguments used to assign value for Animal’s attributes.

+ Create get/set methods

+ Implement all methods from IAnimal to input data and display information of Animal

Question3:                                                                                                                          

Create a class named as Cat which extends from Animal  and have  an extra attribute:

            private String color;

+ Create two constructors, in which

-       Default constructor

-       Constructor with arguments used to assign value for Cat’s attributes and attributes extended from Animal.

+ Create get/set methods

+ Override all methods from Animal to input data and display information of Cat

Question3:                                                                                                                          

Create a class named as Chicken which extends from Animal  and have  some extra attribute:

            private int numberOfLeg;

+ Create two constructors, in which

-       Default constructor

-       Constructor with arguments used to assign value for Chicken’s attributes and attributes extended from Animal.

+ Create get/set methods

+ Override all methods from Animal to input data and display information of Chicken

Question4:                                                                                                                          

Create the test class named as AnimalTest to do the following action:

1)    Create an array for classes: Animal, Cat and Chicken with length 3.

2)    Create only one menthod inputDataForAnimal(): used to input data for array of Animal, Cat and Chicken

3)      Create only one menthod displayDataOfAnimal(): used to display information of all items in array of Animal, Cat and Chicken.

4)      Create main() method to test the above methods

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

https://gokisoft.com/988

Bình luận

avatar
Nguyễn Tiến Đạt [T2008A]
2021-02-28 17:38:13


#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 lesson7.QuanLyDongVat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Animal implements IAnimal{
    private String name;
    private double weight;
    ArrayList<Animal> list = new ArrayList<>();
    
    Animal animal;
    int select, countAnimal = 0, countCat = 0 , countChicken = 0;
    public void inputDataForAnimal(){
        selectAnimal();
        animal.input();
        list.add(select,animal);
    }
    String a;
    public void displayDataOfAnimal(){
        int checkAnimal = countAnimal, checkCat = countCat, checkChicken = countChicken;
        for (Animal x : list) {
            if(checkAnimal == countAnimal){
                System.out.println("List of Animal:");
            }
            else if(checkAnimal == 0 && checkCat == countCat){
                System.out.println("List of Cat:");
            }
            else if(checkCat == 0 && checkChicken == countChicken){
                System.out.println("List of Chicken:");
                checkChicken--;
            }
            x.display();
            if( checkAnimal > 0 ){
                checkAnimal--;
                continue;
            }
            if(checkCat > 0 && checkAnimal == 0 ){
                checkCat--;
                continue;
            }
            if(checkChicken > 0 && checkCat == 0){
                checkChicken--;
                continue;
            }
        }
    }
    
    public void selectAnimal(){
        Scanner scan = new Scanner(System.in);
        System.out.println("1.Animal");
        System.out.println("2.Cat");
        System.out.println("3.Chicken");
        System.out.println("Select animal:");
        int choose = Integer.parseInt(scan.nextLine());
        if(choose == 1 ){
            select = 0;
            animal = new Animal();
            countAnimal++;
        }
        else if(choose == 2 ){
            select = 1;
            animal = new Cat();
            countCat++;
        }
        else if(choose == 3 ){
            select = 2;
            animal = new Chicken();
            countChicken++;
        }
    }

    public Animal() {
    }

    public Animal(String name, double weight) {
        this.name = name;
        this.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;
    }
    
    

    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Name:");
        name = scan.nextLine();
        System.out.println("Weight:");
        weight = Double.parseDouble(scan.nextLine());
    }

    @Override
    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Weight: " + weight);
    }

    
    
    
}


#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 lesson7.QuanLyDongVat;

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

/**
 *
 * @author MyPC
 */
public class AnimalTest {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Scanner scan = new Scanner(System.in);
        int choose;
        do{
            showMenu();
            System.out.println("Select program:");
            choose = Integer.parseInt(scan.nextLine());
            switch(choose){
                case 1:
                    animal.inputDataForAnimal();
                    break;
                case 2:
                    animal.displayDataOfAnimal();
                    break;
                default:
                    System.err.println("Wrong command!!");
                    break;
            }
        }while( choose != 2 );
    }
    
    static void showMenu(){
        System.out.println("1. Input data for Animal");
        System.out.println("2. Display data of Animal");
    }
}


#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 lesson7.QuanLyDongVat;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Cat extends Animal{
    private String color;

    public Cat() {
    }

    public Cat(String color) {
        this.color = color;
    }

    public Cat(String color, String name, double weight) {
        super(name, weight);
        this.color = color;
    }

    public String getColor() {
        return color;
    }

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

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

    @Override
    public void display() {
        super.display(); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Color: " + color);
    }
    
    
    
}


#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 lesson7.QuanLyDongVat;

import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class Chicken extends Animal{
    private int numberOfLeg;

    public Chicken() {
    }

    public Chicken(int numberOfLeg) {
        this.numberOfLeg = numberOfLeg;
    }

    public Chicken(int numberOfLeg, String name, double weight) {
        super(name, weight);
        this.numberOfLeg = numberOfLeg;
    }

    public int getNumberOfLeg() {
        return numberOfLeg;
    }

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

    @Override
    public void input() {
        Scanner scan = new Scanner(System.in);
        super.input(); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Number of legs:");
        numberOfLeg = Integer.parseInt(scan.nextLine());
    }

    @Override
    public void display() {
        super.display(); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Number of legs: " + numberOfLeg);
    }
    
    
}


#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 lesson7.QuanLyDongVat;

/**
 *
 * @author MyPC
 */
public interface IAnimal {
    void input();
    void display();
}


avatar
Đỗ Văn Huấn [T1907A]
2020-03-17 12:30:50


package BaiTapNgay6_3_2020.Bai58;

public interface IAnimal {
void input();
void output();
}
package BaiTapNgay6_3_2020.Bai58;

import java.util.Scanner;

public class Cat extends Animal {
private String color;

public Cat() {
}

public Cat(String name, double weigth, String color) {
super(name, weigth);
this.color = color;
}

public String getColor() {
return color;
}

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

@Override
public void input() {
System.out.println("Nhap thong tin meo:");
super.input();
Scanner can = new Scanner(System.in);
System.out.println("Nhap color: ");
color = can.nextLine();
System.out.println("");
}

@Override
public void output() {
super.output();
System.out.println(toString());
}

@Override
public String toString() {
return "Cat{" +
"color='" + color + '\'' +
'}';
}
}
package BaiTapNgay6_3_2020.Bai58;

import java.util.Scanner;

public class Chicken extends Animal {
private int numberOfLeg;

public Chicken() {
}

public Chicken(String name, double weigth, int numberOfLeg) {
super(name, weigth);
this.numberOfLeg = numberOfLeg;
}

public int getNumberOfLeg() {
return numberOfLeg;
}

public void setNumberOfLeg(int numberOfLeg) {
this.numberOfLeg = numberOfLeg;
}
@Override
public void input() {
System.out.println("thong tin ga:");
super.input();
Scanner can = new Scanner(System.in);
System.out.println("Nhap numberOfLeg: ");
numberOfLeg = Integer.parseInt(can.nextLine());
System.out.println("");
}

@Override
public void output() {
super.output();
System.out.println(toString());
}

@Override
public String toString() {
return "Chicken{" +
"numberOfLeg=" + numberOfLeg +
'}';
}
}
package BaiTapNgay6_3_2020.Bai58;

import java.util.Scanner;

public class Animal implements IAnimal {
private String name;
private double weigth;

public Animal() {
}

public Animal(String name, double weigth) {
this.name = name;
this.weigth = weigth;
}

public String getName() {
return name;
}

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

public double getWeigth() {
return weigth;
}

public void setWeigth(double weigth) {
this.weigth = weigth;
}

@Override
public void input() {

Scanner can = new Scanner(System.in);
System.out.println("nhap name:" );
name = can.nextLine();
System.out.println("Nhap weidth: ");
weigth = Double.parseDouble(can.nextLine());
System.out.println("");
}

@Override
public void output() {
System.out.println(toString());
}

@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", weigth=" + weigth +
'}';
}
}
package BaiTapNgay6_3_2020.Bai58;

public class AnimalTest {

public static void main(String[] args) {
inputDataForAnimal();
displayDataForAnimal();
}

private static void inputDataForAnimal() {
Animal[] animals = new Animal[3];
animals[0] = new Animal();
animals[0].input();

animals[1] = new Chicken();
animals[1].input();

animals[2] = new Cat();
animals[2].input();
}

private static void displayDataForAnimal() {
Animal[] animals = new Animal[3];
animals[0].output();
animals[1].output();
animals[2].output();

}


}