1 / 54

JAVA (something like C)

JAVA (something like C). 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

kateb
Download Presentation

JAVA (something like C)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 -2**31 ~ +2**31 • long 64-bits -2**63 ~ + 2**63

  11. Primitive data types (cont.) Name size |-------range-------| • float 32-bits +- 3.4 X 10**38 • double 64-bits +- 1.8 X 10**308 • 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, y=7, zz, rows=8; • 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 ss=5;

  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, su=“XXX”, s3=“SDE”;

  15. Java DOS commands • Javac A:\>javac test.java • compiler • uses a text file named ????.java • creates a ????.class file of byte code • java A:\>java test • the interpreter (runs the class code) • runs an application • appletviewer A:\>appletviewer 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; • x+=7; x = x + 7; • x*=3; 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 > y) • max = x; • else • max = y; • is the same as • max = x>y ? 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…. • ++ -- • * / % • + - • = • What’s 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 > 6) • System.out.println(“x is bigger); • else • System.out.println(“x is less than 6); • if (x>8) • {} • 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 ((a>b) & (c<=7)) • System.out.println(“both true”); • else • System.out.println(“one not true”); • if ((a>b) && (c<=7)) • System.out.println(“both true”); • else • System.out.println(“one not true”);

  30. || and | • if ((a>b) | (c<=7)) • System.out.println(“at least one true”); • else • System.out.println(“both false”); • if ((a>b) || (c<=7)) • System.out.println(“at least one true”); • else • System.out.println(“both false”);

  31. if ((a>b) && (a++)) • System.out.println(“(true)a=” + a); • else • System.out.println(“(false)a=” + a); • if (a>b) • if (c!=7) • System.out.println(“123456”); • is the same as • If((a>b) & (c!=7)) • System.out.println(“123456”);

  32. Relational operators • == equal to • != not equal • > greater than • < less than • >= greater than or equal to • <= 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; i<5; i++) • { • System.out.println(“counter is “ + i); • } • the ‘for’ statement contains the counter, increment, and test all together

  35. example • For (int k=0; k<3; k++) • System.out.println(k); • for (int d=5; d<3; d++) • System.out.println(k); • for (int d=3, int e=7; d<e; d++, e--) • System.out.println(d + “ and e=“ + e);

  36. for loop – using a comma • for (a =1, b=4; a<b; 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 x=0; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • x++; // increment • }

  39. Example #1 • int x=4; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • x++; // increment • }

  40. Example #2 • int x=4; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • }

  41. Example #3 • int x=14; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • x++; // increment • }

  42. do/while loop • int c=3; // counter • do { • System.out.println(“counter = “ + c); • c++; // increment • } while (c<5); // test • what’s ‘special’ about a do/while loop??

  43. Example #1 • int c=3; // counter • do { • System.out.println(“counter = “ + c); • } while (c<5); // test

  44. Example #2 • int c=13; // counter • do { • System.out.println(“counter = “ + c); • c++; // increment • } while (c<5); // 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: days=31; • break; • case 2: days=28; • break; • case 3: days=31; • break; • case 4: days=30; • break; • | • case 12: days=31; • break; • default: days=0; • }

  48. switch(month) • { • case 9: • case 4: • case 6: • case 11: days=30; break; • case 2: days=28; • break; • default: days=31; • }

  49. More use of “break” • hitting a ‘break’ will terminate a loop • for(int x=0; x<100; x++) { • if (x==5) break; • System.out.println(“X=“ + x); • } • X=0 • X=1 • X=2 • X=3 • X=4

  50. Using ‘break’ in nested loops • for(int x=1; x<4; x++) • { • System.out.println(“X=“ + x); • for (int y=0; y<30; y++) • { • if (y==x) break; • System.out.println} (“Y=“ + y); • } • }

More Related