Lecture 1

searchcoilSoftware and s/w Development

Aug 15, 2012 (5 years and 2 months ago)

219 views


1

Java and MatLab course for Biology students



Lecture 1




JAVA


Java
is
a high
-
level programming language
-

it is closer to a level of human
communication.

A lot of english words and ordinary looking math expressions.



The closer the language is to our
everyday speech, the easier i
t is to deal about more
complex programming
problems.


Java is not the easiest language; but
studying it
has many advantages:

-

it is a modern language with modern concepts ( OOP),

-

it has the older programming languages good th
ings/concepts,

-

and it weaves
the
best old concepts, with modern
concepts.


It is widel
y employed in software industry :

About 70% of the new software beeing written
today
is in Java.




A SUBSET OF JAVA


We’ll study an
small

subset of Java
.


Goal: To a
cquire the

basics of programming


in a well constructed / easy to grasp
computing environment:
NetBeans.



With this subset learned


one can continue to any
Programming L
anguage

(It is a key to the modern programming
concepts
)


We”
ll use this elementary
understanding

aquired,
to
complement
Matlab.



MATLAB

A "mathematically oriented"

programming Language.


Based on math concepts and math functions
.


Has lots of built
-
in math functions.


Therefore s
ome math
knowledge
is needed to
understand and
employ the
se functions
in
the
best way

(Linear Algebra, Matrix operations, etc)
.






2


JAVA & MATLAB


Good

combination

-

to study basics of programming in Java, and

-

to continue to

the math programming with Matlab


Matlab as a first or
only language is not as friendly

as the Java subset
.


Gain:

Experiment with

-

two programming languages

-

two different programming environment softwares



COMPUTERS

A thorough understanding of a
"
computer

architecture"

is not needed for our course.

Each lecture we’ll add some general comp
uter related items, like memory, CPU, etc.



Instead, w
e shall employ a number of simple intuitive models, to grasp the principles
of Object Oriented Programming.


Here is the 1
st

idea:


Assume
you have a
task

to accomplish
: to go to a party
.


You need
: a

haircut
, clo
thes,


a driver
, and a car for him
, an invitation

to the party
,
and a rose.


Suppose you are a wizard, and y
ou can create as many servants as you want

to help
you with the task
, each specializing in a

certain subject

:

-

a hairdresser

-

a tailo
r

-

a shoemaker

-

a car maker

-

a driver

-

a scribe

-

a gardener


So you create all these servants a
nd command

each of them to provide

his
expertise

to the task: a haircut
, clothes, …, a ride to the party, a rose.


You probably are aware that almost all servants pro
vided
you

with something
,

besides
the car maker which provides the car to the driver and not to you.


The driver provides you with a ride.


All servants you created participate in the task completion.


Each servant is a “specialist” in some thing (or thin
gs) he knows how to do.


3



Well, this is the general

idea of the Object Oriented Programming.


So what does the word OBJECT stands for : generally an object is a nicer name for
"a servant".


As a programmer you are a perfect Wizard

that

can

cre
ate as many
servants as needed

to
perform
the task

you have
.


In this Java course we w
ill learn how to
create servants to do our tasks.


We'll st
art with very very simple tasks, and observe the “servant creation” idea, and

how to activate them to fulfill the task.


Knowing which servants w
e need is another talent to be developed.


By the way, Java has huge libraries with
thousands of
ready
-
made servants for you to
take and use freely.

You may also take a ready
-
made servant that has several capabilities and add
to
it
more

capabilities, and so create a new servant that better fits your requirements.


But
this feature
, and other great

techniques
only
in the Java 2 course.


In our course we will
learn to

-

use ready
-
made servants,

-

create our own

servants, and

-

perform

our
tasks by i
nteracting between these guy
s.


When writing a program we make a distinction between the servants we use, or write
and use, and between a “boss” which starts the servants.



A computer program is actually a

set of one or more
textual
descri
ption
s

of

some

servants.


We are
now
going to learn how to compose simple programs
,

and how to

run


activate them, in order to accomplish a task
.



Writing computer Program
s

Basically a
c
omputer program

is a

text
, therefore it is composed from

letters
, digits
and punctuation signs.


We write computer programs as we write texts, with text editors.


In order for the computer to understand it, program
s have to be written very exact,
conform
strict
language rules

(syntax)
.


A programming language book is

actually
a list of statements/commands in thet
language, and
a list of rules
of "
how to
"

write programs in that language.



4

If you
do not
write
by the rules, the computer will

not under
stand the meaning of the
text, and you will

get a syntax error.


Differ
ent Languages have

different syntax rules.


There are rules
for

the structure

of a program.


You should not worry about remembering the syntax rules at the exam. Programming
exams are with open material.


Let
'
s see some Java rules.



A J
ava program is a
set of one ore more structures called
classes
.

(
generally :
a class is a description
-
plan

of a servant
. It contains its name and it
describes exactly its behaviours: what it can do and how to do it
)


Usually we write each class in a separate file.


What is

the format of a class ?

(We mean the text format)

It looks like that:


class
SomeName

{



}


You see a headline and an opening brace
, a
nd at the end a closing brace.


The content of

the class

is written between these braces.


the red items are obligatory

the blue
name
you invent:

-

it is the name you give to your class
, by convention starts with a capital letter

