Skip to content

Instantly share code, notes, and snippets.

@TrevCan
Created October 28, 2020 00:12
Show Gist options
  • Save TrevCan/e9c980edb97f6b7697a8f7d0d81e6c73 to your computer and use it in GitHub Desktop.
Save TrevCan/e9c980edb97f6b7697a8f7d0d81e6c73 to your computer and use it in GitHub Desktop.
AnimalsAndInterfaces
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
public interface Animal {
public void play();
}
public class Cat implements Animal {
double distanceFromOwner = 0;
@Override
public void play() {
distanceFromOwner += (int) (Math.random() * 30 - 15);
}
@Override
public String toString() {
return "Cat{" +
"distanceFromOwner=" + distanceFromOwner +
'}';
}
}
public class Dog implements Animal {
double distanceFromOwner = 0.0;
@Override
public void play() {
distanceFromOwner += (int) (Math.random() * 10 + -5);
}
@Override
public String toString() {
return "Dog{" +
"distanceFromOwner=" + distanceFromOwner +
'}';
}
}
//interface animal en donde se implemente el método play
public class Person {
public String name;
public int age;
public int weight;
public String toString(){
return "\nName: "+this.name+"\nAge: " + this.age + "\nweight: " + this.weight;
}
public void haveBirthday() {
this.age++;
}
}
public class World {
public static void main(String[] args) {
Person trevor = new Person();
trevor.name = "Héctor Alejandro";
trevor.age = 16;
trevor.weight = 62;
System.out.println(trevor);
trevor.haveBirthday();
System.out.println(trevor);
Animal candy = new Dog(), mishi = new Cat();
candy.play();
candy.play();
candy.play();
candy.play();
candy.play();
mishi.play();
mishi.play();
mishi.play();
mishi.play();
mishi.play();
mishi.play();
System.out.println(candy);
System.out.println(mishi);
}
}
//class dog{
//
// void play(){
// //dog advances between -5 and 5
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment