Solving Problems With Google

Learn the Basic Java Concepts Loops, arrays and methods
9 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to the Java development course. So last time, we went over assignment operators and you had some homework to do, your job was to get five doubles from the console, and then print the average of those doubles. So here's essentially what should have done. So here I have a scanner SC, which is a new scanner and system built in, I have a double one, c dot next double double two st dot next double, and so on and so on. So we just get five doubles, then we get the sum of those doubles by just adding them all together. And then we get the average by doing some divided by five, which is the number of doubles.

And then we just print average is equal to and then the average. So let's go and run this application. Okay, we can do 1012. And remember, if we just do it without the decimal point, it'll just cast it to a double. So just, if we enter 12, it'll just cast it to 12.0. So we can do 125 45 and this critic 20.9 and the average is 286.2.

So there we go. That is essentially how we do that. So if you got this, that's very good. If not, don't worry about it. I'm trying to recreate this code, as you know, as always try to maybe move some things around, and you're good to go. So, without further ado, let's get into the lesson.

All right, so today, we're going to be working on solving problems with Google. So when you're going to be coding or programming or you know, doing whatever, in your application, only gonna get coding and programming is pretty much the same thing. But you know, you get what I mean. So if we're going to be creating an application, you're gonna inevitably find yourself in a position where you have some error or you don't know how to do something. And you know, there's nobody around to help you. So what do you do?

You quit? No, I'm of course kidding. You go to Google and you search it up. So that's we're gonna do today. So we're going to go ahead and create a new class. This is gonna be I don't know, let's just say random value.

Now let's call it getting random. Getting random. And let's do public static void, main, and finish. All right, there we go. So here we go. So now we have a class getting random.

And what we need to do is we need to generate a random value. So how can we do this? Well, I mean, I know because I've already looked it up on Google quite a long time ago. But you may not. And you may struggle to deal with this with a lot of different values. Since there is so only so many things that we can cover in the course.

The Java language is extremely huge. So it is very difficult to put all of it into one course. It's practically impossible. So for that, we have Google. So you can go on and go to Google. Go to Google.

So let's go to Google right now. Alright, here we are in Google. And just before we start, I know a lot of people will think, well, a good software engineer will never actually use Google for something. I mean, he knows everything he doesn't. When you know, there are so many things to know that even after you've done something, you may only return to it in a couple years, sometimes in a couple decades. And so in result, you will most likely have to Google something, look it up.

Don't worry about it. Every single software engineer does it. I mean, if if there is a software engineer that does it, maybe there are like maybe one or two of them. But I say 99.999% of software engineers routinely go to Google to search up, you know, whatever they need to know. So let's go ahead and actually do that now. So we need to find out how to get random.

So we're going to do is we're going to search how to get random in Java. All right, there we go. So now we get quite a bit of quite a bit of different Harkness results. So Stack Overflow is usually what's going pop up if you're searching a programming question. And Stack Overflow is actually a website looks like it's sort of like a forum site where people can ask questions regarding software engineering, and then they would get burnout coding, and then they would get answers from more experienced people. So here's essentially an answer.

So I would like to get a random value between one and 50. In Java, how may I do that with the help of math dot random, so we've already we already know that math dot random is one of the it's one of the ways of getting something. So let's try that right now. So we're gonna go ahead and copy in math dot random. So we'll just do int a equals math dot random. So what what will this do so well, we get an error.

Type mismatch cannot convert from double to end. So what is the saying is telling us that math dot random returns a double, and we're trying to put it into an int. So let's do double equals math dot random, and there we go. So now we've got math dot random, let's see exactly what this does. So let's print at this point. There we go.

And let's run this. There we go. So you get 0.14. And now let's run this again, every single time we get a different value. You'll notice at this point, though, that we're getting a value at the double value, and we're getting it from zero to one. So really no, we're not really seeing any, any, you know, values from 10, or 20, or anything like that.

So we're going to take a look at exactly how we can do that in Java as well. So here, he's providing a different way of doing it. So we use a random class, and then just do new random and it's a bit different. So that's it. That's that's also a way of doing it. So we Oh, and here we go.

Sweet. We have using math dot random. We can do random equals math dot random times 50 plus one. Let's see what that does. Math dot random times 50 plus one. What Will that do?

All right, so that'll give us 48. Now it gives us between one and 50 There we go. Okay. Let's see what else there is a random equals into math dot random equals. So here we have something else. So here we have this int before math dot random.

So this is essentially we've already we already gathered gathered more information as to how we could sort of put the the random value into an integer instead of a double. So let's print random. Now. This int value random, and now we get an integer instead of a double. So without the point something, something something. Okay, he also provides a different way.

So using the random class, this is what we saw up here. So we just use the random class to get random next end. Okay, that makes that make sense. Okay, so together now we have used Google to solve a problem which we didn't know how to solve. So we can now get random input which means useful if you really think about it. Now, to go a little bit further into what exactly math dot random does, it actually uses the system time.

So the computer's time has a pretty complicated set of algorithms in there, or, you know, some mathematical, some math inside it to get you a random value based on the system's time. So it really isn't random. But then you know, what is random? It's impossible to, to dispute it's been going on for four generations as to what is random. So yeah, so that's usually what random does. It bases the system time does some complex math and gives you back a number.

And so yeah, so here, what we do is we do math dot random, which returns a double from zero to one, we multiply it by 50. So if we get for example, point five, it will multiply it by 50, which will give us 25. And then we just add one to it. So this makes it This makes it so that if we get zero, for example, as the random value, it'll add one to it. So here we're getting from one to five And then here, we're doing that same thing, except we're just converting it to an integer. And that will then just chop off the decimal point.

So yes, that's really how random works in Java. Okay, so for homework, your job is to write an application that will print a random string. No a random character in Java. So, we didn't really go over how to do this. But your job is to practice what we did this lesson. And to find the answer on Google.

While doing this, try to understand what you're finding, though, because it's easy to fall into a trap where you search something on Google, just copy it in and you're done. And then later on, you know, maybe you need to redo it, the answer is gone, for example, and then you know, you're lost. So if you are going to copy something, go ahead and understand exactly what you're copying. And, you know, don't just copy it and then just forget about it. Just try to understand what you're copying so that later on, you may have a better idea of exactly what's going on. Alright, so anyway, I wish you luck with the homework and I'll see you soon.

Goodbye

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.