Tip:
Highlight text to annotate it
X
Hey there, Teacher Tanti here.
Today I'm going to be showing you what a Java program looks like.
It seems like students can read programs before they know how to write their own.
So I'm going to be writing a Java program which finds prime numbers and
record it for you to follow what I'm doing.
It's a simple program with no mouse, no pictures and no colors
in order to keep things simple.
But don't worry, later will be doing more interesting programs.
I'm going to be using NetBeans in order to write my program which you can
download from the link in the description box below.
Let's get started.
As soon as you start NetBeans, you need start a new project so click on this button up here
and you start a wizard.
Click next
and now you have to give your program a name.
I'm going to name mine "my program".
And then just click down here on finish
and that's it. You've created a new project and can start your program and
this is where you'll be writing your program.
Start by deleting this line here
and we'll start with a display command which is system dot out dot print line.
Now whatever number is written between these brackets will be displayed
after you click on this green arrow up here
and it will be displayed down here. See? There's a one displayed.
If I change that number to, say, a five,
then when I click on this green arrow I'll get instead a five down here.
You can display more than one line
by writing several print line statements under each other.
This program, for example, will display
a "one" for a ten times under each other. See?
Now of course we can change those ones to be a sequence
of numbers from one to ten for example.
When we run this program, we'll have
a sequence
from one to ten.
But there's a much shorter way
to display a sequence of numbers in Java
and that is by using something called a "for loop"
and a for loop
is a statement in Java
where you give it
the number that you want to start from,
the number that you want to end with
and how much
difference there should be between one number and the next.
See? So it starts from one,
goes up to ten and it adds one
between each number.
So it will do
the sequence: one, two, three, four, five... until ten.
If we change that number to a five
it will start from five and go up to ten.
If we change
the second number
to be a five
then the sequence will go from one to five.
And if we change the last number
from a one to a two
then it will add two between each number and so will go
one, three,
five, seven, nine.
Always adding two.
An important part of programming is
asking for user input, asking the user to enter a number for example.
In Java you can do that by writing the statement
new scanner
system dot in
dot next int
and then right-click and choose fix imports.
And we'll call the
input, the number that user entered,
the name
"input"
so then we can display that input.
There are other ways of doing this in Java but they take more time to
explain, so let's try this. Click on run
and now the
program is waiting for us to enter a number. We entered five
and five got displayed back out. If we run it again
then we can enter another number, for example ten, and ten will be displayed back out.
They are better things to do with the input.
We can check for example if it's even
and we do that in Java by saying "if
when the input
is divided by two leaves a remainder of zero",
so it's a multiple of two,
"then
output the word 'even'".
When we run this program
and we enter an even number we get back the
word "even". If we run it again and enter and odd number, for example five, then we get
nothing back.
If we want the word "odd" to be displayed when an odd number is entered
then we need to say
"else",
so if the remainder is not equal to zero,
"else
display the word odd."
So now if we give it
an even number we get the word "even", if we run it again
and give it an odd number we get the word odd.
Now let's instead of checking if the
input of the user is even or odd,
let's instead check
for all the numbers between one and ten and check each number
between one and ten if it's even or odd and we'll be using the for loop as we did
before
and we'll be running it
to go through all the numbers between one and ten.
And of course
we will now check if that number
when divided by two leaves a remainder of zero.
Each number between one and ten.
If we run this program then we get
"odd, even, odd, even, odd even". Why?
because one is odd and two is even and three is odd and four is even etc.
Now if instead we wanted to display the numbers themselves, the even numbers
themselves, then instead of displaying the word "even"
we display the number,
if when divided by two it leaves a remainder of zero. So we get
"two, four six, eight, ten"
all the even numbers between one and ten.
Now if instead we wanted to display the odd numbers
then we check if when divided by two they leave a remainder which is not equal to zero.
Exclamation mark equals zero.
And so we get
"one, three, five"
etcetera.
All the odd numbers between one and ten.
Now since its a for loop then we can easily change the range so now we're
checking
for all the old numbers between one and twenty
and what I'm going to do now is I'm going to
ask the user to enter what the maximum number
should be to check.
So we go between one
and this number which we will call "max number".
So instead of twenty we will now
make the for loop go up to
max number.
So we enter the number, for example five
and we check all the odd numbers between one and five.
If we run it again we can
give it instead, for example a ten,
and it will display all of the odd numbers between
one and ten.
At the moment the program just waits for us to
enter a number without telling us what to do.
So let's make it a little easier to use by
putting a print line which will give us an instruction
such as "Please enter a maximum number".
So now when we run the program
when it's waiting for us to enter the number it will tell us
what to do. And that will make the program a little easier to use.
Let's also input a blank line over there
in order to separate the number that the user enters
from the numbers that the program gives back.
You put a blank line by entering a print line
with nothing between the brackets, like that.
See? And now there's a blank line
separating the input from the output.
Now let's make the program ask you whether or not you want to
use it again or not
when it's finished.
Now in order to make the program repeat itself
we need to use the "do/while" statement. We'll say
"do the following while
the user
enters the number one".
If the user doesn't want to repeat
the program then he'll enter some other number.
So now we'll ask the user to answer a number to say whether or not
he or she wants to repeat program
and we'll call that number "input"
and we'll ask the user
"enter one in order
to repeat
the program".
So we run it,
now it asks us
to do its thing and then it asks us whether or not we want to repeat.
We enter one to repeat and again
it does its thing.
If we enter zero for example then the program will end.
Now let's put
a blank line
before
it asks
whether or not you want to repeat the program.
so we enter the maximum number to display the odd numbers.
See? There's a blank like. We enter one and it repeats itself.
Now enter zero the program ends.
Now let's make the program a little more interesting.
Instead of looking for
odd and even numbers, let's instead make it look for
factors
of a number that the user enters.
So we'll call this number
the name "number"
and we'll ask the user
to enter
a number in order to find the factors of
and we'll be using the for loop
in order
to look for possible factors
of this number that the user enters.
A factor can be
any number between two
and half
of the number
of which we're finding the factors of.
So we're assuming that one
and the number itself are not factors.
And then we'll check if
when the number is divided by that possible factor
it will leave
a remainder of zero
and if it is then we'll display
that possible factor
because it is a factor.
If not then we'll leave it out, we won't display it.
So we run the program,
it asks us enter a number to find the factors of,
we'll enter ten
and the factors are
three and four?
That can't be right.
It seems we have a mistake. Three and four are
not
factors of ten.
Oh there it is.
We're checking
if the possible factor will leave a remainder which is not zero
instead of
if it's zero.
So we change that to an equals,
make sure that the remainder is zero, and there we have it. Two and five.
Factors of ten.
We repeat the program
and now we'll check twenty.
Two, four, five and ten all factors of twenty.
Let's try another number.
Seven. Seven is a prime number so it has no factors so it displays nothing.
Now let's make the program just check if a number is a prime number
and
a number is a prime number if it has no factors.
So now, rather than display the factors of the number,
instead we will count
the number of factors that a number has.
So we start from zero, start counting from zero, and every time that we find a
factor,
which is a number which when you divide
the number that the user entered by it gives a remainder of zero,
everytime that
a factor is found we will add one to the number of factors and at the end of the
loop
we will check
if the number of factors is zero.
If it is zero
then we display that the number is
a prime number.
Else,
if the number of factors is not zero
then we display that the number
is a non-prime number.
Let's try that out.
Okay, so
let's check if, say, ten
is a prime number. It says non-prime.
We repeat the program. We try five.
Five is prime.
Repeat the program again. Let's try some larger numbers
Eighty-one. Eighty-one is non-prime.
Repeat the program.
Eighty-three. Eighty-three is a prime number.
Let's make the program a little more interesting.
Instead of making it
check if a given number
is a prime or not,
instead we can
look for prime numbers
within a certain range.
So for example
we look for all the prime numbers between
two and one hundred.
As you might expect we will be using the for loop in order
to check
every number between two and one hundred.
and if
the number has no factors than it is a prime and we will
display
that number. So we'll display all the prime numbers between two and one hundred.
See? These are all the prime numbers between two and one hundred.
Two, three,
five, seven, eleven, etcetera.
Okay now, let's change the range. Instead of checking for
for it
all the numbers between
two and one hundred, let's check all the prime numbers between
one hundred and two hundred.
See? These are all prime numbers between one hundred and two hundred.
Okay now,
let's let the user choose the range. Let's ask the user to enter
a minimum and maximum number
to check for primes
and we'll call them
min and max.
And we'll make the for loop go through all the numbers between min and max.
Let's try it out. Okay so,
We'll check for all the numbers between
say
one thousand
and one thousand one hundred.
These are all the prime numbers between one thousand and one thousand one hundred.
Let's try it again.
Let's check for all the prime numbers between one million
and one million one hundred.
Oh, so few.
These are all the prime numbers between one million and one million one hundred.
See? Something interesting come out of our program after all.
And there you have it.
A Java program that finds
prime numbers.
Now, next we'll be seeing
other programs which do different things.
If you have any trouble
in using Java then leave me a comment and I'll see what I can do
and I'll see you next time.