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
The 2023 Recession Keeps Getting Worse
What’s up Grandma? It’s guys here. So while everyone is busy watching Tesla drop the price of their cars by up to 20%, we’ve got another issue quietly brewing behind the scenes. That’s the fact that the United States is quickly running out of money, with …
Pronoun number | The parts of speech | Grammar | Khan Academy
So here’s something weird and cool about English and languages in general: they have a sense of number kind of encoded into them. We call this grammatical number. The way this plays out is in the difference between singular and plural in English; the idea…
Finding Michigan’s Wild Side: A Journey through the Upper Peninsula | National Geographic
For years, I’ve heard from friends how the Upper Peninsula of Michigan is this mythical place that I needed to see at some point in my life. I’m very grateful as a National Geographic photographer to travel all around the world to see magnificent landscap…
Stop Caring What People Think | The Stoic Way
It never ceases to amaze me. We all love ourselves more than other people, but care more about their opinion than our own. People spend so much energy on worrying about what other people think about them. The thing is: it’s a huge waste of time; especiall…
Simple Aspect | The parts of speech | Grammar | Khan Academy
Hello gramians. Now previously, we had spoken about just the basic idea of verb aspect, which is kind of like tenses for tenses. I know that’s a little “wheels within wheels,” ridiculous, um, but we’ll make sense of it. What aspect allows you to do is si…
The Ponzi Factor: Proof by Definition
I talked with the author who has written a book so dangerous if this information becomes mainstream it alters the entire engine of our economy. Tong Lu has revealed just how our stock market is the dictionary definition of a Ponzi scheme. Here’s my conver…