Java Programing

advertisement
Java Programing
PSC 120
Jeff Schank
Let’s Create a Java Program
1.
2.
3.
4.
Open Eclipse
Create a project: File -> New -> Java Project
Create a package: File -> New -> Package
Create a Class: File -> New -> Class
How to say “Hello World!”
Let’s Add Some Numbers
Let’s Format the Results
Classes
• Let’s create another class called “Agent”
• File -> New -> Class
Data
• Now, let’s add some data—in this case a
vocabulary
pa c k a ge t al ker ;
publ i c c l a s s Agent {
publ i c St r i ng[ ] vocabul ar y = ne w St r i ng[ ]
{ " I ' m l ear ni ng Java. " ,
" How ar e you t oday?" ,
" I l ove sunny days l i ke t hi s! " ,
" Do you know any comput er l anguages?" ,
" I coul d sur e use a cof f ee! " ,
" I can' t wai t f or t he cl ass t o end t oday. " } ;
}
Methods
• Now, let’s add a method
pa c k a ge t al ker ;
publ i c c l a s s Agent {
publ i c St r i ng[ ] vocabul ar y = ne w St r i ng[ ]
{ " I ' m l ear ni ng Java. " ,
" How ar e you t oday?" ,
" I l ove sunny days l i ke t hi s! " ,
" Do you know any comput er l anguages?" ,
" I coul d sur e use a cof f ee! " ,
" I can' t wai t f or t he cl ass t o end t oday. " } ;
publ i c St r i ng saySomet hi ng( i nt x) {
i f ( x < vocabul ar y . l engt h && x >= 0)
r e t ur n vocabul ar y [ x] ;
el se
r e t ur n " My vocabul ar y i s smal l ! " ;
}
}
Let’s Say Something
pa c k a ge t al ker ;
publ i c c l a s s Tal ker {
/ **
* @pa r a m ar gs
*/
publ i c s t a t i c v oi d mai n( St r i ng[ ] ar gs) {
Agent a = ne w Agent ( ) ;
St r i ng s = a. saySomet hi ng( 1) ;
Syst em. out . pr i nt l n( s) ;
}
}
Let’s Say Something Randomly
pa c k a ge t al ker ;
publ i c c l a s s Agent {
publ i c St r i ng[ ] vocabul ar y = ne w St r i ng[ ]
{ " I ' m l ear ni ng Java. " ,
" How ar e you t oday?" ,
" I l ove sunny days l i ke t hi s! " ,
" Do you know any comput er l anguages?" ,
" I coul d sur e use a cof f ee! " ,
" I can' t wai t f or t he cl ass t o end t oday. " } ;
publ i c St r i ng saySomet hi ng( i nt x) {
i f ( x < vocabul ar y . l engt h && x >= 0)
r e t ur n vocabul ar y [ x] ;
el se
r e t ur n " My vocabul ar y i s smal l ! " ;
}
publ i c
i nt
i nt
r et
}
}
St r i ng saySomet hi ngRandoml y( ) {
l engt hOf Vocabul ar y = vocabul ar y . l engt h;
r andomI nt = ( i nt ) ( Mat h. r andom( ) * ( doubl e ) l engt hOf Vocabul ar y) ;
ur n vocabul ar y [ r andomI nt ] ;
Variables and Their Types
• As we just saw, we define the objects that will interact in our
simulation by defining classes
• Once a class is completely defined, then it can be instantiated many
times
– For example, we could define a class called “Person” and then make
1000 persons that interact in our simulation.
• Classes have members that occupy fields in a class
• A class can have indefinitely many fields and a field is either
occupied by variables or methods
• When defining classes, I prefer to place the variables first and
methods second in a class, but Java does not care how they are
ordered
• Let’s look at some of the types of variables we can define in a class.
Example MyClass
pa c k a ge t al k er ;
publ i c c l a s s My Cl as s {
i nt n; / / a dec l ar ed i nt eger
i nt m = 1; / / a dec l ar ed i nt eger wi t h a v al ue as s i gned t o i t
doubl e x ; / / dec l ar ed a doubl e v ar i abl e, f or s t or i ng r eal number s
doubl e pi = Mat h. PI ; / / a doubl e v ar i abl e pi , wi t h an appr ox i mat i on of
/ / pi as s i gned t o i t , 3. 141592654
bool e a n b; / / dec l ar at i on of a bool ean v ar i abl e
bool e a n x y z = t r ue ; / / dec l ar at i on of a bool ean v ar i abl e as s i gned t he v al ue t r ue
bool e a n t her e_i s _a_Mar t i an_i n_t hi s _r oom = f a l s e ;
/ / i t i s of t en a good i dea t o mak e v ar i abl e names t hat
/ / hav e meani ng t o y ou.
St r i ng s ; / / dec l ar at i on of a s t r i ng v ar i abl e
St r i ng my Name = " J ef f Sc hank " ; / / dec l ar at i on of a s t r i ng v ar i abl e and
/ / as s i gnment of a s t r i ng.
/ / We c an al s o def i ne ar r ay v ar i abl es t hat c an c ont ai n v al ues f or t he t y pe of
/ / ar r ay .
i
i
/
i
/
}
nt [ ] i nt eger Ar r ay ; / / dec l ar at i on of an i nt eger ar r ay v ar i abl e
nt [ ] my Number s = ne w i nt [ 100] ; / / dec l ar at i on of an i nt eger ar r ay wi t h
/ 100 s l ot s f or i nt eger s .
But , no i nt eger s hav e been s pec i f i ed f or t he ar r ay
nt [ ] one_t o_t en = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ; / / dec l ar at i on of an i nt eger ar r ay ,
/ c r eat i on of an ar r ay wi t h 10 s l ot s , wi t h v al ues 1 t o 10 as s i gned t o t he s l ot s .
Access Modifiers
• Variables (and methods) have specifications for
how they are accessed
• There are four types of access modifiers: no
explicit modifier, public, private, and protected.
– public modifier—the field is accessible from all
classes.
– private modifier—the field is accessible only within its
own class.
– protected modifier—the field is accessible within its
own class, package, and subclass.
– no explicit modifier—the field is accessible within its
own class and package
Methods
• Methods specify how objects do things (how they behave)
• Methods also specify how objects interact with other objects
• Methods have at least five features:
1.
2.
3.
4.
5.
Modifiers—such as public, private, and others listed above.
The return type—the data type of the value returned by the
method, or void if the method does not return a value.
The method name—the rules for field names apply to method
names as well, but the convention is a little different.
The parameter list in parenthesis—a comma-delimited list of input
parameters, preceded by their data types, enclosed by parentheses,
(). If there are no parameters, you must use empty parentheses.
The method body, enclosed between braces—the method’s code,
including the declaration of local variables, goes here.
Example Method
1.
Modifier
2. Return
Type
3. Method
Name
4. Parameter
List
5. Body
publ i c i nt r andomI nt eger ( i nt r ange) {
i nt i = ( i nt ) ( Mat h. r andom( ) * ( doubl e ) r ange) ;
r e t ur n i ;
}
Example Method
1.
Modifier
2. Return
Type
publ i c St r i ng
i f (x <
r et
el se
r et
}
3. Method
Name
4. Parameter
List
saySomet hi ng( i nt x) {
vocabul ar y . l engt h && x >= 0)
ur n vocabul ar y [ x] ;
ur n " My vocabul ar y i s smal l ! " ;
5. Body
Example Method
1.
Modifier
2. Return
Type
3. Method
Name
4. Parameter
List
publ i c St r i ng saySomet hi ngRandoml y( ) {
i nt l engt hOf Vocabul ar y = vocabul ar y . l engt h;
i nt r andomI nt = ( i nt ) ( Mat h. r andom( ) *
( doubl e ) l engt hOf Vocabul ar y) ;
r e t ur n vocabul ar y [ r andomI nt ] ;
}
5. Body
Logical Operators
1.
2.
3.
4.
5.
6.
7.
8.
9.
&&
||
==
!
!=
>
>=
<
<=
means roughly “and”
means roughly “or”
means roughly “equals”
means roughly “not”
means roughly “not equal to”
means “greater than”
means “greater than or equal to”
means "less than”
means "less than or equal to"
&& and ||
i nt x = 3;
i nt y = 0;
i f ( x == 3 && y > - 1) { / / i f x i s equal t o 3
y = x* x;
/ / t hen t ake x* x and assi gn t hat val ue t o x.
/ / What i s y?
}
i f ( x == 4 | | y > - 1) { / / i f x i s equal t o 3
y = x* x;
/ / t hen t ake x* x and assi gn t hat val ue t o x.
/ / What i s y?
}
! and !=
i nt x = 3;
i nt y = 0;
i f ( x == 3 && y ! = - 1) { / / i f x i s equal t o 3
y = x* x;
/ / t hen t ake x* x and assi gn t hat val ue t o x.
/ / What i s y?
}
i f ( ! ( x == 4) | | y == - 1) { / / i f x i s equal t o 3
y = x* x;
/ / t hen t ake x* x and assi gn t hat val ue t o x.
/ / What i s y?
}
Arithmetic Operators
1. +
2. –
3. *
4. /
Additive operator but it is also used for
String concatenation.
Subtraction operator
Multiplication operator
Division operator
Examples: +
i nt
i nt
St r i
St r i
i nt
St r i
x = 5;
y = 12;
ng s1 = " f i ve" ;
ng s2 = " t wel ve" ;
z;
ng s;
z = 5 + 12; / / 17
s = s1 + " pl us " + s2 + " = " + ( x + 7) ; / / ?
If-then Statement
Conditions
i f ( <Tr ut h condi t i on( ) >) {
<st at ement 1>;
.
.
.
<st at ement n>;
}
Body
If-then Example
i nt
x = 3;
i f ( x == 3)
x = x* x;
x = 3;
x = x* x* x;
/ / What i s x?
i f ( x == 3) {
x = x* x;
x = 3;
x = x* x* x;
}
/ / What i s x?
For Statements
• Probably, the next most commonly used control
statement is the for statement.
• For control statements are one of several control
statements that allow you to perform a number
of operations over and over again for a specified
number of steps (the others are while and dowhile).
• For statements typically have three statements as
arguments and then a body that is repeated
(there are variations on this theme).
A common form
Modifier
Arguments
f or ( <st ar t i ng condi t i ons>; <t est condi t i on>; <i ncr ement >) {
<st at ement >
<st at ement >
.
.
Body
.
<st at ement >
}
Example
i
i
i
i
i
nt
nt
nt
nt
nt
n
m
o
a
z
=
=
=
=
=
10;
0;
0;
0;
0;
f or ( i nt i = 0; i < n; i ++) {
m++;
m++;
o = o + 2;
a += 2;
z = i;
}
/ / What i s t he val ue of m?
/ / What i s t he val ue of o?
/ / What i s t he val ue of a?
/ / What i s t he val ue of z?
Another Example
i nt m = 0;
f or ( ; ; ) {
m++;
}
/ / When wi l l t hi s f or l oop st op?
/ / What wi l l be t he val ue of m?
The maximum value for an integer is 2147483647
But, since it does not stop at this value, it would
generate an error.
publ i c c l a s s Fi ndMont h {
Switch Statement
s t a t i c St r i ng[ ] mont hs = { " J anuar y " , " Febr uar y " , " Mar c h" ,
" Apr i l " , " May " , " J une" , " J ul y " ,
" Augus t " , " Sept ember " , " Nov ember " ,
" Dec ember " } ;
publ i c s t a t i c v oi d f i ndMont h( i nt mont h) {
St r i ng t heMont h; / / no v al ue as s i gned
mont h = mont h - 1;
s wi t c h ( mont h) {
c a s e 0:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 1:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 2:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 3:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 4:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 5:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 6:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 7:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 8:
t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 9: t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 10: t heMont h = mont hs [ mont h] ;
br e a k ;
c a s e 11: t heMont h = mont hs [ mont h] ;
br e a k ;
de f a ul t : t heMont h = " unk nown" ;
}
Sy s t em. out . pr i nt l n( t heMont h) ;
}
publ i c s t a t i c v oi d mai n( St r i ng ar gs [ ] ) {
f i ndMont h( 4) ;
Fi ndMont h. f i ndMont h( 8) ;
}
}
Scope of a Variable
• The scope of a variable is the region of a
program within which, a variable can be
referenced.
• In Java, the largest scope a variable can have is
at the level of the class.
• So, if variables are declared in a class field,
they can be referenced anywhere in the class
including inside methods.
Examples
publ i c c l a s s MyCl ass {
i nt x = 72;
publ i c s t a t i c v oi d mai n( St r i ng[ ] ar gs) {
f or ( i nt x = 0; x < 5; x++) {
Syst em. out . pr i nt l n( x) ;
}
Syst em. out . pr i nt l n( x) ;
}
}
Examples
publ i c c l a s s MyCl ass {
s t a t i c i nt x = 72;
publ i c s t a t i c v oi d myMet hod( i nt x) {
f or ( x = 0; x < 5; x++) {
Syst em. out . pr i nt l n( x) ;
}
}
publ i c s t a t i c v oi d mai n( St r i ng[ ] ar gs) {
myMet hod( 10) ;
Syst em. out . pr i nt l n( x) ;
}
/ / What i s t he val ue pr i nt ed by myMet hod( 10) ?
/ / What i s t he val ue pr i nt ed by
Syst em. out . pr i nt l n( x) ?
This
publ i c c l a s s MyCl ass {
i nt x = 72;
publ i c v oi d myMet hod( i nt x) {
f or ( x = 0; x < 5; x++) {
t hi s . x ++;
x ++;
}
Syst em. out . pr i nt l n( x) ;
Syst em. out . pr i nt l n( t hi s . x) ;
}
}
/ / What i s t he val ue pr i nt ed by myMet hod( 10) ?
/ / What i s t he val ue pr i nt ed by Syst em. out . pr i nt l n( t hi s. x) ?
Download