From 63e2fe066b732bb8d9d2fa96f038644b79c1e10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Meslin?= Date: Mon, 2 Oct 2023 16:49:17 +0200 Subject: [PATCH] feature: java-poo3-heritage --- Eagle.java | 42 ++++++++++++++++++- Fly.java | 12 ++++++ Nature.java | 3 -- out/production/quest-java-oop3/.gitignore | 31 ++++++++++++++ .../quest-java-oop3/quest-java-oop3.iml | 14 +++++++ quest-java-oop3.iml | 14 +++++++ 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 Fly.java create mode 100644 out/production/quest-java-oop3/.gitignore create mode 100644 out/production/quest-java-oop3/quest-java-oop3.iml create mode 100644 quest-java-oop3.iml diff --git a/Eagle.java b/Eagle.java index d0e8799..f1c65dc 100644 --- a/Eagle.java +++ b/Eagle.java @@ -1,4 +1,4 @@ -public class Eagle extends Bird { +public class Eagle extends Bird implements Fly { private boolean flying; private int altitude; @@ -21,4 +21,44 @@ public boolean isFlying() { public String sing() { return "Screech!"; } + + @Override + public void takeOff() { + if (!this.flying && this.altitude == 0) { + this.flying = true; + System.out.printf("%s takes off in the sky.%n", this.getName()); + } + } + + @Override + public void ascend(int meters) { + if (this.flying && meters > 0) { + this.altitude = Math.min(this.altitude + meters, 325); + System.out.printf("%s flies upward, altitude : %d%n", this.getName(), this.altitude); + } + } + + @Override + public void glide() { + if (this.flying && this.altitude > 0) { + System.out.printf("%s glides into the air.%n", this.getName()); + } + } + + @Override + public void descend(int meters) { + if (this.flying && meters > 0) { + this.altitude = Math.max(this.altitude - meters, 1); + System.out.println(this.getName() + " flies downward, altitude : " + this.altitude); + } + } + + @Override + public void land () { + if (this.flying && this.altitude == 1) { + System.out.println(this.getName() + " lands on the ground."); + } else { + System.out.println(this.getName() + " is too high, it can't land."); + } + } } diff --git a/Fly.java b/Fly.java new file mode 100644 index 0000000..746c948 --- /dev/null +++ b/Fly.java @@ -0,0 +1,12 @@ +public interface Fly { + + void takeOff(); + + void ascend(int meters); + + void glide(); + + void descend(int meters); + + void land(); +} diff --git a/Nature.java b/Nature.java index fe104f5..fadf311 100644 --- a/Nature.java +++ b/Nature.java @@ -12,8 +12,6 @@ public static void main(String[] args) { pingou.getOut(); Eagle hawkeye = new Eagle("Hawkeye"); - // TODO : uncomment the following code in order to test it - /* hawkeye.takeOff(); hawkeye.ascend(120); hawkeye.ascend(30); @@ -22,6 +20,5 @@ public static void main(String[] args) { hawkeye.land(); hawkeye.descend(9); hawkeye.land(); - */ } } diff --git a/out/production/quest-java-oop3/.gitignore b/out/production/quest-java-oop3/.gitignore new file mode 100644 index 0000000..bc408a9 --- /dev/null +++ b/out/production/quest-java-oop3/.gitignore @@ -0,0 +1,31 @@ +# Created by https://www.gitignore.io/api/java + +.idea + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +# End of https://www.gitignore.io/api/java \ No newline at end of file diff --git a/out/production/quest-java-oop3/quest-java-oop3.iml b/out/production/quest-java-oop3/quest-java-oop3.iml new file mode 100644 index 0000000..aa8ca58 --- /dev/null +++ b/out/production/quest-java-oop3/quest-java-oop3.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/quest-java-oop3.iml b/quest-java-oop3.iml new file mode 100644 index 0000000..aa8ca58 --- /dev/null +++ b/quest-java-oop3.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file