CSA: Writing a Method

Alphabets Sounds Video

share us on:

In this lesson, Jevon explains the concept of methods in programming, using the example of creating a “square” method for a robot that can only move forward and turn left. He outlines the process of defining a method’s signature, including its accessibility, return type, name, and parameters, and emphasizes the importance of methods for encapsulating commands, promoting code reuse, and improving programming efficiency. By implementing the “square” method, learners can enhance their coding skills and create more organized programs.

CSA: Writing a Method

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.”

Understanding Methods in Programming

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.

Creating the Square Method

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.

Implementing the Method

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.

The Power of Methods

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.

  1. What aspects of computer science innovation do you find most exciting, and how do they motivate you to learn more?
  2. Reflect on a time when you had to teach a machine or software to perform a task. What challenges did you face, and how did you overcome them?
  3. How do you think the concept of methods in programming can be applied to other areas of your life or work?
  4. What are some potential benefits and drawbacks of using methods to encapsulate commands in programming?
  5. Can you think of a situation where creating a method might not be the best approach? Why?
  6. How does the process of writing a method enhance your understanding of problem-solving in programming?
  7. In what ways do you think learning to write methods could impact your future projects or career in computer science?
  8. What new insights or skills have you gained from learning about methods, and how do you plan to apply them in your programming journey?
  1. Activity 1: Method Signature Workshop

    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.

  2. Activity 2: Code a Square

    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.

  3. Activity 3: Method Refactoring Challenge

    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.

  4. Activity 4: Method Naming Game

    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.

  5. Activity 5: Real-World Application Discussion

    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!

ComputerAn 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.

ScienceThe 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.

ProgrammingThe 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.

MethodA 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.

SquareA 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.

RobotAn 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.

CommandsInstructions 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.

ParametersVariables 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.

PublicAn 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.

VoidA 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.

All Video Lessons

Login your account

Please login your account to get started.

Don't have an account?

Register your account

Please sign up your account to get started.

Already have an account?