From ff367a664b6d3c2d08da2c3f2fd9ee767ccc9aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Fri, 9 Apr 2021 19:19:40 +0200 Subject: [PATCH] Adding autos --- .../src/com/p4ddy/java_tutorial/Auto.java | 40 +++++++++++++++++++ .../com/p4ddy/java_tutorial/HelloWorld.java | 10 ++++- .../src/com/p4ddy/java_tutorial/Rad.java | 18 +++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Auto.java create mode 100644 Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Rad.java diff --git a/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Auto.java b/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Auto.java new file mode 100644 index 0000000..0b44b21 --- /dev/null +++ b/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Auto.java @@ -0,0 +1,40 @@ +package com.p4ddy.java_tutorial; + +import java.util.ArrayList; +import java.util.List; + +public class Auto { + public String hersteller; + public int leistung; + public String farbe; + public int tueren; + public List raeder; + + public Auto(String hersteller, int leistung, String farbe, int tueren) { + this.hersteller = hersteller; + this.leistung = leistung; + this.farbe = farbe; + this.tueren = tueren; + + this.raeder = new ArrayList<>(); + Rad rad1 = new Rad(); + Rad rad2 = new Rad(); + Rad rad3 = new Rad(); + Rad rad4 = new Rad(); + + this.raeder.add(rad1); + this.raeder.add(rad2); + this.raeder.add(rad3); + this.raeder.add(rad4); + } + + public void fahren(){ + System.out.println("Auto fährt!"); + + int neueProfiltiefe = this.raeder.get(0).fahren(); + this.raeder.get(1).fahren(); + this.raeder.get(2).fahren(); + + System.out.println("Neue Profiltiefe: " + neueProfiltiefe); + } +} diff --git a/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/HelloWorld.java b/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/HelloWorld.java index 1e32bcc..5cfede8 100644 --- a/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/HelloWorld.java +++ b/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/HelloWorld.java @@ -2,6 +2,14 @@ package com.p4ddy.java_tutorial; public class HelloWorld { public static void main(String[] args){ - System.out.println("Hello World!"); + Auto auto1 = new Auto("Tesla", 500, "Rot", 5); + Auto auto2 = new Auto("Porsche", 1000, "Gelb", 3); + + System.out.println("Auto soll fahren"); + auto1.fahren(); + auto1.fahren(); + + auto1.raeder.forEach(rad -> System.out.print(rad.getProfiltiefe() + " ")); + System.out.println(auto2.hersteller); } } diff --git a/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Rad.java b/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Rad.java new file mode 100644 index 0000000..7e17050 --- /dev/null +++ b/Gabriel-Java-Learning/src/com/p4ddy/java_tutorial/Rad.java @@ -0,0 +1,18 @@ +package com.p4ddy.java_tutorial; + +public class Rad { + private int profiltiefe; + + public Rad() { + this.profiltiefe = 10; + } + + public int fahren(){ + this.profiltiefe = this.profiltiefe - 1; + return this.profiltiefe; + } + + public int getProfiltiefe(){ + return profiltiefe; + } +}