Assignment 1 – Dart Practice

  1. Due
  2. Objectives
  3. Instructions
  4. Part 1 - Simple Dart
  5. Part 2 - Class Definition
  6. Part 3 - Class Definition, redux
  7. Submission

Due

Tuesday, January 21 @ 11:59pm via Canvas

Objectives

  1. To be introduced to and get comfortable with Visual Studio Code and basic Dart language features.
  2. To be able to compile and run your code on a virtual or real mobile phone.

Instructions

This assignment must be completed individually. You may use Internet sources, but you must cite algorithms that you copy and modify from elsewhere.

  1. Create a new Flutter project called dart_practice using the VSCode command palette as described in Getting started with Visual Studio Code.
  2. Replace lib/main.dart with the following file: main.dart
  3. 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.

Assignment 1 beginning

Part 1 - Simple Dart

Assignment 1 Part 1 output

Part 2 - Class Definition

  1. Define a class called Person in practice.dart below the Practice class:
    • Define three class variables in the Person class:
      1. first a String representing a person’s first name
      2. last a String representing a person’s last name
      3. age 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:

Assignment 1 Part 2 output

Part 3 - Class Definition, redux

  1. Define a class called AgeOptionalPerson in practice.dart below the Person class. Feel free to copy and paste from the Person class to start.
    • Define three class variables in the AgeOptionalPerson class:
      1. first a String representing a person’s first name
      2. last a String representing a person’s last name
      3. age an nullable int representing a person’s age
    • Define a Constructor for the AgeOptionalPerson 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 AgeOptionalPerson class. Create one with the age and one without age.
    • Add some code after the const Text('class AgeOptionalPerson', style: TextStyle(fontWeight: FontWeight.bold)), in main.dart to display the result of getWelcome() called in your two AgeOptionalPerson variables. Feel free to copy, paste, and modify my code.
      1. One person must have an age.
      2. The other person must not have an age. DO NOT pass age: null in that person’s constructor.
    • You should see something similar to the following when completed correctly:

Assignment 1 Part 3 output

Submission

Submit your main.dart and practice.dart files to Canvas.