JAVA (something like C) - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

JAVA (something like C)

Description:

The data is the state and the methods (code) is the behavior. ... int aVal = 17; System.out.println('a = ' aVal); System.out.println('a=' aV ' b=' bV) ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 55
Provided by: james372
Learn more at: http://ww2.nscc.edu
Category:
Tags: java | aval | something

less

Transcript and Presenter's Notes

Title: JAVA (something like C)


1
JAVA (something like C)
2
Object Oriented Programming
  • Process orientated code acting on data
  • Object oriented data controls access to code
  • Encapsulation java has a class. The data is
    the state and the methods (code) is the behavior.
    You control the methods and access to the data
  • Inheritance where one class acquires the
    properties of another class
  • Polymorphism one interface multiple methods

3
JAVA editors
  • did a search for JAVA editors on the NET and
    obtained...
  • JCreator www.jcreator.com
  • Download the free one
  • editPlus - I have used this one
  • http//www.editplus.com/
  • shareware - pay after 30 days
  • WingSoft
  • http//www.wingsoft.com/wingeditor.shtml

4
comments
  • A single line comment
  • int row // keeps track of the row
  • multiple line comments
  • / this is one line of the comment
  • this is the 2nd line
  • this is the third line /

5
Documentation comments
  • acts like multiple line comments
  • / this is one line of the comment
  • this is the 2nd line
  • this is the third line /
  • note the initial / The javadoc program
  • will collect these blocks of comments as
    documentation for the program
  • javadoc filename.java

6
special symbols
  • indicates a block of code
  • ( ) parameters to a method
  • indicates an array
  • terminates a statement
  • , a seperator
  • . used to separate package names
  • Note table of reserved words on page 39

7
Identifier names
  • Start with a letter
  • Contains
  • letters (upper or lower case)
  • numbers
  • _ (the underscore)
  • Can be any length
  • No imbedded spaces or special characters
  • Do not start an identifier with a number

8
Examples/Naming Conventions
  • numberOfRows
  • num_rows
  • row8
  • Naming Conventions
  • classes start with a capitol letter
  • HelloWorld ShoppingCart
  • methods identifiers with a small letter
  • println() getChar()
  • constants are all caps PIE

9
System.out.println( )
  • System.out.println (.) sends a String to the
    monitor
  • System.out.println(Hi Mom)
  • The concatenates two variables
  • int aVal 17
  • System.out.println(a aVal)
  • System.out.println(a aV b bV)

10
Primitive data types
  • JAVA has 8 primitive data type
  • Name size -------range-------
  • byte 8-bits -128 127
  • short 16-bits -32k 32k
  • int 32-bits -231 231
  • long 64-bits -263 263

11
Primitive data types (cont.)
  • Name size -------range-------
  • float 32-bits - 3.4 X 1038
  • double 64-bits - 1.8 X 10308
  • char 16-bits 0 65,535
  • boolean 1-bit true or false
  • (not a 0 or a
    1)

12
Examples of primatives
  • int x
  • int x, y7, zz, rows8
  • boolean isRunning false
  • double dd
  • float f 10.3 (error - defaults to a double)
  • float fx 22.4f note the f
  • byte b1, b2 44, cx
  • short ss5

13
The char
  • char uses a 16-bit Unicode character set
  • The Unicode set allows 65,536 different
    characters. (about half are assigned)
  • char ch a
  • char ch1, ch2 7, ch3 X
  • char delta \u0394 delta symbol
  • char copyrt \u00AE copy write symbol
  • escape seq. page 32 (EX \t is tab)

14
the String class
  • String is a class, not a primitive data type
  • is a sequence of characters
  • not an array of char terminated by a null
  • immutable cannot be changed once created
  • String ss
  • String s1 hi mom
  • String s1, suXXX, s3SDE

15
Java DOS commands
  • Javac A\gtjavac test.java
  • compiler
  • uses a text file named ????.java
  • creates a ????.class file of byte code
  • java A\gtjava test
  • the interpreter (runs the class code)
  • runs an application
  • appletviewer A\gtappletviewer test.html
  • runs an applet

16
Minimum JAVA program
  • class Skeleton
  • public static void main(String args)
  • // end main
  • // end class

17
arithmetic
  • Expression series of variables (methods) that
  • evaluate to a single
    variable
  • assign (evaluate right, assign to right)
  • add x x 7
  • - subtract x 8 cx
  • multiply pay rate time
  • / divide tax too_much / 1.0
  • remainder (mod)
  • row x 4
  • row is the remainder of x / 4
  • ans 33 6 ans is 3

18
Consider the primitives
  • byte 8 bits
  • short 16 bits
  • int 32 bits
  • long 64 bits
  • float 32 bits
  • double 64 bits
  • char 16 bits

19
Some examples
  • Define and initialize all the primitives
  • Add the following to skeleton and compile
  • short byte
  • byte short
  • double float
  • float double
  • int boolean
  • int short byte
  • int short float
  • double int double
  • float short double
  • int char chat int

20
Conversion and casting
  • Automatic
  • types are compatible
  • destination is larger than the source
  • Casting (target type) value
  • byte b (byte) someValue
  • byte b intExample error
  • byte b (byte) intExample OK
  • byte b 50
  • byte x b/2 Error
  • Byte x (byte)(b/2) ok

21
Shortcuts in arithmetic
  • Shortcuts
  • x x x 1
  • x-- x x 1
  • x7 x x 7
  • x3 x x 3
  • x/2 y y / 2
  • q-5 q q 5

22
ternary expression
  • expression01 ? expression02 expression03
  • Expression01 must evaluate to a boolean
  • If expression01 is true,
  • expression 02 is evaluated, else
  • expression03 is evaluated

