Object-Oriented Chapter 6 After-School Exercises

1. Carry out function expansion in the third question.
(1) Add a new animal type: Pig (pig), and implement the shout() method.
(2) Modify the get() method of the Store class: if the incoming parameter is the string dog, it returns a Dog object; if the incoming parameter is the string pig, it returns a Pig object; otherwise, it returns a Cat object .
(3) Test in the test class Test: pass the parameter "pig" to the get() method of the Store class, and call the shout() method on the returned object to see if it is consistent with the expected result.
package kehouzuoye;

public interface Animal {
	void shout();//Animal shout method
}package kehouzuoye;

public class Cat implements Animal {

	@Override
	public void shout() {
		// implement cat meowing
		System.out.println("M M!");
	}

}package kehouzuoye;

public class Dog implements Animal {

	@Override
	public void shout() {
		// implement dog barking
		System.out.println("w w!");

	}

}package kehouzuoye;

public class Pig implements Animal {

	@Override
	public void shout() {
		// implement pig call
		System.out.println("pig call");
	}

}package kehouzuoye;

public class Store {
	public static Animal get(String choice) {//The return type is the method with parameters of the interface Animal
		if(choice.equalsIgnoreCase("dog")) {
			return new Dog();
		}else if(choice.equalsIgnoreCase("pig")) {
			return new Pig();
		}else {
			return new Cat();
		}
	}
}package kehouzuoye;

public class AnimalTest {
	
	public static void main(String[] args) {
		Animal a = Store.get("pig");
		a.shout();
	}

}
2. Reconstruct the class structure of the case electronic pet system throughout this book, and the requirements are as follows:
> Define the Eatble interface, and define the eat( ) method in the interface to indicate the eating function.
>Define the FlyingDiscCatchable interface. In the interface definition, the catchingFiyDise( ) method indicates the function of catching the flying disc.
> Define the Swimmable interface, and define the swim ( ) method in the interface to represent the swimming function.
>Define abstract class Pet, including pet name (mane), health value (health), and owner intimacy (love) attributes. And provide the abstract method print( ) to output pet information.
>Define the dog class Dog, inherit the Pet class, implement the Eatable and FlyingDiscCatchable interfaces, and override or implement each method.
> Define the Penguin class Penguin, inherit the Pet class, implement the Eatable and Swimble interfaces, and override or implement each method.
> Write a test class to realize the functions of dog eating, penguin swimming and dog playing Frisbee game, and output penguin information.
package kehouzuoye2;

public interface Eatable {
	void eat();//Eating function
}package kehouzuoye2;

public interface FlyingDiscCatchable {
	void catchingFlyDisc();//Flying disc function
}package kehouzuoye2;

public interface Swimmable {
	void swim();//Swimming function
}package kehouzuoye2;

public abstract class Pet {
	private String name;//nickname
	private int health;//Health value
	private int love;//intimacy
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		this.love = love;
	}
	public abstract void print();
}
package kehouzuoye2;

public class Dog extends Pet implements Eatable, FlyingDiscCatchable {

	@Override
	public void catchingFlyDisc() {
		// pick up frisbee
		System.out.println("Dog"+super.getName()+"Playing frisbee!");
	}

	@Override
	public void eat() {
		// Have a meal
		System.out.println("Dog"+super.getName()+"I'm full!");
	}

	@Override
	public void print() {
		// output information
		System.out.println("Pet confession:");
		System.out.println("I am a dog"+"\nMy name is:"+super.getName()+"\nMy intimacy with the owner is:"
		+super.getLove()+"My health value is"+super.getHealth());
		eat();
		catchingFlyDisc();
	}

}
package kehouzuoye2;

public class Penguin extends Pet implements Eatable, Swimmable {

	@Override
	public void swim() {
		// Swim
		System.out.println("Penguin"+super.getName()+"Swimming!");
	}

	@Override
	public void eat() {
		// Have a meal
		System.out.println("Penguin"+super.getName()+"I'm full!");
	}

	@Override
	public void print() {
		// output information
		System.out.println("Pet confession:");
		System.out.println("I am a penguin"+"\nMy name is:"+super.getName()+"\nMy intimacy with the owner is:"
		+super.getLove()+"My health value is"+super.getHealth());
		eat();
		swim();
	}

}
package kehouzuoye2;

public class Test {

	public static void main(String[] args) {
		Pet p = new Dog();
		p.setName("Prosperity");//Call the set method to assign
		p.setHealth(100);
		p.setLove(99);
		p.print();//Call the output information method
		System.out.println();
		Pet p1 = new Penguin();
		p1.setName("Nan Nan");//Call the set method to assign
		p1.setHealth(100);
		p1.setLove(78);
		p1.print();//Call the output information method
		
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906176&siteId=291194637