-

it better be a na
me with some significance



describing the class
.


With the prev examples
, significant names could be

:

TheTailor, TheDriver,

TheCarMaker, …


There are also rules how to give names (later)
.


What are the
contents

of a class.

(what will be written between the braces)


Usually zero or more
structures called
methods
, and some other items we'll meet later
on.



Here is a method exa
mple :





5

public static void

main
(
String
[]

args
)
{



}


Again it has a headline with some english words, and we see again the braces.


The content of a method is found between the braces.


All these words on the headline look gibberish,
don't worry about th
at,

you'll get an
explanation for all of them
a little
later.

Java is a wordy language, for now
just
think of this line as a printed line in a
hard to
grasp
tax
tofes you have to fill.


In between the braces we write the java statements
-

commands.


The
place between two encl
osing braces is called a
block

{}


All that about the
“dry’
form

of these structure
.


This method is special, and unique in a program.


Each program has a method with this exact headline.

(well almost exact : you can
invent and cha
nge the blue word )


It is called
the
main
method,
and the program starts by executing its commands
, and
ends when the last command in this method is done
.


We are going to write a single command in between the braces, a command that will
print your name:


public static void

main
(
String
[]

args
)
{


System.out.printl
n
("Iosi Cohen");

}


This command is actually an invocation to a
ready
-
made
java servant

which is
available to us from a java library, and
whose
expertise

is to print whatever text we
gave it as ar
gument :
put in between the "

".



The name of the servant is
System.out
,
and
his

expertise

we want to use,
i
s
called
println
, which means : print the text in the "…"
,

and
then go down one

line
.


(About “going down one line”: it means that the next prin
ting will start at the
beginning of the next row, and nothing will be further printed on the current row).


To invoke him and to co
mmand him perform his “println”

for us
,

we write his name
:

System.out

and add to it

a dot, and the name of his expertise
:

.pr
intln
,
and in the
parantheses we write between “

” the text to print.


System.out
.println(“…”);


6


Lets see the whole program:


class
MyFirstProgram

{


public static void

main
(
String
[]

args
)
{



System.out.println("Iosi Cohen");


}

}


We employed a ready
-
made servant that knows how to print.

Next time we'll write our servants and activate them.


We could employ this servant a few more times:


class
MyFirstProgram

{


public static void

main
(
String
[]

args
)
{

System.out.println("Iosi Cohen

");

System.out.pri
ntln("is a student

");

System.out.println("at HUJI

");


}

}


The output of the program will be these
3 lines
:



Iosi Cohen

is a student

at HUJI


The line



System.out.println("Iosi Cohen");


Is a java statement
(
command
)
, actually the 1
st

statement you
see today
.



The structures presented before are part of the Java Language syntax but the headlines
you saw are not statements.


System.out
has another
expertise

called
print
, which
also
prints the
text,
but it
stays
on the row and it
does not go

down a
row (except if the row

is full )
.


If instead of the above statements we would write:


System.out.
print
("Iosi Cohen

");

System.out.
print
("is a student
");


7

System.out.
print
("at HUJI
");




Then
the output will be
a single row

:


Iosi Cohen is a student at H
UJI


An
other nice printing trick: the effect of

insert
ing

the sign
s

\
n

in the text

that
we
want to print
,
is to “break”
the text
in two:
the text after the sign
will be
printed one

line down.


System.out.println("Iosi Cohen
\
n
is a student
\
n
at HUJI ");


W
e get

two “breaks”
:


Iosi Cohen

is a student

at HUJI


Syntax:

A
fter a command we write the sign
;

This sign
is called

the
command terminator
. I
t

helps
the computer to distinguish
between commands.


We showed a program
made
of one class only.

When the
program has more classes, w
e make a distinction between the class that has
the main method and the other

classes
.


We call the class with the main method : the
Boss
, a
nd
the
other classes : the servants
,
b
ecause the boss calls and coordinates the servants

using the commands in the
method main.


I
n principle each servant can be boss to other servants that he
might
call and
command
.



Ok we wrote our program. It is a structured text, in a file or in a number of files. What
now? We want to run the program.


Basically we need to let the Java System to check its syntax, then to run it (well there
are a few more Java System operations involved here to be described latter).


During the run the servants are

-

created acording to their descriptions found in the r
espective classes.

-

activated to perform their “expertises” which are part of the task we perform


All these steps are done “gracefully” within the
NetBeans environment.


8

Exercise 1


due Jan 22

Write a program that prints a banner of your
first
name made
of stars:

Show u
se
of the servant’s
System.out

two expertises:
print

and

println
,

and use the
line breaker trick:
\
n
.

Print
the
code
of your program
and
its
output,
write your name & id on it,
and bring to
class.


Example:



* *


* *



* * * * *




* *** * * *


* * * *



Notes

Class (large container of methods and fields)

Method (container of J
ava

code)

Methods represent
Actions/Behaviour
expertise of servants


Syntax

Class

M
ethod

Block, braces


Method main

Sets h
ow a program is run

Writing, saving Compiling, R
unning,
debuging
,…


Easy NetBeans mechanics

Compiling, running, saving, editing,…

Download/Install NetBeans on home pc


Statements

print, println, use
\
n to display mu
ltiple lines in one println command

command separator

“;”