Java, OOP and NetBeans

shopfitterpsithurismSoftware and s/w Development

Nov 10, 2012 (4 years and 7 months ago)

254 views


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Java, OOP and
NetBeans


Introduction to Java


Introduction to OOP


What software do we need?

Introduction to NetBeans


Java Console Applications Part 1

Lesson 1, 2


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
2



What is Java?



A Programming Language



Originally developed by Sun (now owned by Oracle)



Its syntax is somewhat similar to that of C++, but simpler!




One of the mostly used languages today




It was designed to support
OOP

and also be
portable
(explained
later…)


Today, Java not only permeates the Internet, but also is the invisible
force behind many of the applications and devices that power our
day
-
to
-
day lives. From mobile phones to handheld devices, games
and navigation systems to e
-
business solutions, Java is everywhere!








http://www.oracle.com


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
3





OOP (Object Oriented Programming) is a style of programming



More technically we call it a
paradigm



Its aim is: a style of programming which resembles our real
-
world




Java supports OOP

i.e. Java makes it convenient for us to use this paradigm




So…OOP should not be too difficult!
But

we have to learn /
remember the basics of Java first!




To learn/practice the basics we will start off using another
(easier…but less elegant?) paradigm called
procedural

programming.
We will then switch to OOP once we get more familiar…



What is OOP?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
4



What do we need?


1) Java
(of which there are different versions!)




Java SE

Java EE

Java ME


Desktop Applications


Web Applications

PDAs

TV set top boxes

Car navigation systems

Since we will be developing both Desktop and Web Applications, we will be using
Java SE
and
Java EE

in this unit. In the link below you can find a link to download both as one
bundle. These would then need to be installed.


http://www.oracle.com/technetwork/java/javase/downloads/index.html


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
5



What do we need?

2)
NetBeans

IDE

This is the program we will use to develop Java applications

(download from
http://netbeans.org/downloads/index.html
)



To be equipped for the whole unit, it is suggested you download and
install the version which includes both Java SE and Java EE.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
6



Our Great App

So let’s get right down to it! In this lesson we will create a simple Java
program.


1)
Open
NetBeans

and create a new Java Application

2)
Name it “
MyGreatApp
” (you can also change saving path)

3)
You can see your new project in the Projects Window

4)
Open
MyGreatApp.java




MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
7



In
MyGreatApp.java
we can
replace the text:

// TODO code application logic here

with our code.





public static void main(String[]
args
)

{


// TODO code application logic here

}


public static void main(String[]
args
)

{


String
theName

= “Johnny Cash”;


System.out.println
(“My name is: “+
theName
);

}

Our Great App

Copy the following code:

?

What do you expect this program to do when it is run?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
8



We can run our new application by:



Pressing F6, OR



Clicking the Run button in the toolbar

Running Our Great App

Look at the Output Pane at the bottom of your IDE.

?

Can you make the program display your name instead?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
9



Now that we have coded and run our first program, we will quicken
our pace. We will go through the following concepts, to allow us to
create more useful programs.

Basic Java Concepts



Printing to Console



Variables and Data Types



Getting user’s input



Operators



Selection (IF statement)



Comments


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
10



In our first program we have printed some text (String) to the
output window of our IDE, called the console. So as we already
should know, the following code:



Printing to Console

System.out.println
(<insert text here>);

will print whatever is inside the braces to the console. Apart from
greeting the user, can you mention other examples where we
might use it?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
11



Variables

String
theName

= “Johnny Cash”;

Variables hold
values

which we need to use in our program.


They can hold a
specific

type of data, for example a string, or a
number, and once we create the variable, this type cannot change
(we say Java is strongly typed).


We give each variable a
name
, and a
value
. We will use the name
throughout our program to use the value inside the variable with
that name. The value inside a variable
can

change.

type

name

value


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
12



Naming your Variables!

When you choose names for variables, there are conventions and
good practices you should follow:




Choose variable names which make sense!



Variable names should use the
lowerCamelCase

convention


e.g.
theName
,
xPosition
,
windowSize



Variable names cannot start with a number!


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
13



Data Types

Besides a String, a variable can be of other types:


boolean (true/false)


char (just 1 character!)


int (whole number)


float (decimal number)

boolean

result = true;

char
capitalC

= 'C';

int

i

= 100000;

float f = 32.5f;

?

Prepare the following variables in your code, making sure
you declare appropriate variable types. Afterwards
display them using a console
println

for each variable.


Your age


The initial of your name


The initial of your surname


Whether you have a pet or not


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
14



Getting User’s Input

We are writing programs for others to use. Occasionally our program
needs to get data from the user.


Let’s say we want to ask the user for his/her:



name,



surname, and



age
(Warning: the user might grudge if she’s a woman!

)



Let’s see how we would get the user’s name first…then you can do
the rest.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
15



