Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions concepts/arithmetic-operators/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blurb": "Arithmetic operators are used to perform calculations with numeric values.",
"authors": [
"marianfoo"
],
"contributors": []
}
26 changes: 26 additions & 0 deletions concepts/arithmetic-operators/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# About

ABAP supports the usual arithmetic operators for numeric values:

- `+` adds values.
- `-` subtracts values.
- `*` multiplies values.
- `/` divides values.
- `MOD` returns the remainder of an integer division.

```abap
DATA total TYPE i.
total = 6 + 4 * 2.

DATA remaining_days TYPE i.
remaining_days = 70 MOD 22.
```

Multiplication and division are evaluated before addition and subtraction. Use parentheses when a calculation should be grouped explicitly.

```abap
DATA result TYPE i.
result = ( 6 + 4 ) * 2.
```

[arithmetic-expressions]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenarith_operators.htm
33 changes: 33 additions & 0 deletions concepts/arithmetic-operators/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Introduction

ABAP supports arithmetic operators for calculations with numeric values.

```abap
DATA day_rate TYPE p LENGTH 8 DECIMALS 2.
day_rate = rate_per_hour * 8.
```

The basic operators are:

- `+` for addition.
- `-` for subtraction.
- `*` for multiplication.
- `/` for division.
- `MOD` for the remainder of an integer division.

```abap
DATA full_months TYPE i.
full_months = floor( num_days / 22 ).

DATA remaining_days TYPE i.
remaining_days = num_days MOD 22.
```

ABAP evaluates multiplication and division before addition and subtraction. Parentheses can be used to make the intended order explicit.

```abap
DATA discounted_rate TYPE p LENGTH 8 DECIMALS 2.
discounted_rate = monthly_rate * ( 1 - discount ).
```

[arithmetic-expressions]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenarith_operators.htm
6 changes: 6 additions & 0 deletions concepts/arithmetic-operators/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenarith_operators.htm",
"description": "ABAP Arithmetic Operators"
}
]
7 changes: 7 additions & 0 deletions concepts/numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blurb": "Numbers represent integer and decimal values used in calculations.",
"authors": [
"marianfoo"
],
"contributors": []
}
17 changes: 17 additions & 0 deletions concepts/numbers/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# About

ABAP has several numeric types. The most common ones for small introductory exercises are `i` for integer values and `p` for packed decimal values.

Use `i` when a value must be a whole number, such as a count of days. Use a decimal type when a value can contain fractional digits, such as an hourly rate.

```abap
DATA hours_per_day TYPE i VALUE 8.
DATA rate_per_hour TYPE p LENGTH 8 DECIMALS 2 VALUE '89.89'.

DATA day_rate TYPE p LENGTH 8 DECIMALS 2.
day_rate = hours_per_day * rate_per_hour.
```

Packed numbers are often used in business applications when a fixed number of decimal places is required. ABAP also has decimal floating point numbers (`decfloat16` and `decfloat34`) and binary floating point numbers (`f`). Binary floating point numbers can be useful for scientific calculations, but they are not a good default for money-like examples because many decimal fractions cannot be represented exactly.

[numeric-types]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenbuiltin_types_numeric.htm
33 changes: 33 additions & 0 deletions concepts/numbers/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Introduction

ABAP has several numeric types. In this exercise, you will use two of them:

- `i` for integer values, such as a number of days.
- `p` for packed decimal values, such as an hourly rate or discount.

```abap
DATA billable_days TYPE i VALUE 22.
DATA rate_per_hour TYPE p LENGTH 8 DECIMALS 2 VALUE '89.89'.
```

Packed numbers define their maximum length and number of decimal places. Decimal literals are commonly written as text values and converted to the target decimal type by the compiler or with `CONV`.

```abap
TYPES discount_rate TYPE p LENGTH 8 DECIMALS 6.

DATA discount TYPE discount_rate.
discount = CONV discount_rate( '0.15' ).
```

ABAP provides built-in numeric functions for rounding. The `floor` function rounds down to the nearest whole number, and `ceil` rounds up to the nearest whole number.

```abap
DATA full_months TYPE i.
full_months = floor( 70 / 22 ).

DATA rounded_cost TYPE i.
rounded_cost = ceil( '14527.2' ).
```

