Hi, I’m Jevon, a computer science student at the University of Washington. What initially attracted me to computer science is the incredible innovation happening in the field. There’s always something new and exciting, and depending on your interests, you can make groundbreaking discoveries in various areas. This constant evolution keeps me motivated to learn and explore as much as possible.
Recently, I got a new robot. However, it only knows two basic commands: move forward and turn left. My goal is to teach this robot to move in a square whenever I give it the command “square.”
In programming, methods are used to represent commands that we can give to objects within our programs. To create a new method in a class, we start by writing the method signature. A method signature includes a name and a parameter list.
Let’s develop a square method for our robot class. We want this method to be accessible throughout the program, so we’ll begin with the keyword public. The next part of the method signature specifies what the method should return. Since the square method is meant to perform an action without returning a value, we’ll use the keyword void. This indicates that the method does not return any value.
Next, we need to name the method. In this case, it’s square. We also need to decide if the method requires any parameters. The square method doesn’t need any, so we’ll use empty parentheses. We’ll then open a set of curly braces to define the block of code that will execute when this method is called. Inside these curly braces, we’ll write the code to make the robot move in a square by moving forward and turning left repeatedly until it completes the square.
Let’s create a robot object named myRobot. With our new square method, we can command myRobot to move in a square as many times as we want.
Writing your own methods allows you to encapsulate commands with a name. This makes it easy to reuse these methods whenever needed, saving you the effort of rewriting code each time.
By understanding and utilizing methods, you can enhance your programming skills and create more efficient and organized code.
In this activity, you’ll work in pairs to create method signatures for different scenarios. Each pair will be given a set of tasks, and your job is to write the method signatures that would best accomplish these tasks. Discuss with your partner why you chose specific names and parameters, and present your findings to the class.
Using a simple programming environment, write the square method for a virtual robot. Implement the method so that the robot moves in a square pattern. Test your method by calling it multiple times and observe the robot’s movement. Share your code with classmates and discuss any challenges you encountered.
Take an existing piece of code that performs a repetitive task and refactor it by creating a method. This will help you practice encapsulating code into reusable methods. Present your refactored code to the class and explain the benefits of your changes.
Participate in a game where you are given a description of a task, and you must come up with the most descriptive and concise method name possible. Compete with classmates to see who can create the best method names, and vote on the most effective ones.
Engage in a group discussion about how methods are used in real-world applications. Research a specific software or technology and present how methods are utilized within it. Discuss with your group how these methods improve the software’s functionality and maintainability.
Sure! Here’s a sanitized version of the transcript:
—
[Music]
Hi, I’m Jevon, and I’m a computer science student at the University of Washington. What first drew me to computer science is the sheer amount of innovation in the field. There’s a lot going on at any given time, and depending on what you choose to do, you can make groundbreaking discoveries in many areas. That really keeps me motivated to keep learning and do as much as I can.
I got a brand new robot, but it only knows how to move forward and turn left. I want to teach my robot how to move in a square. The robot should do this whenever I give it the command “square.”
We write methods to represent commands that we can give objects in our programs. To write a new method in a class, we first have to write the method signature. A method signature consists of a name and a parameter list.
Let’s write a square method in our robot class. We want the method to be accessible by the whole program, so we’ll start with the keyword “public.” The next part of the method signature specifies what the method should return. In this case, the square method should perform an action and not return a value, so let’s add the keyword “void.” This indicates that the method has no return value.
Next, we’ll state the name of the method. Then we need to decide if this method will have any parameters. The square method does not need any parameters, so we’ll just write empty parentheses. We’ll open a set of curly braces to indicate the block of code that should run when this method is called. Inside the curly braces, we’ll write our code to move in a square, so we’ll move forward and then turn left until we’ve moved into a square.
Let’s create a robot object called “myRobot.” We can use our new square method to have my robot move in a square as many times as we’d like.
[Music]
You can write your own methods to give a name to a command. You can use these methods as many times as you need, and it saves you the hassle of having to rewrite code every time.
[Music]
—
Let me know if you need any further modifications!
Computer – An electronic device that processes data and performs tasks according to a set of instructions or programs. – The university’s computer lab is equipped with high-performance machines for coding and data analysis.
Science – The systematic study of the structure and behavior of the physical and natural world through observation and experiment, often applied in computing to develop new technologies. – Computer science is a rapidly evolving field that combines theoretical studies with practical applications.
Programming – The process of designing and building an executable computer program to accomplish a specific computing task. – Students in the programming course learn to write code in various languages such as Python and Java.
Method – A function or procedure in programming that performs a specific task and can be called upon within a program. – The method calculateSum() efficiently adds two numbers and returns the result.
Square – A mathematical operation that multiplies a number by itself, often used in algorithms and coding exercises. – The function square() takes an integer as input and returns its square.
Robot – An automated machine capable of performing tasks, often programmed to follow specific instructions or algorithms. – In the robotics class, students learn to program a robot to navigate through a maze.
Commands – Instructions given to a computer or robot to perform specific actions or tasks. – The shell script contains a series of commands to automate the backup process.
Parameters – Variables that are used to pass information into functions or methods in programming. – The function accepts two parameters: the base and the height of the triangle.
Public – An access modifier in programming that allows a class, method, or variable to be accessed from any other class. – The public method getName() can be accessed by any class in the program.
Void – A keyword in programming that indicates a method does not return any value. – The void method printMessage() outputs a greeting to the console without returning any value.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |