Browse Source

docs(person): correct grammar

DEV-4541-Go-go-reloaded-synchronous-project-reveiw
davhojt 2 years ago committed by Dav Hojt
parent
commit
7766a540fa
  1. 84
      subjects/mobile-dev/person/README.md

84
subjects/mobile-dev/person/README.md

@ -1,30 +1,32 @@
# Person
## Person
### Instructions
Create a class named `Person`.
Create a `class` named `Person`.
Its attributes:
- name - `string`
- surname - `string`
- cityOfOrigin - `string`
- age - `int`
- height - `int`
- `name`: `string`
- `surname`: `string`
- `cityOfOrigin`: `string`
- `age`: `int`
- `height`: `int`
Constructor:
- name - `required`
- cityOfOrigin - `required`
- age - `required`
- height - `required`
- surname - `optional`
- `name`: `required`
- `cityOfOrigin`: `required`
- `age`: `required`
- `height`: `required`
- `surname`: `optional`
### Object Oriented Programming
Dart supports object oriented programming which Flutter framework relies on. Classes have 2 main concepts: attributes and methods. Attributes are needed to store various data in the instance of Class and methods are functions, which can use Class attributes for various manipulations. To understand OOP and its difference from functional programming visit [this website](https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/).
Dart supports object oriented programming, and it features heavily in Flutter. Classes have 2 main concepts.
- Attributes: store data about the instance of a class.
- Methods: are func functions, which can use class attributes for various manipulations.
Here is an example of the Class in Dart:
Here is an example of a class in Dart. `Point` is the name of the class, while `x` and `y` are attributes:
```dart
class Point {
@ -33,69 +35,65 @@ class Point {
}
```
**Point** - name of the class, **x, y** - are attributes.
What if you want to initiate a Point with different x and y? To do that declare a **_Constructor_** - which is a function that specifies how to create object of a class given a set of parameters.
What if you want to initiate a `Point` with different `x` and `y`? We use constructors to solve this problem. A constructor is a function which specifies how to instantiate an instance of a class, from a set of given parameters.
```dart
class Point {
double x = 0; // attribute
double y = 0; // attribute
// Attributes
double x = 0;
double y = 0;
// Constructor
Point(double x, double y) {
this.x = x; // initializing attributes
// Initializing attributes
this.y = y;
this.x = x;
}
}
```
In Dart we can also use Constructor declaration of type **_Constructor(param1, param2...);_** thus saving a few lines of code if all we want to do is to pass arguments to values.
In Dart we can also use **constructor declaration** to save a few lines of code.
```dart
class Point {
double x = 0;
double y = 0;
double x;
double y;
Point(this.x, this.y); // "this" is meant for current class.
Point(this.x, this.y);
}
```
Now let's instantiate two objects of class Point:
Now let's instantiate two objects of class `Point`:
```dart
var p1 = Point(5, 4);
var p2 = Point(8, 3);
```
What if we would like to know what is the distance between these 2 points? We can do that by declaring a class function (or method of the class) and passing one of the objects as a parameter:
Suppose we want to know the distance between these 2 points? We can do that by declaring a method, and passing one of the objects as a parameter:
```dart
import 'dart:math';
class Point {
double x = 0; // field
double y = 0; // field
double x;
double y;
Point(double x, double y) { // constructor
this.x = x; // initializing field
this.y = y; // initializing field
Point(this.x, this.y);
// Method
double distanceTo(Point end) {
return sqrt(pow((end.x - this.x), 2) + pow((end.y - this.y), 2));
}
// method
double distanceTo(Point end) {
return sqrt(pow((end.x - this.x), 2) + pow((end.y - this.y), 2));
}
}
```
```dart
void main() {
var p1 = Point(5, 4);
var p2 = Point(8, 3);
var p1 = Point(5, 4);
var p2 = Point(8, 3);
print(p1.distanceTo(p2));
}
print(p1.distanceTo(p2));
```
- Note: please see the [documentation](https://dart.dev/guides/language/language-tour#classes) for examples.
Check out the [class](https://dart.dev/guides/language/language-tour#classes) section of the dart language tutorial.

Loading…
Cancel
Save