You are on page 1of 2

_____________________________________

public interface pet {


public String move();
public String speak();
public String toString();
public String getName();
}

____________________________________________

public class Dog implements pet{


protected String name;
protected int weight;
public Dog(String s,int weight)
{
//name = s;
this.name = s;
this.weight = weight;

}
public String move() {
return "";
}
public String toString() {
return toString() + name+ " is a dog and weighs "+ weight + " pounds";
}
public int getWeight() {
return weight;
}
public String speak() {
return "Wof wof wof";

}
@Override
public String getName() {

return name;
}

_____________________________________________

public class Snake implements pet{


protected String name;
int length;
public Snake(String s, int len) {
name = s;
length = len;
}
public int getLength(int lenght) {
return length;

}
public String speak() {
return "His His";
}
public String move() {
return "Slither";
}
public String toString() {
return "My pet "+name+" is a snake and is "+length+" inches long";
}
@Override
public String getName() {
return name;
}
}

___________________________________________

public class poodle extends Dog{


protected String n;
protected int w;

public poodle(String n, int w) {


super(n, w);
this.n = n;
this.w = w;
}

public String speak() {


return "Yib Yib";
}

public String toString() {


return "My pet " + n + " is a poodle and weighs " + w;
}
}

_______________________________________

public class pets3 {


public static void main(String args[]) {

pet[] myPets = new pet[4];


myPets[0] = new Dog("George", 100);
myPets[1] = new Dog("Spot", 50);
myPets[2] = new poodle("Rocky", 16);
myPets[3] = new Snake("Bruno", 20);
for(int j = 0; j < myPets.length; j++)
{
System.out.println(myPets[j].toString());
System.out.println("Speak: " + myPets[j].speak());
System.out.println(myPets[j].move() + " " + myPets[j].getName() +
"\n");

}
}

You might also like