-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'code-differently:main' into lesson7
- Loading branch information
Showing
17 changed files
with
803 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Check Lesson 08 Pull Request | ||
|
||
on: | ||
pull_request: | ||
branches: [ "main" ] | ||
paths: | ||
- "lesson_08/collections/**" | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies. | ||
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md | ||
- name: Setup Gradle | ||
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 | ||
|
||
- name: Build Lesson 08 with Gradle Wrapper | ||
working-directory: ./lesson_08/collections | ||
run: ./gradlew check | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
132 changes: 132 additions & 0 deletions
132
.../objects/objects_app/src/main/java/com/codedifferently/lesson7/jordaneldridge/Animal.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.codedifferently.lesson7.jordaneldridge; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Animal { | ||
|
||
public enum AnimalType { | ||
MAMMAL, | ||
BIRD, | ||
REPTILE, | ||
FISH, | ||
AMPHIBIAN, | ||
INSECT | ||
} | ||
|
||
private String name; | ||
private boolean canBreathe; | ||
private int age; | ||
private String color; | ||
private String sound; | ||
private AnimalType type; | ||
private static List<Animal> allAnimals = new ArrayList<>(); | ||
|
||
public Animal( | ||
String name, boolean canBreathe, int age, String color, String sound, AnimalType type) { | ||
this.name = name; | ||
this.canBreathe = canBreathe; | ||
this.age = age; | ||
this.color = color; | ||
this.sound = sound; | ||
this.type = type; | ||
|
||
allAnimals.add(this); | ||
} | ||
|
||
public static List<Animal> getAllAnimals() { | ||
return allAnimals; | ||
} | ||
|
||
public String canFly() { | ||
return (type == AnimalType.BIRD) ? "This animal can fly!" : "This animal cannot fly."; | ||
} | ||
|
||
public String makeSound() { | ||
System.out.println("The " + this.name + " makes a sound!"); | ||
return this.sound; | ||
} | ||
|
||
public void growOlder() { | ||
this.age += 1; | ||
System.out.println("The " + this.name + " is now " + this.age + " years old."); | ||
} | ||
|
||
public void changeColor(String newColor) { | ||
this.color = newColor; | ||
System.out.println("The " + this.name + " has changed color to " + this.color + "."); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public boolean getCanBreathe() { | ||
return this.canBreathe; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public String getSound() { | ||
return sound; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setCanBreathe(boolean canBreathe) { | ||
this.canBreathe = canBreathe; | ||
} | ||
|
||
public void setAge(int age) throws InvalidAgeException { | ||
if (age < 0) { | ||
throw new InvalidAgeException("Age cannot be negative."); | ||
} | ||
this.age = age; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
public void setSound(String sound) { | ||
this.sound = sound; | ||
} | ||
|
||
public class InvalidAgeException extends Exception { | ||
public InvalidAgeException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public void repeatSound(int times) { | ||
for (int i = 0; i < times; i++) { | ||
System.out.println(this.getSound()); | ||
} | ||
} | ||
|
||
public String toString() { | ||
return "Animal [name=" | ||
+ name | ||
+ ", canBreathe=" | ||
+ canBreathe | ||
+ ", age=" | ||
+ age | ||
+ ", color=" | ||
+ color | ||
+ ", sound=" | ||
+ sound | ||
+ "]"; | ||
} | ||
|
||
public Object getType() { | ||
return this.type; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ects/objects_app/src/test/java/com/codedifferently/lesson7/jordaneldridge/AnimalTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.codedifferently.lesson7.jordaneldridge; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.codedifferently.lesson7.jordaneldridge.Animal.AnimalType; | ||
import com.codedifferently.lesson7.jordaneldridge.Animal.InvalidAgeException; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AnimalTest { | ||
|
||
@Test | ||
public void testConstructor() { | ||
Animal animal = new Animal("Dog", true, 5, "Brown", "Bark", AnimalType.MAMMAL); | ||
assertEquals("Dog", animal.getName()); | ||
assertTrue(animal.getCanBreathe()); | ||
assertEquals(5, animal.getAge()); | ||
assertEquals("Brown", animal.getColor()); | ||
assertEquals(AnimalType.MAMMAL, animal.getType()); | ||
} | ||
|
||
@Test | ||
public void testCanFly() { | ||
Animal animal = new Animal("Owl", true, 1, "Brown", "whistles", AnimalType.BIRD); | ||
assertEquals("This animal can fly!", animal.canFly()); | ||
} | ||
|
||
@Test | ||
public void testMakeSound() { | ||
Animal animal = new Animal("lizard", true, 3, "Green", "Hissing", AnimalType.REPTILE); | ||
String sound = animal.makeSound(); | ||
assertEquals("Hissing", sound); | ||
} | ||
|
||
@Test | ||
public void testRepeatSound() { | ||
Animal snake = new Animal("henry", true, 2, "brown", "hissing", AnimalType.REPTILE); | ||
snake.repeatSound(2); | ||
} | ||
|
||
@Test | ||
public void testSetAge() { | ||
Animal animal = new Animal("Dog", true, 5, "Brown", "Bark", AnimalType.MAMMAL); | ||
Exception exception = | ||
assertThrows( | ||
InvalidAgeException.class, | ||
() -> { | ||
animal.setAge(-1); | ||
}); | ||
|
||
String expectedMessage = "Age cannot be negative."; | ||
String actualMessage = exception.getMessage(); | ||
|
||
assertTrue(actualMessage.contains(expectedMessage)); | ||
} | ||
|
||
@Test | ||
public void testGetAllAnimals() { | ||
Animal dog = new Animal("Josie", true, 5, "Brown", "Bark", AnimalType.MAMMAL); | ||
Animal cat = new Animal("hunter", true, 3, "Black", "Meow", AnimalType.MAMMAL); | ||
Animal bird = new Animal("Owl", true, 1, "Brown", "whistles", AnimalType.BIRD); | ||
|
||
List<Animal> allAnimals = Animal.getAllAnimals(); | ||
|
||
assertTrue(allAnimals.contains(dog)); | ||
assertTrue(allAnimals.contains(cat)); | ||
assertTrue(allAnimals.contains(bird)); | ||
assertEquals(8, allAnimals.size()); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Lesson 08 | ||
|
||
## Homework | ||
|
||
* Complete three [coding interview questions](#coding-interview-questions). | ||
|
||
## Coding Interview questions | ||
|
||
To complete this assignment, please implement the functions in [Lesson8.java][lesson-link] and submit a pull request with your solutions. | ||
|
||
* Find a cycle in a LinkedList. | ||
* Complete function that returns whether an array of elements contains a duplicate. | ||
* Write a function that returns the maximum path of a tree. | ||
|
||
[lesson-link]: ./collections/collections_app/src/main/java/com/codedifferently/lesson8/Lesson8.java |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# | ||
# https://help.github.com/articles/dealing-with-line-endings/ | ||
# | ||
# Linux start script should use lf | ||
/gradlew text eol=lf | ||
|
||
# These are Windows script files and should use crlf | ||
*.bat text eol=crlf | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Ignore Gradle project-specific cache directory | ||
.gradle | ||
|
||
# Ignore Gradle build output directory | ||
build |
Oops, something went wrong.