Skip to content

Commit c83ed3f

Browse files
authored
feat: adds Lesson 06 assignment content (code-differently#177)
* chore: sets up new project Signed-off-by: Anthony D. Mays <[email protected]> * feat: adds method stubs to implement Signed-off-by: Anthony D. Mays <[email protected]> * feat: adds additional assignment question and configures build Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds build action Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds helper method for string comparison Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds more documentation Signed-off-by: Anthony D. Mays <[email protected]> * chore: adding more test cases for gpa method Signed-off-by: Anthony D. Mays <[email protected]> * chore: refactors string function Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds more documentation Signed-off-by: Anthony D. Mays <[email protected]> * docs: adds homework details Signed-off-by: Anthony D. Mays <[email protected]> * tests: bolstering binary search test Signed-off-by: Anthony D. Mays <[email protected]> * chore: undoing less6 change Signed-off-by: Anthony D. Mays <[email protected]> * chore: adding return for binary search Signed-off-by: Anthony D. Mays <[email protected]> * docs: updates assigned reading Signed-off-by: Anthony D. Mays <[email protected]> * test: updates doco Signed-off-by: Anthony D. Mays <[email protected]> * chore: updating file organization Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds Helpers file Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]>
1 parent 800db7d commit c83ed3f

File tree

21 files changed

+769
-380
lines changed

21 files changed

+769
-380
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Check Lesson 06 Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
paths:
7+
- "lesson_06/conditionals/**"
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up JDK 17
19+
uses: actions/setup-java@v4
20+
with:
21+
java-version: '17'
22+
distribution: 'temurin'
23+
24+
# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
25+
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
26+
- name: Setup Gradle
27+
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
28+
29+
- name: Build Lesson 06 with Gradle Wrapper
30+
working-directory: ./lesson_06/conditionals
31+
run: ./gradlew check
32+

.github/workflows/check_push.yml

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
- "lesson_03/quiz/**"
1010
- "lesson_04/expression/**"
1111
- "lesson_05/types/**"
12+
- "lesson_06/conditionals/**"
1213

1314
jobs:
1415
build:
@@ -46,6 +47,10 @@ jobs:
4647
working-directory: ./lesson_05/types
4748
run: ./gradlew check
4849

50+
- name: Build Lesson 06 with Gradle Wrapper
51+
working-directory: ./lesson_06/conditionals
52+
run: ./gradlew assemble && ./gradlew spotlessCheck
53+
4954
- name: Build Shared Lib with Gradle Wrapper
5055
working-directory: ./lib/java/codedifferently-instructional
5156
run: ./gradlew check

lesson_02/quiz/.settings/org.eclipse.buildship.core.prefs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
arguments=--init-script /root/.local/share/nvim/mason/packages/jdtls/config_linux/org.eclipse.osgi/55/0/.cp/gradle/init/init.gradle
2-
auto.sync=false
1+
arguments=--init-script /var/folders/jd/13987k395q3_lnyhg9db_bc00000gn/T/d146c9752a26f79b52047fb6dc6ed385d064e120494f96f08ca63a317c41f94c.gradle --init-script /var/folders/jd/13987k395q3_lnyhg9db_bc00000gn/T/52cde0cfcf3e28b8b7510e992210d9614505e0911af0c190bd590d7158574963.gradle
2+
auto.sync=true
33
build.scans.enabled=false
44
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
55
connection.project.dir=
66
eclipse.preferences.version=1
77
gradle.user.home=
8-
java.home=/root/.sdkman/candidates/java/17-open
8+
java.home=/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
99
jvm.arguments=
1010
offline.mode=false
1111
override.workspace.settings=true

lesson_03/quiz/lesson_03_quiz/.classpath

+5-329
Large diffs are not rendered by default.

lesson_03/quiz/lesson_03_quiz/.project

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22
<projectDescription>
33
<name>lesson_03_quiz</name>
44
<comment></comment>
5-
<projects/>
6-
<natures>
7-
<nature>org.eclipse.jdt.core.javanature</nature>
8-
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
9-
</natures>
5+
<projects>
6+
</projects>
107
<buildSpec>
118
<buildCommand>
129
<name>org.eclipse.jdt.core.javabuilder</name>
13-
<arguments/>
10+
<arguments>
11+
</arguments>
1412
</buildCommand>
1513
<buildCommand>
1614
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
17-
<arguments/>
15+
<arguments>
16+
</arguments>
1817
</buildCommand>
1918
</buildSpec>
20-
<linkedResources/>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
22+
</natures>
2123
<filteredResources>
2224
<filter>
2325
<id>1</id>
26+
<name></name>
2427
<type>30</type>
25-
<name/>
2628
<matcher>
2729
<id>org.eclipse.core.resources.regexFilterMatcher</id>
2830
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>

