New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Introduction to Programming

by: Roma Larkin II

Introduction to Programming CPSC 112

Marketplace > Yale University > ComputerScienence > CPSC 112 > Introduction to Programming
Roma Larkin II
GPA 3.73


Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 50 page Class Notes was uploaded by Roma Larkin II on Thursday October 29, 2015. The Class Notes belongs to CPSC 112 at Yale University taught by Staff in Fall. Since its upload, it has received 97 views. For similar materials see /class/231019/cpsc-112-yale-university in ComputerScienence at Yale University.


Reviews for Introduction to Programming


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/29/15
Lecture 1 Sept 5 Welcome and Introduction Hand out and discuss the CS Courses summary discuss goals of CS1 12 Also introduce TA s On the surface syntax Underneath algorithms and data structures Classroom experiment 1 Have eight people come fonNard and stand in line sidebyside 2 Choose another person as the computer and ask himher to rearrange the line in order of height 3 Then ask How did you do that Suppose you were an employer and you had to spell out every last detail ie write down directions Point out that that s what you have to do when programming a computer 4 Point out issues such as access to the people in the line moving from one to the other measuringcomparing the height of people and so on 5 Have students hold hands work through an algorithm that works and point out its relationship to a linked list 6 Mention Google Ask Find the tallest shortest nth tallest etc What is the algorithm Point out access issue again and mention addressing 7 Now ask Find the person who is exactly 6 feet tall if one exists and point out relationship to Google search How do we do that Get seven more volunteers and create a tree structure Ask how many steps it takes to nd the person using a tree compared to a linear list Point out that the tree is logarithmic and the list is linear Show a chart of the amazing difference between the two 000 So which representation do you think Google uses to store its data Point out relationship between speci cation mathematical algorithm computational but abstract and program computational but concrete Add to that a compiler interpreter sequence with object machine code and pointing out that the compiler interpreter isjust another program software and the computer CPU isjust a hardware interpreter Introduce C briefhistory why we use it in CS112 One of the details that will drive you crazy is the syntax of C or pretty much any language It is totally unforgiving A misplaced semicolon or misspelled word will crash the program It requires patience Mention Visual Studio NET it is an example ofan integrated development environment or IDE Everything else is in the details graphical user interfaces graphics and animation multimedia databases the operating system the Internet and so on The NET library has millions of lines of code that can be reused in many different applications Ask a nal question If I can give you a mathematically precise specification do you think that it s always possible to come up with an algorithm to meet that speci cation Ask for show of hands Then explain the halting problem Point out that it s a program looking at a program just like VS so don t expect miracles At end 1 Mention website textbook TA s grading syllabus assignments etc 2 Review grading homework and exams 3 Mention Visual Studio the VS Servers and Remote Desktop Connection 4 Each student should send an email to one of the TA s with their Netd I will give a demo of Visual Studio on one ofthe VS Servers on Friday Lecture 2 Sept 8 Reminders The website is pluckycsyaleedu TA of ce hours are now posted You should join CPSC112 on classesv2 server Problem Set 0 has been posted on the website due anytime next week we just want to be sure that you can log on use the software etc You must send your netid to one ofthe TA 3 in order to do the assignment OakA A Today A look at our rst C program Warning I will go painfully slow at rst but will speed up rapidly so stay with me Suppose I write the following x 42 y X2 Everybody s Ok with this right Now suppose I write number 42 squared number2 Everybody still Ok In writing programs we often choose descriptive names for our variables instead ofx y a b c etc Furthermore we don t have the option usually of superscripting the exponent 2 above so we need to instead write number 42 squared number number We will learn later that there is also a library function that will square a number discuss here the issue of whitespace and the need for semicolons Now the question is what do we do with the result of this calculation One thing we could do is print it out on the console explain WriteLine squared As it turns out there are zillions well not quite of functions procedures or methods in C and they are organized into hierarchical libraries so in fact what we really need to write is SystemConsoleWriteLine squared give reverse analogy to URLs for example pluckycsyaleedu googlecom mapsgooglecom To get even fancier we could write SystemConsoleWriteLine The square of 0 is 1 number squared explain Ok now how do we get this whole thing to run We need to type it into a le and somehow feed it to the C compiler or interpreter But alas to do that we need to add the following boilerplate public class Square static void Main Draw hierarchical picture ofa class method and statements What s worse here s what Visual Studio does using System namespace ConsoleApplicationl class Square STAThread static void Mainstring args l l Give analogy to digging a hole with a shovel or backhoe Lecture 3 Sept 11 Demonstration of VSNET Lecture 4 Sept 13 Mostly from Chapter 2 If you ask a real estate agent what the three most important things in real estate are she ll say location location location If you ask me what the three most important things in programming are I d say abstraction abstraction abstraction What is abstraction Webster says abstract vt 1 remove separate 2 to consider apart from application to a particular instance Programming languages provide varying degrees of abstraction mechanisms and C does a pretty good job One important abstraction mechanism is the method in C In the example above we de ned one class and one method We can also define a second method public static int SquareItint number i return number number i and use it instead of number number Q Why is this better Some answers 1 Improved clan39ty readability and is more concise 2 Allows repeating the operation in many places recall Webster above 3 To make a change we only need to do it one place We could also define a new class and place the new method within the class public class Squarelt i public static int Squareint number return number number i l But then to use it we would write SquareltSquarenumberquot Q Why is this better Some answers 1 Modularity it allows us to group common methods together within the same class again recall Webster 2 Scalability it provides a good way to organize large programs Back up for bigger picture Draw diagram of software lifecycle Requirements analysis Speci cation Design ImplementationCoding Testing and Maintenance Mention that dominant cost for large systems is in maintenance a dollar saved in design is worth ten in maintenance One ofthe most dif cult things is determining in advance what the best abstractions are the language provides abstraction mechanisms but the software designer must define what the actual abstractions are for a particular application Variables and numbers Discuss difference between variables in C and variables in math mention that variables are an abstraction of a location in memory Discuss differences between int number declaration number 42 assignment int number 42 declaration assignment initialization number number 1 assignment changes old value based on old value const int number 42 a constant not allowed to change number Mention comments and l Mention types int is the type of xed precision integers which is the type of numbers that ts more or less into one 32bit memory address Since the encoding from computer to computer is sometimes different it would be nice to know what the max is So it is in intMaXValue What happens when the sum of two numbers say exceeds the max Overflow results Note integer over owis notsignaled as an errorin C or in many languages Be careful Q What is the meaning of abc How about abc or abc Discuss precedence and associativity If in doubt add parentheses Discuss difference between syntax and semantics but note how the two are intricately tied together Increment and decrement operations see page 5053 bad idea in language design My personal opinion 80 therefore I m going to use instructor privileges and not discuss them Input from the Console So far we have talked about outputto the console via ConsoleWriteLine What about input There is a method called ConsoleReadLine that reads a line of input text from the console It takes no argument but returns a string result in contrast to WriteLine which takes a string argument and returns no result ie void 80 for example str ConsoleReadLine lfthe user types hello the str will have the string value hello lfthe user type 123quot str will have the string value 123quot Note that this is a string not a number T convert it into a number we have to parse it num intParsestr Note that this intParse is the parser for int s Other kinds of numbers have their own parsers Finally discuss homework assignment Problem Set 1 Lecture 5 Sept 15 Discuss difference between expressions return a useful result and commands may or may not return a useful result but also cause side effects Then discuss the dangers of mixing the two commands inside of expressions depend on the evaluation order which although wellde ned in C is considered by many to be bad style Related aspects of methods 1 Parameters in call can take constants variables or expressions 2 There might be no operations example printing something void 0 no return The last point highlights that methods may be functions or procedures or both Explain the differences Answered lots of questions including the issues of callbyvalue vs for later in the course callbyreference Chapter 3 Control Structures So far all of our programs have been straight line programs there is one sequential ow of control But what if we want to 0 Do one thing or something else depending on some kind ofa test 0 Repeat something a certain number oftimes To handle these situations we need control structures The text gives a cute example involving getting up in the morning Here is a different one If wearebeingattacked then trydiplomacy if thatdoesn twork then attacktheenemy othenNise peace othenNise peace or while alQaedais stillactive occupyIraq How do we do this in C Syntactically things are a little different then is not used otherwise is else and is optional But semantically the idea is sound And semantically we need to understand what Boolean values and relational operators are In C the two Boolean values are true and false which if printed out become True and False resp The Boolean type is bool So I can do bool test true More importantly there are various mostly infix operators whose result is of type bool Many of these have to do with arithmetic gt lt gt lt But the latter two l and can be used with other data types give examples Lecture 6 Sept 18 The syntax of a conditional statement is If ltBooleanexpressiongt ltstatementgt For example If temperaturegt212 WriteLine The boiler is about to blowl Note the indenting is convention and is considered good style lfthere are multiple things to do they must be enclosed in curly braces add a statement SoundAlarm to the above This is sometime called a onearmed conditional because there is no othenNise part lfwe want an othenNise part we use an else clause If temperaturegt212 WriteLine The boiler is about to blowl else WriteLine Everything is cool pardon the pun And once again if there s more than one thing to do add parens Flowcharts The ow of control specified by conditional statements is sometimes better seen graphically using a flow chart There are several kinds of nodes diamond shaped conditional with 1 entry and 2 exits a simple command block and a join circle give examples and note that the join can be eliminated While Loop The if else statement allows us to fork the flow of control But in order to repeat things we need something more Graphically using a flow chart we need something like this show the ow chart for while Fig 39 which looksjust like a conditional but with feedback or a loop ie the graph is cyclic Q How can we get this loop in C A Older languages provided something called a goto statement but this resulted in unwieldy programs Better is to have one construct that does it all In C this is called a while loop and has syntax while ltBooleanexpressiongt statement Lecture 7 Sept 20 Go over solution to PS1 online with VSNET Work through a program to compute the average ofa number oftest scores typed in by the user using a sentinel say 1 to indicate that there are no more test scores Draw a owchart rst then develop code online Note the text computes a sum but the average is more complex because you need to keep track ofthe number of scores Lecture 8 Sept 22 Review solution to averaging program from last lecture Point out that o Querying the user is more complex for P82 Q3 because there I am asking that if the number typed is out of range the number is ignored and the user is asked to try again Ask the students how they will do this Point out that there is a bug in the averaging program division by zero How do we x this Develop further by showing how loops can be nested in this case create an outer loop that asks the question at the end Do you want to compute another average and if the user types yes then it starts over othenNise it ends Also show how this can be done by making the current program a method with a name other than Main and then sticking it inside ofa loop in the new Main Chapter 4 More Control Structures and Tvpes lntroduce ampamp H and i not briefly discussed in a previous lecture Discuss problem with dangling else using If ltcondition1gt lf ltcondition2gt ltstatement1gt else ltstatement2gt Q which if is the else matched up with A in C the nearest if in this case the nested one Mention that whitespace doesn t matter To get the other semantics we need to do If ltcondition1gt If ltcondition2gt ltstatement1gt else ltstatement2gt Discuss if followed by lots of else ifquots Then introduce switch statement switch lteXpressiongt case 0 ltstatementsgt break square brackets means optional case 1 ltstatementsgt break default ltstatementsgt break Mention that there are lots of ways to do P82 Q3 could use straight conditionals using ampamp could use nested if39s or could use switch Lecture 9 Sept 25 Mention that o The TA Yitong Yin has put a file PS1gradetxt into your VSNET folder which should contain a grade and if necessary some comments lfyou did not get such a le let me know right away Overall the grades were very good mostly A s Also who is jsg63 If you have a question problem try to resolve it with the TA rst ifthat fails come see me Solutions to PS1 are online Reminder P82 is due tonight at 1159 Open the floor to questions Finish Chapter 4 We ve discussed bools and relational operators as well as the switch statement Today we will look at the for and do loops do loop The do loop is just a variation of while and is often called repeat in other languages draw owchart for while then for do The syntax is do ltstatementgt while ltconditiongt Example recall the problem ofrepeating an entire program over and over again assuming that the user wants to Suppose that Averager is this program Using a while loop we would write something like Averager userResponse QueryUser returns yes or no while userResponse yes Averager userResponse QueryUser But using do we could just do pardon the pun this instead do Averager userResponse QueryUser while userResponse yes Lecture 10 Sept 271 Ask the class how they are doing Mention that this is about the point in the semester where start to lose some people please let me know how you are doing either now or in private Go over P82 quickly if possible online Then try to finish Chapter 4 for loop The following ow of control often repeats itself draw diagram from page 136 One could try to abstract this behavior into a method and that would be a Good Thing but one would have a little dif culty with the update part 80 C and several other languages goes a step further by providing special syntax to capture this behavior which makes it even easier to write for ltinitializegt lttestgt ltupdategt ltstatementgt The most idiomatic way to use this is when doing something a specific number of times For example adding the numbers from 1 to 10 int sum 0 for int i 1 ilt10 ii1 sum sum 1 Types Q What types have we discussed so far A int bool string double Here are a few more starting with mumbers 0 oat singleprecision oatingpoint decimal numbers 0 long doubleprecision integers I I lntegers I Floating point I Sin le recision int I float I Double precision Low I double I There are other forms of integers namely shorter ones but they are less often used Mention Math library Mathxxx where xxx is Sin Cos Min Max Log Exp Pow Sqrt Pl etc Characters Characters type char are what make up strings and are written a b c and so on Special characters include n new line r return to beginning of line t tab b backspace double quote 0 backslash which can also be included in strings So I can write char c1 a char c2 n char c3 But I can t write char c4 ab Mention that chars can be used in a switch statement Also mention the String library such as StringFormat which constructs a string using the 0 etc format as with ConsoleWriteLine Enumerations Numbers are good for representing quantitative things in the real world like distance or grades or size or weight But suppose we want to represent something like gender We could use bool to encode it such as true represents female and false represents male But what about colors Well perhaps we could encode using numbers And what about states Well perhaps strings Generally these encoding can lead to trouble they are not perspicuous and they can lead to programming errors not caught by the type system Better enumerations For example enum GenderMale Female enum Color Red Green Blue lndigo Violet What the book doesn t tell you is that these have to be declared at the level ofa method ie inside the class but not inside the method Then once you do that you can do Gender g GenderFemale Color c ColorBlue And so on Furthermore and what the book doesn t say you can use these with switch statements switch c case ColorRed case ColorBlue But in reality this is a cheat enumerations are really just integers Lecture 11 Sept 29 Chapter 5 I will introduce today a new abstraction mechanism objects C is an object oriented programming language as is Java and thus relies heavily on objects But so far we have avoided them indeed objects are not necessary but they are a useful tool in our toolbox of abstraction mechanisms Although useful they can be overused and in particular are brittle rigid in exible so be judicious Examples 1 The sorting we did on day one instead ofone sorter perhaps every value should know how to sort itself 2 Printing same idea 3 From textbook Avending machine An object consists of three things state behavior and identity Q In the case of a vending machine what are these three things A 0 State contents and money but more details it may be broken it might be able to make change might not 0 Behavior interaction with user but more detail that its public behavior but it might also have some kind of private behavior when it interacts with the vending machine supplier It also might have internal behavior Identity there may be many vending machines and they all have different state The last point brings us to an important issue An object is an instance ofa class So far we have used classes to organize code but in general they can be used in a far more powerful way they can describe a template for an object From that template many instances objects can be created each with a unique identity A class declaration in C looks like this public class ltnameofclassgt ltinstance variablesgt de ne the state ofobjects ltconstructorsgt for creating unique identities of objects ltinstance methodsgt describe the behavior of objects For example let s design a really simple vending machine in which everything is free so no money is exchanged and there are only three items candy bars crackers and chips public class VendingMaching instance variables note that they are private private int candy 0 private int crackers 0 private int chips 0 constructors note 1 always the same name as the class and 2 have no result type Q what is the result type public VendingMachine candy 0 crackers 0 chips 0 public VendingMachineint ca int cr int ch candy ca crackers cr chips ch methods this is pretty much as we have always done except that they have direct access to the instance varables public bool GetCandy if candy gt O candy candy1 return true else return false similarly for GetCrackers and GetChips public void AddCandyint amt candy candy amt similarly for AddCrackers and AddChips To create an instance of this class we do VendingMachine vm1 new VendingMachine VendingMachine vm2 new VendingMachine2442 Draw picture of the two vending machines with two different states Interactions with an object are often described as sending messages to it and pragmatically amount to invoking methods Point out that it is only the public methods or variables that we can access everything else is hidden Now walk through several interactions with the vending machines Lecture 12 Oct 2 Answer any questions about the homework that is due tonight Q what abstraction mechanisms have we talked about in C so far A variables naming methods loops three kinds and classes Note that classes are used in two ways 0 To organize code ie group common methods together 0 As a template for objects The latter is what we talked about on Friday To review a method consists of 0 State instance variables 0 Behavior instance methods 0 Identity constructors Recall vending machine example and sketch design Now it turns out that the organizational idea of classes has a place with objects too Class variable and methods Speci cally sometimes we want to keep track of some information about all of the objects that are instances of a class For example we may want to keep track of the total number of transactions conducted by users of all of the vending machines To do this we use class variables and class methods which are indicated using the static keyword Ring a bell private static int transactions 0 public static int NumberOfTransactions return transactions Then each of the user methods getCandy getCrackers and getChips needs to increment the class variable transactions by one To invoke NumberOfTransactions we use the class name as in int n VendingMachineNumberOfTransactions Draw a picture of multiple instances of VendingMachine with transactions shown as shared state Lecture 13 Oct 4 Go over solutions to PS3 online The use of classes to both organize code and be templates for objects is confusing So let me summarize To organize code class MyMath public static double pi 314 public global variable private static int n 0 private global variable public static double Sthdouble X public method To access methods or globals we use the class name double sqx MyMathSthX double y MyMathpi 1 MyMathn will not work As a template for ob39ects public class VM private static double transactions 0 class variables private double candy O instance variables public static int GetTransactions class method public void GetCandy instance method But other combinations of private and static are possible 0 private int Poly private instance method 0 private static Accounting private class method 0 and so on Lecture 14 Oct 6 Referring to thyself Objects can be passed to other methods For example VendingMachine vm1 new VendingMachine Foovm1 public static void Foov vgetCandy But sometime we need to pass the object from within the method that is within the object For example suppose in getCandy we want to pass the current object to another method because that method may want to invoke some method within the object For example public bool GetCandy if candy gt O candy candy1 return true else OwnerReportEmptyCandym return false public void ReportEmptyCandyvm vmfillCandy Note the keyword this it refers to the current object Strings The textbook spends a lot of time on strings I don t intend to do this But there is one important thing to note Strings are objects When we write string s Hello World It is really just special syntax for something like string s new String H e l l o etc Obiect Identity and Obiect References A really important thing about objects is that they are passed to methods by reference and not by value For example s above is really a reference to the string and not the string itself Draw picture 80 if we write string t s C will create a new variable t having the same reference as s thus the string is not copied This is important for ef ciency reasons but it also has subtle semantics For example does st Since they have the same reference and thus point to the same string the answer is of course yes But suppose we write string s Hello world string t Hello world Now does st The answer is no because the references are different Draw picture 80 how do we compare two strings with unique identities The answer is use the Equals method sEqualst The book also mentions a more general CompareTo method which returns 1 if sltt 0 if st and 1 if sgtt where the ordering reflects the lexicographic ordering of strings The same is true of vending machines lf vm1 is a vending machine and we assign vm2 to vm1 then vm1vm2 returns true But if they are unique vending machines then the answer will be false even if theirinternal state is the same Q How do we compare two vending machines for equality A Write an Equals methodll public bool EqualsVendingMachine v vca vHowMuchCandy vcr vHowMuchCrackers vch vHowMuchChips return vcacandy ampamp vcrcrackers ampamp vchchips Then we just do vm1Equalvm2 Q How can we make candy crackers etc more abstract A Change them to thisHowMuchCandyquot etcl Comment on how all this relates to PS4 Tic Tac Toe Lecture 15 Oct 9 Give general advice regarding PS4 the Tic Tac Toe program and then open the oor for questions General advice Read through the assignment and follow the top level directions exactly Start with the classes and write them out using VSNETto help you organize things Then go back and ll in stuff as you need it This is called TopDown Design Go through process on blackboard First write classes with sections for state identity and behavior Then add constructors Then add methods Then add instance variables but don t ll in completely let them gure this out Also add Player s class variable for Random and explain Then pop up to Main Point out that it s here where the objects are created and used And it s here that a flowchart might help out more If you are unsure what the individual methods should do try writing the code that uses them rst again this is topdown design 80 in essence try writing the code for Main rst Advice 0 Use private methods for auxiliary functions 0 Convert xy board position to a unique single number that can be used with a switch statement Ask how that can be done Lecture 16 Oct 111 Remind class where we stand We will nish Chapter 6 today We will do Chapter 7 today and Friday Problem Set on Chapter 7 will be due Monday or Wed The following Monday will be a midterm exam comment on that After that graphics and animation Chapter 8 Le fun Interfaces nal topic in Chapter 6 We can raise the level ofabstraction of objects and classes one more level through the use of interfaces which basically separate the typespeci cation of a class from its implementation The motivation here is that many different objects might share the same interface and thus we d like to abstract them We will not use them much in this class but you need to know about them in orderto use libraries such as NET One example is from Tic Tac Toe we have a Player but also a SmartPIayer The example from the book will also do well There are many kinds of cars vans sports cars SUV s etc but they all share a common interface starting and stopping and so on So we write public interface lDriveable starting an interface name with l is a convention void Start void Stop void Accelerate void Decelerate void Turnstring direction A method using this interface should work for any object that satis es the interface For example public static void GoFonNardlDriveable d the interface looks just like a class dStart dAccelerate dDecelerate Then we can have vans and sports cars say that implement this interface class SportsCar lDriveable note new syntax class Van lDriveable To use all this we do SportsCar s new SportsCar Van v new Van GoFonNards in a sense we have overloaded GoForward GoFonNardv any object satisfying the lDriveable interface is Ok Also note the SportsCar and Van classes could have additional public methods Also there private instance variables don t have to be the same That s the real point internally they may be quite different from each other Arrays Chapter 7 Several times I commented on the need for arrays For example the bins in the histogram program long lists of words in the crazy paragraph program and the 3by3 board in the Tic Tac Toe program An array is an object that contains an arbitrarily long but xed sequence of values The values may be retrieved or changed by indexing into the array with an integer Because arrays are so common there is special syntax for dealing with them but you should think of them as objects By analogy strings have special syntax too but recall that they are just objects Examples of array creation lnt intList 01 23 double numList 023147 char charList h e l l o string stringList hello world VendingMaching me new VendingMachine new VendingMachine123 And so on Lecture 17 Oct 13 Arrays cont d Review array creation above To access an element in an array do this intList2 9 2 charList1 9 e numList4 9 runtime error If your index out of range you will get a runtime error this is bad but in some languages you don t get an error Mention C and the buffer over ow exploit used by hackers You can also declare an array without initializing it like this int intList new int10 Now it looks more like an object You can also do things like intListLength 9 10 Indeed the 10 above could be a variable or in fact any computation such as int intList new intsize42 Example static method to add all the numbers stored in an array public static int AddArrayint a int s aLength int sum 0 for int i0 ilts ii1 sum sum ai Key new point like objects arrays have identity and are not copied in method calls This means that we can change an array in a method call and it will be felt by the callee Draw picture of shared state Example static method to reverse the elements in an array public static int RevArrayint a int right aLength int left 0 while leftltright swapleftrighta left left1 right right1 private static void swapija int temp ai ai all all temp To use this we could do reva RevArraya But since RevArray actually modi es the array we could also do RevArraya reva a And we would probably make the return type void But what if we wanted to keep the old a and generate a new reversed a No problem create a new array and write a simpler loop to copy from one to the other Work through code for RevCopyArray We can also copy an array via ArrayCopy old oldStart new newStart n Where old is the array to be copied new if the array to be copied into oldStart and newStart are the starting indices and n is the number of elements to be copied The last problem set was tough long and conceptually difficult The next one will be easier and then there s the midterm The new problem set has two questions 1 Rewrite the histogram program Mention that a Rewriting old code is a Good Thing b Hint don t use nested ifthenelse s instead compute the array index directly 2 Bubblesort Finally solving the problem we started on Day 1 This problem will require nestedoops Lecture 18 Oct 16 Multidimensional Arrays A 2 by2 array is often called a matrix But we can also have 3by3 arrays and beyond As an example suppose I have an array to keep track of your grades In other words l have an array of students and each student has an array ofgrades lfl represent each student as an integer I can write int studs new int 5512 This is a matrix of 55 students each having twelve grades Notice the syntax for the type the constructor and the access How confusing If we know the scores already we can input them directly like this lnt studs 5246 5357 78 6 3 This creates a 3by4 array We can access an element in the matrix by writing studsij which is the jth score of the ith student C stores arrays like this is contiguous memory and computes an index to access the array elements in one step To get the size of the different dimensions ofthe array we do studsGetLengthO for the size of the first dimension and studsGetLength1 for the size of the seond dimension Arrays of Arrays It s also possible to have arrays of arrays where the arrays stored as elements have different lengths For example another professor may allow a varying numbers of grades To handle this we can do int studs2 new int3 This says we have an array of3 arrays but we don t know how long each of those 3 arrays is Later we could do studs20 new int4 studs21 new int2 and so on Or we could do int studs2 new int3 new int4 new int3 which is the same as int studs2 new int4 new int3 We can access an element in the array of arrays by writing studsij which is the jth score of the ith student Note now that C cannot access an element in one steps it takes two steps 80 arrays of arrays are more general but slightly less ef cient Note studsLength gives the size of studs and studsnLength gives the size of the nth element in the array Example C8112 grader Work through example of computing the grades for C8112 Homework Review P85 1 Redoing the histogram program and 2 Bubblesort Then open the floor for questions Lecture 19 Oct 18 Chapter 8 EventDriven Programming graphics Chapter 9 User Interfaces GUs Chapter 13 Threads and Animation Why graphics animation and GUls are different from the console ask the class then ll out this list by making two columns console graphics window There is only one console but there may be many different graphics windows and thus we need to be able to create them destroy them manipulate them and keep track of them I The console takes sequential text whereas a graphics window is random access any point on the screen can be written to A The default console size is 25x80 ie can display 2000 characters A graphics window may be 1000x1000 pixels or larger ie over 1 million pixels A The console deals only with text a graphics window deals with pixels and colors as well as text having different fonts etc 01 The console takes sequential text input from one location a GUI can take text input from multiple locations It can also take input from the mouse both mouse location and mouse clicks 0 The console is completely sequential there s only one thing going on at a time whereas with graphics many things can be going on concurrently for example animation and user input 7 When a console is hidden and then reappears we expect the same thing to appear each time Q how does this happen This is not always true however for a graphics window and we will want to specify what happens 80 graphics animation and GUI programming are necessarily more complex than what we have seen so far But there aren t too may drastically new ideas most of it builds on what we already know There are a couple of big exceptions however The graphics window coordinate system The window is Cartesian but with a slightly different orientation that what is taught in math classes For an mxn window point 00 is in the upper left and m1n1 is in the lower right draw picture This odd numbering scheme is a holdover from people programming consoles where the output would appear lefttoright and toptobottom The graphics context Since there are possibly many different graphics windows we need to distinguish them These are called graphics contexts in Windows but we will just call them graphics objects The Graphics class supports many kinds ofoperations For example gDrawStringltstringgt ltfontgt ltbrushgt ltxcoordgt ltycoordgt Many more methods are discussed in the book Lecture 20 Oct 20 Go over P85 online Reveiw for the MidTerm Exam If you follow the tableofcontents ofthe book you should be ne There are however some overarching themes the most important being Abstraction consolidating repeating patterns into one place for clarity modularity and reusability Let s list the many abstractions that we ve seen variables naming values methods naming subprograms primitive operations on primitive types loops three kinds while do and for arrays as a collection of names control structures conditional and switch classes for organizing code 0 classes as templates for objects Understanding the difference between and subtleties ofthe last two abstraction mechanisms above is confusing but important Objects vs pn39mitive values The latter include Booleans integers doubles and characters Pretty much everything else is an object even though there is special syntax for some ofthem eg strings and arrays Types 0 Everything has a type in C 0 Method declarations must include the types ofarguments and the type of the result 0 Array elements must all be of the same type 0 Sometimes C will coerce things from one type to another for example int to double but depending on it requires caution Objects 0 Have three components state de ned by instance variables identity de ned by constructors and behavior defined by instance methods 0 Are rstclass can be passed as argument to methods stored in data structures or other objects and returned as values from method calls 0 Are always passed around via references whereas primitive values are not they are copied o A class can also have class variables and class methods that are shared by all instances ie objects of the class Lecture 21 Oct 23 Midterm Exam Lecture 22 Oct 25 Hand back and go over midterm exam General comments c The Problem Sets scores have been high lnclass exams are useful for differentiating those who really know the material from those who don t The Bad News the average was only 8072 the mean was 845 The Good News I drop the lowest score But the nal will be similarto the midterm only longer and it counts as two grades So if you didn t do well on the midterm you need to gure out why 0 Are you getting too much help 0 Was the timepressure a problem 0 Are you missing some fundamental concepts 0 There will be five more Problem Sets 0 The next three will be due in Wednesdays Nov 1 8 and 15 0 Then there s Spring Break 0 The nal two will due on Fridays Dec 1 and 8 The new topic that we are on is Chapter 8 EventDriven Programming I will also sneak in an important powerful enabling tool recursion Lecture 23 Oct 27 Forms and Inheritance Form is a fairly fancy and complex class for creating forms which are the basis of a GUI Unfortunately using them effectively requires understanding a concept known as inheritance which is not covered until Chapter 10 sigh So we need to do a little detour The idea of inheritance is this Sometimes we want to create a class that has all of the methods in some existing class plus more In the case of Form we will often want to inherit all of its methods and de ne some of our own Furthermore we sometimes less often want to override one of the methods in the parent class Let s do an example online 0 Show how to create a V ndows Application project 0 Go through Example 81 exactly as it is in textbook but pointing out the extra designer stuff that VSNET introduces 0 Explain in detail every line of code including inheritance overriding events graphics context etc Note the definition of Main within the class itself to me this is a bit odd So let s pull it out into a separate class 0 We can even invoke it twice in succession Note that o The windows appear one at a time we need concurrency to do othenNise o If we make count static it retains the count between invocations 0 Now add some code to draw an ellipse Recursion Recursion is more powerful than iteration anything expressible as an iteration can be expressed recursively but not vice versa Consider the multiplication example from the midterm exam Here is a recursive method to achieve the same result public int Prodint m int n if n0 return m else return m Prodmn1 Even better we can use a conditional expression which has the form lttestgt lttrueresultgt ltfaseresultgt Note that this is an expression not a command So we can write public int Prodint m int n return n0 m m Prodmn1 How elegant Work through another example factorial starting with the mathematical de nition and then translating to C Lecture 24 Oct 301 Announcements Ask the students what days they would prefer for TA of ce hours In particular if assignments are due on Wednesday evenings for the next three weeks is there any reason to have a Sunday office hour Problem 2 on PS6 has a problem doubleclicking doesn t appear to work right 80 forget about double clicks When a single click occurs make a blue rectangle appear When a subsequent click inside the rectangle appears make it disappear And so on Once a rectangle appears a click outside of the rectangle should not do anything Show the students the fractal snow ake Mouse Events Mouse movement and mouse clicks create events just like uncovering a window They are sent to methods such as OnMouseDown and OnMouseUp For example protected override void OnMouseDownMouseEventArgs e eX is the x position of mouse click eY is the y position of mouse click You will need this on Problem 2 of PS6 Review of recursion Remember the slogan To iterate is human To recurse divine Anonymous Point out that recursion will be needed on Problem Set 6 but not the conditional expression that I introduced on Friday One more example binary search Recall the sorting program for an array Suppose we now want to see ifa particular number called the key is in the array We could use a loop and search the array sequentially Q what is the worst case and average number of steps required to do this Or we could use the following method Compare the key to the middle element lfthey re the same return true OthenNise ifthe key is less than the middle element recursively search the left half othenNise search the right half Q Can this be done with a loop A Yes a while loop would do the trick But a cleaner solution is to use recursion as follows Let s assume that the array contains words instead of integers which makes it more realistic and also less confusing because ints are also used for array indices Work through this code public bool BinSearchstring a string key int left int right if left gt right return false else mid leftright2 switch StringComparekey amid case 1 return BinSearchakeyleftmid1 case 0 return true case 1 return BinSearchakeymid1right To call this in Main do BinSearcha foobar 0 aLength1 Lecture 25 Nov 1 Are there any questions regarding PS6 mention my experience with Q2 Chapter 9 Graphical User Interfaces GUls in C rely on inheritance and the eventdriven model of computation that we saw for graphics The new ideas are 0 Controls buttons labels etc are dynamic visual objects that we create and place on a form 0 Delegates are a way to create and register eventhandlers for these objects since each of them may behave differently o VSNET can write a lot of the boilerplate code for us The Control class contains lots of stuff and lots of classes are derived from it Draw parts of Fig 91 in text showing at least Button Label and Form There s also one other idea that keeps arising and that I keep skirting around namely modi ers Here is a quick summary ofthem Fig 1013 in text 0 public Accessible anywhere the class name is accessible 0 protected internal Accessible in derived classes and in the program in which the variable or method is declared o protected Accessible in derived classes 0 internal Accessible in the program in which the variable or method is declared 0 private Accessible only in the class in which the variable or method is declared These can be arranged in degree of privacy as follows most public public protecteld internal protected internal private most private For simplicity we could just make everything public But it s better to make things as private as possible while still allowing access where needed Let s work through the example from the book public class Forml SystemWindowsFormsForm Required designer variable private SystemComponentModelContainer components null private Button print new Button private Button clear new Button private Label message new Label public Forml Required lnitializeComponent Text quotButton Pressquot printText quotPrintquot clearText quotClearquot messageText quotMessage goes herequot Size new Size3003OOL messageSize new SizemessagePreferredWidth messagePreferredHeight printLocation new Point203OL clearLocation new Point3O printWidth 30L messageLocation new Point4O printWidth clearWidth 30 ControlsAddprint ControlsAddclear ControlsAddmessage printClick new EventHandlerPrinticlick clearClick new EventHandlerCleariclick protected void Printiclick0bject sender EventArgs e messageText quotHi therequot protected void Cleariclick0bject sender EventArgs e nu messageText l The main entry point for the application STAThread static void Main ApplicationRunnew Forml Lecture 26 Nov 3 Go over solutions to Problem Set 6 online While online demonstrate VSNET s ability to write boilerplate code for GUls Also go over example 91 While doing this point out that No need to repaint Why Chapter 9 covers lots ofdifferent controls widgets The differences between them are in the details you should read through the many examples The most important thing is to embrace the notion ofevent driven programming Think about what needs to happen and when and separate issues of control from issues of display MA to Lecture 27 Nov 61 Ask if there any questions about PS7 Discuss the issue of having one event handler to handle more than one button For this to work one needs to be able to distinguish the buttons The event handler should look something like protected void NumberClickObject sender EventArgs e Note that the rst argument is an Object the mother of all objects This will actually be the Button object that invoked NumberClick So the solution I gave in the hint is to just test to see if sender above is equal to one of the numeric Button objects You can package this in a method as suggested in the hint But there is another way What we are really trying to do is get the value ofa button right But we can get the text of the object so why not do this int i intParseButtonsenderText Note how the sender is coerced cast into the object that it is But there is even another way What we d really like to do is associated the value with the button itself Ordinary Button s don t have such values but we can use inheritance public class NumButton Button the value associated with a numeric button public int Value and then use Button sender Value to get the value when we need it Lecture 28 Nov 8 Delegates The notion of delegates is the one truly new idea in Chapter 9 Delegates are a way to package up methods so that they are first class and can thus be passed in a type safe way to other methods or stored in data structures etc They are used in a critical way in C s Control class namely as event handlers which are also sometimes called callback routines But you can also declare your own delegate like this public delegate ltreturntypegt ltdelegatenamegtltformalparamsamptypesgt For example here is an example from the text public delegate double ComputeDelegatejdouble d This declaration serves two purposes 1 It behaves as a simple class declaration Thus ComputeDelegate serves as both a new type name and a constructor name just like any other class But the odd thing about it as a class declaration is that it contains extra info about types the stuff underlined above That info de nes the function type ofthe argument that is passed to the constructor l The following example is a simpli cation of the one in the text and is motivated once again by abstraction Suppose we see a repeated calculation everything is the same except that in one place we use x and y and in the other place we use u and v 80 we define a method public int Fooint a int b where a and b are the formal parameters Then we replace the original code with Foox y and Foou v Right But now suppose that these repeated calculations are the same except that one operator say or or one method call say for g is different Then we d like to abstract that out too So we d like to write something like this public int Fooint a int b h where h is the formal parameter forthe abstracted operator or method But what should the type ofh be The answer is that it should be delegate type as follows public int Foo int a int b ComputeDelegate h hab Then when we call Foo we have to rst make or or for g or whatever into a delegate as follows ComputeDelegate cd1 new ComputeDelegateMathSqrt ComputeDelegate cd2 new ComputeDelegateArea public double Areadouble r return Mathpi r r Then we can replace the original code with Fooabcd1 Fooabcd2 Note how ComputeDelegate is used in two ways above as a type and as a constructor Using delegates in PS7 Interestingly there s a cool way to use delegates on the Calculator program In the hint I suggested using a common event handler not just forthe numeric keys but also for the three operator keys To distinguish the keys we could convert them into integers as discussed earlier Or we could look at the Text in the keys and do a dispatch on them But there is a better way Just as we stored the number into the numeric keys we could store the operator function into the operator keys which requires a delegate In other words public class OpButton Button public BinOp Op l public delegate int BinOpint i int j Talk through other details like the fact that the Enter key needs to know which operator key was pressed which can be done by storing a delegate in an instance variable Ask if there are other questions on PS7 Lecture 29 Nov 10 Kevin Yip lectured He went over PS7 solutions and also did the following on Exception Handling Concepts What are exceptions Source of exceptions Illegal user inputs eg FormatException Programming errors eg lndexOutOfBoundException System problems eg OutOfMemoryException What can we do with an exception Catch it try catch we want to handle Do not rethrow the exception should not disrupt the normal execution Rethrow the program should be disrupted but we want to do something before that Let the caller handle it we don39t want to handle we don39t know how to handle Finally always execute eg to release resources Can selectively handle some classes of exceptions Actively throw exceptions did not have time to cover using System public class ExceptionConcepts l public static void Main lllegalUserlnputs ProgrammingErrors SystemProblemslOOOOOOL CatchWithoutRethrow CatchAndRethrowlnullL CatchAndRethrow2nullL ActiveThrowll O ActiveThrow2l O static void lllegalUserlnputsU ConsoleWritequotPlease enter an integer quot int i intParseConsoleReadLine static void ProgrammingErrorsU int a new int5 for int iO ilt5 i ai i static void SystemProblemsint i i if igt SystemProblemsi l static void CatchWithoutRethrow int i 0 while i lt O try ConsoleWritequotPlease enter positive integer quot i intParseConsoleReadLine if i lt 0 ConsoleWriteLinequotSorry the number is non positivequot catch FormatException e ConsoleWriteLinequotSorry invalid integerquot static int CatchAndRethrowlstring m try int i sLength return i catchNullReferenceException e ConsoleWriteLinequotnull parameter is not allowedquot throw e static int CatchAndRethrow2string m int i sLength return i catchNullReferenceException throw new NullReferenceExceptionquotnull parameter is not allowedquot e static int ActiveThrowlint numerator int denominator return numerator denominator static int ActiveThrow2int numerator int denominator if denominato throw new DivideByZeroExceptionquotDenominator parameter of ActiveThrow2 should not be zeroquot else return numerator denominator Lecture 30 Nov 13 The conceptually new ideas in Chapter 11 are 1 Exceptions 2 Commandline arguments 3 File lO Kevin lectured on 1 on Friday 2 and 3 are notjust about C they touch on operating systems issues as well Do the following online 0 Show how to use MSDN as a reference tool for C VSNET and libraries 0 Show how a program can be executed by doubleclicking on it discuss binaries and how VSNET is really only for development 0 Show how a program can be executed from a command prompt 0 Show how a program can take commandline arguments Kevin covered 1 and we just did 2 the only other thing you need to know for P88 is 3 above File lO Files are special data structures that are handled by the operating system external to a program Their formats vary from one 08 to another but forthe most part they are the same and programming languages have standard interfaces for dealing with them There are two issues input and output In C we can open a le by creating a stream reader as follows StreamReader input new StreamReader filenametxt This can throw several exceptions such as FileNotFoundException but the generic class of all lO errors is lOException We often don t need to distinguish amongst them so this will do in most cases Once we have successfully created a StreamReader we can read from it by using the familiar ReadLine method String line inputReadLine If we have reached the end ofthe le ReadLine will notthrow an error instead it will return the value null which can be tested via line null Once we are done the le s StreamReader should be closed via inputclose In general the right way to do le input is try StreamReader input new StreamReader filenametxtquot catch lOException e nally inputclose The nally clause ensures that the le is closed regardless ofthe kind oferror that occurs note that an error other than lOException could occur in the first above so it will not be caught andthe code after the catch will not be executed unless a finally clause is included as above For output we use a similar abstraction new StreamWriter output new StreamWriter footxt and familiar friends outputWrite Line Hello World The text also talks about binary les but I will skip this discussion so that we have time for other things Lecture 31 Nov15 Static vs Dynamic Program Structure Conceptually exceptions are doing something different Let s try to understand this better First a few definitions The lexical structure ofa program relates to the way it is laid out as text in a le In C we have things like class A private int i1 public int i2 method Main int a1 string a2 method M2 class B publicintj1 private int j2 method N1 bool b1 double b2 method N2 This layout directly affects the visibility of things for example i1 and i2 are visible to Main and M2 and Main and M2 are visible to each other regardless of their visibility modifiers Also a1 and a2 are only visible inside of M1 This is called lexical scoping Of course modi ers also affect visibility outside ofthe classes but even this is considered a lexical issue In contrast note that Main may call M2 which in turn may call N1 which might call itself recursively 100 times and then call N2 which might then call M1 This forms what is called a method call stack because it behaves like a stack on which you put things on and take things off like a stack of pancakes also called lastin rstout or LIFO The stack is initially empty The program begins with a call to Main so it is placed on the call stack If we draw the stack it grows as method calls are made but then shrinks as method calls return results and also grows again as more calls are made etc draw this as vertical pancake stack MainM2N1N1N1 etc Eventually the stack shrinks till Main is all that is left which then returns to the operating system leaving an empty stack and the program terminates Note that whereas the lexical structure is completely static ie it doesn t change the call stack is very dynamic it changes constantly Now the important thing to remember about exceptions is that they work with respect to the dynamic call stack and have little to do with lexical structure More importantly exceptions allow short cutting of the incremental stacklike behavior For example in the above call stack if M2 s call to N1 were done within a try then if an exception occurs deep within a recursive call to N1 or any other method the control jumps immediately to the try the rest ofthe call stack is completely abandoned Contrast this with another method to handle errors namely the encoding of errors in a data structure this would require de ning that data structure changing the type of each method that would need to pass it back through the call chain and writing code to handle that possibility in each method This is more work for the programmer and less ef cient So exceptions really do add something new to a language Ask if there are any questions regarding P88 due tonight Discuss what remove extra space between words means and mention sSpIit and sToCharArray Announcement made on Classesv2 after class To answer two questions raised in class 1 The reason that you need to initialize the StreamReader and StreamWriter outside of the try block is that the try block introduces a new lexical scope which was exactly the issue that l was lecturing about in class today lfthose variables were only to be used inside the try everything would be fine but since we need to close the streams after exiting the try block we need them to be visible outside of the try block 2 The reason fClose doesn39t work when fis null is quite simple the null object doesn39t have any methods so naturally trying to nd the Close method will fail I should have realized that simple explanation in class sorry Bottom line the quotrightquot way to do this including the use ofquotfinallyquot is StreamReader f null StreamWriter g null try f new StreamReader g new StreamWriter catch nally if flnull fClose if glnull gClose Lecture 32 Nov 17 Go over P88 Point out the issue of try introducing a lexical scope and point out that in fact loops do the same thing Give an overview of remainder of course 0 Chapter 13 Threads and Animation 0 Chapter 16 ASPNET Thanksgiving Break Lecture 33 Nov 27 Welcome back Reminders 0 Two more problems sets both due on Fridays 0 TA hours have changed see website 0 Final exam is at 9am on Monday Dec 18 Chapter 13 Threads and Animation The main new idea in Chapter 13 is threads A thread is a thread of control and there may be more than one of them They are not really parallel which would require more than one processor but are concurrent their execution is interleaved on one processor which is called multitasking Actually they might even be parallel ifyou are using one of the new multicore processors Now it s clear that the operating system say V ndows already does a fair amount of multitasking Ee multiple aps downloading les while other aps are running or even different aspects ofthe same ap animations in web aps etc In addition you may want to program an ap that requires concurrent threads as well How do we do this C has an object type called Thread Threads are created like any other object ie via a call to a constructor Thread t new Thread ltuserde neddelegategt Where ltuserde neddelegategt is the method that you wish to run as a thread but wrapped up as a delegate For example public delegate void MyThreadType This allows any method with no arguments and a void result to be turned into a thread Let s say that method is de ned by public void Down Load File So we would do Thread t new Thread new MyThreadTypeDownLoadFile Or ifyou prefer MyThreadType threadDel new MyThreadTypeDownLoadFile Thread t new Thread threadDel Then to run the thread we just do tStart This in fact doesn t pass control to t but it makes it ready to execute once the current thread relinquishes control to the operating system How does a thread relinquish control There are at least four ways 1 It terminates duh 2 It executes an operation requiring a call to the 08 not just expensive things like le download but even simple things like ConsoleWriteLine 3 It goes to sleep via a call to ThreadSleep in a sense this is a special case of 2 above 4 It is interrupted by the operating system because ofa higherpriority thread that needs to run A thread may put itself to sleep by executing ThreadSleeptime Where time is the sleep time in milliseconds Finally a thread may be Suspended via tSuspend Resumed via tResume Aborted via tAbort Q What do you think happens ifan aborted thread is resumed Or ifa suspended thread is suspended again Or A I m pretty sure that the former will throw an exception whereas the latter will not Why There are too many cases to rememberthem all so just be careful that you know what you are doing in a particular application Note The operating system has a schedulerto decide which thread to execute next The scheduler keeps a queue ie rstin rstout data structure to keep track of the pending threads in a reality is a priority queue but I digress In general the scheduling of threads is not something that is easy to predict so it s best to think of the threads as really executing in parallel There are many things that might interrupt a thread s execution Point out problems A thread may go into an in nite loop and never relinquish control On the other hand sometimes was want the thread to be an in nite loop Two threads may share the same resource could create invalid results Two threads may compete for the same resource deadlockl Lecture 34 Nov 29 Let s consider the problem of animation The example to focus on is 135 in the text since that s what you ll need for your homework assignment Example 135 from text Look at the big picture first public class AnimateBall Form ltinstance methods for ball position buttons and the animation threadgt public AnimateBall ltthe class constructor sets up all the buttons and creates and starts an animation thread based on Rungt public void Run ltthe method corresponding to the animation thread consisting ofan infinite loop to continuously change the position ofthe ball The loop is slowed down via sleepinggt protected override void OnPaint ltpaints the ball in location specified by instance variablesgt protected void AbortClick protected void SuspendClick protected void ResumeClick ltthese are the event handlers for each of the buttonsgt public static void Main ltthe usual main methodgt It s important to understand how all of these pieces work together to achieve the desired result It is done via a combination of eventdriven programming and threads Q What changes are needed for each of the new capabilities asked for in P89 Bouncing ball Horizontal line New button Score label Other functionality Lecture 35 Dec 1 Problems with threads Programming with threads or in general any kind of programming involving concurrency introduces interesting problems Two in particular First simultaneous access to shared data can result in inconsistent ie wrong behavior For example suppose Mom and Dad are depositing to two ATMs at the same time but into the same account Work through example on board showing how the result can be wrong The solution to this is to allow some means to lock the data while it is being processed C has a particular way to do this which I will not describe see Chapter 13 In general there are lots of ways to do this Second most looking mechanisms lead to other problems such as deadlock For example suppose that two threads need to gain access to two resources Work through example leading to deadlock transferring money from one account into a second thus requiring locks on both ofthem HTML Chapter 16 is about ASPNET a way to run programs from within web pages But first this requires knowing something about HTML or HyperText Markup Language which is the main format for web pages start with explanation of how browsers work URLs etc Here is the basic set of HTML tags as explained in the text ltbrgt break ltpgt new paragraph ltemgt ltemgt text emphasis lttitlegt lttitlegt title at top of window lth1gt lth1gt toplevel header lth2gt lth2gt secondlevel header and so on six levels deep ltulgt ltulgt unodered list ltigt list item note no ending lta href gt ltagt an anchor or hypertext link ltimg srcquot gt an image Work through some examples online Lecture 36 Dec 4 Draw diagram of CS1 12 a server plucky and other machines clients and emphasize the following points CS112 a and plucky communicate via a LAN localarea network using a V ndows XP protocol that allows le sharing 80 plucky is a le server Plucky is also a web server recall that the course website is there The web server uses HTTP protocol to communicate with other machines connected via a WAN widearea network The m drive on your account is actually in a subdirectory called CS112 containing all of the student accounts It can be accessed via le sharing for example I and the TA s have access to it And it can now be accessed via http as httppluckycsyaleeducs112asp Show VSNET and Wordpad running on CS112 a Show an HTML le on plucky and explain how it s downloaded via HTTP Now show an ASP aspx le which is just like an HTML le except that the server does some processing on it before sending it off if you hit view source in a browser you can see the difference This processing might in fact involved executing a C program either embedded in the ASP le or as code behind Communications using HTTP can be done via get or post explain and diagram the differences Key point there is no state in these transactions everything must be explicitly saved somewhere o On the server this is usually done via a database or writing to a le or whatever o On the client this is done via session tracking which is library in C that basically reads to and writes from a special file called a cookie The threetier architecture is the client the web server and a database We are not using a database but you can think of our writing to a le as the third tier instead Describe the homework assignment in more detail saying where the bits of state need to go Point out the race condition on the file that s on the server resolving that is one ofthe things that a database does Lecture 37 Dec 6 Online Go over the P89 solution Also show the GetOrder examples from the text 0 Show the webconfig le 0 Show what happens when an error occurs in the ast file 0 Show what happens when an error occurs in the cs file For PS10 point out two issues regarding the state First the le on the serverside should be referenced as quotecsllZltnetidgtResultstxtquot In other words do something like StreamReader f new StreamReaderquotecsllZltnetidgtResultstxtquot But ifyou want to use backslashes you need to use quotecsllZltnetidgtResultstxtquot Why Second on the client side we want to use session tracking The Session object is an association list that works like this SessionAddkey1 value1 SessionAddkey2 value2 and so on stores keyvalue pairs into the association list Then Sessionkey1 value1 Sessionkey2 value2 and so on Since any values can be stored in Session it s important to cast them appropriate when they are used For example if strings are stored as values then you might do String s StringSessionkey1 The example in the text stores an ArrayList yet another data structure that we have not discussed But you don t need this for the assignment you just need to store something really anything that denotes whether or not the vote occurred Q What could you store What if the cookie doesn t exist yet Q Regarding the overall application is their a race condition somewhere Or the potential for deadlock Lecture 38 Dec 8 Are there any questions regarding P810 Final exam Monday December 18 9am12pm here in 220DL The nal exam will be much like the midterm except that it will cover more material and it will be longer Review for nal The material will be comprehensive le everything up to the midterm plus everything after The focus will be more on the stuff after but of course much ofthat builds on the stuff before As I said before the midterm If you have been attending class doing well on the homework and doing the homework on your own you should be in good shape New ideas since the midterm Eventdriven programming Recursion GUl s Delegates Exceptions File lO Threads and concurrency Animation HTML and ASP The Final will be structured similarly to the midterm matching problems truefalse perhaps a multiple choice problem but also thinking problems ie where you have to solve a problem and write the code fragment to express your solution It will be closed book closed notes closed laptop but of course these won t help you on the latter kinds of problems anyway Note I will put all ofthese lecture notes on the website as a study guide Final thoughts What should you take away from this course Do I care whether or not you remember the syntax ofa for loop No Here are some thoughts worth remembering 0 First there are conceptual issues like algorithms and data structures and recursion and so on that transcend programming languages and have to do with the very essence of computation which is a wonderfully abstract and important idea Personally I think that these ideas should be required topics in a liberal arts education 0 Second there are issues of complexity which come in two forms 0 Qualitative complexity relates to the very nature ofa problem and the inherent dif culty in solving it Qualitative complexity is ultimately re ected in the sophistication of the algorithms and data structures that comprise a solution to the problem The solution might not be a lot of code but it might take a while to come up with or understand the code 0 Quantitative complexity relates to the size ofa problem which in turn is re ected in the amount of memory or amount oftime that is needed to solve a problem 0 And sometimes there is a little or a lot of both 0 Third even if you don t ever program again hopefully you have an appreciation for the dif culties involved in programming We only tackled some of the most basic ideas and problems but even some ofthem were problematical Also imagine that as a problem gets larger you have to be part of a team that has to solve it this is not easy 0 Finally despite the nerdlike reputation of programmers I hope that you can see that software design can be a challenging creative and rewarding career Recent studies have shown that in fact software designers had the highest ratings in job satisfaction Also there are great opportunities for research in computer science lftime permits describe some of my own research In the last lecture I joked about not caring about the real world but of course I do in fact very much so The goal of my research is to 0 Reduce the dif culties involved in programming by designing better languages and software tools 0 Provide ways to verify the correctness of computer programs to ensure that they are bugfree To do this requires a fair amount of mathematics and other theoretical tools but it is all motivated by the desire to improve things in the real world Applications of my work include graphics and animation mobile and humanoid robots and computer music For an interesting departing thought consider the complexity the challenge the reward and the fun that must have gone into creating Animusic see wwwanimusiccom


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Allison Fischer University of Alabama

"I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over $600 per month. I LOVE StudySoup!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.