Java Fundamentals

Java Basics Exercise

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Java Basics Exercise" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie walks through creating a gross pay calculator, files, packages, main methods, and Java naming conventions. The built-in text scanner class Scanner, defining variables, setting variable types, and how to make comments are also covered in this segment.

Preview
Close

Transcript from the "Java Basics Exercise" Lesson

[00:00:00]
>> So we're good to go, we have our project set up and we can go ahead and jump right into the basics of Java. So we're gonna start by writing our first program, this is gonna be a very hands on course. We're gonna write code and talk about it as we write it so that we can learn what we're doing, okay?

[00:00:22]
So the first program that we're gonna write is a gross pay calculator. So it says, write a program that calculates an employee's gross pay. All right, so back to our IDE. Now, if you look in the left panel under Project, this is where all of our files are going to live that we create, okay?

[00:00:50]
So you should have a little thing under here that says source, a little folder. Under Source there's a main and a test. So we're gonna write all of our code in the main. And then there's a folder under main that says Java, all right? So, the very first thing we wanna do is to create a package.

[00:01:13]
So a package in Java is simply a folder that contains a bunch of Java files. It doesn't have to be a bunch, it could be one, zero, whatever. But it contains Java files, and it's some sort of logical containment. So for example, this program that we're gonna write is a basic Java program, so we're gonna create a package that's for basics, okay?

[00:01:39]
So, under Java, you're gonna right click and you're going to hover over new. And then you want to say package, new package. Now, the convention, we're gonna talk about conventions all throughout. The convention, what it means, convention means this is what's widely accepted by Java developers all over the world as this is what we do.

[00:02:05]
It's not a rule. Nothing will break if you don't do this, but people will look at you funny, all right? So your package, the name of that starts with a lowercase letter, if it needs to contain multiple words in it people will typically do underline or something like that.

[00:02:27]
So for ours, we're gonna call it basics, all lowercase. All right, so there's our package, our folder, inside of the package is where we'll create a new class, so a class is basically a Java file. This is what you write your Java code in. So on basic we're gonna do new and then Java class.

[00:02:53]
And then we need to give it a name. Convention says that your class name should begin with a capital letter, okay? So we're gonna call ours GrossPayCalculator, and notice this is kind of like three words, you cannot have spaces in your class names so they all need to be one word, but we use camel case to distinguish between the words, okay?

[00:03:19]
So notice pay has a capital P. Calculator has a capital C. Press Enter. Okay. And now we have our new class. Okay, so notice here we have our package defined on the first line that is required. And then we have the class declaration. So it starts with public, class, and then the name of the class.

[00:03:51]
The name of the class has to be the same name as the file, okay? And Java is case sensitive. So if you happen to have named this with a lowercase g, I would get an error. I don't know what you're talking about, right? So it has to match this exactly, and it's case sensitive.

[00:04:14]
All right, so the class after the class name, we have a set of curly braces, this encompasses the class. So anything written between these curly braces belongs to this class, all right? So our assignment was to write a program that calculates an employee's gross pay, right? So we want to one, get some input from whoever is going to use this program, asking them about their number of hours worked, as well as their pay rate.

[00:04:50]
So within the class you need methods. And then you can write code within the methods. And we'll have a whole section on methods. But for now, trust me, we're gonna write a main method. And the main method is where all Java applications start from. So it's gonna look for a main method when the program runs, and it's gonna start executing that.

[00:05:13]
So to write the main method, it has to be exactly like this public_static void main, and inside of parentheses, you're gonna write String, a set of square brackets, and then args. Then within the main method, we can write our code. So, again, we're gonna do output, we're gonna say System.out.println.

[00:05:46]
And then we're going to print a string text. This is within quotation marks, okay? So we can ask them, how many hours did you work? And then we're gonna want to read this back in. So we can use what's called a scanner. So it's a class in Java.

[00:06:11]
Java provides it automatically. So when we write Scanner, notice there's an error right here, and there's also a suggestion. So I can click on this. And I basically did the shortcut that IntelliJ told me which was option and return, and it imported the scanner for me. So if you wanna use a class that is outside of this one, you wanna use any of its methods or anything like, that you need to import that in.

