|
| 1 | +public class Week1 { //declaration of class |
| 2 | + public static void main(String[] args) { //Main Method: Operates code |
| 3 | + |
| 4 | + //SYNTAX |
| 5 | + |
| 6 | + //Declaring Variables |
| 7 | + double test = 4.0; |
| 8 | + //type name = value; |
| 9 | + final double pi = 3.14; |
| 10 | + //final signifies that you cannot change it later in the code |
| 11 | + |
| 12 | + //Printing |
| 13 | + System.out.print("Hello"); |
| 14 | + //Prints Hello |
| 15 | + System.out.println("Hello");//(More useful) |
| 16 | + //Prints Hello and then creates a new line beneath it |
| 17 | + System.out.println(1); |
| 18 | + //number values do not have to be in quotations. |
| 19 | + |
| 20 | + //VARIABLE TYPES |
| 21 | + |
| 22 | + //Double variables: Can hold large decimal values |
| 23 | + double alpha = 1.0; |
| 24 | + double beta = 2.0; |
| 25 | + System.out.println(alpha + beta);// would output 3.0 |
| 26 | + |
| 27 | + //Integer Variables: can hold integer values |
| 28 | + int alpha2 = 1; |
| 29 | + int beta2 = 2; |
| 30 | + System.out.println(alpha2 + beta2);// would output 3 |
| 31 | + |
| 32 | + //String Variable: Can hold multiple characters |
| 33 | + String delta = "Hello"; |
| 34 | + System.out.println(delta);//would output Hello |
| 35 | + |
| 36 | + //Char Variable: Can hold one character |
| 37 | + char letter = 'a'; |
| 38 | + |
| 39 | + //OPERATORS |
| 40 | + |
| 41 | + double a = beta + alpha;//Addition: a = 3.0 |
| 42 | + double b = beta - alpha;//Subtraction: b = 1.0 |
| 43 | + double c = beta * alpha;//Multiplication: c = 2.0 |
| 44 | + double d = alpha/beta;//Division: d = 0.5 |
| 45 | + double f = beta%alpha;//Modulus:(provides remainder) f = 0.0 |
| 46 | + double g = alpha++;//Increment:(adds 1) g = 2.0; |
| 47 | + double h = alpha--;//Decrement:(subtracts 1) h = 0.0; |
| 48 | + |
| 49 | + //CASTING |
| 50 | + |
| 51 | + double decimal= 1.71; |
| 52 | + //int integer = casting; |
| 53 | + //will not work because integers cannot hold decimal values. |
| 54 | + //Instead you should cast the double as an int. |
| 55 | + int integer =(int) decimal; |
| 56 | + // changes the double value into an int by cutting off decimals NOT ROUNDING |
| 57 | + |
| 58 | + |
| 59 | + double fraction = 1/2;//fraction will = 0.0 |
| 60 | + //while 1/2 = 0.5 in real life, when two ints are divided in java, the result is also an int. |
| 61 | + //This means the resulting 0.5 would be shortened to 0 |
| 62 | + |
| 63 | + //to fix this either the numerator or denominator must be made a double |
| 64 | + fraction = 1.0/2; |
| 65 | + //or the number must be cast as a double |
| 66 | + fraction = (double) 1/2;//fraction = 0.5 |
| 67 | + |
| 68 | + //Extra notes |
| 69 | + |
| 70 | + /* typing // creates a comment that does not change the code |
| 71 | + * words like double, int, and String are key words that cannot to be used as variable names |
| 72 | + * the = sign sets a value to the variable, it does not signify that the values are equal |
| 73 | + * while doubles cannot go into ints, ints can always go into doubles |
| 74 | + * (ie. double a = 1 works, but int a = 1.2 does not) |
| 75 | + * there are many more variable types, but most are not useful for beginners right now |
| 76 | + */ |
| 77 | + |
| 78 | + //If you have any questions feel free to ask any of the teachers on classroom. |
| 79 | + //We would be more than happy to help. |
| 80 | + |
| 81 | + } |
| 82 | +} |
0 commit comments