Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds lesson_04 content #125

Merged
merged 21 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lesson_04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Lesson 04

## Homework

1. Read CC 12.
1. Read article entitled [3 Questions That Will Make You A Phenomenal Rubber Duck][article-link]
1. Complete the [Expression Calculator](#expression-calculator) exercise

## Expression Calculator

For this assignment, you will need to implement the functions and logic required to calculate a mathematical expression. After implementing the `add`, `divide`, and `multiply` functions, you will combine these functions to compute the final result.

1. Update the code in the [ExpressionCalculator.java][calculator-file] file.
2. To check your work, you can run the application using the first command below and run the tests using the second one.
```bash
./gradlew run --console=plain
./gradlew test
```
3. As usual, make sure that you format your code and run the check command before creating your pull request.
```bash
./gradlew spotlessApply
./gradlew check
```
4. You must only submit changes to the `ExpressionCalculator.java` file to receive full credit.

[article-link]: https://blog.danslimmon.com/2024/01/18/3-questions-that-will-make-you-a-phenomenal-rubber-duck/
[calculator-file]: ./expression/expression_app/src/main/java/com/codedifferently/lesson4/ExpressionCalculator.java
9 changes: 9 additions & 0 deletions lesson_04/expression/.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_04/expression/.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
28 changes: 28 additions & 0 deletions lesson_04/expression/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>lesson_04</name>
<comment>Project quiz created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1708474157606</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
13 changes: 13 additions & 0 deletions lesson_04/expression/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=--init-script /Users/anthonymays/.local/share/nvim/mason/packages/jdtls/config_mac/org.eclipse.osgi/54/0/.cp/gradle/init/init.gradle
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
24 changes: 24 additions & 0 deletions lesson_04/expression/expression_app/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="src" path="/instructional-lib">
<attributes>
<attribute name="without_test_code" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin/default"/>
</classpath>
34 changes: 34 additions & 0 deletions lesson_04/expression/expression_app/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>expression_app</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=..
eclipse.preferences.version=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.classpath.outputOverlappingAnotherSource=ignore
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
org.eclipse.jdt.core.compiler.source=17
66 changes: 66 additions & 0 deletions lesson_04/expression/expression_app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
eclipse
id("com.diffplug.spotless") version "6.25.0"
id("org.springframework.boot") version "3.2.2"
}

apply(plugin = "io.spring.dependency-management")

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation("com.codedifferently.instructional:instructional-lib")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.assertj:assertj-core:3.25.1")
testImplementation("at.favre.lib:bcrypt:0.10.2")

// This dependency is used by the application.
implementation("com.codedifferently.instructional:instructional-lib")
implementation("com.google.guava:guava:31.1-jre")
implementation("org.projectlombok:lombok:1.18.30")
implementation("org.springframework.boot:spring-boot-starter")
}

application {
// Define the main class for the application.
mainClass.set("com.codedifferently.lesson4.Lesson4")
}

tasks.named<JavaExec>("run") {
standardInput = System.`in`
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}


configure<com.diffplug.gradle.spotless.SpotlessExtension> {

format("misc", {
// define the files to apply `misc` to
target("*.gradle", ".gitattributes", ".gitignore")

// define the steps to apply to those files
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
})

java {
// don't need to set target, it is inferred from java

// apply a specific flavor of google-java-format
googleJavaFormat()
// fix formatting of type annotations
formatAnnotations()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codedifferently.lesson4;

public class ExpressionCalculator {
/**
* Returns the calculation of ((a + b) * c) / d^e
*
* @param a
* @param b
* @return
*/
public double calculate(double a, double b, double c, double d, double e) {
// Implement your code here to return the correct value.
return 0d;
}

public double pow(double base, double exponent) {
return Math.pow(base, exponent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.codedifferently.lesson4;

import java.util.Scanner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication(scanBasePackages = "com.codedifferently")
public class Lesson4 implements CommandLineRunner {

public static void main(String[] args) {
// To test, use `./gradlew run --console=plain`
var application = new SpringApplication(Lesson4.class);
application.run(args);
}

public void run(String... args) {
// Use Scanner to get input from the user.
var scanner = new Scanner(System.in);

System.out.print("\nEnter a value for variable 'a': ");
int a = scanner.nextInt();

System.out.print("Enter a value for variable 'b': ");
int b = scanner.nextInt();

System.out.print("Enter a value for variable 'c': ");
int c = scanner.nextInt();

System.out.print("Enter a value for variable 'd': ");
int d = scanner.nextInt();

System.out.print("Enter a value for variable 'e': ");
int e = scanner.nextInt();

// Close the scanner to avoid memory leaks.
scanner.close();

System.out.println(
String.format("\nNow computing the value of ((%s + %s) * %s) / %s^%s", a, b, c, d, e));

var calculator = new ExpressionCalculator();
double result = calculator.calculate(a, b, c, d, e);

System.out.println(String.format("The result is: %s", result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.codedifferently.lesson4;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Method;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class Lesson4Test {

private ExpressionCalculator calculator;

@BeforeEach
void setUp() {
calculator = new ExpressionCalculator();
}

@Test
void testCalculate() {
// Arrange
var a = 1;
var b = 2;
var c = 3;
var d = 4;
var e = 5;
var expected = ((a + b) * c) / Math.pow(d, e);

// Act
double actual = calculator.calculate(a, b, c, d, e);

// Assert
assertThat(actual).isEqualTo(expected);
}

@Test
void testAdd_works() throws Exception {
// Arrange
Method method = calculator.getClass().getDeclaredMethod("add", double.class, double.class);

// Act
var result = (double) method.invoke(calculator, 1, 2);

// Assert
assertThat(result).isEqualTo(3);
}

@Test
void testMultipy_works() throws Exception {
// Arrange
Method method = calculator.getClass().getDeclaredMethod("multiply", double.class, double.class);

// Act
var result = (double) method.invoke(calculator, 1, 2);

// Assert
assertThat(result).isEqualTo(2);
}

@Test
void testDivide_works() throws Exception {
// Arrange
Method method = calculator.getClass().getDeclaredMethod("divide", double.class, double.class);

// Act
var result = (double) method.invoke(calculator, 1, 2);

// Assert
assertThat(result).isEqualTo(0.5d);
}

@Test
void testPow_works() throws Exception {
// Arrange
Method method = calculator.getClass().getDeclaredMethod("pow", double.class, double.class);

// Act
var result = (double) method.invoke(calculator, 2, 3);

// Assert
assertThat(result).isEqualTo(8);
}
}
Binary file not shown.
6 changes: 6 additions & 0 deletions lesson_04/expression/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading