Programming Basics with Java – Introduction to Programming – Exercise

Hi everyone! I’m Annie and this is our
first exercise session. So I’m going to start with some revision and afterward, we are going to solve some problems. So let’s start! What is coding? As you might already know this is like telling your computer what to do. Basically, it’s like communicating in a language that you both understand so that your computer does what you want to do. Here we have a basic command
because these types of you know here I want this, ok I’m going to do it, this is
like a command. So as you can see here we want something printed so we have a text. Oh, sorry, that was ugly. So we have some text and we want to print it
so here you can see this is the command it may seem a bit complicated at first but I promise you… sorry… you’re going to get used to it. Let’s… let’s see a few others. So here we have a computer program which is a few commands. So here we had only one command, this is only one,
and when we stack some commands we get the program,
so this here is a program that’s I think, yeah, it prints a few lines of text. Again, you’re going to see this many, many times. So we have another command here we
calculate some expression. As you can see coding it lets us do mathematical expressions. Here we have 5 plus 5. Here we do something with the text I
know it may sound… it may seem complicated now but basically, what it does it is
like a plus sign for… oh no, sorry what this command basically does is it checks whether this text here contains this text here. And as you can see it’s pretty self-explanatory. You’re just asking whether this
string contains another one. And shocking, the keyword is “contains”. And here we have another example. We are going to see it in a few
sessions. So what is an algorithm? it may seem a bit scary at first but basically these are the steps that you take in order to solve a problem. For example when you… I don’t know, fry your eggs in the morning so you go to the fridge, you open the door,
you get the eggs you put them in a frying pan or whatever so all these things that you do in order to achieve something, the steps, they are called an algorithm and in programming, we have to solve some problems and when you have a problem to solve
you have to take steps in order to do so. So this is an algorithm. Ahm… Here we know that programming is basically these programs
that you already saw so a program is a set of commands and as you can see when we program we basically
write commands which result in programs. And we are called developers or programmers. And we also use an IDE which is the environment
in which we write our code we also run it there, so for example, if you want to calculate some expression, 5 plus 5 you just run your program and if your result
is 10 then it is correct but if it’s 11 then there is an error
you have to run it again you have to test it you have to correct
your error and so on. So these are the bugs ahm… you find the bug, you find the error, don’t
worry, they don’t bite and when you are done your program
can run smoothly and correctly. So here we have another example. So this is a bit more complex. Here as you can see
we have a variable which is an integer it has its type so we know it’s a whole number,
it has some name in this case, its “size” and it has value. And what we do here is we print something so… here our output would be “Size=5” and here we calculate the value of the area. I think it’s а square. Let me see. Yeah. So we have the result and this, this is our…
this is how the console in our IDE looks. it could be black in your case if you
have chosen a different theme it doesn’t matter. And as you can see on the first
line we got size equals 5 and then we have oops sorry and then we have the
area which is equal to 25 which is 5 by 5 and this is just a simple Java program and you’re going to write many of these and then even more complex ones. So let’s go on. Here we have a complete Java program so… as you might have already noticed in order to
have a program we don’t only write commands so we need some type of frame
in order to write code there and you’re going to see it in
our code editor too. This class so these are some words that it is
totally normal that you don’t understand them. I, in the beginning,
had no idea what they meant it’s completely okay in time you are going to get
used to them, you are going to understand exactly what each one of these words
means and why it is there but this is, you know, it’s not necessary right
now, these are some details that you’re going to, you know, you’re going to learn about them later. And here we have actually,
I think the same example just in this frame that it’s necessary
for our program. Again don’t worry about this. In the beginning, you are just going to remember that each time you have to use it and over time you’re going to
understand exactly why. So let’s talk about the Java language. Okay, great so as I already mentioned it’s kind of like communicating with the computer so like in the natural languages, you have some rules that you have to follow in order
to be understood so if you want your computer to understand
the commands that you’re giving you have to follow rules and
these rules, this is called, the syntax. So each programming language has different syntax. The whole concept is kind of the same so if you learn Java and if you want to learn C# the only thing new to you would be the syntax and most of the languages they
don’t differ so much but that’s another topic. So we have compiled languages Java, C# these are compiled and we have those like JavaScript and Python which kind of work runtimes so if you have an error you have to firstly start your
program and it will stop on the line that you have the error
it’s going to say – okay, this line here doesn’t work so this is kind of some theory that it’s not exactly necessary for you to solve a problem but it’s nice to know. So yeah the compiler finds the errors at compile time while the interpreter finds them run time. Whatever that means. So… yeah, great. what I didn’t say is congratulations
for choosing Java I might say I myself love the language especially for beginners it is great, the syntax isn’t that complex and it has a huge community. So so so many developers write code in Java so whenever you have a problem you can just ask someone, you can ask Google and the answer will be right there it’s great for creating different types of applications,
you can write even games great for back-end and yeah, it’s also object-oriented. well it kind of it will be hard for you to understand what
exactly object-oriented programming language is but basically, it’s it’s like structured, you can use it to write structured code which is separated into
classes, okay, that was too much you’re going to learn about it later. So in a few courses, you’re going to know what
object-oriented programming is and you’re going to write it effortlessly, I promise. It’s not that hard. So let’s… okay well basically this is what we already mentioned high-level language again this is something that actually
makes it easier for developers to use Basically, it’s closer to us than to the computer. low-level languages are harder to understand by us,
they are something that the computer works with so we know that the code that we
write is actually called source code and again the compiler which we already mentioned. So JDK this is something that you’re going to need in order to work with Java, inorder to write code in Java you can download the JDK if you have not already I advise you to, it’s essential and yeah there is something different which is called JRE which allows the programs to run it cannot develop its own programs
but it allows them to run. Here we have some steps in order to
download and install JDK I’m not going to go into detail right now
because we’re only doing some revision yeah, it’s pretty simple, basically,
next, next next, and finish and you are ready. So the IDE we use some IntelliJ IDEA during the… part of this session where we solve some problems
I’m going to show you in details what how, where, what you have to click what you have to, you know, use so you’re going to see it many times. So basically, the IDEs are the developers
best friend they show you where you have might miss something,
where you have might made a mistake when you run your code you see
whether it is okay, whether… it doesn’t break somewhere and if it does break, so if you have a bug, then… you have to follow some steps in order to find your mistake, in order to fix it. So basically it simplifies the work of a developer. And of course, you can use some other IDE but we here believe that IntelliJ is the best for Java and I myself use it daily and I think it works great. So yeah. You might have already heard
that you can also use an online IDE which is kind of fun an alternative option it’s okay if you don’t want to download
anything on your machine. Completely okay. So… yeah some other details, if you have a Windows, Linux, macOS doesn’t matter it can on run all three of them Again, here we have some installation details. I have to mention, if you don’t
already know, we use the community so this one is completely free you don’t
have to pay anything and for this course it is okay you don’t need anything more
than what it offers in order to learn, in order to solve the problems, so you don’t have to worry and you don’t have to pay anything. So I’m going to show this step-by-step how we create a project, how we give it a name so I’m just going to
go through these a little bit faster. We know that we have to select [New Project] we want some option so I’m just going to show
them too, and we click [Finish] it’s really simple and once
when you get used to it you stop thinking about it so I’m just going to skip through these again this frame that I already mentioned
we have this “public class Main” whatever that means and we have this which is our main method here, by clicking this we basically start our program and this is, here, this is the place where we are going to write our code. So again we can’t go without errors there are code bugs I already mentioned they are not scary, they don’t bite and what’s great about these IDEs is that
they are a great friend and here as you can see the developer has made a mistake and the
IDE says: Okay, here you have a mistake. It underlines it the color is it in red, in yellow, in orange to see, you know, it screams like this here is, wrong fix it. So you don’t have to spend time looking through every line thinking okay is this right, is it wrong. The IDE helps you with this and as you
can see here some typical mistakes For instance, writing outside this main
method, so outside this frame if you hear so wrong capital or lower casing, again
that’s a common mistake and missing this sign here at the end of the commands and again we have some more typical mistakes. As you can see everywhere where we have a mistake it is underlined, it is colored in red, so you can’t miss it. And now let’s start with the problems. So here is the problem section,
we can see all of the problems we have for today. I’m going to solve 1 or 2 here on the right side, we can see the problem description so this is the place where we
paste our code or we type it and by clicking this button here we
can run and test our solution. So let’s start with the first problem we have to write a program which prints “Hello SoftUni” on the console and in order to do so I’m going to use IntelliJ just to mention this is a basic problem one of the most simple ones that every developer initially solves. So here is IntelliJ IDEA I’m going to create a new project just by clicking
[Create new project], nothing complex. Now we select Java, it is already selected by default, here are some other options. The SDK, mine is 12 it doesn’t matter which version
we use if you have Java 8 or another one doesn’t matter. we click [Next] and we want to
create our project from a template it just means that we are going to
have the main method inside which will be very useful
especially for beginners like you. Now we click [Next] and
we have to give our project a name. Let’s say “Intro to Programming”. It should be something meaningful. We don’t need a package
and we click [Finish]. Now, we wait for a bit. So that it can load and as you can see we have the
main method inside the main class and here is where we type in our code. This is the so-called project structure we have some folders here, you’re going to find out exactly what we use them for a bit later maybe in the next course. and I just, as you can see, I zoomed in I’m going to show you how you can do it too. So I’m going to go to [Settings] and I’m going to look for “Zoom”. Okay. So we have a couple of options here. We go to [General] and
then here as you can see we want to change the font size by zoom and you know by clicking [Control] and the mouse wheel. So this is how you enable this feature. And now let’s start with our problem-solving. So what we want to do is
we want to print something on the console. So we use “System.out.println” dot “println” and the text that we want to print it, we use brackets and inside we have to use quotes. So the quotes we use when we want to print text. So “Hello SoftUni” is the text that we want to print it. Let’s see if we have any punctuation.
No, we don’t. So it should be correct this way and by clicking the right button and run I’m going to run the program so that we can see the result Okay. We wait. A bit more. Actually, you can also use green triangles. Here. And the green triangle there to run the program. So let’s see we have “Hello SoftUni” printed. It seems correct. And now what I’m going to do is I’m going to copy and paste it so that we can see whether the test pass [Ctrl + C] and [Ctrl + V] you know, for copy and paste, and we run so we wait and 100 out of 100. So congratulations that was our first
solved problem in the exercise session. And let’s continue with a couple more problems. So another one we’ve got here, so we have a rectangle area, we have to calculate the area of a rectangle and we have the sides of this rectangle which are 5 and 10. So we know the formula 5 by 10. You know the first side multiplied by the second one. So I’m creating a new class so that our project is better separated. [New] and we want a Java class here as you can see we have some other options you’re going to learn
about them in the following courses they are not important now. Let’s think of a name so we have a problem which is called “Rectangle area” so this is how I’m going to name it. “RectangleArea” We click [Enter] and we have our class. We have to write our main method. So I’m going to use a
shortcut which is “psvm”. You don’t have to remember by heart the whole expression you can just use “psvm”. We click [Enter] and it will be
automatically generated for us. And now we can type in our solutions here. I’m just going to show it again. Okay, great. So let’s start. We have to print the rectangle area again when we have to print something we have to use “System.out.println” and inside the bracket we just tell
our program what we want to print it. So here oh, this is an important one when we calculate something, so when it’s not just text we don’t need the quotes. You’re going to see later why. So here I only want the expression, the mathematical expression that we want to calculated and printed. So we have 5 multiplied by 10 which are our two sides and basically, this is it. So we have 5 and 10 and… the result of this expression will be 50 which will be the expected area and let’s see whether it is correct. And on the console, as you can see,
50 which seems alright but if another developer looks at your code he won’t understand what you were trying to do. So these are the so-called magic numbers, 5 and 10, and it’s a great practice to avoid them and what I’m going to do is I’m going to use variables instead of these numbers so the five will be my first side or side A, it
doesn’t matter and 10 will be my second side or side B. They’re integers, so we have “sideA” which is 5 let’s just type it out so “sideA” equal to 5 and another one which would be “sideB” for instance, which would be 10 and now we know that our result is again the two sides multiplied
by each other so “sideA” by “sideB” and… this will be again 50. So the result doesn’t change only the code. And even better I’m going to have another
a variable which will be the result and it will be the result of this expression and now when someone looks at our code they will know that we want the result
printed not only something 5 by 10 which doesn’t make any sense. And again the result will be 50. And even better we can rename it. I’m just going to click, there is a shortcut, so I’m clicking [Shift + F6] and okay, great. and now we rename it
so this will be the area of the rectangle. Okay, great. And this is it. Again, the result doesn’t change, it must
be 50. We can run it once again so that we are sure. And we have 50. So I’m just going to copy these lines so that I can paste them and test them. Okay, paste. And we click [Run]. And we wait. As you can see 100 out of 100. Again, we can check the output section where we can see that our result our output is 50 which is the correct one. Okay, great! So… you know having variables named
properly it’s a great practice. We are going to talk about them in the next
training session. And you know, it’s important to give meaningful names. Okay, we have one more problem. “Calculations” we have to calculate program which calculates, okay we have to write a program, sorry which calculates some expression and prints the result it has to be printed on a separate line so again I’m creating another class for better separation which will be named “Calculations” “Calculations”, okay. Let me hide these and we zoom a bit. Again, we don’t forget the main method, so “psvm”. As you know this is the shortcut, so “psvm” and we click [Enter]. So here we have… the solution. So the first thing that we have to
mention is that we want something printed so we use “System.out.println” and here in the braces, we have the expression which I’m going to copy so that I don’t make a mistake Okay, copy and paste. Great. And… this will be calculated and
it will be printed on the console the second one “System.out.println” for the second expression. So again you can see we have
print and println so when we use println we additionally
print a new line I’m going to show it a bit later again, I copy and paste it here that is great and as you can see these are only
the expressions if we put them in quotes then they won’t be calculated
it will be printed only as text. So we will have 4 x 3 x 2 on the console which we don’t want We want the result. Okay. So it’s different, that’s my point, and the last one – “System.out.println” and the last one, okay, copy oops okay, right, oops okay I copied something else copy again and paste it here, great. and now we click [Run] and this is our result so 11, 24 and 8 I think that these are correct and… here I’m going to show you something if I delete the “ln” what actually happens so let me run it again and as you can see
it is all on the same line which we don’t want. The “ln” basically prints a new line which we need for this particular problem so I’m going to add “ln” here. the expression is same one and [Run] again. Okay. So now what happens is that after 11 we got a new line but all of the other results will be printed on the same line and as you can see that it’s not
what we were looking for I’m going to revert it so we’ve got the “ln” again and this seems correct, okay,
every value is printed on a separate line. I’m just going to copy this. Okay, firstly, um we can create new
variables like in the previous problem and… every expression could be in a different variable and that would be even better so copy and paste here and we click [Run] and 100 out of 100. Great, so here you can see again the output and this is it. Okay, you have two more problems here we didn’t solve them today but they are pretty simple
so you can do them on your own and I advise that you do it so you take the time
and try to solve them and during our next sessions, we are going to learn how to solve even more complex problems so until then see you next time.

Leave a Reply

Your email address will not be published. Required fields are marked *