Posts

Showing posts from March, 2019

Power On!

Welcome to this series of blogs. Let's do a review of our favorite languages! Looking for the next piece on Applications ^ Data? See here! These blogs are designed to trigger questions and discussions. It's not about being right or wrong. The purpose is to discover, know and develop. Feel free to leave comments, ask and answer questions, voice opinions and enjoy! Guideline : Please be respectful and welcoming of everyone's views and questions. You are now a part of our team :) Click here  to attempt the  assessment of this series Notes: - You can login using your gmail id . If you have provided us the gmail id already, it will succeed. - Otherwise, please write a mail from your gmail id to sudeep@rapalearning.com with subject Bootcamp access - Each lesson has an assessment. Each assessment can be submitted once. Here's is a sequence we suggest: Building block of polymorphism : Start here for a fun exercise to program an artillery firing sequence... in C

JavaScript functions

Functions are famously called first-class citizens of the JavaScript world. Functions can smoothly be treated as a variable,  For example, if we execute this in a browser: function myFunction(a, b) {   return a * b; } var x = myFunction; console.log(x(3,4)); It will log 12. The declaration of myFunction makes it a 'value', which is assigned to a variable 'x'. Notice that there are no parenthesis in that assignment. Next, we have a log that invokes myFunction by applying the parenthesis () operator on the variable. The log statement logs the return value of myFunction, which is the product of 3 and 4. Let's say we forget the parenthesis: console.log(x); This will log the function itself! Try it out in your browser by enabling the console display via the 'developer tools' option. Of course, this will just call the function normally: var x = myFunction(3,4); It's just the application of the () operator to call a

JavaScript data-types

JavaScript is a versatile language that's used in programming everything from web pages to servers and desktop applications. We will use this interactive tutorial  as a reference in our journey. JavaScript is different from the other languages we've seen here (C++, C#, Java), in that it doesn't have a compiler. The code is interpreted while running it. This brings us to a distinction in data-types as well. JavaScript doesn't fix the type of its variables. For example, this is a valid sequence for the interpreter: var x;           // x is undefined to start with x = 15;          // After this assignment, x is a Number x = "Phil";      // Here x becomes a String Many other fun things can happen with JavaScript data, as you can see here . What could be the use of such flexibility? Tell us what you feel below. Hint: Think of real-life arrays - like array of vehicles, array of chat messages, etc. Once you are done, let's step int

Java Characters

Characters are represented by codes. ASCII is a standard for alphabets and a few symbols. For example, the code 65 is used for the letter 'A' char c1 = 65; System.out.println(c1); Will print the character A. Modern Java compilers and Operating Systems are aware of Unicode characters. Unicode is a standard system of encoding characters in all known scripts. Here's code to print the recent 'Rupee' symbol: char c1 = 0x20b9; System.out.println(c1); Will print the symbol  ₹ If you don't have a local Java compiler, you can use this online compiler . Explore Unicode. Share some characters and the corresponding codes of your mother-tongue. Click here to power back

Java Strings

If you are unfamiliar with Java or interested in a refresher, use this link . In this blog we look at some quirky dealings with Java Strings. We'll be using concepts of Java variables , data-types and operators and especially Strings . The following code constructs two strings  s1  and  s2 . When they are compared, their contents are identical. String s1 = "12345"; String s2 = "123"; s2 += "45"; if(s1 == s2) {     System.out.println(s1 + " equals " + s2); } else {     System.out.println(s1 + " does NOT equal " + s2); } Strangely, this code reports that the strings are not equal: 12345 does NOT equal 12345   Why is that? How do we fix this? Notice the change in bold below. String s1 = "12345"; String s2 = "123"; s2 += "45"; if(s1.equals(s2)) {     System.out.println(s1 + " equals " + s2); } else {     System.out.println(s1 + " does

C# Collection and Iteration

C# has powerful structures for dealing with collections and other data structures. In this blog, we'll be using string formatting  and lists . Click on the links if you haven't done them in C# before. var names = new List<string>   { "nobita", "bheem", "oggy", "oggy cockroach", "ninja" }; foreach (var name in names) {     if(name.StartsWith("oggy")) {         Console.WriteLine($"Need to remove {name}!");         names.Remove(name);     } } This code tries to remove names starting with "oggy" from a list. Try it in the interactive editor offered by the above tutorial (click 'Enter focus mode' at the bottom of this site ) Does it work? No! Which is the line that's causing the crash? How would you resolve it? Click here to power back

C# loop optimization

See here for an interactive tutorial on control flows in C#. At the end of the tutorial, you see this piece of code, to sum all numbers between 1 and 20, which are divisible by 3 int sum = 0; for (int number = 1; number < 21; number++) {   if (number % 3 == 0)   {     sum = sum + number;   } }   Check the cyclomatic complexity  of this code. Is there a way you can simplify it by removing the nesting? Type your thoughts below. Going ahead, we'll look at using loops to iterate over a collection

C# arithmetic

See here for a short interactive tutorial about number-arithmetic in C# Of course, arithmetic in C# is similar to any other language.  Here's something to think about: Normally, if we divide & multiply by the same number, we get back the variable. For example: (c / a) * a  Would evaluate to  c  itself. However, there are some situations in which this is not true. It will not evaluate to  c What do you think? Write the situations that come to your mind below. Next, we look at control structures and a small optimization problem

C++ functions

See here for a discussion on the types of arguments that a C++ function can take. A discussion on passing arrays in particular is given here . More about this in the assessment :) Click here to power back

