Project - Working with Arrays

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
57m
Students
6
Description

This course covers the fundamentals of data structures specifically looking at the regular built-in arrays that Java provides as part of the language, as well as the ArrayList class, which lives in the java.util package.

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

Earlier in this section, you learned about how to work with regular built-in arrays in Java. You know that they are fixed size, and that they contain homogeneous data, that is the elements are of the same type. In this lecture, you will attempt your first project of the section. For your project, you will create a file named Proj4_1_Arrays. You will ask the user to input five integers storing each of them into the regular built-in array. Then, you will print the values in the array in a separate loop multiplied by two to the console. 

Note that you should not modify the values in the array but simply print the value multiplied by 2 to the console. In other words, don't restore the value multiplied by 2 back into the element. All right, here's a demo of your Proj4_1_Arrays before you actually do it so you can see what the input/output is supposed to look like. Doesn't have to be exactly the same as what I do, but it should be pretty close. And we've got to enter an integer, so 10, and then let's say 22, 50, 11, and 9. And you will see that the output is indeed double the values that were input. Remember that we store these in an array first and then we loop through and multiply each of them by 2. So, there you go. So, pause the video. Come back when you're done or if you need some help.

How did this project go for you? Were you able to complete it? Let's work on it together. So, first let's create the Proj4_1_Arrays. New Java Class. Proj4_1_Arrays. All right, pretty good so far. We know that we're going to need the java.util.Scanner because we're asking for user input. public static void main. All right. Scanner keyboard = new Scanner(System.in). int[] someValues = new int[5]. So, that makes our array right there, named some values. And remember the arrays themselves are reference types even if they hold primitive types. for(int i = 0; i < someValues.length; i ++). End of that for loop. And we're going to say System.out.print. This time we'll use print. We'll say ("enter an integer\t"). So, we'll give a little space, and then set someValues [i] = keyboard.nextInt(). And now you might have used an intermediate variable or something else, and that's okay. But you can do it directly just like this, because when you access elements at a particular index in array, you can treat them just like any regular plain variable of that type.

So, for example, this is just as if I'm doing something with just a single integer variable. I'm just accessing it from an array. So, now that I will get the data from the user, I can now print the data back out multiplied by 2. So, we're going to say someValues.length again. And of course we could use the enhanced for loop if we had desire to, and right here I'm going to make it a little easier. int result = someValues[i] * 2. So, we didn't modify what's in the array, we just multiplied by 2 and returned that as a result. And println() here. ("result is " + result) and there we go. Let's run this and see what happens. Right click, run it. Give it a second. Enter an integer, let's say 15, 10, 22, 5, and 7. All right. And it prints out 30, 20, 44, 10 and 14. Nice. So, the first loop that we have up here stores the values and from the keyboard, from the user, one by one. Then the second loop, which could again be an enhanced for loop if you'd prefer that, restores the value, and multiplies it by 2 and prints it out.

Note that we could accomplish this second loop's purpose without the extra result variable. So, if I wanted to modify this, I can actually get rid of this result variable, which I'm going to do right now, and it doesn't know what result is now. So, we could, this isn't actually required to put it in parentheses, but to keep it really clear what's going on, I'm putting an extra set of parentheses around it. If you were adding, you'd have to put parentheses because it would think that was concatenation. So, that would get confusing. But when you're doing arithmetic in general or when I do it, I often put parentheses around it so it's perfectly clear what I'm doing, but technically don't need this. So, what's going to happen here is this gets multiplied by 2, and then we concatenate it with results is. So, we don't really need that intermediate variable, but it could make it easier to read the code.

So, let's right click this and run it again and make sure that we get the same results. So, 5, we'll use a different set of numbers. So, I guess it won't be the same results, but we'll get the correct results. 10, 7, and 9. And there we go. We get 10, 42, 20, 14, and 18. That's what we expect. Pretty cool. That's great work, everyone. Make note that the extra parentheses around the multiplication again are not strictly required, but they do serve as a bit of self-documentation. So, there's a term you will become familiar with. Self-documentation means you write it directly in the code and it is clear what you're doing. So, it makes it clear that the multiplication is being done first before the concatenation, even though technically it will get done anyway without the parentheses. In the next lecture, you'll work on your second project for the section- dealing with ArrayLists. I'll see you there.

 

About the Author
Students
157
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