Skip to content

Commit

Permalink
Merge branch 'code-differently:main' into lesson7
Browse files Browse the repository at this point in the history
  • Loading branch information
RichHawkins authored Mar 16, 2024
2 parents 00754e8 + 202db1f commit 6610bf0
Show file tree
Hide file tree
Showing 17 changed files with 803 additions and 12 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/check_lesson_08_pr.yml
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


6 changes: 6 additions & 0 deletions .github/workflows/check_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
- "lesson_04/expression/**"
- "lesson_05/types/**"
- "lesson_06/conditionals/**"
- "lesson_07/objects/**"
- "lesson_08/collections/**"

jobs:
build:
Expand Down Expand Up @@ -55,6 +57,10 @@ jobs:
working-directory: ./lesson_07/objects
run: ./gradlew assemble && ./gradlew spotlessCheck

- name: Build Lesson 08 with Gradle Wrapper
working-directory: ./lesson_08/collections
run: ./gradlew assemble && ./gradlew spotlessCheck

- name: Build Shared Lib with Gradle Wrapper
working-directory: ./lib/java/codedifferently-instructional
run: ./gradlew check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public static boolean canVote(int age) {
}

/**
* Compares two strings lexographically.
* Compares two strings lexicographically.
*
* @param value1 The first `String` to compare.
* @param value2 The second `String` to compare.
* @rerturn -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
*/
public static int compareStrings(String a, String b) {
// The distance will be a number less than 0 if string `a` is lexographically less than `b`, 1
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
// if it is greater, and 0 if the strings are equal.
int distance = Helpers.computeLexographicDistance(a, b);
int distance = Helpers.computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

Expand Down Expand Up @@ -75,10 +75,10 @@ public static double addNumbers(double[] values) {
}

/**
* Returns an array of the first `n` fibonacci numbers starting from 1.
* Returns an array of the first `n` Fibonacci numbers starting from 1.
*
* @param n The first `n` of fibonacci values to compute.
* @return An array containing the first `n` fibonacci values.
* @param n The first `n` of Fibonacci values to compute.
* @return An array containing the first `n` Fibonacci values.
*/
public static int[] getFirstNFibonacciNumbers(int n) {
return null;
Expand All @@ -88,8 +88,8 @@ public static int[] getFirstNFibonacciNumbers(int n) {
* Finds a value in an array of values.
*
* @param values The values to search.
* @param The left most index to search.
* @param The right most index to search.
* @param start The left most index to search.
* @param end The right most index to search.
* @param value The value to look for.
* @return The index of the value if found in the array and -1 otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

public class Helpers {
/**
* Computes a score describing the lexographic distance between two strings. For two strings of
* Computes a score describing the lexicographic distance between two strings. For two strings of
* equal length, the score will be the difference between the first differing character. In the
* case where either is a sub-string of the other, then the difference in length determines the
* score.
*
* @param a The first string to compare.
* @param b The second string to compare.
* @return A score representing the lexographic distance between two strings.
* @return A score representing the lexicographic distance between two strings.
*/
public static int computeLexographicDistance(String a, String b) {
public static int computeLexicographicDistance(String a, String b) {
for (var i = 0; i < a.length(); ++i) {
if (a.charAt(i) == b.charAt(i)) {
continue;
Expand Down
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;
}
}
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());
}
}
15 changes: 15 additions & 0 deletions lesson_08/README.md
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
9 changes: 9 additions & 0 deletions lesson_08/collections/.gitattributes
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

5 changes: 5 additions & 0 deletions lesson_08/collections/.gitignore
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
Loading

0 comments on commit 6610bf0

Please sign in to comment.