Created
October 28, 2020 00:12
-
-
Save TrevCan/e9c980edb97f6b7697a8f7d0d81e6c73 to your computer and use it in GitHub Desktop.
AnimalsAndInterfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Animal { | |
| public void play(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 + | |
| '}'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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