[00:06:44]
So we'll say scanner, we'll call it scanner = new Scanner. And we wanna read from the input the console. So we'll say System.in. All right? Now with this scanner object, we can now use the dot operator to show us a bunch of methods that are available from this class.

[00:07:10]
So what we want is to read from the input. And so, we can do what's called next. And next will read whatever was the last thing that was on the console, okay? So, this will read this in. Now, notice here, we're asking for hours. So, this might be a whole number, an integer.

[00:07:37]
So we want to read specifically, there's a bunch of methods here. Next it's gonna read a string and we can read nextInt to say, I'm expecting an integer to come back here, all right? Question.
>> So the scanner is to read from the command line?
>> Yes. All right, now, we're reading this from the command line, however, we need to store it in a variable if we want to use it later on, okay?

[00:08:13]
So a variable is essentially a memory location that can hold data. Java is a statically typed language different than JavaScript. Meaning that the compiler needs to know what the data type is before you can execute the program, okay? So when we declare our variables, we have to preface it with the data type.

[00:08:40]
So we want to say int, which is the data type for an integer, we're gonna give the variable a name, we'll call it hours, and then we'll put an equal sign, okay? So, on the left side of this equal sign, is the declaration of the variable, on the right is the value that'll be stored in that variable.

[00:09:00]
Good? All right. So, we have, how many hours they work. We also need their pay rate. So, let's ask them another question. To do so, we're gonna do another System.out.println. Now, if you're using IntelliJ, there's a lot of nice shortcuts. Java is a very verbose language, meaning you have to type a lot, right?

[00:09:23]
In IntelliJ, you'll see me do this. I'll just say the whole thing, but I'm gonna do sout, which is a shortcut. If I press Tab, it'll go ahead and auto-complete that for me. So you'll see me do that a lot, but I won't say it anymore, all right?

[00:09:40]
So how many hours did you work? And now we need to know what is your hourly pay rate? Okay, we wanna read this Int again, right? So we've already declared scanner on line 10. We don't have to do that again, we can just use it again. And this time we're asking for a pay rate, it could be a decimal number.

[00:10:07]
So instead of int, we're gonna use double as the type, and then we want to name this variable, we'll name it rate. Variables, convention, start with a lowercase letter. And then we'll say scanner.nextDouble, right? We're reading a double. Now that we're done with the input we can go ahead and close that scanner, so we'll just say scanner.close.

[00:10:39]
Notice, every statement in Java ends with a semicolon. This is not optional, must end with a semicolon. So now that we have our input, we can do our calculation. So in order to determine the close pay, we can say hours times rate. What will be the data type for that?

[00:11:04]
You're multiplying an integer times a double.
>> Double.
>> Double, good. So we're gonna say double, let's call this payRate. Notice, two words. Again, variables cannot have space either. If you're doing a multi where you use the cam case, start it with lower case. Double payRate = hours * rate, great, and now we can print this out to the user.

[00:11:34]
So we'll say Gross Pay I'm gonna put a little space there. Now I want to append the value from the variable to this stream. I can do that with a plus sign, okay? I just write the pay rate right here, okay? Good to go. Should we run it?

[00:11:59]
All right, let's go ahead and run this Lovely, so, we see on the console is asking how many hours did you work? I'm gonna say 40 hours. And pay rate, let's say maybe 15, 50, okay? So the gross pay would then be 620. Lovely, all right, so that was our first program.

[00:12:31]
We learned a lot of the very basics of Java, such as packages, classes, main method, input and output. Any questions? Yeah.
>> What happens if you don't close the scanner?
>> Great question. Okay, what happens if you don't close the scanner? So this is a good question, I'm gonna use this opportunity to teach two things.

[00:13:00]
So, I'm gonna comment this out. A comment is essentially, you can put two slashes and it's telling the computer don't listen to this, ignore that line, right? Don't try to execute that. All right, so if you don't close the scanner, essentially that resource is left open once the program like eventually closes, and ends, and terminates it may release that, but if not it'll just kind of be running in the background.

[00:13:33]
If this was a really lengthy program, you had a lot of other resources going on, you could see how that might start interfering with things. So it's not the end of the world if you forget to close it. Unless you're on some big, huge enterprise application you got a bunch of these things going, and it could cause some performance issues.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now