C++ control structures

See here for a quick refresher of looping in C++ Usage of while statements are quite common. Why do you think a do-while loop is required to be present in the language at all? Have you used a do-while loop to solve any problem? If you haven't, do you think it's useless? Let's know what you think. Next, we look at functions and arguments in C++ .

C++ scope

Variables store the state of a program. We know that it's the way a program 'remembers' something. This 'state' is also notoriously prone to side-effects. A variable altered in one place can have un-intended consequence in another. The 'scope' of a variable determines where it can be accessed. A variable with lower scope can be changed with higher confidence (= lower side-effects). See here for an introduction to scope in C++ C++ also scopes variables in classes. See here for details. Is the scope of a variable checked at compile-time or run-time? In other words, can we access a private data member if the class returns a pointer to it? class PrivacyViolator {   private:     char name[256];   public:     char* getName() {       return name;     } }; Can I call  getName  from outside this class and change its contents?       PrivacyViolator p;       char* s = p.getName();     char source[] = "Accessed priv

C++ data types

See here for an introduction to the data types provided by C++. Let's explore some data types. Consider this code - it initializes an 'unsigned short' to 0 and decrements it. Will it become negative, though it is unsigned? unsigned short int i = 0; i--; Try it out and check the value of the variable after decrementing. Consider that the variable i is used to index an array: char name[] = "Helloo"; char c = name[i]; What would be the outcome of accessing the array in the second line? Let us know your thoughts below. Next, we look at variables in C++  and their scope.

Polymorphism problem statement

In the previous post, we saw a possible way to fire a series of artillery guns in quick succession:   void synchronized_fire() {   howitzer_v1_fire(GUN_ID1);   dhanush_fire( GUN_ID2 );   bofors_v2_fire( GUN_ID3 );   howitzer_v2_fire( GUN_ID4 );   dhanush_fire( GUN_ID5 ); } And we asked ourselves, what's wrong in writing the function this way? Well, to start with, adding a new gun will need us to change this function and re-compile.  Next, suppose we need a new routine of firing - like triggering alternate guns to save ammunition. Then we need two functions to fire odd and even guns: void synchronized_fire_odd() {   howitzer_v1_fire(GUN_ID1);   bofors_v2_fire( GUN_ID3 );   dhanush_fire( GUN_ID5 ); } void synchronized_fire_even() {   dhanush_fire( GUN_ID2 );   howitzer_v2_fire( GUN_ID4 ); } Gradually, such functions proliferate. After this, adding a new type of gun (or replacing an obsolete piece) will trigger change in all tho

Polymorphism via Function pointers

Function pointers in C provide a method to achieve polymorphism. In fact, they are one of the ingredients in the C++ object model as well. See here for a quick refresher on C function pointers. What we need is 'a function to fire the guns one by one'. Here's an outline of such a function: void synchronized_fire(/*accept array of guns here*/) {    //iterate through the array    //and fire each element in there } What we've done is to remove the logic of 'which gun' and 'how to fire' from this function. Where do we put that logic? Let's try a struct:   struct Gun {   int gun_id;   void (*fire)(int); } Observe that this struct has two members. One is a noun (the gun identification) and another is a verb (the activity of firing). Then the function to fire them all would look like this. Remember that an array in C always needs to be accompanied by its bounds: void synchronized_fire(struct Gun[] guns, unsigned int n_g