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 Value of Rooftop Farming for the Community | Farm Dreams
Things are living, and things are growing on the roof, so they’re always living and growing. The flowers look amazing! Oh man, and you know the flowers are bringing the pollinators. Yes, we got the butterflies coming; we got the birds coming. So it’s goin…
Khan Academy Live: AP Calculus
Hi and welcome to live tutoring for the AP Calculus exams provided by Con Academy! In case you are curious, I am not Sal Con; my name is Dave. I first took the AP Calculus test back in 2006, and before joining KH Academy, I was an AP Calculus teacher. So…
Arthritis Has Me Down | The Boonies
Courtesy out here, I don’t use a whole lot of fold of money. I don’t make a whole lot of fold of money. It’s just a barter system. It’s a whole lot more fun than that. Common pull the wine out of your pocket in the Clearwater mountains of Idaho. Bear Cla…
Trailer | The Crux | National Geographic
Traditionally, climbers are seen as very friendly, lovely people. But there’s something going on at the moment. We perform the best on the big stage. This is the most intense season I’ve ever experienced. I’ve been training for 10 hours a day, eating, sle…
Estimating decimal addition (thousandths) | Adding decimals | Grade 5 (TX TEKS) | Khan Academy
So we have two questions here, but don’t stress out. Anytime I even see a lot of decimals, I’m like, okay, is this going to be a lot of hairy arithmetic? But what we see here, it does not say what 8.37 + 4926 is equal to. The equal sign is squiggly. That …
Steve Jobs on Consulting
I mean, you guys, most of you come from companies where you’ve had work experience, right? How many of you are from manufacturing companies? Oh, excellent! Where the rest of you from? Okay, so how many from consulting? Oh, that’s bad; you should do someth…