lesson_04/expression/.settings/org.eclipse.buildship.core.prefs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
arguments=--init-script /Users/anthonymays/.local/share/nvim/mason/packages/jdtls/config_mac/org.eclipse.osgi/54/0/.cp/gradle/init/init.gradle
2-
auto.sync=false
1+
arguments=--init-script /home/vscode/.vscode-server/data/User/globalStorage/redhat.java/1.28.1/config_linux/org.eclipse.osgi/55/0/.cp/gradle/init/init.gradle --init-script /home/vscode/.vscode-server/data/User/globalStorage/redhat.java/1.28.1/config_linux/org.eclipse.osgi/55/0/.cp/gradle/protobuf/init.gradle
2+
auto.sync=true
33
build.scans.enabled=false
44
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
55
connection.project.dir=
66
eclipse.preferences.version=1
77
gradle.user.home=
8-
java.home=/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
8+
java.home=/usr/local/sdkman/candidates/java/current
99
jvm.arguments=
1010
offline.mode=false
1111
override.workspace.settings=true

lesson_06/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Lesson 06
2+
3+
## Homework
4+
5+
* Read CC 16, 19; PIE 8
6+
* Complete [Conditionals and Loops](#choosing-the-right-data-types) exercise.
7+
8+
## Conditionals and Loops
9+
10+
This exercise will provide you ample opportunities to practice your understanding of conditional expressions and loops. To complete this assignment, implement the functions in [Lesson6.java][lesson6-file], ensure the tests pass, and submit a PR. Remember to use the [Conventional Commits][conventional-commits] spec for your commit messages and pull requests.
11+
12+
[lesson6-file]: ./conditionals/conditionals_app/src/main/java/com/codedifferently/lesson6/Lesson6.java
13+
[conventional-commits]: https://www.conventionalcommits.org/en/v1.0.0/

lesson_06/conditionals/.gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

lesson_06/conditionals/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
plugins {
2+
// Apply the application plugin to add support for building a CLI application in Java.
3+
application
4+
eclipse
5+
id("com.diffplug.spotless") version "6.25.0"
6+
id("org.springframework.boot") version "3.2.2"
7+
id("com.adarshr.test-logger") version "4.0.0"
8+
}
9+
10+
apply(plugin = "io.spring.dependency-management")
11+
12+
repositories {
13+
// Use Maven Central for resolving dependencies.
14+
mavenCentral()
15+
}
16+
17+
dependencies {
18+
// Use JUnit Jupiter for testing.
19+
testImplementation("com.codedifferently.instructional:instructional-lib")
20+
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
21+
testImplementation("org.springframework.boot:spring-boot-starter-test")
22+
testImplementation("org.assertj:assertj-core:3.25.1")
23+
testImplementation("at.favre.lib:bcrypt:0.10.2")
24+
25+
// This dependency is used by the application.
26+
implementation("com.codedifferently.instructional:instructional-lib")
27+
implementation("com.google.guava:guava:31.1-jre")
28+
implementation("com.google.code.gson:gson:2.10.1")
29+
implementation("org.projectlombok:lombok:1.18.30")
30+
implementation("org.springframework.boot:spring-boot-starter")
31+
}
32+
33+
application {
34+
// Define the main class for the application.
35+
mainClass.set("com.codedifferently.lesson6.Lesson6")
36+
}
37+
38+
tasks.named<Test>("test") {
39+
// Use JUnit Platform for unit tests.
40+
useJUnitPlatform()
41+
}
42+
43+
44+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
45+
46+
format("misc", {
47+
// define the files to apply `misc` to
48+
target("*.gradle", ".gitattributes", ".gitignore")
49+
50+
// define the steps to apply to those files
51+
trimTrailingWhitespace()
52+
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
53+
endWithNewline()
54+
})
55+
56+
java {
57+
// don't need to set target, it is inferred from java
58+
59+
// apply a specific flavor of google-java-format
60+
googleJavaFormat()
61+
// fix formatting of type annotations
62+
formatAnnotations()
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.codedifferently.lesson6;
2+
3+
import com.codedifferently.lesson6.util.Helpers;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
@SpringBootApplication(scanBasePackages = "com.codedifferently")
10+
public class Lesson6 {
11+
12+
public static void main(String[] args) {
13+
var application = new SpringApplication(Lesson6.class);
14+
application.run(args);
15+
}
16+
17+
/**
18+
* Returns true if the provided age meets the minimum US voting age and false otherwise.
19+
*
20+
* @param age The age to check.
21+
* @return True if the age corresponds to a voting age and false otherwise.
22+
*/
23+
public static boolean canVote(int age) {
24+
return false;
25+
}
26+
27+
/**
28+
* Compares two strings lexographically.
29+
*
30+
* @param value1 The first `String` to compare.
31+
* @param value2 The second `String` to compare.
32+
* @rerturn -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
33+
*/
34+
public static int compareStrings(String a, String b) {
35+
// The distance will be a number less than 0 if string `a` is lexographically less than `b`, 1
36+
// if it is greater, and 0 if the strings are equal.
37+
int distance = Helpers.computeLexographicDistance(a, b);
38+
39+
// TODO(you): Finish this method.
40+
41+
return 0;
42+
}
43+
44+
/**
45+
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
46+
* scale. See
47+
* https://bigfuture.collegeboard.org/plan-for-college/college-basics/how-to-convert-gpa-4.0-scale
48+
* for details.
49+
*
50+
* @param gpa The GPA value.
51+
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
52+
*/
53+
public static String convertGpaToLetterGrade(double gpa) {
54+
return "F";
55+
}
56+
57+
/**
58+
* Computes the factorial of the given value of `n`.
59+
*
60+
* @param n The value for which to compute the factorial.
61+
* @return The factorial of n.
62+
*/
63+
public static int computeFactorial(int n) {
64+
return 0;
65+
}
66+
67+
/**
68+
* Adds all of the provided values and returns the sum.
69+
*
70+
* @param values The values to sum.
71+
* @return The sum of all the values.
72+
*/
73+
public static double addNumbers(double[] values) {
74+
return 0;
75+
}
76+
77+
/**
78+
* Returns an array of the first `n` fibonacci numbers starting from 1.
79+
*
80+
* @param n The first `n` of fibonacci values to compute.
81+
* @return An array containing the first `n` fibonacci values.
82+
*/
83+
public static int[] getFirstNFibonacciNumbers(int n) {
84+
return null;
85+
}
86+
87+
/**
88+
* Finds a value in an array of values.
89+
*
90+
* @param values The values to search.
91+
* @param The left most index to search.
92+
* @param The right most index to search.
93+
* @param value The value to look for.
94+
* @return The index of the value if found in the array and -1 otherwise.
95+
*/
96+
public static int binarySearch(int[] values, int start, int end, int value) {
97+
if (end < start) {
98+
//
99+
return -1;
100+
}
101+
102+
int pivotIndex = (start + end) / 2; // The index in the middle of the array.
103+
104+
// TODO(you): Finish implementing this algorithm
105+
106+
// If values[pivotIndex] is equal to value then return `pivotIndex`.
107+
// Else if values[pivotIndex] is greater than the value, then
108+
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
109+
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
110+
return -1;
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codedifferently.lesson6.util;
2+
3+
public class Helpers {
4+
/**
5+
* Computes a score describing the lexographic distance between two strings. For two strings of
6+
* equal length, the score will be the difference between the first differing character. In the
7+
* case where either is a sub-string of the other, then the difference in length determines the
8+
* score.
9+
*
10+
* @param a The first string to compare.
11+
* @param b The second string to compare.
12+
* @return A score representing the lexographic distance between two strings.
13+
*/
14+
public static int computeLexographicDistance(String a, String b) {
15+
for (var i = 0; i < a.length(); ++i) {
16+
if (a.charAt(i) == b.charAt(i)) {
17+
continue;
18+
}
19+
return (int) a.charAt(i) - (int) b.charAt(i);
20+
}
21+
22+
if (a.length() != b.length()) {
23+
return a.length() - b.length();
24+
}
25+
26+
return 0;
27+
}
28+
}

0 commit comments

Comments
 (0)