23
Example of ?
  • if (x gt y)
  • max x
  • else
  • max y
  • is the same as
  • max xgty ? x y

24
pre post
  • Difference between x and x
  • Example
  • int x 5
  • System.out.println(x x)
  • int x 5
  • System.out.println(x x)

25
Operator precedence
  • Do work within ( )
  • Then, left to right.
  • --
  • /
  • -
  • Whats the answer to the following..
  • n 1 - 2 3 - 4 5

26
if statement
  • if(expression) // expression is evaluated
  • block of code // execute if true
  • else
  • block of code // execute if false
  • Note a block of code can contain a nested
    if statement

27
examples
  • if (x gt 6)
  • System.out.println(x is bigger)
  • else
  • System.out.println(x is less than 6)
  • if (xgt8)
  • else
  • System.out.println(x is less than 8)

28
logical operators
  • evaluate both, both must be true
  • evaluate both, either must be true
  • both must be true. If 1st is false, do not
    evaluate the 2nd
  • one must be true. If 1st is true, do not
    evaluate the 2nd
  • tilda - not
  • Shift and bit-wise operators you are on your
    own (page 80)

29
and
  • if ((agtb) (clt7))
  • System.out.println(both true)
  • else
  • System.out.println(one not true)
  • if ((agtb) (clt7))
  • System.out.println(both true)
  • else
  • System.out.println(one not true)

30
and
  • if ((agtb) (clt7))
  • System.out.println(at least one true)
  • else
  • System.out.println(both false)
  • if ((agtb) (clt7))
  • System.out.println(at least one true)
  • else
  • System.out.println(both false)

31
  • if ((agtb) (a))
  • System.out.println((true)a a)
  • else
  • System.out.println((false)a a)
  • if (agtb)
  • if (c!7)
  • System.out.println(123456)
  • is the same as
  • If((agtb) (c!7))
  • System.out.println(123456)

32
Relational operators
  • equal to
  • ! not equal
  • gt greater than
  • lt less than
  • gt greater than or equal to
  • lt less than or equal to

33
The loops
  • For a loop you need 3 things
  • a counter of some kind
  • a comparison statement - a test
  • increment the counter some time
  • the loops are

34
for loop
  • for (int i 6 ilt5 i)
  • System.out.println(counter is i)
  • the for statement contains the counter,
    increment, and test all together

35
example
  • For (int k0 klt3 k)
  • System.out.println(k)
  • for (int d5 dlt3 d)
  • System.out.println(k)
  • for (int d3, int e7 dlte d, e--)
  • System.out.println(d and e e)

36
for loop using a comma
  • for (a 1, b4 altb a, b--)
  • System.out.println(a a)
  • System.out.println(b b)
  • Gives a 1
  • b 4
  • a 2
  • b 3

37
for loop some variations
  • boolean done false
  • for (a 1 !done a)
  • done true
  • boolean done false
  • for ( !done )
  • done true

38
while loop
  • int x0 //
    counter
  • while (xlt5) // test
  • System.out.println(counter x)
  • x //
    increment

39
Example 1
  • int x4 //
    counter
  • while (xlt5) // test
  • System.out.println(counter x)
  • x //
    increment

40
Example 2
  • int x4 //
    counter
  • while (xlt5) // test
  • System.out.println(counter x)

41
Example 3
  • int x14 //
    counter
  • while (xlt5) // test
  • System.out.println(counter x)
  • x //
    increment

42
do/while loop
  • int c3 //
    counter
  • do
  • System.out.println(counter c)
  • c //
    increment
  • while (clt5) // test
  • whats special about a do/while loop??

43
Example 1
  • int c3 //
    counter
  • do
  • System.out.println(counter c)
  • while (clt5) // test

44
Example 2
  • int c13 //
    counter
  • do
  • System.out.println(counter c)
  • c //
    increment
  • while (clt5) // test

45
Switch statement
  • switch(expression)
  • case value01 statement
  • break
  • case value02 statement
  • break
  • default statement
  • The expression is evaluated. Jump to the
    corresponding case statement with value

46
  • expression must be a byte, short, int, or char
  • When you jump to the proper case value, you
    execute code until the end of the switch
    statement or a break statement is executed
  • No matches jump to default
  • No default out of switch

47
  • Switch(month)
  • case 1 days31
  • break
  • case 2 days28
  • break
  • case 3 days31
  • break
  • case 4 days30
  • break
  • case 12 days31
  • break
  • default days0

48
  • switch(month)
  • case 9
  • case 4
  • case 6
  • case 11 days30
  • break
  • case 2 days28
  • break
  • default days31

49
More use of break
  • hitting a break will terminate a loop
  • for(int x0 xlt100 x)
  • if (x5) break
  • System.out.println(X x)
  • X0
  • X1
  • X2
  • X3
  • X4

50
  • Using break in nested loops
  • for(int x1 xlt4 x)
  • System.out.println(X x)
  • for (int y0 ylt30 y)
  • if (yx) break
  • System.out.println (Y y)

51
break with the while loop
  • while (xlt100
  • if (x5) break
  • System.out.println(X x)
  • x
  • X0
  • X1
  • X2
  • X3
  • X4

52
break with a label
  • outer for(i1 ilt100 i)
  • for (j4 jlt50 j)
  • .
  • if((ji)0)
  • break outer
  • ..
  • // end j for loop
  • // end i for loop

53
continue statement
  • for (int x0 xlt8 x)
  • System.out.print(x )
  • if(x2) continue
  • System.out.println( )
  • 0 1
  • 2 3
  • 4 5
  • 6 7

54
break / continue / go to
  • break and continue may specify a label
  • a label is a JAVA identifier that indicates a
    block of code
  • aLabel
  • stuff
  • It is close to a GoTo. Dont use it
Write a Comment
User Comments (0)
About PowerShow.com