# 3.2 Fare Calculator Part 1

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or

Time
4 hours 19 minutes
Difficulty
Beginner
Video Transcription
00:00
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.
00:21
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.
00:33
All right, so at this moment, I'd like you to
00:35
go ahead and open up intelligence
00:39
and in intelligent please go and make a new project.
00:43
As always, if you have forgotten the path to create a new object, you go on file a new project.
00:52
And actually, before we do that, I'd like to first make a folder
00:57
that will store my project. So we'll go in.
01:02
Sorry. Course Model three. And here will make any folder and we'll call it.
01:08
Uh,
01:11
one second will call it
01:12
fair calculator
01:17
kill cutely tour
01:19
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
01:30
and on this screen, I'm gonna select the path in which I would like to store my project.
01:38
So we'll go and click on this folder over here.
01:45
And once you have clicked finish, it'll give you an option to either replace the current window or open up a new one.
01:53
So go ahead and click on new window.
02:00
All right, So what this should do now is open up a new window for you,
02:07
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
02:17
organizations sake.
02:20
Ah, very delicate you, Erica accumulator.
02:24
Very calculator perfect. And then click. Okay,
02:30
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
02:39
you've set up your class properly. And to do that, I usually just
02:45
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.
03:04
So in this case, I'll just
03:06
sprint hello world again.
03:08
And then we can go ahead and click this icon over here to run our program.
03:14
What?
03:17
All right, so we'll give that a few
03:19
seconds and, uh, it should be out putting a result fairly quickly.
03:24
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
03:32
now moving on to the actual code itself.
03:38
What does a fair calculator entail? What, what? What are some of the things that we would like to have in our program?
03:46
So one of the things we'd like to include is an H
03:50
an age for the user
03:52
and
03:53
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
04:06
total cost after the taxes applied, and then a total tax which will basically calculate the amount of tax.
04:16
So let's go ahead and create those. Now, keep in mind I will
04:20
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
04:40
just as a reminder, you can write multiple or declare multiple variables on the same line as long as you separate them with commas.
04:48
So then I'm gonna quit one called Standard Price
04:51
and then another one called
04:53
total price.
04:55
And then finally a last one called
04:58
Total Tax
05:00
Perfect. So now we've created all these variables
05:02
and
05:04
we need to set
05:06
our total place too
05:09
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.
05:17
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.
05:33
Now we would like to ask the user, too.
05:36
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
05:49
java dot you Tilda scanner
05:51
and that will import all of the functionalities we need to accept user in boot.
05:57
And I'm gonna go ahead and have a system got out. Dr. Print Line,
06:02
which will ask the user to enter, essentially enter their age. So I'd say, please enter your age.
06:11
Perfect.
06:13
No,
06:14
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
06:26
and using the new cured to tell the program that we're making a new scanner
06:30
which will
06:31
accept input from the system, which is system in.
06:38
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
06:58
and then dot next end.
07:00
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.
07:13
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
07:30
in the next lab. Part two of this current lap.
07:33
Thank you for watching.
Up Next