540 likes | 685 Views
This guide delves into the fundamentals of Java, focusing on its object-oriented programming principles such as encapsulation, inheritance, and polymorphism. Learn about the significance of classes and methods, the variety of primitive data types, and essential naming conventions. Additionally, discover useful Java editors and the basics of Java comments and documentation. Enhance your coding skills with practical examples of data types, conversions, and the execution of Java programs. This resource is perfect for beginners aiming to master Java programming.
E N D
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
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
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 */
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
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
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
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
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)
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
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)
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;
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)
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”;
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
Minimum JAVA program • class Skeleton • { • public static void main(String args[]) • { • } // end main • } // end class
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
Consider the primitives • byte 8 bits • short 16 bits • int 32 bits • long 64 bits • float 32 bits • double 64 bits • char 16 bits
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;
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
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;
ternary expression • expression01 ? expression02 : expression03 • Expression01 must evaluate to a boolean • If expression01 is true, • expression 02 is evaluated, else • expression03 is evaluated
Example of ? : • if (x > y) • max = x; • else • max = y; • is the same as • max = x>y ? x : y;
‘pre’ & ‘post’ • Difference between x++; and ++x; • Example • int x = 5; • System.out.println(x + “ “ + x++); • int x = 5; • System.out.println(x + “ “ + ++x);
Operator precedence • Do work within ( ) • Then, left to right…. • ++ -- • * / % • + - • = • What’s the answer to the following.. • n = 1 - 2 * 3 - 4 + 5;
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
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”);
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)
&& 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”);
|| 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”);
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”);
Relational operators • == equal to • != not equal • > greater than • < less than • >= greater than or equal to • <= less than or equal to
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
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
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);
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
for loop – some variations • boolean done = false; • for (a =1; !done; a++) • { ……… done = true; } • boolean done = false; • for ( ; !done; ) • { ……… done = true; }
while loop • int x=0; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • x++; // increment • }
Example #1 • int x=4; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • x++; // increment • }
Example #2 • int x=4; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • }
Example #3 • int x=14; // counter • while (x<5) // test • { • System.out.println(“counter = “ + x); • x++; // increment • }
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??
Example #1 • int c=3; // counter • do { • System.out.println(“counter = “ + c); • } while (c<5); // test
Example #2 • int c=13; // counter • do { • System.out.println(“counter = “ + c); • c++; // increment • } while (c<5); // test
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
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
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; • }
switch(month) • { • case 9: • case 4: • case 6: • case 11: days=30; break; • case 2: days=28; • break; • default: days=31; • }
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
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); • } • }