package javareview; /** * * @author Mayelin */ public class JavaReview1 { public static void main(String[] args) { /****************************/ /* Chapter 2 */ /****************************/ // primitive data types double gpa_1 = 3.85; int gpa_2 = 2; boolean valid = true; System.out.println("The value of the gpa_1 variable is " + gpa_1); System.out.println("The value of the gpa_2 variable is " + gpa_2); System.out.println("The value of the valid variable is " + valid + "\n"); // Conversion between primitive data types: converting from a type that // holds a smaller value to a type that holds a larger value happens // automatically. gpa_1 = gpa_2; // to convert the other way around, use the cast operator. // remember that casting a double to an int discards the fractional part gpa_2 = (int)gpa_1; // converting a String into an int int intValue = Integer.parseInt("5"); // constant declaration final double MAX_GPA = 4.0; // arithmetic operators int result = 5 + 2; System.out.println("Result should be 7: " + result); result = result - 2; System.out.println("Result should be 5: " + result); result = result * 2; System.out.println("Result should be 10: " + result); result = result / 2; System.out.println("Result should be 5: " + result); result = result % 2; System.out.println("Result should be 1: " + result + "\n"); // operator precedence: in order for the addition to be performed before the division, // we need the parenthesis double totalGPA = (gpa_1 + gpa_2) / 2; // concatenation operator System.out.println("The value of the sum of gpa_1 + gpa_2 is " + (gpa_1 + gpa_2) + "\n"); // unary operators result--; System.out.println("Result should be 0: " + result); result++; System.out.println("Result should be 1: " + result); result = -result; System.out.println("Result should be -1: " + result); boolean success = false; System.out.println("Success is true: " + !success + "\n"); // combined assignment operators int temp = 4; temp += 6; System.out.println("temp should be 10: " + temp); temp /= 2; System.out.println("temp should be 5: " + temp); temp %= 2; System.out.println("temp should be 1: " + temp); // using the increment/decrement operators in prefix and postfix form int i = 3; i++; System.out.println("This prints 4: " + i); ++i; System.out.println("This prints 5: " + i); System.out.println("This prints 6: " + ++i); System.out.println("This prints 6: " + i++); System.out.println("This prints 7: " + i + "\n"); // String class String name = "John Smith"; System.out.println("The character at position 2 is: " + name.charAt(2)); System.out.println("Does name contain the sequence \"hn\"? " + name.contains("hn")); System.out.println("A 4 character substring of name is: " + name.substring(6)); System.out.println("She said: \"See you ma\u00F1ana\""); /****************************/ /* Chapter 3 */ /****************************/ // if statement double salary = 30000.50; int yearsOnJob = 3; if( salary >= 20000 ) { if( yearsOnJob >= 2 ) System.out.println("You qualify for the loan.\n"); else System.out.println("You must have been on your current job for at least 2 years.\n"); } else { System.out.println("You must earn at least $20,000.00 per year to qualify for a loan.\n"); } // if-else-if statement int testScore = 75; if (testScore < 60) System.out.println("Your grade is F.\n"); else if (testScore < 70) System.out.println("Your grade is D.\n"); else if (testScore < 80) System.out.println("Your grade is C.\n"); else if (testScore < 90) System.out.println("Your grade is B.\n"); else System.out.println("Your grade is A.\n"); // logical operators int x = 25; int y = 2; if ((x >= 0 && x <= 10) || (y >= 5 && y <= 15)) { System.out.println("The first check is true\n"); } else { if ((x >= 11 && x <= 20) || (y >= 16 && y <= 25)) { System.out.println("The second check is true\n"); } else { System.out.println("Both checks are false\n"); } } // syntax for the conditional operator - also called ternary operator: // result = testCondition ? value1 : value2; y = (x >= 0) ? x : -x; // is equivalent to: if (x >= 0) y = x; else y = -x; // switch statement int number = 3; switch (number) { case 1: System.out.println("The number is 1"); break; case 2: System.out.println("The number is 2"); break; case 3: System.out.println("The number is 3"); break; case 4: System.out.println("The number is 4"); break; case 5: System.out.println("The number is 5"); break; default: System.out.println("The number is not 1, 2, 3, 4, or 5"); } // using the printf method to format output double currency = 2454645.125478; int myIntValue = 564654554; // - left justify // , include comma separator // 12 width // .2 precision points // f floating-point number // d integer System.out.printf("The amount is: $%-,12.2f\n", currency); System.out.printf("The amount is: $%-,12d\n\n", myIntValue); // using the format method in the String class to format output String formattedString = String.format("The amount is: $%-,12.2f\n", currency); System.out.println(formattedString); } }