As a driver, I can see the current speed of my vehicle so that I can see if I'm within the speed limit or not.
import java.util.*; class Vehicle { final int SPEED_LIMIT = 60; int currentSpeed; public void setCurrentSpeed(int[] accelerations) { this.currentSpeed = Arrays.stream(accelerations).sum(); } public int getCurrentSpeed() { return this.currentSpeed; } public boolean isWithinSpeedLimit() { return getCurrentSpeed() <= this.SPEED_LIMIT; } }
import static java.util.stream.IntStream.of;- import java.util.*;
class SpeedLimit {static int sequence(int[] arr) {return of(arr).sum();- class Vehicle {
- final int SPEED_LIMIT = 60;
- int currentSpeed;
- public void setCurrentSpeed(int[] accelerations) {
- this.currentSpeed = Arrays.stream(accelerations).sum();
- }
}- public int getCurrentSpeed() {
- return this.currentSpeed;
- }
- public boolean isWithinSpeedLimit() {
- return getCurrentSpeed() <= this.SPEED_LIMIT;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class VehicleTest { @Test void test_currentSpeed() { Vehicle vehicle = new Vehicle(); vehicle.setCurrentSpeed(new int[]{}); // empty array assertEquals(0, vehicle.getCurrentSpeed()); vehicle.setCurrentSpeed(new int[]{1, 2, 3}); // array with positive numbers assertEquals(6, vehicle.getCurrentSpeed()); vehicle.setCurrentSpeed(new int[]{-1, -2, -3}); // array with negative numbers assertEquals(-6, vehicle.getCurrentSpeed()); vehicle.setCurrentSpeed(new int[]{1, -1, 2, -2}); // array with positive and negative numbers assertEquals(0, vehicle.getCurrentSpeed()); } @Test void test_isWithinSpeedLimit() { Vehicle vehicle = new Vehicle(); vehicle.setCurrentSpeed(new int[]{25, 25}); assertTrue(vehicle.isWithinSpeedLimit()); vehicle.setCurrentSpeed(new int[]{25, 50}); assertFalse(vehicle.isWithinSpeedLimit()); } }
- import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;- import static org.junit.jupiter.api.Assertions.*;
class SpeedLimitTest {- class VehicleTest {
- @Test
void testSequence() {assertEquals(0, SpeedLimit.sequence(new int[]{})); // empty arrayassertEquals(6, SpeedLimit.sequence(new int[]{1, 2, 3})); // array with positive numbersassertEquals(-6, SpeedLimit.sequence(new int[]{-1, -2, -3})); // array with negative numbersassertEquals(0, SpeedLimit.sequence(new int[]{1, -1, 2, -2})); // array with positive and negative numbers- void test_currentSpeed() {
- Vehicle vehicle = new Vehicle();
- vehicle.setCurrentSpeed(new int[]{}); // empty array
- assertEquals(0, vehicle.getCurrentSpeed());
- vehicle.setCurrentSpeed(new int[]{1, 2, 3}); // array with positive numbers
- assertEquals(6, vehicle.getCurrentSpeed());
- vehicle.setCurrentSpeed(new int[]{-1, -2, -3}); // array with negative numbers
- assertEquals(-6, vehicle.getCurrentSpeed());
- vehicle.setCurrentSpeed(new int[]{1, -1, 2, -2}); // array with positive and negative numbers
- assertEquals(0, vehicle.getCurrentSpeed());
- }
- @Test
- void test_isWithinSpeedLimit() {
- Vehicle vehicle = new Vehicle();
- vehicle.setCurrentSpeed(new int[]{25, 25});
- assertTrue(vehicle.isWithinSpeedLimit());
- vehicle.setCurrentSpeed(new int[]{25, 50});
- assertFalse(vehicle.isWithinSpeedLimit());
- }
- }
making it verbose and using camel case.
public class Program { public static int returnInputNumber(int num) { return num; } }
- public class Program
- {
public static int GetValue(int i)- public static int returnInputNumber(int num)
- {
return i;- return num;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void test_returnInputNumber() { Program.returnInputNumber(10); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
void testSomething() {Program.GetValue(10);- void test_returnInputNumber() {
- Program.returnInputNumber(10);
- }
- }