Java - Fahrenheit to Celsius (and vice versa) Tutorial! Temperature Calculator in Java and Eclipse!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube it's rafael here from xx raphael productions and in this video i'm going to show you how to create a fahrenheit celsius calculator and also a celsius to fahrenheit calculator so i'm going to go ahead and create a whole new java project for this if you already have an existing project and you just want to incorporate this into it this will also work anyway just as long as you have the correct code okay so i'm gonna do temperature calculator tutorial obviously whatever you want to call it is up to you let's go ahead and click finish and when it comes to the module info it's going to ask you for one just simply click don't create because we don't need it and let's go ahead and once you've created your new project you should go to the drop down box and then right click on source and you should create a new class for this all right and for this gonna name this class let's see you can do it calculator script i guess whatever really you want to call it and then for method stubs make sure you take public static void main all right because this one we're going to be using a bunch of if statements and also be sure to load up your um in google i already have my mine loaded up but i wanted to get the formula so that i know how what to do when i incorporate that into my code we'll be using both of these formulas as we continue okay so we'll don't worry about that for now we'll first get everything set up so click finish we'll set up our scanner as well so to do that i'm going to import this um utility over here on the top yes we right before the class import um java.util and then we're gonna do scanner that will let us as users take user input as well all right so we're gonna do just to utilize our scanner let's go ahead and space this out and do on scanner scan equals new scanner system dot in all right now this is what i called my scanner is simply scanned but if you want to call it any other name you are free to do so just be sure to use that when you refer to it again the next time all right and then next thing i'm gonna do because this is gonna take in our user input let's ask them a question so you system that out the print line and as this is going to be a temperature calculator we'll be doing fahren um can spell fahrenheit 2 celsius bracket one or celsius to fahrenheit and then two so don't worry if you don't understand what the one and two are for don't worry about it because it will make more sense when we do our if statements that will be for later all right that will make a lot more sense but let's go ahead and finish setting up our scanner by making a an integer going to declare one we're going to call it simply result equals scan.nextint and that should um be scanning the value since one two are integers we'll be scanning that and whatever one we choose will result in the if statement all right whatever if statement will run depending on the user input so now we're going to set up our if statements as i promised earlier so do if bracket result equals equals one so now this should make a little bit more sense this is simply if we want to convert fahrenheit to celsius let's list it down here so we don't get confused calculate fahrenheit to celsius this comment in java all right then do else if result equals equals two and this will be the opposite as you may guess calculate celsius to fahrenheit and then lastly we're going to do an else statement so this is going to take care of all the other general answers so let's say our user pressed three or five or any other number this will simply run any number that is not one or two all right and we're gonna do system.out the println this is the easiest one so let's take care of this one first system that out the print line and this will say not a valid choice so basically we want to limit them to only picking one or two we don't want them to like press three or four and cause potential glitches so this statement here this block of code will take care of that so we don't have to really do anything here we just have to really focus on the first two if statements because this is where most of our main code will stay at all right so i'm gonna go and space it out and this one is gonna make us know that we have chosen so let's do system that out the print line you have chosen fahrenheit to celsius okay and then we're gonna do another print statement this time will be print line and this will be enter fair now this will give the users a choice enter the fahrenheit value okay and now for this we're going to go ahead and make sure that we have our scissors we're doing fahrenheit to celsius to get fahrenheit to celsius loaded on google it should automatically get with the with the formula let me go ahead and erase this so the formula is simply fahrenheit minus 32 times five over nine so that's going to take that's going to take care of it if you think about it it'll be like pemdas where you do the parentheses first before this like order of operations all right so let's go ahead and we will incorporate that into our project so um we're gonna declare a double because this sometimes some um temperatures you might have a decimal point so the double value equals let's see scan the next double now you may be wondering why i did scan next double rather than scan next end well if we were to do integer for this it will give an error because we declared value as a double so we will do scan that next double to take in user input for this double all right so let's go and this will be our final results of value final and now is where we take in the formula so it was fahrenheit minus 32 times five over nine so value minus 32 times five over nine and that should fix all the errors okay so the last thing we're to do is print that out as well system that out the print and don't do print line do simply print i'll explain why in a minute celsius is and then the last one we'll do is a print line statement alright so that'll be math dot round this will round our values so value final times 10.0 divided by 10.0 now the reason why we did this is because basically what we what we want is that we want our um decimal place to be rounded it when we get our temperature we don't want to like stretch out too much when we do it okay that's like i i tried it before without it and it kind of gives an inaccurate result you've seen some thermometers they kind of rounded to like the 10th place that's why i decided to incorporate this that is optional but i suggest you do it this way as well so let's go ahead and test it out okay fahrenheit to celsius let's go run our code we're running the play button here and now if we choose our first result so it will ask fahrenheit celsius one we still haven't done result two so we'll do fahrenheit celsius first and now we can enter our fahrenheit value so what we want to enter well we can probably do let's say 98.4 for example and let's see our result it's 98.4 celsius is 36.9 so here in google's calculator it is round it is stretching its numbers all the way but in here we want our temperature to round up to the tenth so we it goes 36.9 36.8 eight is greater than five it will round up to 36.9 so that is what this line of code here takes care of it will take care of rounding the decimal place so yeah very nice we've got the fahrenheit celsius working let's go and now do celsius to fahrenheit so we can theoretically copy and paste all this but if you are a new programmer i would suggest to type it all out to get used to the java syntax and also just get used to typing code in general this only if you're new to it i guess that's a better option practice makes perfect really so like yeah um but also in other things we'll be using a different formula so you notice for this we use the value minus 32 for our other one which is celsius to fahrenheit we will be using um let's take this number out celsius times nine divided by five plus 32 that is just celsius to fahrenheit all right let's go ahead and do that now split out the print line and you have chosen celsius to fahrenheit simply like that just like the first one except we inverted it and then we do system.out the print line this will take in our celsius value you make sure you end all this with a semicolon and then do double value two we want to make the vowel the name of our double different this time value two scan the next double because we declared it as a double and then now here comes our formula final result equals value to value of fahrenheit no value of um celsius more like and then times and then we would if we go back to the formula it is nine over five now personally the way i like to do this i load up my calculator and i want to get the value of nine divided by five and i honestly want to use the decimal place so do 1.8 times 1.8 and then plus 32 now that is simply the same thing really so if you were to do like celsius times 9 divided by 5 or celsius times 1.8 they are the same thing now this is really up to personal preference i want to use the decimal place for this because i actually kind of prefer it i feel like it's more um i guess more accurate i guess but if you want to do 9 over 5 that should give you the same result but personally for me i want to do 1.8 for this formula it is all the same 9 over 5 will equal to 1.8 when you divide them so that's pretty much the same formula really it will do order of operations where you multiply first before adding or subtracting so we don't have to worry about any parentheses over here all right and then system.out.print again not print line because we want this to print in the same line so fahren can't spell fahrenheit is and then lastly the print line now again we are going to round it up so do math.round um this time we called our double final result times 10.0 as we are rounding to the tenths place so 10.0 again all right and that should take care of it let's go ahead and run our application now we can also do number two because we have now finally coded our if statement so enter celsius value let's probably do let's see 37.5 maybe and let's go ahead and put that over here and we should get the exact same result we have nothing to round so 99.5 99.5 now let's go ahead and try another value maybe um 39.4 or perhaps or let's do a value that requires us to round so this will be 103.46 that will make us round to 103.5 let's experiment and see if that works so celsius to fahrenheit 2 and our celsius value was 39.7 and as you can clearly see it is rounding up to 103.5 so our calculator is working exactly as we intended now let's go ahead and try our else statement so if i were to like input let's say five it will give us not a valid choice because it is not one or two that is what this else statement takes care of so once you've got that working you have pretty much got your application set up and ready to go so um the reason why we did print uh i i believe i said i would explain this let's go ahead so we wanted this last one to be print line but this one is print because we want we don't want it to like print a new line we want all of it to be in the same line so like if i were to let's say run my application again and do let's say any random fahrenheit value like 76.8 if we were to do print line the value of celsius would be printed in a whole new line and that's not what we want we wanted it to be in the same line now nothing is wrong if you want it to be in the different line because the values should be correct either way but to make it look prettier let's go ahead and just leave it as print all right so thank you very much for watching that is how you make a um temperature calculator in java well relax temperature calculator conversion in java the python tutorial is linked down below as well i made something similar back in python that will be in the description below if you want to look at it but yeah i will see you in my next video peace out
Info
Channel: xxRafael Productions - Rafael Vicuna
Views: 820
Rating: 5 out of 5
Keywords: code in java, java, java calculator tutorial, fahrenheit, celsius
Id: 6EU_POQF2Q8
Channel Id: undefined
Length: 14min 39sec (879 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.