Skip to content

Latest commit

 

History

History
141 lines (108 loc) · 3.89 KB

assignment.md

File metadata and controls

141 lines (108 loc) · 3.89 KB

1) The following function takes two integers as parameters. Make it return a string containing both integers separated by a space. For example, stringify(2, 3) should return '2 3'.

String stringify(int x, int y) 

{
  // Return a formatted string here
}

2)Try putting the ??= and ?? operators to work below.

String foo = 'a string';
String bar; // Unassigned objects are null by default.

// Substitute an operator that makes 'a string' be assigned to baz.
String baz = foo /* TODO */ bar;

void updateSomeVars() {
  // Substitute an operator that makes 'a string' be assigned to bar.
  bar /* TODO */ 'a string';
}

3) Try using conditional property access to finish the code snippet below.

// This method should return the uppercase version of `str`
// or null if `str` is null.
String upperCaseIt(String str) {
  // Try conditionally accessing the `toUpperCase` method here.
}

4) Try setting the following variables to the indicated values.

// Assign this a list containing 'a', 'b', and 'c' in that order:
final aListOfStrings = 

// Assign this a set containing 3, 4, and 5:
final aSetOfInts = 

// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12:
final aMapOfStringsToInts = 

// Assign this an empty List<double>:
final anEmptyListOfDouble = 

// Assign this an empty Set<String>:
final anEmptySetOfString = 

// Assign this an empty Map of double to int:
final anEmptyMapOfDoublesToInts = 

5) Try finishing the following statements, which use arrow syntax.

class MyClass {
  int _value1 = 2;
  int _value2 = 3;
  int _value3 = 5;
  
  // Returns the product of the above values:
  int get product =>
  
  // Adds 1 to _value1:
  void incrementValue1() => 
  
  // Returns a string containing each item in the
  // list, separated by commas (e.g. 'a,b,c'): 
  String joinWithCommas(List<String> strings) =>
}

6) Imagine you have a shopping cart class that keeps a private List of prices. Add the following:

*A getter called total that returns the sum of the prices

*A setter that replaces the list with a new one, as long as the new list doesn’t contain any negative prices (in which case the setter should throw an InvalidPriceException).

class InvalidPriceException {}

class ShoppingCart {
  List<double> _prices = [];
  
  // Add a "total" getter here:

  // Add a "prices" setter here:
}

7)Implement tryFunction below. It should execute an untrustworthy method and then do the following:

*If untrustworthy throws an ExceptionWithMessage, call logger.logException with the exception type and message (try using on and catch).

*If untrustworthy throws an Exception, call logger.logException with the exception type (try using on for this one).

*If untrustworthy throws any other object, don’t catch the exception.

*After everything’s caught and handled, call logger.doneLogging (try using finally).

typedef VoidFunction = void Function();

class ExceptionWithMessage {
  final String message;
  const ExceptionWithMessage(this.message);
}

// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
  void logException(Type t, [String msg]);
  void doneLogging();
}

void tryFunction(VoidFunction untrustworthy, Logger logger) {
  // Invoking this method might cause an exception. Catch and handle
  // them using try-on-catch-finally.
  untrustworthy();
}

8)Add a one-line constructor to MyClass that uses this. syntax to receive and assign values for all three properties of the class.

class MyClass {
  final int anInt;
  final String aString;
  final double aDouble;
  
  // Create a constructor here.
}

9)Give the Color class a constructor named Color.black that sets all three properties to zero.

class Color {
  int red;
  int green;
  int blue;
  
  Color(this.red, this.green, this.blue);

  // Create a named constructor called "black" here:
}