Skip to content

Commit d69620a

Browse files
TalesDiasgithub-actions[bot]Silvamirkoperillo
committed
Implement concept exercise numbers
* Implement concept exercise numbers * [CI] Format code * Checked code style * Meta Improvments * update config.json * style review * fix config.json Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Silva <silva@localhost.localdomain> Co-authored-by: mirkoperillo <mirko.perillo@gmail.com>
1 parent 7f7eda1 commit d69620a

10 files changed

Lines changed: 343 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
One of the key aspects of working with numbers in Java is the distinction between integers (numbers with no digits after the decimal separator) and floating-point numbers (numbers with zero or more digits after the decimal separator).
2+
3+
Java has other [datatypes][numeric-datatypes] apart from `int` and `double`
4+
5+
```java
6+
//8-Bit Integer
7+
byte a = 127;
8+
9+
//16-Bit Integer
10+
short b = 262143;
11+
12+
//64-Bit Integer
13+
long d = 18446744073709551999L;
14+
15+
//32-bit Single-Precision Floating-Point
16+
float e = 5409.29f;
17+
```
18+
19+
Both integers and floating-point numbers can use the `_` character as a _digit separator_, which can help when defining large numbers:
20+
21+
```java
22+
int largeInt = 1_000_000;
23+
// => 1000000
24+
25+
double largeDouble = 9_876_543.21;
26+
// => 9876543.21
27+
```
28+
29+
Arithmetic is done using the standard [arithmetic operators][arithmetic-operators] (`+`, `-`, `*`, etc.). Numbers can be compared using the standard [comparison operators][comparison-operators] (`<`, `>=`, etc.) along with the equality operator (`==`) and inequality operator (`!=`).
30+
31+
```java
32+
5 * 6
33+
// => 30
34+
35+
1.2 > 0.8
36+
// => true
37+
38+
120 == 100
39+
// => false
40+
41+
2 != 4
42+
// => true
43+
```
44+
45+
When converting between numeric types, there are two types of numeric conversions:
46+
47+
1. Implicit conversions: no data will be lost and no additional syntax is required.
48+
2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required.
49+
50+
As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an [implicit conversion][type-casting]. However, converting from a `double` to an `int` could mean losing data, so that requires an [explicit conversion][type-casting].
51+
52+
```java
53+
int i = 9;
54+
double d = 2.72;
55+
56+
// Safe conversion, thus implicit conversion
57+
double fromInt = i;
58+
59+
// Potentially unsafe conversion, thus explicit conversion
60+
int fromDouble = (int)d;
61+
```
62+
63+
An `if` statement can be used to conditionally execute code. The condition of an `if` statement must be of type `boolean`. Java has no concept of _truthy_ values.
64+
65+
```java
66+
int x = 6;
67+
68+
if (x == 5){
69+
// Execute logic if x equals 5
70+
} else if (x > 7){
71+
// Execute logic if x greater than 7
72+
} else{
73+
// Execute logic in all other cases
74+
}
75+
```
76+
77+
[arithmetic-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
78+
[comparison-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
79+
[type-casting]: https://www.programiz.com/java-programming/typecasting
80+
[numeric-datatypes]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## General
2+
3+
- [Numbers tutorial][numbers].
4+
5+
## 1. Calculate the production rate per second
6+
7+
- Determining the success rate can be done through a [conditional statement][if-statement].
8+
- Java allows for multiplication to be applied to two different number types (such as an `int` and a `double`). It will automatically return the "broader" data type.
9+
- Numbers can be compared using the built-in [comparison][comparison-operators] and [equality operators][comparison-operators].
10+
11+
## 2. Calculate the number of working items produced per second
12+
13+
- Whereas an `int` can be automatically converted to a `double`, the reverse does not hold. The reason for this is that an `int` has less precision than a `double` so rounding has to be applied, also the range of numbers an `int` can represent is smaller than a `double`. To force this conversion you can use a [cast to an int][cast-int].
14+
15+
[cast-int]: https://www.w3schools.com/java/java_type_casting.asp
16+
[numbers]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
17+
[if-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
18+
[comparison-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from `0` (off) to `10` (maximum).
2+
3+
At its lowest speed (`1`), `221` cars are produced each hour. The production increases linearly with the speed. So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:
4+
5+
- `1` to `4`: 100% success rate.
6+
- `5` to `8`: 90% success rate.
7+
- `9`: 80% success rate.
8+
- `10`: 77% success rate.
9+
10+
You have two tasks.
11+
12+
## 1. Calculate the production rate per hour
13+
14+
Implement the `AssemblyLine.productionRatePerHour()` method to calculate the assembly line's production rate per hour, taking into account its current assembly line's speed :
15+
16+
```Java
17+
AssemblyLine.productionRatePerHour(6)
18+
// => 1193.4
19+
```
20+
21+
Note that the value returned is a `double`.
22+
23+
## 2. Calculate the number of working items produced per minute
24+
25+
Implement the `AssemblyLine.workingItemsPerMinute()` method to calculate how many working cars are produced per minute:
26+
27+
```Java
28+
AssemblyLine.workingItemsPerMinute(6)
29+
// => 19
30+
```
31+
32+
Note that the value returned is an `int`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
There are two different types of numbers in Java:
2+
3+
- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `-500000`.
4+
- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-20.4`, `0.1`, `2.72`, `16.984025` and `1024.0`.
5+
6+
The two most common numeric types in Java are `int` and `double`. An `int` is a 32-bit integer and a `double` is a 64-bit floating-point number.
7+
8+
Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators and the equality (`==`) and inequality (`!=`) operators.
9+
10+
Java has two types of numeric conversions:
11+
12+
1. Implicit conversions: no data will be lost and no additional syntax is required.
13+
2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required.
14+
15+
As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion.
16+
17+
In this exercise you must conditionally execute logic. The most common way to do this in Java is by using an `if/else` statement:
18+
19+
```java
20+
int x = 6;
21+
22+
if (x == 5) {
23+
// Execute logic if x equals 5
24+
} else if (x > 7) {
25+
// Execute logic if x greater than 7
26+
} else {
27+
// Execute logic in all other cases
28+
}
29+
```
30+
31+
The condition of an `if` statement must be of type `boolean`. Java has no concept of _truthy_ values.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "TalesDias",
5+
"exercism_username": "TalesDias"
6+
}
7+
],
8+
"forked_from": ["csharp/numbers"]
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Learning objectives
2+
3+
- Know of the existence of the two most commonly used number types, `int` and `double`.
4+
- Understand that an `int` represents whole numbers, and a `double` represents floating-point numbers.
5+
- Know of basic operators such as multiplication, comparison and equality.
6+
- Know how to convert from one numeric type to another; know what implicit and explicit conversions are.
7+
- Know how to conditionally execute code using an `if` statement.
8+
9+
## Out of scope
10+
11+
- Any other numeric types besides `int` and `double` (so no `float`, `byte`, etc.).
12+
- Parsing a `string` to an `int` or `double`.
13+
- Converting an `int` or `double` to a `string`.
14+
15+
## Concepts
16+
17+
- `numbers`: know of the existence of the two most commonly used number types, `int` and `double`; understand that the former represents whole numbers, and the latter floating-point numbers; know of basic operators such as multiplication, comparison and equality; know how to convert from one numeric type to another; know what implicit and explicit conversions are.
18+
- `conditionals`: know how to conditionally execute code using an `if` statement.
19+
20+
## Prerequisites
21+
22+
This exercise's prerequisites Concepts are:
23+
24+
- `basics`: know how to define methods.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
public class AssemblyLine {
3+
4+
private final int defaultProductionRate = 221;
5+
6+
public double productionRatePerHour(int speed) {
7+
return productionRatePerHourForSpeed(speed) * successRate(speed);
8+
}
9+
10+
public int workingItemsPerMinute(int speed) {
11+
return (int) (productionRatePerHour(speed) / 60);
12+
}
13+
14+
private int productionRatePerHourForSpeed(int speed) {
15+
return defaultProductionRate * speed;
16+
}
17+
18+
private double successRate(int speed) {
19+
if (speed == 10) {
20+
return 0.77;
21+
}
22+
23+
if (speed == 9) {
24+
return 0.8;
25+
}
26+
27+
if (speed >= 5) {
28+
return 0.9;
29+
}
30+
31+
return 1;
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.13"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
12+
}
13+
14+
test {
15+
testLogging {
16+
exceptionFormat = 'short'
17+
showStandardStreams = true
18+
events = ["passed", "failed", "skipped"]
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class AssemblyLine {
2+
3+
public double productionRatePerHour(int speed) {
4+
throw new UnsupportedOperationException("Please implement the AssemblyLine.productionRateperHour() method");
5+
}
6+
7+
public int workingItemsPerMinute(int speed) {
8+
throw new UnsupportedOperationException("Please implement the AssemblyLine.workingItemsPerMinute() method");
9+
}
10+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import org.junit.Before;
2+
import org.junit.Test;
3+
import org.junit.Ignore;
4+
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
8+
public class AssemblyLineTests {
9+
10+
private AssemblyLine assemblyLine;
11+
12+
@Before
13+
public void setUp() {
14+
assemblyLine = new AssemblyLine();
15+
}
16+
17+
@Test
18+
public void productionRatePerHourForSpeedZero() {
19+
assertThat(assemblyLine.productionRatePerHour(0)).isEqualTo(0.0);
20+
}
21+
@Test
22+
@Ignore("Remove to run test")
23+
public void productionRatePerHourForSpeedOne() {
24+
assertThat(assemblyLine.productionRatePerHour(1)).isEqualTo(221.0);
25+
}
26+
27+
@Test
28+
@Ignore("Remove to run test")
29+
public void productionRatePerHourForSpeedFour() {
30+
assertThat(assemblyLine.productionRatePerHour(4)).isEqualTo(884.0);
31+
}
32+
33+
@Test
34+
@Ignore("Remove to run test")
35+
public void productionRatePerHourForSpeedSeven() {
36+
assertThat(assemblyLine.productionRatePerHour(7)).isEqualTo(1392.3);
37+
}
38+
39+
@Test
40+
@Ignore("Remove to run test")
41+
public void productionRatePerHourForSpeedNine() {
42+
assertThat(assemblyLine.productionRatePerHour(9)).isEqualTo(1591.2);
43+
}
44+
45+
@Test
46+
@Ignore("Remove to run test")
47+
public void productionRatePerHourForSpeedTen() {
48+
assertThat(assemblyLine.productionRatePerHour(10)).isEqualTo(1701.7);
49+
}
50+
51+
@Test
52+
@Ignore("Remove to run test")
53+
public void workingItemsPerMinuteForSpeedZero() {
54+
assertThat(assemblyLine.workingItemsPerMinute(0)).isEqualTo(0);
55+
}
56+
57+
@Test
58+
@Ignore("Remove to run test")
59+
public void workingItemsPerMinuteForSpeedOne() {
60+
assertThat(assemblyLine.workingItemsPerMinute(1)).isEqualTo(3);
61+
}
62+
63+
@Test
64+
@Ignore("Remove to run test")
65+
public void workingItemsPerMinuteForSpeedFive() {
66+
assertThat(assemblyLine.workingItemsPerMinute(5)).isEqualTo(16);
67+
}
68+
69+
@Test
70+
@Ignore("Remove to run test")
71+
public void workingItemsPerMinuteForSpeedEight() {
72+
assertThat(assemblyLine.workingItemsPerMinute(8)).isEqualTo(26);
73+
}
74+
75+
@Test
76+
@Ignore("Remove to run test")
77+
public void workingItemsPerMinuteForSpeedNine() {
78+
assertThat(assemblyLine.workingItemsPerMinute(9)).isEqualTo(26);
79+
}
80+
81+
@Test
82+
@Ignore("Remove to run test")
83+
public void workingItemsPerMinuteForSpeedTen() {
84+
assertThat(assemblyLine.workingItemsPerMinute(10)).isEqualTo(28);
85+
}
86+
}

0 commit comments

Comments
 (0)