Assignment 1 – Dart Practice
- Due
- Objective
- Instructions
- Part 1 - Simple Dart
- Part 2 - Class Definition
- Part 3 - Class Definition, redux
- Submission
Due
Wednesday, January 24 @ 11:59pm via Canvas
Objective
To be introduced to and get comfortable with VSCode and basic Dart language features.
Instructions
This assignment must be completed individually. You may use Internet sources, but you must cite algorithms that you copy and modify from elsewhere.
- Create a new Flutter project called
dart-practice
using the VSCode command palette as described in Getting started with Visual Studio Code. - Replace
lib/main.dart
with the following file: main.dart - Download practice.dart and place it in the
lib/
folder.
Run the Flutter application from main.dart
. You should see a simple UI with text, including a few “FIX ME”s.
main.dart
contains the GUI code for this very simple UI. It also instatiates aPractice
class, which is defined inpractice.dart
. A few of the methods on thePractice
class are called and their results are displayed inText
widgets.practice.dart
contains the implementation of thePractice
class. This is pure Dart code.
Part 1 - Simple Dart
- You do not need to edit
main.dart
for this part. -
Change the methods in
practice.dart
to fulfill the methods’ comment descriptions. Test code is already in place. The correct output will look like the following: - “Hot reload” should automatically update the code on your device when you save your file.
- If you do not see changes when you save:
- Check the VSCode “Debug Console” for errors and exceptions, which you then need to fix. The code editor windows will highlight some syntax errors for you.
- If there are no errors, try manually re-running the Flutter application.
- VSCode provides you with “autocomplete” information as you are typing – VSCode calls this “IntelliSense”. Autocomplete is a great time saver and a way to explore Flutter.
- Code formatting is important for readability. Right click inside the code editor for
practice.dart
and you will see an option for “Format Document”. Selecting this will automatically format your code with proper spacing, tabs, and line breaks. Always format your document before you submit your assignment, and do it any time your code is getting messy. The keyboard shortcut is Shift+Alt+F on Windows and Shift+Option+F on Mac.
Part 2 - Class Definition
- Define a class called
Person
inpractice.dart
below thePractice
class:- Define three class variables in the
Person
class:first
a String representing a person’s first namelast
a String representing a person’s last nameage
an int representing a person’s age
- Define a Constructor for the Person class such that a call to
Person("Sammy", "Seahawk", 22)
will initialize the appropriate class variables. - Define a method named
getWelcome()
with no parameters that returns a String that formats the values of the class variables as:My name is {first} {last} and I am {age} years old.
- To test, uncomment lines 35 and 65-67 in
main.dart
. - Change line 35 of
main.dart
to contain your name and age. - You should see the following (with your info) when completed correctly:
- Define three class variables in the
Part 3 - Class Definition, redux
- Define a class called
OptionalPerson
inpractice.dart
below thePerson
class. Feel free to copy and paste from thePerson
class to start.- Define three class variables in the
Person
class:first
a String representing a person’s first namelast
a String representing a person’s last nameage
an nullable int representing a person’s age
- Define a Constructor for the Person class wherein the first and last names are required, but the age is optional, i.e., age does not need to be passed as a parameter.
- Define a method named
getWelcome()
with no parameters that returns a String that returns the values of the class variables in the following format:- “My name is {first} {last} and I am {age} years old.” iff age is not null.
- “My name is {first} {last}.” iff age is null.
- When you are ready to test, uncomment 70-71 in
main.dart
. - Below line 36 of
main.dart
, declare two variables that instantiate instances of your new OptionalPerson class. Create one with the age and one without age. - Add some code after the
const Text('class OptionalPerson', style: TextStyle(fontWeight: FontWeight.bold)),
inmain.dart
to display the result ofgetWelcome()
called in your two OptionalPerson variables. Feel free to copy, paste, and modify my code. - You should see something similar to the following when completed correctly:
- Define three class variables in the
Submission
Submit your main.dart
and practice.dart
files to Canvas.