From a059b274f4a19c466fb4e84d54df5bc8e00b2d78 Mon Sep 17 00:00:00 2001 From: Ariel Wang Date: Wed, 12 Jul 2023 15:39:53 +1000 Subject: [PATCH 1/2] Practice Creating Objects --- .../examples/CharacterPractice.java | 17 ++++++++++ 2. Inside Classes/examples/DuckPractice.java | 34 +++++++++++++++++++ 2. Inside Classes/examples/PondPractice.java | 17 ++++++++++ 3 files changed, 68 insertions(+) create mode 100644 2. Inside Classes/examples/CharacterPractice.java create mode 100644 2. Inside Classes/examples/DuckPractice.java create mode 100644 2. Inside Classes/examples/PondPractice.java diff --git a/2. Inside Classes/examples/CharacterPractice.java b/2. Inside Classes/examples/CharacterPractice.java new file mode 100644 index 0000000..3884769 --- /dev/null +++ b/2. Inside Classes/examples/CharacterPractice.java @@ -0,0 +1,17 @@ +package examples; + +public class CharacterPractice { + + private String name; + private String species; + + public CharacterPractice(String name, String species) { + this.name = name; + this.species = species; + } + + public static void main(String[]args) { + CharacterPractice bilbo = new CharacterPractice("Bilbo", "Hobbit"); + CharacterPractice legolas = new CharacterPractice("Legolas", "Elf"); + } +} \ No newline at end of file diff --git a/2. Inside Classes/examples/DuckPractice.java b/2. Inside Classes/examples/DuckPractice.java new file mode 100644 index 0000000..11ea52f --- /dev/null +++ b/2. Inside Classes/examples/DuckPractice.java @@ -0,0 +1,34 @@ +package examples; + +public class DuckPractice { + + // member variable: data + private String name; + private int lifeExpectancy; + private String favoriteFood; + + public DuckPractice(String name, int lifeExpectancy, String favoriteFood) { + super(); + this.name = name; + this.lifeExpectancy = lifeExpectancy; + this.favoriteFood = favoriteFood; + } + + // methods: functionality + void waddle() { + String message = String.format("%s is waddling.", this.name); + System.out.println(message); + lifeExpectancy ++; + } + + void quack() { + System.out.println("quack quack"); + } + + @Override + public String toString() { + String message = String.format("%s likes %s. Life expectancy is %d", name, favoriteFood, lifeExpectancy); + return message.toString(); + } + +} diff --git a/2. Inside Classes/examples/PondPractice.java b/2. Inside Classes/examples/PondPractice.java new file mode 100644 index 0000000..cb06dd0 --- /dev/null +++ b/2. Inside Classes/examples/PondPractice.java @@ -0,0 +1,17 @@ +package examples; + +public class PondPractice { + public static void main(String[] args) { + String me = "june"; + + // = new () + DuckPractice mobyDuck = new DuckPractice("Moby", 90/3, "celery"); + + mobyDuck.quack(); + mobyDuck.waddle(); + mobyDuck.waddle(); + + System.out.println(mobyDuck); + + } +} From 7b5f16b64b6643729da8cf9527dfbc0de3b860f9 Mon Sep 17 00:00:00 2001 From: Ariel Wang Date: Mon, 17 Jul 2023 15:40:06 +1000 Subject: [PATCH 2/2] Inheritance --- .classpath | 1 + 5.Inheritance/examples/ExtraRandom.java | 13 +++++++++++++ 5.Inheritance/examples/Random.java | 11 +++++++++++ 3 files changed, 25 insertions(+) create mode 100644 5.Inheritance/examples/ExtraRandom.java create mode 100644 5.Inheritance/examples/Random.java diff --git a/.classpath b/.classpath index 1fdd8ff..c928414 100644 --- a/.classpath +++ b/.classpath @@ -10,5 +10,6 @@ + diff --git a/5.Inheritance/examples/ExtraRandom.java b/5.Inheritance/examples/ExtraRandom.java new file mode 100644 index 0000000..7e4d2b9 --- /dev/null +++ b/5.Inheritance/examples/ExtraRandom.java @@ -0,0 +1,13 @@ +package examples; + +import java.util.Random; + +public class ExtraRandom extends Random { + + public String nextLetter() { + int letterStartAt = 97; + int randomInt = new Random().nextInt(26) + letterStartAt; + + return "" + (char)randomInt; + } +} diff --git a/5.Inheritance/examples/Random.java b/5.Inheritance/examples/Random.java new file mode 100644 index 0000000..65484ea --- /dev/null +++ b/5.Inheritance/examples/Random.java @@ -0,0 +1,11 @@ +package examples; + +public class Random { + + public static void main(String []arg) { + ExtraRandom extraRandom = new ExtraRandom(); + + System.out.println(extraRandom.nextLetter()); + } + +}