Project - Sum Of Elements

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 19m
Students
4
Description

This course looks at methods, which are named, self-contained blocks of code that you can call upon to help solve problems for you. We'll then take a look at some projects which will put methods into practice.

Learning Objectives

  • Understand the fundamentals of writing your own methods
  • Learn how to classify methods by their return types as well as their parameters
  • Learn about parameter passing schemes that are used by programming languages to determine how methods treat their parameters
  • Explore method overloading and two-dimensional arrays

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

Throughout this section, we've discussed how to work with methods in Java. The project in this lecture will allow you to use what you know and also do some extrapolation with your coding and problem-solving. For this project, you will create a file called Proj6_1_SumOfElements. You will write a method called sumElements that takes an ArrayList of integers as a parameter. The method should be able to handle an ArrayList of essentially any size. So, you won't always know if there's a 3, 7, 15, or 150 element ArrayList that you're working with. Write the method so it can handle any general ArrayList object. You will sum the elements of the ArrayList and then return the sum to the caller. In the main method, you should test this method out and print out the sum that is returned.

So, let's run this application without using the code, so you'll see what the output should look like. So, I'm going to right click the 'File', go to 'Run 'Proj6_1_Sum of Elements'', and it just simply prints sum is 208. Hopefully, that helps. Get ready and put your thinking hat on, pause the video, and come back when you're done or if you need some help. How did that work out for you? Were you able to complete this project? Let's work on it together. So, I'm going to right click, go to 'New', 'Java Class'. And this is Proj6_1_SumOfElements. Okay, public static void main(string [] arghs), and there's our main. And we want to write this method public static int sumElements and it takes an ArrayList of Integers. 

Remember we can't use the primitive type integer because that's not allowed with ArrayLists. You have to use the wrapper class. Now, it doesn't know what that is because we didn't import it, so I'm going to hit Alt + Enter, and you could also just type import java.util.ArrayList, and this is expecting to return an integer. So, for right now I'm just going to return just integer that's a zero, that doesn't really have any meaning. So, that's called stubbing out of method just to get it to compile. Let's see how we're going to use it in main. ArrayList of integers someList = new ArrayList<>() right there. someList.add, we'll just add some numbers, 10, someList.add(22), 55, and maybe 121. And of course, I'm going to create a variable to hold the sum, call our methods sumElements, and pass it someList. And finally, we're going to print out the sum. Actually we'll put sum is and then sum, right there. 

Now the real meat of this project, the real challenge of this project is coming up with this method right here, which isn't super difficult. So hopefully, you figured it out. So, we'll do that to start. We probably want to spell return correctly, that would help. And we have for(int i = 0; i < list.size (); i++); There we go. And of course, let's run it. So, I'm going to run the project Proj6_1_SumOfElements. It's a sum is 208. You can verify this yourself by adding these together. So, 176+32 is in fact 208. Awesome. Hopefully, that wasn't too difficult for you. However, even if you had trouble with it, that's totally okay. You will get better as you practice and are exposed to more problems to solve and more Java language features. In the next lecture, we have another project. This upcoming project is very cool and particularly challenging. If you're up for the challenge, so am I. It's a tic-tac-toe game application. I'll see you there.

 

About the Author
Students
152
Courses
20
Learning Paths
3

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics