OneCompiler

you like this?

void main() { print('Hello World!'); } Drawing a Shape Code runs in the order it's written print(' /|'); print(' / |'); print(' / |'); print('/|'); Variables int age = 70; String name = "Mike"; print("There once was a man named 𝑛 𝑎 𝑚 𝑒 " ) ; 𝑝 𝑟 𝑖 𝑛 𝑡 ( " 𝐻 𝑒 𝑤 𝑎 𝑠name");print("Hewas{age} years old"); age = 25 print("He really liked the name 𝑛 𝑎 𝑚 𝑒 " ) ; 𝑝 𝑟 𝑖 𝑛 𝑡 ( " 𝐵 𝑢 𝑡 𝑑 𝑖 𝑑 𝑛 ′ 𝑡 𝑙 𝑖 𝑘 𝑒 𝑏 𝑒 𝑖 𝑛 𝑔 name");print("Butdidn ′ tlikebeing{age}"); Data Types int age = 26 double gpa = 3.5 bool isRegisteredVoter = false String name = "Mike" print(name) Strings String greeting = "Hello"; // 01234 greeting.length(); greeting[0]; greeting.toUpperCase(); greeting.indexOf("e"); greeting.contains("llo"); greeting + " World!"; Numbers import 'dart:math'; int quantity = 300; double price = 5.99; quantity += 5 print( 5 + 3 ) print( 10 % 3 ) min(5, 6) sqrt(144) Getting User Input import 'dart:io'; print("What is your name") String name = stdin.readLineSync() print("Hello name")BuildingaBasicCalculatorprint("Enteryourfirstnumber:");intnum1=int.parse(stdin.readLineSync());print("Enteryourfirstnumber:");intnum2=int.parse(stdin.readLineSync());print(num1+num2);Buildingamadlibsgameprint("Enteracolor:");Stringcolor=stdin.readLineSync();print("Enterapluralnoun:");StringpluralNoun=stdin.readLineSync();print("Enteracelebrity:");Stringcelebrity=stdin.readLineSync();print("Rosesare𝑐𝑜𝑙𝑜ð‘Y¨");𝑝ð‘Y¨ð‘–𝑛𝑡("color");print("pluralNounareblue");print("Ilove{name}") Building a Basic Calculator print("Enter your first number:"); int num1 = int.parse(stdin.readLineSync()); print("Enter your first number:"); int num2 = int.parse(stdin.readLineSync()); print(num1 + num2); Building a madlibs game print("Enter a color:"); String color = stdin.readLineSync(); print("Enter a plural noun:"); String pluralNoun = stdin.readLineSync(); print("Enter a celebrity:"); String celebrity = stdin.readLineSync(); print("Roses are 𝑐 𝑜 𝑙 𝑜 𝑟 " ) ; 𝑝 𝑟 𝑖 𝑛 𝑡 ( " color");print("{pluralNoun} are blue"); print("I love {celebrity}"); Lists List favNumbers = [4, 8, 15, 16, 23, 42]; // 0, 1, 2, 3, 4, 5 favNumbers.length; favNumbers[1]; favNumbers[2] = 5; favNumbers.add(5); favNumbers.indexOf(8); favNumbers.contains(8); favNumbers.remove(42); print(favNumbers); Functions void drawTriangle(){ print(' /|'); print(' / |'); print(' / |'); print('/|'); } drawTriangle(); drawTriangle(); drawTriangle(); Parameters & Arguments void sayHi(String name, int age){ print("Hello 𝑛 𝑎 𝑚 𝑒 , 𝑦 𝑜 𝑢 𝑎 𝑟 𝑒 name,youare{age}"); } sayHi("Mike", 26); sayHi("Joe", 55); Return Statements int addNumbers(int num1, int num2){ return num1 + num2; } int answer = addNumbers(6, 7); print(answer); Building a prompting function String prompt(String promptText){ print("{promptText}:"); String answer = stdin.readLineSync(); return answer; } If Statements Read the prompt text from the website I wake up If I'm hungry I eat breakfast I leave my house if it's cloudy I bring an umbrella otherwise I bring sunglasses Im at a restaurant if I want meat I order a steak otherwise if I want pasta I order spaghetti & meatballs otherwise I order a salad. bool isSmart = true; if(isSmart){ print("You are smart!") } else { print("You need to study") } String name = "Mike"; if(name.contains("M")){ print("you have an M in your name"); } &&/|| operators, elseif bool isSmart = true; bool isStudent = true; if(isSmart && isStudent){ print("You are a smart student!"); } else if (isSmart && !isStudent){ print("You are a smart non-student!"); } else { print("You need to study"); } Building a better calculator double promptNumber(){ print("Enter a number:"); return double.parse(stdin.readLineSync()); } double num1 = promptNumber(); print("Operator (+,-,*,/):"); String operator = stdin.readLineSync(); double num2 = promptNumber(); if(operator == '+') print(num1 + num2); else if(operator == '-') print(num1 - num2); else if(operator == '') print(num1 * num2); else if(operator == '/') print(num1 / num2); else print("invalid operator!"); Switch Statements switch(operator){ case '+': print(num1 + num2); break; case '-': print(num1 - num2); break; case '/': print(num1 / num2); break; case '': print(num1 * num2); break; default: print("invalid operator"); } While Loops int i = 0; while(i < 5){ print(i); i++; } Building a guessing game String promptGuess(){ print("Enter a guess:"); return stdin.readLineSync(); } String answer = 'michael scott'; String guess = ''; while(guess != answer){ guess = promptGuess(); } print("You Win!"); For Loops First talk about how this is a common occurance for(int i = 0; i < 10; i++){ print(i); } List friends = ["Jim", "Kevin", "Stanley"]; for(String friend in friends){ print("Hello {friend}"); } Drawing multiple triangles String drawTriangle(int quantity){ for(int i = 0; i < quantity; i++){ print(' /|'); print(' / |'); print(' / |'); print('/___|'); } } drawTriangle(5); Comments // single line comment /* multi line comment */ Classes & Objects class Book { String title; String author; int noPages; } Book hp = Book(); hp.title = "Harry Potter"; hp.author = "JK Rowling"; hp.noPages = 500; print(hp.author); Constructors class Book { Book(String aTitle, String aAuthor, int aNoPages){ this.title = aTitle; this.author = aAuthor; this.noPages = aNoPages; } String title; String author; int noPages; } Book hp = Book("Harry Potter", "JK Rowling", 500); print(hp.author); Building a quiz List questions = [ MathQuestion('1 + 1', '2'), MathQuestion('2 - 7', '-5'), ]; int score = 0; for(MathQuestion question in questions){ String answer = prompt(question.prompt); if(answer == question.answer){ print("Correct!"); score++; } else { print("Incorrect!"); } } print("You got 𝑠𝑐 𝑜 𝑟 𝑒 / score/{questions.length} correct"); Class Functions class Student { String name; double gpa; Student(String name, double gpa){ this.name = name; this.gpa = gpa; } bool hasHonors(){ return this.gpa >= 3.5; } }

No answers yet!

1 year ago by