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

Wip-Implement_new_Concept_Exercise-basics_379 #563

Closed
Closed
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
89 changes: 89 additions & 0 deletions concepts/basics/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# About

Functions, methods and classes allows you to group code into a reusable unit. We are focusing on on methods, which
are functions that attached to a class. You'll learn more about classes concepts later on.

## Variables and values

All parameters must be explicitly typed; there is no type inference for parameters.
It is good practice to initialize values right after the declaration.

## Arithmetics operators

All arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified. Examples are `*`, `+` or `/`.

```cpp
int i = 6;
int j = 7;
int addition = i + j; // addition is 13
int multiplication = i * j; // multiplication is 42
```

## Methods

Methods in the same class can be called without any prefixes. They differ by return type, name and the number of parameters and their types. If you call a method you have to match all of these.

```cpp
// A definition of a method that returns and integer and has three integer arguments:
int myFormula(int a, int b, int c) {
int x = a * b;
int m = -c;
return m*x;
}

int intermediate = myFormula(1, 3, 52);
int final_result = myFormula(10, intermediate, 11);
```

## A word on classes

C++ code is often divided into a header file, that ends in `.h` and the implementation in the `.cpp` file.
The definition of the class can tell you, which methods it has and how you can use them.

```cpp
namespace bikeInfo {
class ShigeruSpecial {
public:
static int getGears();
int getID();
};
}
```
The code above does not tell you about the implementation details, but you know that the `ShigeruSpecial` bike has
a `getGears` and a `getID` method. Both return an integer. Both have the `public` modifier, meaning they can be accessed from code outside the `ShigeruSpecial` class. Another information is the `static` modifier. It tells you, that the `getGears` method can be invoked without creating an object of the class first. All those bikes have the same amount of gears, but every bike has its own ID.

## Invoking methods

Invoking a function is done by specifying the function's name and passing arguments for each of the function's parameters in parenthesis. It is also possible to have function and methods without parameters. Class methods
can be called with the `.` operator with the object, or with the `::` operator, when you do not want to create
an object for a static method.

When you are writing methods in a class you can invoke other class methods without the `::` operator.

```cpp

using namespace bikeInfo;

// calling the static method directly
int gearNumber = ShigeruSpecial::getGears();

// Creating the object and calling the method on the object
ShigeruSpecial bike = new ShigeruSpecial();
int gearsFromObject = bike.gearNumber();

// this way you can also call non-static methods:
int id = bike.getID();
```

## Comments

C++ has single line comments that start with `//` and continue until the end of the line. There are also multi-line comments as shown below:

```cpp
int myInteger = -1; // Comments can be in the same line as instructions

/* Multi-line comments
do not end
until they are closed via:
*/
```
24 changes: 24 additions & 0 deletions exercises/concept/lasagna-master/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hints

## General

- Don't forget the `;` after each statement.
- You can define an integer with `int myInteger = 44;`.

## 1. Get the time that is spend baking.

- The introduction has information on the time for the recipe.
- A method can return a value in the form of `return 2;`

## 2. Get the preparation time.

- You can access the number of layers by using the respective parameter.
- You can do calculations when you define an integer: `int myInteger = 3 + myOtherInteger;`

## 3. Calculate the time left in the oven

- You can call other methods and use them like variables: `return 22 * myOtherCalculation();`

## 4. Calculate the total time spend for the lasagna

- The output should combine the time for the preparation and the time the lasagna has already spent in the oven.
42 changes: 42 additions & 0 deletions exercises/concept/lasagna-master/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Instructions

In this exercise you'll be automating some timing tasks for your weekly lasagna cooking session.
As you vary your recipes often, you want to know how much time you have to spend for different
lasagna types.

You want to write a program to show you the time the lasagna has to be in the oven, how much time
you will have preparing and also how much time is left until the lasagna is ready.

All you lasagna recipes need `40` minutes in the oven and the preparation takes `2` minutes per layer.

## 1. Get the time that is spend baking.

Implement the `lasagna::ovenTime` method to return a the minutes the lasagna has to stay in the oven.

## 2. Get the preparation time.

Implement the `lasagna::preparationTime` method to return the preparation time based on the number of layers.

```cpp
lasagna::preparationTime(11)
// => 22
```

## 3. Calculate the time left in the oven

Implement the `lasagna::remainingOvenTime` method that takes the number of minutes the lasagna has already spend in the oven and
returns the remaining minutes.

```cpp
`lasagna::remainingOvenTimet(4)
// => 36
```

## 4. Calculate the total time spend for the lasagna

Implement the `lasagna::elapsedTime` method that takes the number of layers and the number of minutes the lasagna has already spend in the oven and returns the total time spend preparing and waiting for the oven to finish.

```cpp
`lasagna::elapsedTime(40, 20)
// => 100
```
114 changes: 114 additions & 0 deletions exercises/concept/lasagna-master/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Introduction

Functions, methods and classes allows you to group code into a reusable unit. We are focusing on on methods, which
are functions that attached to a class. You'll learn more about classes concepts later on.

## Variables and values

All parameters must be explicitly typed; there is no type inference for parameters.
It is good practice to initialize values right after the declaration.

## Arithmetics operators

All arithmetic operators compute the result of specific arithmetic operation and returns its result. The arguments are not modified. Examples are `*`, `+` or `/`.

```cpp
int i = 6;
int j = 7;
int addition = i + j; // addition is 13
int multiplication = i * j; // multiplication is 42
```

## Methods

Methods in the same class can be called without any prefixes. They differ by return type, name and the number of parameters and their types. If you call a method you have to match all of these.

```cpp
// A definition of a method that returns and integer and has three integer arguments:
int myFormula(int a, int b, int c) {
int x = a * b;
int m = -c;
return m*x;
}

int intermediate = myFormula(1, 3, 52);
int final_result = myFormula(10, intermediate, 11);
```

## Parameters vs. Arguments

```cpp
int secretFormula(int first, int second) {
int secret = 14;
return first * secret - second;
}
```
Let's quickly cover two terms that are often confused together: `parameters` and `arguments`.
Method parameters are the names defined in the function's signature, such as `first` and `second` in the function `secretFormula` above.
The arguments are the concrete values passed to the method parameters when we invoke the function.
For instance, in the example below, `11` and `-4` are the arguments passed to the `first` and `second` parameters:

```cpp
secretFormula(11, 1-4);
```

## Return Values

Values are returned to the calling code from functions using the `return` keyword.
The execution of the function ends as soon as it hits one of those `return` statements.
The type of the return value is defined by the type in front of the method's name. In the
case above we know that `secretFormula` will return an integer. The hello-world example was
a function that returned a `string`.

## A word on classes

C++ code is often divided into a header file, that ends in `.h` and the implementation in the `.cpp` file.
The definition of the class can tell you, which methods it has and how you can use them.

```cpp
namespace bikeInfo {
class ShigeruSpecial {
public:
static int getGears();
int getID();
};
}
```
The code above does not tell you about the implementation details, but you know that the `ShigeruSpecial` bike has
a `getGears` and a `getID` method. Both return an integer. Both have the `public` modifier, meaning they can be accessed from code outside the `ShigeruSpecial` class. Another information is the `static` modifier. It tells you, that the `getGears` method can be invoked without creating an object of the class first. All those bikes have the same amount of gears, but every bike has its own ID.

## Invoking methods

Invoking a function is done by specifying the function's name and passing arguments for each of the function's parameters in parenthesis. It is also possible to have function and methods without parameters. Class methods
can be called with the `.` operator with the object, or with the `::` operator, when you do not want to create
an object for a static method.

When you are writing methods in a class you can invoke other class methods without the `::` operator.

```cpp

using namespace bikeInfo;

// calling the static method directly
int gearNumber = ShigeruSpecial::getGears();

// Creating the object and calling the method on the object
ShigeruSpecial bike = new ShigeruSpecial();
int gearsFromObject = bike.gearNumber();

// this way you can also call non-static methods:
int id = bike.getID();
```

## Comments

C++ has single line comments that start with `//` and continue until the end of the line. There are also multi-line comments as shown below:

```cpp
int myInteger = -1; // Comments can be in the same line as instructions

/* Multi-line comments
do not end
until they are closed via:
*/
```
11 changes: 11 additions & 0 deletions exercises/concept/lasagna-master/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"authors": [
{
"github_username": "vaeng",
"exercism_username": "vaeng"
}
],
"forked_from": [
"go/lasagna"
]
}
36 changes: 36 additions & 0 deletions exercises/concept/lasagna-master/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Learning objectives

- Know what a variable is.
- Know how to define a variable.
- Know how to update a variable.
- Know how to use type inference for variables.
- Know how to define a method.
- Know how to return a value from a method.
- Know how to call a method.
- Know that methods must be defined in classes.
- Know about the public access modifier.
- Know about the static modifier.
- Know how to define an integer.
- Know how to use mathematical operators on integers.
- Know how to define single- and multiline comments.


## Out of scope

- Naming rules for identifiers.
- Generic values.
- Memory and performance characteristics.
- Method overloads.
- Lambda's.
- Classes.
- Organizing methods in namespaces.
- Visibility.


## Concepts

- `basics`: know what a variable is; know how to define a variable; know how to update a variable; know how to use type inference for variables; know how to define a method; know how to return a value from a method; know how to call a method; know that methods must be defined in classes; know about the `public` access modifier; know about the `static` modifier; know how to defined an integer; know how to use mathematical operators on integers; know how to define single and multiline comments.

## Prerequisites

- There are no prerequisites.
28 changes: 28 additions & 0 deletions exercises/concept/lasagna-master/.meta/exemplar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "lasagna_master.h"

namespace lasagna_master {
// ovenTime returns the amount in minutes that the lasagna should stay in the oven.
int Lasagna::ovenTime() {
return 40;
}

/* PreparationTim estimates the preparation time based on the number
of layers and an average time per layer and returns it.
*/
int Lasagna::preparationTime(int numberOfLayers) {
int prepTimePerLayer = 2;
return numberOfLayers*prepTimePerLayer;
}

/* RemainingOvenTime returns the remaining
minutes based on the actual minutes already in the oven.
*/
int Lasagna::remainingOvenTime(int actualMinutesInOven) {
return ovenTime()-actualMinutesInOven;
}

// ElapsedTime calculates the total time spend to create and bake the Lasagna so far.
int Lasagna::elapsedTime(int numberOfLayers, int actualMinutesInOven) {
return preparationTime(numberOfLayers) + actualMinutesInOven;
}
} // namespace lasagna_master
21 changes: 21 additions & 0 deletions exercises/concept/lasagna-master/.meta/exemplar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if !defined(LASAGNA_MASTER_H_)
#define LASAGNA_MASTER_H_

/* IMPORTANT:
To solve this exercise you do not need to edit
the contents of this file.
*/


namespace lasagna_master {
class lasagna {
public:
static int ovenTime();
static int preparationTime(int numberOfLayers);
static int remainingOvenTime(int actualMinutesInOven);
static int elapsedTime(int numberOfLayers, int actualMinutesInOven);
};

} // namespace lasagna_master

#endif
Loading