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

Java Lesson 6 | java.lang.Math


4m read
·Nov 3, 2024

Hey, this is Jake. I'm a special guest of Maads 101, and in this Java tutorial, we'll be talking about this right here:

import java.math.*;

With a capital M, and this allows us to do some more math stuff besides just add, subtract, multiply, and divide. So if you want to start making some programs, uh, with some formulas, it's going to help you a lot.

I'm not really going to be making a program here; I'm just going to be printing out a bunch of um, stuff, basically. That I'll take a number and use one of the methods on it, or some of them don't take any arguments, but I'm just going to be printing a bunch of stuff out.

So just that out:

System.out.println();

And I'm just going to copy and paste this a bunch of times, right? I have a lot of methods; I'll see if I'm using them all.

Probably going to wind up using more. So, the first one I'm going to show, yeah, I'm going to be using more, um, is Math and to use all these, you do Math.PI, in caps.

And that Math.PI um, is just basically, you can use that as Pi. Same kind of thing with Math.E, and that's E. So this would print out Pi and it would print out E, and um, Math.random() and you put those empty parameters there, and that will just print out a random number just out of the blue.

Um, Math.abs() and I'll put in -5, and Math.abs() gets the absolute value of something. Yeah, I'm going to be going over these real quick, and if you don't know what some of them are, um, just don't worry about it because you're probably not going to be um, using them right now.

So I'm just going to go over just what kind of stuff there is in there. Then you have um, Math.round() and I'll just put 0.6, and that will round. And this is in parentheses, by the way, and that will round 0.6, and that should be 1.

Um, Math.max() and this requires explaining. Um, you put in two numbers and it will um, get the highest one. So like 15, comma, 10, and that would do 15, 'cause 15 is higher than 10.

And the opposite is Math.min(), so if I did 15, comma, 10, it would get the lowest of the two, which is 10. Alright, um, next here, I'm just going to copy and paste this a couple more times. Alright, and so now the next one is square um, square root.

Then I'll do cube root. So Math.sqrt() and that will get you the square root of something. I'll just put 100 because that's nice and easy.

And Math.cbrt() will be the cube root, and I'll do 64 because that is also nice and easy. So that would be 10, and that would be 4. So Square root of 100 and cube root of 64.

Math.sin() I'll just do sin(10), and of course, there's also Math.cos(), I'll do that of 10 too. And if there's cosine and sine, of course, tangent.

I'll do the tangent of 10. So to get to use sine is Math.sin(), Math.cos(), Math.tan(). If you're going to be doing some trig kind of stuff, um, there’s Math.pow(), and this takes two things, and this is for exponents.

One is your number, so I'll do like 6, and I'll do 2. And what this would do is take the number 6 and raise it to the power of 2. So the second number is the exponent you're going to put on this number here, so 6^2, 6 squared, which is 36, and that's what that'll do.

And then um, the last one I'm going to show, or last two are for logarithms: Math.log() and in here, and that's going to be using um, log. The base with just Math.log is E, it's the base it’s going to be using, so I'll just do like Math.log(12); doesn't matter.

And System.out.println(). This is the last one: Math.log10(), and that will be using 10 um as the logarithmic base, and I'll do 12 again.

Alright, so let me just review this one more time. Um, Math.PI, which is pi, Math.E, which is E, Math.random(), which is a random number, Math.abs(), we'll get to the absolute value, Math.round(), we'll round a number, Math.max() will get you the higher of two values, Math.min() will get you the lower of two values, Math.sqrt() will get you the square root of a value, Math.cbrt() will get you the cube root of a value, Math.sin() will get you the sine, Math.cos() will get you the cosine, Math.tan() will get you the tangent, Math.pow() will raise the first number to the power of the second number, and Math.log() will um, just regular log is with the base of E, and Math.log10() is with um, a base of 10.

So I'm just going to run all this, and you'll see. Alright, so that's Pi, that's E, that's a random number, that's absolute value -5, which is 5, it rounded 0.6 to 1, it found the greater of 10 and 15, which is 15.

Then it found the lesser of 10 and 15, which is 10. It got the square root of 100, which is 10, it got the cube root of 64, which is 4, and I got the sine of 10, the cosine of 10, the tangent of 10.

It did 6^2, which is 36, and that did log(12) with the base of E, and then log(12) with the base of 10.

So that is just some stuff you can use, so um, see you next time.

More Articles

View All
You’ll NEVER look at money the same way again…in under 4 minutes
Because at some point, my investment should be able to cover anything I want to buy. And that’s the point when you realize you’ve made it. What’s up, you guys? It’s Graham here. So ever since learning about compound interest and reading the book “Rich Da…
How To Build A Brand in 2024?
So I want to talk about what does it take to build a brand. People come to me all the time saying, “Oh, I have a brand.” You can’t build a brand; your customers build the brand. They’re the ones that gift you the value and the equity of a brand. So what …
How to Build a Blind | Live Free or Die: DIY
[Music] When I talk to people about seeing all kinds of wildlife, they’re always amazed. They say they go into a forest, or a park, or a wooded area to take pictures of wildlife or birds, and they don’t see anything. So come along and I’ll show you how t…
When to Launch Your Startup and When to Wait
I think this is the image founders have of the launch, which is it’s going to be like the launch, and it’s going to be like the Oscar ceremony or something, where there’s just going to be like hordes of people. And like you’re going to be treated like a c…
Worked example: alternating series | Series | AP Calculus BC | Khan Academy
What are all positive values of P such that the series converges? So let’s see, we have the sum from n equal 1 to infinity of ((-1)^{n + 1} \frac{p}{6^{n}}). There’s a couple of things that might jump out at you. This ((-1)^{n + 1}) as (n) goes from 1 t…
15 Crucial Decisions Everyone Needs To Make
The Alex lady you all know and love is still around. We have a lot of work to be done, so we got a new team member. Me think of me as the Alex lady’s helper. Okay, back to the video: 15 crucial decisions everyone needs to make. Life is a roller coaster o…