[numeric-types]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenbuiltin_types_numeric.htm
[floor]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenfloating_point_functions.htm
10 changes: 10 additions & 0 deletions concepts/numbers/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"url": "https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenbuiltin_types_numeric.htm",
"description": "ABAP Numeric Types"
},
{
"url": "https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenfloating_point_functions.htm",
"description": "ABAP Numeric Functions"
}
]
23 changes: 23 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
],
"prerequisites": [],
"status": "wip"
},
{
"slug": "freelancer-rates",
"name": "Freelancer Rates",
"uuid": "0aff2fa7-55ea-47e9-af4a-78927d916baf",
"concepts": [
"numbers",
"arithmetic-operators"
],
"prerequisites": [
"basics"
],
"status": "wip"
}
],
"practice": [
Expand Down Expand Up @@ -453,6 +466,16 @@
"uuid": "4a11ac86-a699-43e9-922e-f75681c86fd9",
"slug": "basics",
"name": "Basics"
},
{
"uuid": "fb681c0e-2bff-464e-9f4a-66a6ba13cd58",
"slug": "numbers",
"name": "Numbers"
},
{
"uuid": "3a8b883f-a4ea-4c5c-b2db-1d285acef657",
"slug": "arithmetic-operators",
"name": "Arithmetic Operators"
}
],
"key_features": [
Expand Down
16 changes: 16 additions & 0 deletions exercises/concept/freelancer-rates/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Hints

## 1. Calculate the day rate given an hourly rate

- Use the arithmetic operators as mentioned in the introduction of this exercise.

## 2. Calculate the number of workdays given a budget

- First determine the day rate, then calculate the number of days, and finally round that number down.

## 3. Calculate the discounted rate for large projects

- Round down the result from division to get the number of full months.
- `100% - discount` equals the percentage charged after the discount is applied.
- Use `MOD`, the remainder operator, to calculate the number of days exceeding full months.
- Add the discounted month rates and full day rates and round the result up.
54 changes: 54 additions & 0 deletions exercises/concept/freelancer-rates/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Instructions

In this exercise you will be writing code to help a freelancer communicate with their clients about the prices of certain projects.
You will write a few utility methods to quickly calculate the costs for the clients.

```exercism/note
The `rate_per_hour` parameter and the `day_rate` method are related to money.
The units of measurement are money for a unit of time: hours and days respectively.
```

## 1. Calculate the day rate given an hourly rate

A client contacts the freelancer to enquire about their rates.
The freelancer explains that they **_work 8 hours a day._**
However, the freelancer knows only their hourly rates for the project.
Help them estimate a day rate given an hourly rate.

```abap
DATA(freelancer_rates) = NEW zcl_freelancer_rates( ).

freelancer_rates->day_rate( 89 ).
// => 712
```

The day rate does not need to be rounded or changed to a "fixed" precision.

## 2. Calculate the number of workdays given a fixed budget

Another day, a project manager offers the freelancer to work on a project with a fixed budget.
Given the fixed budget and the freelancer's hourly rate, help them calculate the number of days they would work until the budget is exhausted.
The result _must_ be **rounded down** to the nearest whole number.

```abap
freelancer_rates->days_in_budget(
budget = 20000
rate_per_hour = 89 ).
// => 28
```

## 3. Calculate the discounted rate for large projects

Often, the freelancer's clients hire them for projects spanning over multiple months.
In these cases, the freelancer decides to offer a discount for every full month, and the remaining days are billed at day rate.
Your excellent work-life balance means that you only work 22 days in each calendar month, so **_every month has 22 billable days._**
Help them estimate their cost for such projects, given an hourly rate, the number of billable days the project contains, and a monthly discount rate.
The discount is always passed as a number, where `42%` becomes `0.42`. The result _must_ be **rounded up** to the nearest whole number.

```abap
freelancer_rates->price_with_monthly_discount(
rate_per_hour = 89
num_days = 230
discount = '0.42' ).
// => 97972
```
69 changes: 69 additions & 0 deletions exercises/concept/freelancer-rates/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Introduction

## Numbers

ABAP has several numeric types. In this exercise, you will use two of them:

- `i` for integer values, such as a number of days.
- `p` for packed decimal values, such as an hourly rate or discount.

```abap
DATA billable_days TYPE i VALUE 22.
DATA rate_per_hour TYPE p LENGTH 8 DECIMALS 2 VALUE '89.89'.
```

Packed numbers define their maximum length and number of decimal places.
Decimal literals are commonly written as text values and converted to the target decimal type by the compiler or with `CONV`.

```abap
TYPES discount_rate TYPE p LENGTH 8 DECIMALS 6.

DATA discount TYPE discount_rate.
discount = CONV discount_rate( '0.15' ).
```

### Rounding

ABAP provides built-in numeric functions for rounding.
The `floor` function rounds down to the nearest whole number, and `ceil` rounds up to the nearest whole number.

```abap
DATA full_months TYPE i.
full_months = floor( 70 / 22 ).

DATA rounded_cost TYPE i.
rounded_cost = ceil( '14527.2' ).
```

## Arithmetic Operators

ABAP supports arithmetic operators for calculations with numeric values.

```abap
DATA day_rate TYPE p LENGTH 8 DECIMALS 2.
day_rate = rate_per_hour * 8.
```

The basic operators are:

- `+` for addition.
- `-` for subtraction.
- `*` for multiplication.
- `/` for division.
- `MOD` for the remainder of an integer division.

```abap
DATA remaining_days TYPE i.
remaining_days = num_days MOD 22.
```

ABAP evaluates multiplication and division before addition and subtraction.
Parentheses can be used to make the intended order explicit.

```abap
DATA discounted_rate TYPE p LENGTH 8 DECIMALS 2.
discounted_rate = monthly_rate * ( 1 - discount ).
```

[numeric-types]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenbuiltin_types_numeric.htm
[arithmetic-expressions]: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenarith_operators.htm
23 changes: 23 additions & 0 deletions exercises/concept/freelancer-rates/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"marianfoo"
],
"contributors": [
"mbtools"
],
"files": {
"solution": [
"zcl_freelancer_rates.clas.abap"
],
"test": [
"zcl_freelancer_rates.clas.testclasses.abap"
],
"exemplar": [
".meta/zcl_freelancer_rates.clas.abap"
]
},
"forked_from": [
"javascript/freelancer-rates"
],
"blurb": "Learn about numbers whilst helping a freelancer communicate with a project manager about day- and month rates."
}
26 changes: 26 additions & 0 deletions exercises/concept/freelancer-rates/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Design

## Learning objectives

- Know how to use integer and decimal numeric values.
- Know when an integer result is expected.
- Know how to use packed numbers for decimal calculations.
- Know how to round numbers down and up with `floor` and `ceil`.
- Know how to use arithmetic operators (`+`, `-`, `*`, `/`, `MOD`).
- Know how parentheses can make arithmetic precedence explicit.

## Out of scope

- Decimal floating point numbers.
- Binary floating point numbers.
- Parsing numeric text entered by a user.
- Converting numbers to strings.

## Concepts

- `numbers`
- `arithmetic-operators`

## Prerequisites

- `basics`
Loading