3.2 Fare Calculator Part 1
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
hi and welcome back to the course now has mentioned in the last video we said we would be making a fair calculator in today's video. And in this love and two lists out some learning objectives for today's lab, we're going to be aiming to apply the concept of if statements by creating a fair calculator program.
And essentially, what will happen is the fare to the festival or concert. If we use that as an example, will be determined based on an individual's age.
All right, so at this moment, I'd like you to
go ahead and open up intelligence
and in intelligent please go and make a new project.
As always, if you have forgotten the path to create a new object, you go on file a new project.
And actually, before we do that, I'd like to first make a folder
that will store my project. So we'll go in.
Sorry. Course Model three. And here will make any folder and we'll call it.
one second will call it
kill cutely tour
perfect fairy calculator. All right, so then I'm gonna follow the standard procedure of creating a new project. Click next to leave the default settings and click next on this one here
and on this screen, I'm gonna select the path in which I would like to store my project.
So we'll go and click on this folder over here.
And once you have clicked finish, it'll give you an option to either replace the current window or open up a new one.
So go ahead and click on new window.
All right, So what this should do now is open up a new window for you,
and now we can essentially get started with our program. So go ahead and create a new class. And let's keep all the names the same for
Ah, very delicate you, Erica accumulator.
Very calculator perfect. And then click. Okay,
All right. So that has created our class. And now, as I mentioned in one of the previous lives, it's always best to test if
you've set up your class properly. And to do that, I usually just
have a quick console dot Not sorry, not a console that log. I mix it up with Oscar, but a system dot out dot print, which will print out a certain sentence, were number whatever it may be to the consul to see if we're actually doing things correctly and our class or program is working.
So in this case, I'll just
sprint hello world again.
And then we can go ahead and click this icon over here to run our program.
All right, so we'll give that a few
seconds and, uh, it should be out putting a result fairly quickly.
All right, so there we go. It's said hello world over here, which is a thumbs up sign for us to continue with our program
now moving on to the actual code itself.
What does a fair calculator entail? What, what? What are some of the things that we would like to have in our program?
So one of the things we'd like to include is an H
an age for the user
three or four other things we'd like to include are the tax that will be applied on the ticket as well as a standard plus price, a total price which will include the
total cost after the taxes applied, and then a total tax which will basically calculate the amount of tax.
So let's go ahead and create those. Now, keep in mind I will
have all these set as a double. So the data type will be a double since tax or a price or total tax. Any of these numbers can be decimal, so having them as fixed integers does not make much sense. So I'm gonna go ahead and name a variable called Tax. And
just as a reminder, you can write multiple or declare multiple variables on the same line as long as you separate them with commas.
So then I'm gonna quit one called Standard Price
and then another one called
And then finally a last one called
Perfect. So now we've created all these variables
we need to set
our total place too
zero so that the program knows we're starting with, like, zero in our total price. And there's nothing in it as of now.
And for a tax ID like to use a standard tax rate. Now, here, in what many provinces in Canada we use 13% as tax, so I'm just gonna put in 0.13 as a tax percentage.
Now we would like to ask the user, too.
Enter their age. So we're essentially asking for input, and that should ring a bell. We need to import the scanner, don't you? Till class. So we'll do that by typing import
java dot you Tilda scanner
and that will import all of the functionalities we need to accept user in boot.
And I'm gonna go ahead and have a system got out. Dr. Print Line,
which will ask the user to enter, essentially enter their age. So I'd say, please enter your age.
we talked about accepting user input, and for that we'd have to create a new scanner. And we could do that by typing scanner and having a name for our scanner
and using the new cured to tell the program that we're making a new scanner
accept input from the system, which is system in.
All right, So one more thing that we need to do before moving on to the bigger blocks of the program is store the user and put in our age variable. Since what we are receiving from the user is an age so that we will do by typing age equals user input, which is the name of our scanner
and then dot next end.
Now, the reason we have said H equals user input dot next in't is because we're accepting an integer. The type of age is an integer.
So I'm gonna go ahead and stop this lab over here and in part two of the lab. We will be completing the rest of the program. Hopefully. And this is what we have so far. So if there's anything that you do not understand, make sure you go over it and I'll see you
in the next lab. Part two of this current lap.
Thank you for watching.