|
Post by Blaine on Jul 28, 2005 13:46:08 GMT -5
Java lessons 1 -"hello World"
Source -
public class Hello { public static void main (string[] args) { System.out.println("Hello world"); } }
"public class" delcares program name and is followed by {} any code within those brackets defines hello class.
public static void main ( string[] args) Statard code to define the starting point of nearly all java programs. Contains the method named "main" That will contain the actual programming instructions with the brackets.
public static void willl be defined in a later lesson same with ( string[] args)
System.out.println( " hello world"); ( !statements preforming a task must end with a simicolon)
System.out.pritln just prints whatever is in the (" ").
|
|
|
Post by Blaine on Jul 29, 2005 2:49:09 GMT -5
Java lesson 2 - "First Variable"
Source - public class Firstvar { public static viod main(string[] args) { String str = "Variable One"; System.out.println(str); str = "variable Two"; System.out.println(str); } }
variable syntax "datatype Variablename = value;"
Here are some Variable data types
char- A single character string- a string of characters int - any integer from -2.14 billion to 2.14 billion float- number with a decimal point boolean- true or false
|
|
|
Post by Blaine on Jul 29, 2005 15:15:29 GMT -5
Lesson 3- "Arithmetical operators"
+ : addition ( and Combining strings) - : subtraction * : multiplication / : Division % : modulus ++: Increment -- : Decrement
Source: public class artimetic { public static void main (string[] args) { int add = 40 + 10; String addstr = " Java is" + " great"; float sub = 35.75f - 28.25f; int mul = 7 * 5; int mod = 65 % 2; int incr = 5 ; incr = ++incr int decr = 5 ; decr = --decr;
System.out.println("Numbers add total " +add); System.out.println(" Strings Combined " +addstr); System.out.println(" Subtraction result is: " +sub); System.out.println("Multiplaction Result: " +mul); System.out.println("modulus Result: " + mod); System.out.println(" Increament Result: " +incr); System.out.println(" Decrement Result: " + decr); } }
The output Panel should show Numbers add total 50 Strings combined Java is Great Subtraction result: 7.5 Multiplaction result: 35 Modulus Result : 1 Increment Result: 6 Decrement Result: 4
|
|
|
Post by Blaine on Jul 29, 2005 15:48:44 GMT -5
Java lesson 4 - " Assignment Operators"
Operator Example Equivalent (=) (a = b) (a = b) (+=) (a +=b) (a = a +b) (-=) (a -= b) (a = a - b) (*=) (a *= b) (a = a * b) (/=) (a /= b) (a = a / b) (%=) (a %= b) (a = a & b)
Source:
public class assignment { public class static void main (string[] args) { String a = "Java", b= " Rules"; a += b System.out.println(" Combined Strings: " + a); int c = 10, d = 10; c += d; System.out.println(" Adding two numbers: " +c );
float e = 7.5f, f = 2.25f; e -=f; System.out.println(" Subtracting floats: " + e);
int g = 8, h = 2; g *= h; System.out.println (" Multiplying Integers: " +g); int i = 8, j = 4; i /= j; System.out.println(" Dividing Integers: " +i ); int k = 8, l = 4; K %= l; System.out.println(" Modulus Result " + k); } }
The Output of this will be:
Combined strings: java rules adding two numbers: 20 Subtracting floats: 5.25 Multiplying Integers: 16 Dividing Integers: 2 Modulus Result: 0
|
|
|
Post by Blaine on Jul 29, 2005 16:13:21 GMT -5
Lesson 5 " logical operators"
&& - Logical AND || - Logical OR ! - Logical NOT
Source-
Public class logical { public static void main (String[] args) { boolean a = true, b = false; boolean c1 = ( a && a); // sees if both are true boolean c2 = ( a && b); boolean c3 = (b && b); boolean c4 = (a || a); // sees if either is true boolean c5 = (a || b); boolean c6 = (b || b);
boolean c7 = !a; boolean c8 = !b;
System.out.println(" And: \n1: "+c1+" 2: "+c2+" 3: "+c3); System.out.println(" OR: \n4: "+c4+" 5: "+c5+" 6: "+c6); System.out.println(" NOT: \n7: "+c7+" 8: " +c8); } }
output AND: 1: true 2: false 3: false OR: 4: true 5: true 6: false NOT: 7:false 8:true
|
|
|
Post by Blaine on Jul 29, 2005 17:26:37 GMT -5
Lesson 6 - "Comparison Operators"
== - Equality != - Inequality > - greater than < - less than >= - greater than or equal to <= - less than or equal to
source-
public class camparison { public static void main (String[] args) { boolean testsrt ("java" == "monkey"); System.out.println(test stings: " + teststr);
boolean testnum = ( 1.785 == 1. 785); System.out.println("test numbers: " + testnum);
boolean testbool = ( true != true); System.out.println("Test booleans: " + testbool);
boolean testless = ( 2 < 200); System.out.println(" Test less than: " + testless);
boolean lesseq = (2 <= 200) ; System.out.println("test less or equal: " + lesseq);
boolean testgtr = ( -1 > 1); System.out.println("test greater than: " + testgtr); } }
output window test strings: false test numbers: true test booleans: false test less than: true test less or equal: true test greater than: false
|
|