Created
May 5, 2022 04:47
-
-
Save Chinecherem20199/6fef38dfb789f763a23e31861e48c80e to your computer and use it in GitHub Desktop.
const, final, var etc in Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
//Exercise 3 | |
//Question 1 | |
//Use the declared variables below to decide whether or not a person is eligible to rent movies | |
//A person is eligible when the age is more than 20 and the person can show his/her ID | |
//An example printout: Eligible to rent movies? false | |
int age = 15; | |
bool id = true; | |
print('Eligible to rent movies? ${age > 20 && id}'); | |
//Question 2 | |
//Use the variable below and work out the price the customer will pay to enter the Wild Life Park | |
//For a Bike, the driver will pay $10 entry | |
//For a Car, the driver will pay $20 entry | |
//For a Bus, the driver will pay $30 entry | |
//Example printout: You will pay $10 to enter the Wild Life Park | |
String type = 'Bike'; | |
print('You will pay ${type == 'Bike'? '\$10' :type =='Car'? '\$20' : '\$30' } to enter the Wild Life Park'); | |
//Question 3 | |
//Use the email declared below and test if it is a valid email address | |
//For an email address to be valid, it must contain the @ symbol and a . | |
//Example printout: Valid email address? true | |
String email = '[email protected]'; | |
print('Valid email address? ${email.contains('@') && email.contains('.')} '); | |
print('$email'.contains('@')? 'Valid email address': 'Not Valid'); | |
//Question 4 | |
//Look at the following declarations of variables. You need to change all the String declarations to | |
//const, final or var | |
//try this first on a piece of paper and then check it in coding | |
const firstName = 'Peter'; | |
const lastName = 'Johnson'; | |
var fullName = '$firstName $lastName'; | |
final fullNameLength = fullName.length.toString(); | |
fullName = 'Peter Pollock'; | |
print(fullName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment