![]() Execution then returns to where the method was called. The computer executes the code inside the Duke Learn to Program Library, figures out that the answer is 270, and returns that value to where the method was called. Now, a similar process happens for the call to fgImage.getHeight. We finished the assignment statement by making a box for w and putting 480 in it. Now execution comes back to where it was, which is halfway through this assignment statement. Another way to think of that is that it's like a mathematical operation with the result 480. Since this method's answer is 480, the call fgImage.getHeight() evaluates to 480. That code would figure out what the width of fgImage is, that it's 480 pixels, and decide that 480 is its answer. The computer will then go into that code and start executing it. We don't actually know what the code is, but that is fine as long as we know what it does. ![]() The code for getWidth() is in the Duke Learn to Program Library. The first line we have seen before, it makes an image and initializes fgImage to refer to it. Then you continue executing code after the method call. The method call, which is actually an expression, evaluates to whatever value the body returned. At some point, the method will figure out what answer it wants to give back, which is called returning its answer. It has the same semantics, whether it is inside a method or not. You do that code by following all the normal rules for executing code. First, execution goes into the method, you stop executing statements where you were, go inside the code for the method, and then do whatever code is there. Before we step through the behavior, we'll describe the semantics. Now, that you have seen the syntax of a method call, let us see the semantics. If the method took any parameters, you would specify them inside the parentheses. Parentheses after a name indicate that it is a method, or a function, which is similar. Next is the name of the method that we want to call. We want the getHeight method inside of fgImage. Next is a dot, the dot operator means inside of. Which SimpleImage's height do we want to get? Whatever SimpleImage we name here. ![]() First is the name of the object we want to invoke the method on. Here is an example of calling two methods, getWidth and getHeight, to get the width and height of an image respectively. In general, methods let you perform some operation, possibly one that is quite complex, on an object. How could you do these things? The answer is that you can call some methods that are built into SimpleImage, which already have the code to perform these operations. But you also need to know how to do things like look at particular pixels and change their colors. “Even if you have no interest in writing code as a career, everyone should try a little bit so they know how it works,” she said.Okay, so now you know about variables. Hall says similar courses can cost hundreds of dollars elsewhere, so the Duke Roots course gives participants a lot of educational value. “Foundations” is unique in that it uses an open online course to deliver most of the content. “Foundations in HTML, CSS and Javascript” is part of the Co-Lab’s Roots Program, which offers short courses on technology skills like web development, 3D printing and programming. “The in-person session can help you get caught up. Hall emphasized that participants can come to the weekly meetups even if they are behind in the online part of the course. “It’s a good opportunity for people to go at their own pace and not be committed to an in-person session, but to still have the option to get in-person help if they need it,” said Sally Hall, a developer and educator at the Co-Lab who is one of the course TAs. ![]() The course will be taught in a “flipped” format: learners will watch video lectures on their own time, and then attend weekly meetups for in-person help from teaching assistants. All Duke students, faculty and staff can enroll for free through the Co-Lab. The course is being offered through Duke’s Innovation Co-Lab and is based on a open online course on Coursera created by four Duke faculty. Participants will learn foundational programming concepts, web development fundamentals and how to solve problems like a programmer. The four-week course, “Foundations in HTML, CSS and Javascript,” starts March 28.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |