CSE116的学习笔记-Lec3-4:Functions
FunctionsWe have functions today:)
Custom Sorting
We can sort any type with any comparatror
But what if we want to sort points by their distance from a reference point
In general: what if the comparator needs more parameters than just the two elements?
We can dynamically create a new function with the additional parameters “built-in”
Returning Functions
We can write a function / method that takes all the needed parameters and returns a funcrtion that fits the signature of a comparator
Th ...
CSE116的学习笔记-Lec3-3:Recursion
Recursion123456789101112def computeGeometricSum(n: Int): Int = { if(n <= 0){ 0 }else{ n + computeGeometricSum(n-1) }}def main(args: Array[String]): Unit = { val resullt: Int = computeGeometricSum(3) println(result)}
Computes the geometric sum of the input
Ex: if n == 3, geometric sum is 3+2+1 == 6
Base Case:
An input with a trivial output
Geometric sum of 0 is defined as 0
We could also add 1 -> 1 a ...
CSE116的学习笔记-Lec3-2:More_Sorting
Merge Sort / RecursionTaco Tuesday!!!
Runtime Analysis
Last time we said Selection sort is inefficient
Let’s be more specific
We’ll measure the asymptotic runtime of the algorithm
Often use big-O notation
Count the number of “steps” the algorithm take
A step is typically a basic operation (+, -, &&, etc)
Asymptotic runtime
Measures the order of magnitude of the runtime in relation to the size of the input
Name the input size n
For sorting - Size of the input is the number of ...
CSE116的学习笔记-Lec3-1:Sorting_Revisited
Sorting - RevisitedWith First-Order Functions
SortingOrder elements in a data structure according to a comparator function
123val numbers = List(5, -23, -8, 7, -4, 10)val numberSorted = numbers.sortedprintln(numberSorted) // List(-23, -8, -4, 5, 7, 10)
The sorted method returns a new List containing the same elements as the original, but in sorted order
Integer values have a default comparator
Less than function
If an element is less than another element, it must be placed before the other e ...
CSE116的学习笔记-Lec2-6:State_Pattern
State Pattern - Demo!Go check the video, no content on this page!!!
State Pattern - Closing ThoughtsState pattern trade-offs
Pros
Oranizes code when a single class can have very different behavior in different circumstances
Each implemented method is only concerned with the reaction to 1 event (API call) in 1 state
Easy to change or add new behavior after the state pattern is setup
Cons
Can add complexity if there are only a few states or if behavior does not change significantly across state ...
CSE116的学习笔记-Lec2-5:State_Pattern
State Pattern - More ExampleCheck the example Jumper below:)
Jumper
2 Player vertical scrolling platform
Screen scrolls up as the players climb the platforms
The bottom of the screen is game over
Goal: Climb faster than the other player
We’ve seen how physics was added to the game
Platforms / Wall extend StaticObject
Players extend DynamicObject
FUlly compatible with the Physics Engine HW
Jumper - PhysicsWalls and Platforms extend StaticObject
Add behavior after collision w ...
CSE116的学习笔记-Lec2-4:State Pattern
State Pattern
Design Patterns
Approaches to common programming design problems
There are many design patterns
We’ll only focus on the state pattern in this course
For more patterns, search “The Gang of Four”
The Primary goal of design patterns is to simplify the Design and Maintainability of our programs
Applies Polymorphism
Every object contains state and behavior
We use state variables to change the state of an object and its behavior can depend on this state
What if we wa ...
CSE116的学习笔记-Lec2-3:JSON
JSONAn Application of Polymorphism
JSON - Reminder
JSON is [mostly] used to communicate betweent programming languages
Consist of 6 types
String
Number
Boolean
Array
Object
Null
In Python
json.dumps to convert from Python types to JSON string
json.loads to convert from JSON string to Python types
In JavaScript
JSON.stringify to convert from JavaScript types to JSON string
JSON.parse to convert from JSON string to JavaScript types
JSON
What about Scala?{“timestamp”:1550774961,”message” ...
CSE116的学习笔记 Lec2-2:Polymorphism
Polymorphism
Scala Type Hierarchy-1
All objects share Any as their base typoes
Classes extending AnyVal will be stored on the stack
*Unless they are a state variable of an object
Classes extending AnyRef will be stored on the heap
Recall
Scala Type Hierarchy-2
Classes you define extend AnyRef by default
HealthPotion has 6 different types123456val potion1: HealthPotion = new HealthPotion(new PhysicsVector(), new PhysicsVector(), new PhysicsVector(), 6) val potion2: InanimateObject = new Heal ...
CSE116的学习笔记 Lec2-1:Inheritance
Inheritance
Scala Type Hierarchy
All objects share Any as their base types
Classes extending AnyVal will be stored on the stack
Classes extending AnyRef will be stored on the heap
Overview
Let’s do some world building
If we’re making a game, we’ll want various objects that will interact with each other
we’ll setup a simple game where
Each player has a set health and strength
Players can pick up and throw balls
If a player gets hit with a ball, they lose health
Players can collect health pot ...