If your IDE doesn't detect the project files, run the following in the terminal:
cd labs/week1
dart pub get
- In the top bar, select the
Week 1
run configuration. - Click the green play button (
Run
).
- Open the "Run and Debug" view from the sidebar.
- Select the
Week 1
run configuration. - Click the green play button (
Run
).
- Navigate to the
labs/week1
directory. - Execute
dart run
.
Note
Make sure to add print statements to your solutions (in the partX
functions)
to present your progress.
-
Create a simple Q&A program.
- Ask the user (
stdin
) about their name and favorite color. You can add more questions. - Then, print a greeting using their data.
- Ask the user (
-
Print a triangle of asterisks, something like this:
* ** *** **** ***** ****** *******
package:collection
might be useful.
- Lists & sets
- Create a list of 30 random integers between 0 and 20 (use the
Random
class). - Sort the list in descending order.
- Filter the list, so that it contains only numbers divisible by 3.
- Remove duplicates from the list (i.e., make a set out of it).
- Print each element, its index in the list, and whether it is even or odd.
- Create a list of 30 random integers between 0 and 20 (use the
- Maps
- Create a collection of Strings (e.g., names, cities, etc.).
- Create a map that maps each string to its length.
- Group the elements by their first letter.
-
Define a following class hierarchy of animals. All animals have an age and a name. Their
description
should include these two fields.Make sure that
Dog
,Cat
andCow
are the only animals allowed in this hierarchy.--- title: --- classDiagram class Animal { int age String name get String description void makeSound()* } Animal <|-- Dog class Dog { String color } Animal <|-- Cat class Cat { String color } Animal <|-- Cow class Cow { int weight }
Example usage:
final myDog = Dog('Rex', age: 3, color: 'brown'); final aCow = Cow('Betsy', age: 5, weight: 500);
-
Create a list of various animals. Iterate over this list and make every animal make a sound. Also, for each animal, print its additional fields like
color
orweight
.Use pattern matching to simplify matching subclasses and reading their properties.
-
Define a generic
Container
class that holds a single value.Example usage:
final intContainer = Container<int>(42); final stringContainer = Container<String>('Hello, world!');
Make sure
null
cannot be stored in the container, i.e. the type parameter is not nullable:// This shouldn't compile final invalid = Container<String?>(null);
-
Create a list of various containers. Then loop over them: if the contained value is a number, print its square. If it's a string, print its length.
-
Define a function called
generate
that returns a random triple of integers. -
Define a function called
transform
that accepts a triple of integers and:- If all values are 0, returns
"All zeros!"
. - If the first value is greater than 10 and the second one is equal to 5, returns the third one.
- If the first value is even, returns the first and second values.
- Else, returns the entire triple.
Don't use
if
statements in this function.Try to define
transform
with the arrow syntax=> ...
. - If all values are 0, returns
-
Define a function called
randomize
that returns a random record of:- Two integers
x
andy
- A boolean
enabled
- A double
temperature
- Two integers
-
Read the four values from
randomize
into variables insidepart5
without creating any additional variables or using property access (i.e.obj.prop
).