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
DON'T START YOUTUBE BEFORE WATCHING THIS!
Lesson one. Clickbaity titles. Gotchu! Didn’t I? Smash like! When people ask what I do for a living, older people, basically, and I say I do YouTube, I’m always met with, “Oh! That must be amazing!” “You must earn a lot of money!” Or, “It must be so cool…
Gene expression and regulation | Inheritance and variation | High school biology | Khan Academy
By now, you are likely familiar with the idea that DNA (deoxyribonucleic acid) is the molecular basis of inheritance. You might also have a sense that it is somehow involved with chromosomes. In this video, I want to make sure we can connect the dots with…
What advice do you have for someone wanting to be an entrepreneur?
So, what advice would I have for someone who wants to be an entrepreneur? Everyone’s path is different, so take anything I have to say with a grain of salt. A lot of folks think of entrepreneurship as, “Hey, I have a new idea for a business,” whether it’…
United Nations Messenger of Peace | Before the Flood
Hi, how are you? Pleasure, pleasure, great to great pleasure to see you. We can remove this, this can be, oh wow, this is for height control for shorter leaders like this. Taller leaders, what specific message do you think is the most important? Climate …
Once You Get Rich You Will Make These 15 Mistakes
Getting rich is extremely hard. Staying rich? It’s almost impossible. This is why most celebrities, athletes, or one-hit wonders reach the end of their lives poor or in massive debt. Money flees the hands of those who can’t hold it, and you don’t want to …
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…