Getting User’s Input

public static void main(String[]
args
)

{


Scanner s = new Scanner(
System.in
);


System.out.println
("Your name: ");


String name =
s.next
();




System.out.println
("Your name is "+name);

}

1)
We create a Scanner and name it “
s”.
We will use this in Step 3,
to read the user’s input from the console

2)
We tell the user to input his name

3)
We wait for the user’s input. As soon as the user presses

Enter
”, the input of the user is stored in variable “
name


4)
We print to the console the input of the user


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
16



Import Statements

If we copy the code right into NetBeans, we note that there is a nice
red line drawn under the
Scanner
. This is an error, and we need
to solve it before we can run the program.


We will go into detail about imports and packages later on, but for
now just add the following line to your code:

package
mygreatapp
;


import
java.util.Scanner
;


public class
MyGreatApp

{


//rest of class

}

When we use certain

commands we have to import

what we call classes into our

project (but more on this

later…)


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
17



Getting User’s Input

?

The code should now run without problems.

Try to add more code so that you also ask the user for his
surname and age, and afterwards display them.




N.B. The command
s.next()
can only be used to read a value
and put it in a variable of type String.

To do the same but for an int, we can use
s.nextInt()

instead...
Similarly we also have
s.nextFloat()
, and
s.nextBoolean()


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
18



Operators

Java provides Arithmetic, Logical and Relational operators which
we can use with our variables. Let’s have a look at them:

ARITHMETIC

Operator Name

Operator

Symbol
in Java

Addition

+

Subtraction

-

Multiplication

*

Division

/

Modulus

%

LOGICAL

Operator Name

Operator

Symbol in
Java

AND

&&

OR

||

NOT

!

Mostly used with IF
statements (later)

RELATIONAL

Operator Name

Operator

Symbol in
Java

Equal To

==

Not

Equal To

!=

Greater Than

>

Greater

Than or Equal To

>=

Less Than

<

Less
Than or Equal To

<=


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
19



Operators

System.out.println
("You can give the dogs "+ bones/dogs +" bones each!");

?

Use the line of code below to create a working program.

First you have to ask the user how many dogs and how
many bones he has!


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
20



Selection

One of the most important constructs in a programming language
is the
Selection

construct, which gives us the ability to make
decisions in our programs.

We make decisions very often in our everyday lives...

If
I feel better
I will go to the party tonight,

Else I will stay home.

If I have
between

60 and

80

then I’ll buy an
iPod Shuffle
,

Else if I have
between

80 and

120
I’ll go for an
iPod
Nano
,

Else if I have more I’ll get the
iPod Classic
!

Or:

Note that the decision we take is based upon some condition. In
the first case the condition is how I feel, in the second case it is
how much money I have.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
21



Selection

Let’s look at some code:

We have 3 segments in our
IF statement. Note that
ONLY ONE
println can be
executed! The println that
is executed is that the
condition of which
evaluates to true, in this
case the second.

int

temperature = 26;


if (temperature >= 35)


{
System.out.println
(“Hot!”); }



else if (temperature >= 25)


{
System.out.println
(“Moderate!”); }


else


{
System.out.println
(“Cold!”); }



For the first condition we always use the
if

keyword.


For the other conditions (if any) we use
else if
.


Finally we can have an
else
. This is the last condition, in case
none of the others are true.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
22



Selection

Continue on your dog/bone example.

1) If the user will give
0 bones
to each of the dogs, then
they will starve!

2) If the user will give
between 1 and 3 bones

to each of
the dogs, then they will satisfy their hunger.

3) If the user will give
more than 3 bones
to each of the
dogs, then they will become extra super happy dogs!

Create an IF statement, and print the statements below
for the three conditions above:

1)
“Dogs are starving!”

2)
“Dogs are okay!”

3)
“Dogs are happy!”

?


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
23



Comments

Finally, in this lesson we will learn how to comment our code. This
is useful so that others who might use/change our code, and also
ourselves later on, will understand what our code is doing.

int

temperature = 26;
//create a variable to hold temperature


//if temperature is larger than or equal to 35 degrees, say it’s hot

if (temperature >= 35)


{
System.out.println
(“Hot!”); }



//if temperature is larger than or equal to 25 degrees, say it’s moderate

else if (temperature >= 25)


{
System.out.println
(“Moderate!”); }


/*if both conditions above fail, then we can assume that temperature is

under 25 degrees, and therefore say it’s cold */

else


{
System.out.println
(“Cold!”); }


Single line
comment

// hey

Multi line
comment

/* hey */


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
24

What have you learnt today?

Questions


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
25

References

http://www.oracle.com


http://www.codeobsessed.com/java.html


http://www.oracle.com/technetwork/java/javase/downloads/index.html


http://netbeans.org/downloads/index.html