Dart Hybrid Exercises
# Exercises
These are the Hybrid exercises for MAD9135. See BS LMS for the exact due dates.
I would suggest that you read through all the material and do these simple exercises as soon as possible. Don't wait until the React Native material gets harder or we start the Flutter section of the course. You do NOT need to wait until the due date before submitting these exercises.
If you have ANY questions about these exercises, please either ask in class or post them on the #mad9135 channel on Slack so that everyone can see the answers.
The full reference for all the Dart core and built-in classes is here (opens new window)
# Exercise 1
# Dartpad Classes and Datatypes
Your first exercise with Dart requires you to create an account on DartPad by logging in with your Github credentials.
Create a new Pad that is a Dart application, not a Flutter one. You will be saving it as a public Gist
through the menu that appears when you click your Avatar. Choose the option that says
Create Public Gist
. This will update the URL for your code. You will be submitting this URL through BS LMS.
You are going to be building the main
function plus three classes. Two of the classes need to extend the first and @override
a method from the first. The two subclasses should also have at least
one method that is unique to each. The two subclasses should also have at least one member field that is unique to each.
Here is a sample version (opens new window) to give you an idea of what you are building.
void main() {
Dolphin flipper = Dolphin('bottlenose');
print('Flipper is a ${flipper.type} dolphin.');
flipper.eat('crab');
flipper.eat('shark');
flipper.swim();
Kangaroo skippy = Kangaroo('Western Grey');
print('Skippy is a ${skippy.breed} kangaroo.');
skippy.eat((type: 'cow', isPlant: false));
skippy.hop();
skippy.eat((type: 'grass', isPlant: true));
}
class Animal {
Animal(); //the parent super class
eat(var food){
print('Thanks for the $food');
}
}
class Dolphin extends Animal {
late String type;
List<String> foodTypes = ['crab', 'octopus', 'herring'];
Dolphin(this.type);
eat(var food){
if(foodTypes.contains(food)){
print('Thanks for the $food');
}else{
print('Yuck. I cannot eat $food');
}
}
swim() {
print('Splish. Splash.');
}
}
class Kangaroo extends Animal {
late String breed;
Kangaroo(this.breed);
eat(var food){
// expects a record like: (type: '', isPlant: true)
if(food.isPlant){
print('Yum! ${food.type}!');
}else{
print('I only eat plants');
}
}
hop(){
print('hop hop hop');
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
and the output from the above code is:
Flipper is a bottlenose dolphin.
Thanks for the crab
Yuck. I cannot eat shark
Splish. Splash.
Skippy is a Western Grey kangaroo.
I only eat plants
hop hop hop
Yum! grass!
2
3
4
5
6
7
8
You need to make up your own three classes.
Your version should import dart:math
and use methods or properties from that package. Eg: Random()
.
You program should include the following:
- One parent super class that has a constructor and one method.
- Two sub classes that each have at least one field and two methods.
- At least one field that is a
String
. - At least one field that is an
int
. - At least one field that is a
List
. - At least one field that is a
double
. - At least one if statement.
- Each method should have a
print()
call, so you see something in the terminal when this runs. - A
main()
function that instantiates each of the sub classes at least once. - The
main()
function should call each of the methods in the sub classes at least once.
See BS LMS for the due date and time. WEEK 2
# Exercise 2
# Arguments, Environment, and Random Integers
Your second Dart exercise is to create a Dart console application in VSCode. You will be saving this project as a public Github repo and submitting the URL for the repo through BS LMS.
This exercise will give you practice working with command line arguments, environment variables, and building console applications in VSCode.
For this exercise we want to create two temporary Environment variables in the Terminal.
export MIN=10
export MAX=100
2
The two variables will be called MIN
and MAX
. They will each hold an integer value... although the values of all ENV variables are saved as Strings.
We will use these variables in the console app.
In your bin
folder there will be a dart file that contains the void main()
function.
In your lib
folder there will be a dart file that has a class called Lucky
. The Lucky
class has a constructor which will create an instance of the class Lucky
. The constructor will also read
the two environment variables MIN
and MAX
and create a random integer between those two numbers. You will need to import both the dart:io
and dart:math
packages to be able to do these things.
Save the random int
as a member field that can be accessed externally.
Your main
function back in the bin
folder will read the List
of command line arguments. When you run the dart file, you should add a list of names as the arguments. Eg:
dart run bin/main.dart tony adesh tyler anoop
The main
function needs to loop through the list of names that were read from the command line arguments.
For each name:
- capitalize the first letter of the name;
- create an instance of the
Lucky
class so you can access it's member field that has the random integer; print()
a message"Hello <name>. Your lucky number is <integer>"
.
Replace the <name>
and <integer
> parts of the output with the name from the arguments list and the member field from the Lucky
instance.
Turn your project into a public repo on Github and submit the Repo URL in BS LMS.
See BS LMS for the due date and time. WEEK 4
# Exercise 3
# Dart Collections
The third Dart exercise is also a Dart console application in VSCode. You will be saving this project as a public Github repo and submitting the URL for the repo through BS LMS.
This third exercise will give you practice working with Dart List
and Map
.
Create a Dart class called Students
with the following features:
- a constructor will accept a
List< Map<String, String> >
. - the List will be saved in a member field -
List<Map<String, String>> people
. - a
sort(String field)
method that will sort the List based on the field name that is passed to the method. - an
output()
method that will loop throughprint()
each of the items in the List. - a
plus(Map<String, String> person)
method that will let you add a singleMap
to theList
. - a
remove(String field)
method that will remove an item from theList
using theremoveWhere()
List method.
Here is the reference for the List class (opens new window).
The main()
method should have a String variable containing a JSON string. Convert it to a List<Map<String, String>>
and then call the Students
constructor. Then call the sort
, output
,
plus
, and remove
methods each at least once.
Your Dart file needs to import the dart:convert
package so you can convert a JSON string into a List<Map<String, String>>
that will be passed to the Students
constructor.
Here is a sample version of the List that you can use for testing. Feel free to edit the contents.
String json = '''
[
{"first":"Steve", "last":"Griffith", "email":"griffis@algonquincollege.com"},
{"first":"Adesh", "last":"Shah", "email":"shaha@algonquincollege.com"},
{"first":"Tony", "last":"Davidson", "email":"davidst@algonquincollege.com"},
{"first":"Adam", "last":"Robillard", "email":"robilla@algonquincollege.com"}
]
'''
2
3
4
5
6
7
8
See BS LMS for the due date and time. WEEK 6
# Exercise 4
# ASYNC HTTP and JSON
The fourth Dart exercise is also a Dart console application. Remember to start with the void main()
function.
Your app will use the https://random-data-api.com/
API and make an HTTP GET Request for some random user data. You should fetch 8-12 users.
Use the following packages:
package:http/http.dart
fromhttps://pub.dev/packages/http
;dart:convert
to convert the JSON from the file into a List of Map objects.dart:async
but this is a dependency ofhttp.dart
so you won't have to write the import.
Remember that the http
package needs to be included in your pubspec.yaml
file.
Use async
and await
for your http.get
method, and wrap it in a try..catch
.
Loop through the results of your HTTP Request and output the uid
, first_name
, and last_name
of each user as a single string. There should be only one print()
method call inside your loop.
You should be able to use dart run
from the Terminal to make your console app run from VSCode. Each time you run the program it should generate a different series of ids and names.
Make your project a public Github Repo and submit the URL for your Repo in BS LMS.
See BS LMS for the due date and time. WEEK 9