focused on practical understanding of concepts covered recently in class
slides and material available after class
How to follow along
Small breakout rooms of working together
Work for 10-15 minutes together, feel free to use paper, whiteboards, online share tools.
Use the jeed playground on the 125 homepage for the interactive running
1) Function overloading
Function overloading is when you make different functions with the same name.
This is to deal with different types of and number of inputs
Different data types may be processed differently
intarea(int l, int h){
return l * h;
}
floatadd(float l, float h){
return l * h
}
1) Function overloading (contd)
We distinguish functions via their signatures for overloading.
Signature = name + inputs(names, type, order)
area(int, int), area(float, float)
Note that having the return type different doesn't change the signatures. float area(int l, int h) and int area(int l, int h) are the same function as far as overloading is concerned.
Having the same type, order and number but different name does not work either. area(int a, int b) and area(int l, int h) are the same function as far as overloading is concerned.
1) Function overloading - Practice
Write two overloaded functions area to calculate the area of a circle and triangle
double area(double radius) - πr2. Feel free to use π=3.14159
double area(double base, double height) - 21bh
1) Function overloading - Starter code (10 minutes)
It's easy to start thinking of these as rows and columns, much like excel or graph paper
This works fine for 2d arrays where all sub arrays are the same length, but this falls part for 3d or 4d arrays, or when 'rows' have different lengths.
This can also limit your thinking in understanding what this data structure really is, which is simply arrays made of arrays.
2) Multidimensional array - Practice
Create a function double average(double[][] array) that takes the average of all the numbers in the array
Careful to make sure this works well with non rectuangular (sub arrays not equal) arrays. {{0.1}, {0.2, 0.3}} for example
Strings are one of the most common data types programmings deal with because they represent any text
This file is a string, the code playground stores everything as a string, your email is a string.
Yet string are just text, so we need to often convert it to more useful data formats like int, double
3) String parsing - useful functions
These are the most useful functions you will use while dealing with this.
String substring(int begin) or String substring(int begin, int end) - begin is inclusive, end is exclusive - we use this to get a portion of a string out
String split(String delim) - splits a String based on what is given, commonly we split based on ,, :,\n (newline, note the \)
String trim() - removes starting and ending whitespace (so if a cat types it in we can deal with it)
String equals(String other) - use this to compare strings and not == (which sometimes fails)
3) String parsing - CSV
CSV Data file formats is super common in the real world for sending data by strings
Each entry is on a new line, you can use split them using \n
Each column is split by commas ,
3) String parsing - restaurant orders
Calculate the total revenue, each line has the name of the dish, price and number ordred