Hello there, and welcome back to this Java development course. So last time, we went over the very basics of object oriented programming, notably classes and objects. So you had some homework to do. Your job was to create a class Hippo, with two attributes weight in grams and height in millimeters. Then create two methods, one, which will return the weight in kilograms, and one which will return the weight, the height in meters. There we go.
So here's essentially what should have done. So you have a class hippo. It has an integer weight in grams and an integer height in millimeters. Then I have a method which returns a double get weight in kilograms. And then I have a get height in meters method as well, which returns the height in millimeters. So To convert kilograms to grams just divided by 1000 and height, millimeters to meters just divided by 1000 as well.
And so here's the main code. So we create a new Hippo, we set them high 10 millimeters to 10,000, and the weight in grams to 100,000. And then we print get height and meters and get weight in kilograms. And so we get 10 for the height, and then in meters, and then 100 for the weight in kilograms. And there we go. So essentially, what you should have done.
So make sure you get this because it really is important to understand it so that you can move on to the other concepts of object oriented programming. So anyway, without further ado, let's get into today's lesson. Alright, so today we're going to be going over constructors and constructors are a pretty simple part. It's not really object oriented programming so much It is just part of an object part of class. But you know, either way it's usually are doing object oriented programming. So let's go over that together right now.
So we have our Hippo class, right? We have a hippo class. What if we wanted to make it so that when we create our Hippo class, we are automatically sort of forced, I guess, to input the weight in grams and the height and millimeters. So here? Yeah, we do just set it by doing H dot height, middle grams, height in millimeters, and a weight in grams. But we can just not do this.
And what happens if we do this now? You'll see we actually run this. We get 0.0. So we didn't set anything. And so what is this like a no Hippo? No, we need to set something for the hippo.
So that's where constructor comes in. So a constructor is essentially just a method like any other method, except this is what it's called. What is called whenever we do the new hippo. So when we do new Hippo, the essentially the method has been called the hippo method. So if you go to create a hippo method here, Hippo There we go. So now we have now created a constructor.
So here we can input int. weight in grams, whoops, there we go. Comma, int, height and millimeters. There we go. And now to set it remember how we need to access the class level variable. This dot weight in grams, equals weight in grams.
There we go. This dot height in millimeters equals height in millimeters. It should have should have auto completed there. There we go. All right, and there we go. Okay, so now we can go ahead and go back to classes and objects.
And you'll see we get we now get an error. So why do we get an error? Well, the constructor Hippo is undefined. So essentially, whenever we create a public class without a constructor, so just like this, what Joe will do is it'll just create a default constructor that does nothing. So essentially, it just creates this. There we go.
All right, so yeah, so if we don't have a constructor at all, it'll just create this constructor, which is just a constructor with nothing in it. So there's no sort of no code and then no inputs either. And so this is actually essentially what it's called when we do new Hippo normally. So if we save this you'll see now that since we have a default constructor here with no inputs, then it is no problem. Now remember how with methods if we had two methods have the same name, but have different input types so we have int weight in grams, and then double weight in grams, then That's really, then those can be two different methods. And it's the same story with constructors.
So we can have multiple constructors with the same exact name. Or I mean, I mean the constructors all have the same exact name. Then won't be infrastructure if it didn't, but yeah, you get the idea. So yeah, so we can have multiple constructors. So now we can do either this constructor. So let's just print System dot out dot print done.
I got called constructor with no inputs. And then here we can do System dot out dot print ln called truck door with two inputs. There we go. Alright, so now if we go back to our classes and objects, and run this, there we go. So you'll see it's Prince called constructor with no inputs. So Since we didn't put any inputs into these parentheses, you'll see results called because this, but if we put two integer in here, two comma three, and now run this.
Why did yeah, there we go called constructor with two inputs. There we go. All right. And that's giving 00 here because it's trying to divide it. So if we do not two, three, but let's say 10,000 and 100,000. Now it'll give us the correct one.
There we go, says 1000 and 100. Yeah. So you'll see how now since we have two inputs in the constructor, and now calls this constructor, which prints call the constructor with two inputs and then assigns the inputs that we inputted to the values in the class. All right, and so that is essentially how constructors work. All right. So homework Your job is to change the weight in grams to weight in kilograms.
There we go, and it's gonna be a double and then change the double height in height in meters. There we go. And now your job is to create a constructor where you pass in a weight in grams and a height in millimeters. And that gets assigned to the weight in kilograms and height in meters accordingly. So you would change the grams to kilograms and then the meter millimeters to meters in the constructor and then change the corresponding methods as well. So pretty simple homework.
If you can understand constructors, you can understand this and really nothing too difficult. And yeah, I wish you luck with homework and I'll see you next time soon.