yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 6


5m read
·Nov 3, 2024

Hey guys, this is Maids in1 with Java lesson six. In this Java lesson, we're going to be learning a few new things that are majorly important concepts that I haven't really brought through to you guys.

The first one of these concepts is methods, voids, or functions. The second one that is kind of cooler is the random number generator. So, I'm going to open up Xcode, and now I'm going to create a new project, Java Java tool, and I'm going to call it Random Numbers.

Okay, now we go into Source RandomNumbers.java. So, there are a few things in this code that I haven't pointed out to you yet. The first one being that right here is public class RandomNumbers, which is the name of the class that you're currently editing. RandomNumbers can refer to it from other classes since it's a public class, and I'll explain that to you later.

Inside of here is something called public static void main. Within that, we put our code, but what does public static void main mean? Well, public means it's accessible from anywhere, static means it's the same in all class instances, void means nothing, so it's not going to return anything, and main is the name of the function.

When Java runs an application, it looks for a main function in the code. Now, the cool thing about Java is that you can have multiple functions that look kind of like this: public static void main. The way it works is you do public or private, you can do static, then you do the variable type that it's going to return. Void means nothing, and then you have the function name.

Under this static void, we can put another one, and I'll make it an int and I'll call it Rand. Now, left parentheses, if you have parameters you want to set on it, then this is how you do it. So, as you can see right here, left bracket or left parentheses, then there's a type of variable, right parentheses.

So, in this case, it's an array of strings called args. Anywhere in here, we can say args, and it refers to the variable called args, which is declared here. So, right here between these parentheses, I'm going to say int Max, left parentheses. Now, I'll use curly braces to represent what's inside the function. So, right here, this int means that we are going to have to return an int later on, and this int means that when someone else is calling it, they'll have to give us an int. Max stands for maximum, by the way.

There's another type of variable called random, so I'll make it random called Rand, and I'll make it a new random. I'll say int n equals rand.nextInt, and what Rand is, it's a class called random. A class is kind of like an object. When we say new Random, we're making a new object called that, that's a random called Rand.

Rand.nextInt is going to run the function nextInt on the class Rand. So, somewhere in java.il is a class called Random, and inside of that class random, there's a function on it that returns an int called nextInt, and in that nextInt, you pass it the maximum amount that can go. So, I'm just going to say Max because that's being declared up here. Then I'll do right parentheses, semicolon, and on the next line, I'm going to do return n.

So, rand.nextInt is going to create an integer that's totally random from, I believe it's zero, or maybe it's one to the maximum value. So, it's going to be less than the maximum value, and it's going to be more than one. Now, I'm returning n, so right here, here's a cool thing. Right here, I can say int randomNumber equals, then watch this, I say Rand(left parentheses 10 right parentheses).

So, that is going to make this code run right here, and it's going to make this integer be 10, and it's going to return a random number between 1 and 10 and assign it to randomNumber, which is an int we have up here. Hello world, and then here I'm going to print out the random number.

The number was randomNumber, so now I'll run it. The number was one. So, if I build and go again, it'll say the number was, let's say, four. I again, six. Now it's zero and so on. So, this is how to make a function that returns an int.

If you want to make a function that prints out an int, I'm just going to say public static void, that means I don't have to return anything, printRandomNumber int number. I'm going to go here, what this is going to do, all it's going to run is system.out.println(randomNumber, number), and there's that.

So, now I'm going to get rid of this, and I'm going to call printRandomNumber(randomNumber), and there we go. So, now randomNumber. I spelled random wrong. Either way, this is how to do functions and how to do random numbers.

Just as a good example, here's what else you can do. You can even make a public static string. If you want to, string randomMessage, and I'm going to give it no parameters; that means that's why there's nothing in here. Now, I'll say return quote quote, and inside of these quotes, I'm going to say randomNumber.

Okay, and now I'm going to, instead of printing out randomNumber, I'm going to say randomMessage plus number. And watch this, there we go, randomNumber zero. So, this is a string that just returns a string, and it has no parameters. You can also do two parameters, for instance, like this. I'm going to make it a boolean;

I'm going to make useRandomMessage. Now, I'm going to say if useRandomMessage, then I'll print it out this way, using the randomMessage function, and else I'll print it out using my own string. So, as you'll see here in a bit, I'm going to add, comma, space true to this, and this is how you do multiple parameters; you just use commas.

So right here, as our same old function, I'm going to make it use the randomNumber, randomMessage, else they'll say that if I change this to false, now it'll just say number because it's going to this instead.

So, that's pretty cool. So, now what I'll do just for fun is I'll say if Rand less than five, then I'll just print out number, and else I'll print out randomNumber true. So, this way, what I can do is I can make it so that it prints out just plain old number colon if the random number that's been generated is less than five.

If it's more or equal to five, then it'll do this, it'll use the randomMessage, which is randomNumber. Now, another cool thing that you might notice is I don't use curly braces here. This is because if it's a one-line if statement, you don't have to use curly braces. You can literally just do it this way, if no semicolon or anything, the code semicolon, and then you directly after that, you can put an else.

You don't have to use semicolons. Now, I prefer to put an enter after this so that way it looks more like an if statement, but remember, enter means nothing, semicolons are the magic. But anyway, that's that. So, now, oops, I forgot to say randomNumber. I said Rand.

Okay, so now if I run it, it says number there. If I run it again, it says randomNumber, and again, it says another randomNumber. See if we can get another number. Seems like a lot of things are greater than five. Yeah, there we go, so another number.

So, this is how random numbers work and how functions work. So, I hope you get this. If you do not, I will post this entire Xcode project online, just to prove that I'll zip it up now, and this will be exactly what you see in the description of the video.

So anyway, thanks for watching Maids in1. Subscribe, and goodbye.

More Articles

View All
Truly Terrible Advice: Find Your "True Self" and Be Authentic | Michael Puett | Big Think
We tend to think we know what the good life is. So we tend to think basically to lead a good life, you should look within, try to find yourself, find your true self, and then spend your life being as sincere and authentic to that true self as you can. And…
Freeing energy from the grid - Justin Hall-Tipping
[Music] [Music] [Applause] Why can’t we solve these problems? We know what they are. Something always seems to stop us. Why? I remember March the 15th, 2000. The B15 Iceberg broke off the Ross Ice Shelf. In the newspaper, it said it was all part of a norm…
Does Black Friday Shopping Keep Workers from Their Families? | C. Nicole Mason | Big Think
One of the things that really, really upsets me – a lot of things upset me but this upsets me – is that the people who are talking about poverty and what should be done are the people who are not or are least likely to be affected. So we’re talking about …
How Will You Diversify a $100,000,000 Portfolio? (Asset Allocation)
If you had $1100 million, how would you invest it? How much of it would go where? Well, as of 2024, according to the Wealth Report by Douglas, Elon, and KN Frank’s Flagship Report, there are around 626,000 ultra-high net worth individuals in the world. Th…
Why is Alzheimer’s disease so difficult to treat? - Krishna Sudhir
Around the world, tens of millions of people have Alzheimer’s disease, a debilitating brain disorder that gradually destroys a person’s memory and other cognitive abilities. It takes a heavy toll on both patients and families, as caring for a loved one wi…
Understanding place value when subtracting
What we’re going to do in this video is get some practice subtracting multi-digit numbers. I’m going to use 1000 minus 528 as our example, but it’s really to understand different methods and how they all fit together and why it actually makes sense. So, …