Java Programming Tutorial 09 – Class Methods with Parameters
Video Title: Java Programming Tutorial 09 – Class Methods with Parameters
Hello welcome to this lesson of mastering Java this is lesson number nine here we have again our example we’re using with the aircraft we have the aircraft class down here same member variables same member method that we had before we’re not changing anything I just copied this from a previous lesson
Here we’re declaring our new objects for Cessna and Piper Saratoga and populating the member variables with the information so all of this has been copied before what we want to do in this lesson is learn how to add methods to our class that do something other than
Well I should rephrase it we want to be able to add methods down here that take arguments or parameters calculate a result and return the answer back up you see this method we have here doesn’t require any argument we just call it for the aircraft that we care about and it
Multiplies or divides two of these numbers here and returns an answer but there are many many cases when maybe I want to use the data in here but I want to pass a value down that’s going to be involved in the calculation the simplest example I can think of is this all of
These aircraft the objects that we create we have a variable called fuel burn rate that’s how many gallons per hour this airplane sucks down from the gas tank so it might be useful to know hey if I want to fly for three hours how many gallons and gas is that going to
Use or if I want to I for six point four hours how many gallons of gas is that going to use that’s something you do as a pilot is you you factor in how long is my leg and how much am I using of gas
And you want to see how much gas that takes so that you can see how much reserve you have so it’s very easy to do that we can just add a new method here so this is the method that we already have we just continue we just skip down
A little bit this closing curly is closing off the aircraft class we want to be inside of that and then we create a new method and we’re going to it’s going to have a double as a return value because we’re going to be returning how many hours and that we can stay aloft
For a given oh I should say we’re going to be returning how many gallons it’s going to require to fly a certain time which can and can in some cases end up giving you a decimal you we’re going to call this method fuel needed right and we’re gonna open up the
Guy here and then we’ll do an opening curly brace hit enter and there we have the closing curly brace automatically created now inside of here we want to pass a number down into the method down here and the number that we’re going to pass is time because here we have
Gallons per hour we want to know how many gallons is gonna take to fly two hours or to fly four hours or to fly eight hours or whatever so we’ll create another variable as a parameter inside of the parentheses and call it time so whatever number we stick in here is
Going to be temporarily associated with something called time now inside of the method we want to do the calculation and return the result all right and so what we can do is we can just put the return keyword here and do the calculation as follows we can say fuel burn rate times time
Semicolon notice what this is doing so if we call this method then let’s say we call it with three hours we want to be in the air for three hours that number gets associated with this variable called time we take that variable multiply it by the burn rate which is gallons per hour
Multiplied by hours will give us a number in terms of gallons it’s going to result in an answer that’s a double because the the variable I’m or the parameter I have defined here is a double it’s multiplied by fuel burn rate which is a double so the return value
Will be a double which matches what I’ve typed in front of the name of the method so basically the return value has to match what you put out here all right now I’m doing the calculation inside of the return statement I don’t have to do that I could come and create another
Variable here and this and calculate it and assign it to a variable and then return the name of that variable it’s all the way that you want to do it we’ve kind of seen that before when we’ve worked with methods but in any case this
Will be perfectly fine to do this way so after we’ve assigned this information let’s go and do system dot out dot print Ln and then inside of here we can do something like for for Cessna 172 to fly for 2.5 hours it takes and I’m gonna do I’m gonna do the
Calculation however many gallons of fuel it takes so over here I’m gonna do a plus sign and here I’m gonna call that method so I’m gonna say Cessna let me go and do a spacer Cessna 172 dot and when I hit the dot notice I have all of my
Member variables but now I have different options the endurance options what I use before here is the new method I just calculated so I’ll double click that and so this is the method it’s asking for and it’s asking me to put a number in for the time because that’s what I’m trying to
Do so I’m gonna put 2.5 because that’s how many hours I’m I’m after here and then I will put another plus sign here I will put gallons of fuel all right and because this is kind of a long statement now let’s just leave it on one line it’s
Not that hard to understand so what we have is 4 Cessna 172 to fly for 2.5 hours it takes and then this is calling the method it’s calling the fuel needed method that’s a member of this object and it’s passing a value of 2.5 so what
Happens is the code then jumps down here and it comes into this method with 2.5 in here it assigns 2.5 to this number multiplies by the fuel burn rate for the Cessna because we’re calling it for the Cessna object it returns a number back to the caller which in this case is
Gonna be stuck right inside of a print statement so this whole thing’s in a result in a number and then we append gallons of fuel so the whole point is to calculate how many gallons of fuel so let me hit save and let me hit run for
Assessment of 172 to fly for two and a half hours it takes 23 point seven five gallons of fuel so now you can see how this would be useful information to know let’s say I’m planning a flight on another day and I want to go for four
Point two hours so then I can go over here and change the value that I pass in terms of time two 4.2 hours I can hit save and run for Cessna 172 to fly for 4.2 hours it takes thirty nine point nine gallons of fuel now of course we’ve done this
Calculation just for the Cessna but since we’ve added the method to the class we can use it to operate on any objects that we’ve created from the class so let me just to make this simple to show you let me go ahead and copy this whole print line I’m just gonna hit
Ctrl C to copy it and then over here I’ll just skip down a little bit I’ll hit ctrl V to paste it and then I’m going to change a little bit so for for the piper to fly for let’s do it for now let’s leave it for point two hours and
We will change this this guy right here let me go and delete that just to show you what’s gonna pop up piper Sarah toga that’s the object that we have dot whenever the dot comes up I can select calculate endurance or fuel needed that’s the one I need and I’m gonna put
A value of four point two hours gallons of fuel so it’s basically the exact same print statement but I’m using the the method associated with Piper Saratoga and I’m passing the four point two down there whenever it gets down here then it multiplies the time times the fuel
Burned for the piper the reason that’s using the piper information is because it’s part of that object so let me go into hit and run for a piper to fly four point two hours it takes eighty six gallons of fuel so you might look at
This and say well for a Cessna it takes 39 gallons to go for this many hours and for a piper it takes almost twice as much to go for four point two hours or a little more than twice as much boy isn’t a piper a terrible airplane so then you
Might just look at those numbers and say it’s terrible but don’t forget the piper can carry more people and it actually goes faster so you know you may you may actually travel a longer distance there the point is that you can take input let’s do one point two five
For instance and for the piper you can calculate at one point two five whatever number you pass it you can display a different result alright so that’s very very important for you to understand so what we have here is the capability to add into a class numerous methods here we have a method
Already in the class that doesn’t require any arguments to just calculate something in returns value and here we’ve demonstrated when we can add a method to the class that requires an argument a calculation is performed and it returns the value back from above that’s what you’re going to see more
Times than not is a situation when the method actually calculates something that uses an argument and then you return the value up above and I can use these method calls in print statements I can use them for arithmetic I can use them in mathematical computations and so on so that’s basically what’s happening
There make sure you understand what’s going on here and then I strongly encourage you to go off to the exercises here whenever I we work in with the grocery store class because that is another example of when you’ll get some practice working with methods and classes in Java so we could add
Additional classes or I should say additional methods down here to calculate different things the main idea for you to realize is if you’re accepting an argument you operate on the member variable return the value back and I hope you can see that grouping all this stuff together in a class
Definition makes a lot of sense because it’s all relevant for the same thing and if I create here I have two airplanes assassin and a piper I can go create six more airplanes and populate with the appropriate information I don’t have to do this calculation numerous
Times I’ve defined it once and then I can just pass it values as we need in order to calculate what we’re trying to do so go off to the exercise right now work on that and then follow me on to the next lesson or we’ll continue working with Java
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
What would be the difference between having the class of aircraft with the word public and without?
Hi, where can I get the exercises?
So happy I met @Darktrode12 on Telegram he changed my life after being duped on this platform so many times he got my cards delivered to me with great balance on it.So I’m so proud of him and vouch for him
Thanks for the videos. I am learning more here than in class!!
SO grateful to have found this channel!
Thank You man, you Are awesome
I love you
where are the exercises? Your videos are wow. thanks. but please show me where the exercises are
Wow what a very excellent explanation! one thing though I can't find the exercises that you are telling us at the end of the lesson.
thx buddy.
Well organized explanation..thank you so much for the time you spent to teach others
Hi your explanation is very well organised. how can I get the Grocery exercise to practice as you shown above?
Thank you for these videos! Definitely needed these to supplement a bad teacher. Is there any way you could make videos regarding read and write csv and text files also?