Java Basic- OOP - Interface - Quản lý động vật java - đề tiếng anh
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
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
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();
}
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-17 12:30:50
package BaiTapNgay6_3_2020.Bai58; |
package BaiTapNgay6_3_2020.Bai58; |
package BaiTapNgay6_3_2020.Bai58; |
package BaiTapNgay6_3_2020.Bai58; |
package BaiTapNgay6_3_2020.Bai58; |