Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d2c181

Browse files
Add files via upload
1 parent cf189e8 commit 0d2c181

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1397
-0
lines changed

‎src/A_01_Primitive_Data_Types.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class A_01_Primitive_Data_Types {
2+
public static void main(String[] args) {
3+
// In front of every datatypes there must be a Variable (e.g: int x = 1, where x is the Variable)
4+
// Integers
5+
System.out.println("Integers");
6+
byte b = 10;
7+
short s = 20;
8+
int i = -30;
9+
long l = 1000;
10+
System.out.println(b);
11+
System.out.println(s);
12+
System.out.println(i);
13+
System.out.println(l);
14+
15+
// Floating Point
16+
System.out.println("Floating Point");
17+
float f = 10.54f;
18+
double d = 20.12;
19+
System.out.println(f);
20+
System.out.println(d);
21+
System.out.println(f+d);
22+
23+
// Character
24+
System.out.println("Characters");
25+
char ch = 'A';
26+
System.out.println(ch);
27+
28+
// Boolean
29+
System.out.println("Boolean");
30+
boolean bool = true;
31+
boolean flag = false;
32+
System.out.println(bool);
33+
System.out.println(flag);
34+
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class A_02_Identifiers_Variable_Rules {
2+
public static void main(String[] args) {
3+
// keyboard tip: Alt + z = To view all the contents
4+
// keyboard tip: Ctrl + / = Comment a line
5+
6+
// Rules:
7+
// 1) Variable Names can contain letters, digits, underscores, and dollar signs
8+
// 2) Variable Names must begin with a letter, $ or _ (doesn't matter Uppercase or Lowercase)
9+
// 3) Variable Names best practice should start with lowercase letter, and cannot contain whitespace
10+
// 4) Variable Names are case-sensitive (e.g: "Name" and "name" are different variables)
11+
// 5) Reserved words like Java keywords (e.g: int, boolean, char) cannot be used as Variable Names.
12+
13+
int a_1$ = 10;
14+
int A_1$ = 10;
15+
16+
// as you can see there is an error as it has a white spacing in between.
17+
// int a_ 3$ = 10;
18+
19+
System.out.println(a_1$);
20+
System.out.println(A_1$);
21+
22+
// this file will not run as there is an error
23+
// System.out.println(a_ 3$);
24+
25+
// similarly there will be an error cause you cannot use reserved words as Variable Names.
26+
// String int = 'Hello World!'
27+
// int String = 10;
28+
29+
// however, why does it work for this? It's cause the string is not a data type keyword *String* is not string. Hence, it why Java is case sensitive.
30+
int string = 10;
31+
System.out.println(string);
32+
33+
}
34+
}

‎src/A_03_Type_Casting.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class A_03_Type_Casting {
2+
// Implicit Type Casting (automatic by compiler)
3+
// byte -> short -> char -> int -> long -> float -> double
4+
5+
// Explicit Type Casting
6+
// double -> float -> long -> int -> char -> short -> byte
7+
8+
public static void main(String[] args) {
9+
// Implicit
10+
int number1 = 10;
11+
double number2 = number1;
12+
13+
System.out.println(number2);
14+
15+
// Explicit
16+
double num1 = 20.44;
17+
int num2 = (int)num1;
18+
19+
System.out.print(num2);
20+
}
21+
}

‎src/A_04_Arithmetic_Operators.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class A_04_Arithmetic_Operators {
2+
public static void main(String[] args) {
3+
// using the '+' operator you can join 2 Variable string together as shown below
4+
String msg1 = "Hello World!";
5+
String msg2 = "My name is Ang Jianming.";
6+
7+
System.out.println(msg1 + " " + msg2);
8+
9+
// Arithmetic Operator (+, -, *, /, %)
10+
int a = 10;
11+
int b = 20;
12+
13+
int c = a + b; // Addition
14+
System.out.println(c);
15+
16+
int d = a - b; // Subtraction
17+
System.out.println(d);
18+
19+
int e = a * b; // Multiply
20+
System.out.println(e);
21+
22+
int f = b / a; // Divide
23+
System.out.println(f);
24+
25+
int g = a % b; // Modulus
26+
System.out.println(g);
27+
}
28+
}

‎src/A_05_User_Input.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Author AngJianming
3+
*
4+
*/
5+
6+
// it is a must to import java.util.Scanner; to access some external files which has the functionality to read input from the console. In Java this is the way to get certain functionalities which is not build in therefore, needing to import them.
7+
8+
import java.util.Scanner;
9+
10+
public class A_05_User_Input {
11+
public static void main(String[] args) {
12+
Scanner user_input = new Scanner(System.in);
13+
System.out.print("Enter your name: ");
14+
15+
String name = user_input.nextLine();
16+
System.out.println("My name is " + name);
17+
18+
19+
// Example Calculator
20+
Scanner scanner = new Scanner(System.in);
21+
22+
// Prompt the user to enter the first number
23+
System.out.print("Enter the first integer: ");
24+
int x = scanner.nextInt();
25+
26+
// Prompt the user to enter the second number
27+
System.out.print("Enter the second integer: ");
28+
int y = scanner.nextInt();
29+
30+
// Perform arithmetic operations
31+
int sum = x + y; // Addition
32+
int difference = x - y; // Subtraction
33+
int product = x * y; // Multiplication
34+
35+
// To handle division by zero, you can check if b == 0
36+
// but for simplicity, we assume b != 0 in this example
37+
double quotient = (double) x / y; // Division
38+
int modulus = x % y; // Modulus (remainder)
39+
40+
// Print the results
41+
System.out.println("\n--- Results ---");
42+
System.out.println(x + " + " + y + " = " + sum);
43+
System.out.println(x + " - " + y + " = " + difference);
44+
System.out.println(x + " ×ばつ " + y + " = " + product);
45+
System.out.println(x + " ÷ " + y + " = " + quotient);
46+
System.out.println(x + " % " + y + " = " +"R"+ modulus);
47+
48+
// Close the scanner
49+
scanner.close();
50+
}
51+
}

‎src/B_01_Assignment_Operators.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class B_01_Assignment_Operators {
2+
public static void main(String[] args) {
3+
// Assignment Operators (=, +=, -=, /=, *=)
4+
5+
int a = 10;
6+
int b = 5;
7+
8+
a += b; // meaning a = a+b or a = 10+b
9+
System.out.println(a);
10+
// a = 15 this should be the expected result
11+
12+
a -= 2; // meaning a = a-2;
13+
System.out.println(a);
14+
15+
a /= 2; // meaning a = a/2
16+
System.out.println(a);
17+
18+
a *= 2; // meaning a = a*2
19+
System.out.println(a);
20+
21+
a %= 5; // meaning a = a%5
22+
System.out.println(a);
23+
}
24+
}

‎src/B_02_Relational_Operators.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class B_02_Relational_Operators {
2+
// ture, false (boolean)
3+
// Relational Operators ( ==, !=, <, >, <=, >=)
4+
5+
public static void main(String[] args) {
6+
int a = 10;
7+
int b = 15;
8+
9+
System.out.println(a == b); // if a is equal to b true, otherwise false
10+
System.out.println(a != b); // 10 is not equal to 15 true, otherwise false
11+
System.out.println(a < b); // 10 is less than 15 true, otherwise false
12+
System.out.println(b > a); // 10 is more than 15 true, otherwise false
13+
14+
System.out.println(a <= b); // 10 is less or equal to 15 true, otherwise false
15+
16+
System.out.println(b >= a); // 10 is more than or equal to 15 true, otherwise false
17+
18+
// 10 is less than, or equal to 10 true, otherwise false.
19+
System.out.println(10 < 10); // is false because it cannot 10 cannot be less than 10
20+
System.out.println(10 <= 10); // is true because it can be less than or EQUAL to 10 (Keyword 'equal')
21+
22+
System.out.println(10 > 10); // is false because it cannot 10 cannot be more than 10
23+
System.out.println(10 >= 10); // is true because it can be more than or EQUAL to 10 (Keyword 'equal')
24+
25+
26+
27+
}
28+
}

‎src/B_03_Logical_Operators.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.security.spec.RSAOtherPrimeInfo;
2+
3+
public class B_03_Logical_Operators {
4+
public static void main(String[] args) {
5+
// Logical Operator ( &&, ||, !)
6+
7+
8+
// AND (&&) - is all condition is true then return true.
9+
// true && true = true
10+
// true && false = false
11+
// false && true = false
12+
// false && false = false
13+
System.out.println(10>5);
14+
System.out.println(20>14);
15+
System.out.println((10>5) && (20>14));
16+
17+
18+
// OR (||) - if any condition is true, then return true.
19+
// true || true = true
20+
// true || false = true
21+
// false || true = true
22+
// false || false = false
23+
System.out.println((10>5) || (20<14));
24+
System.out.println((10<5) || (20<14) || (10<2) || (1000<999) || (1<0));
25+
// false, false, false, false, true. Hence, it's true still.
26+
27+
System.out.println((10<5) || (20<14) || (10<2) || (1000<999) || true);
28+
// If at the end is true then all is true.
29+
30+
System.out.println((10<5) || (20<14) || (10<2) || (1000<999) || false);
31+
// If at the end is all is false then false.
32+
33+
34+
// NOT(!) - vice-versa
35+
// true = false
36+
// false = true
37+
38+
System.out.println(!(100>99)); // Not true. Therefore, false
39+
40+
System.out.println(!(100>99) && (2>0));
41+
// !true AND true = false
42+
43+
System.out.println(!(20>20) || !(10<=20));
44+
// !false OR !true = true
45+
46+
47+
48+
}
49+
}

‎src/B_04_Ternary_Operator.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class B_04_Ternary_Operator {
2+
public static void main(String[] args) {
3+
// Ternary Operator ( ?: )
4+
// condition ? true : false
5+
6+
int n1 = 10;
7+
int n2 = 20;
8+
9+
// if (n2 is greater than n1) then n2 is assigned to variable greaterNum.
10+
11+
/*
12+
* So the expression (n2 > n1) ? n2 : n1 means:
13+
* - If n2 > n1 is true, then use n2.
14+
* - Otherwise, use n1.
15+
*/
16+
int greaterNum = (n2 > n1) ? n2 : n1;
17+
System.out.println(greaterNum);
18+
19+
int n3 = 100;
20+
int n4 = 20;
21+
22+
int greaterNum2 = (n3 > n4) ? n3 : n4;
23+
System.out.println(greaterNum2);
24+
25+
}
26+
}

‎src/B_05_Increment_Operator.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class B_05_Increment_Operator {
2+
public static void main(String[] args) {
3+
// Increment Operator (++)
4+
5+
int a = 10;
6+
// a++; // post-increment (a = a+1)
7+
// ++a; // pre-increment (a = 1+a)
8+
9+
System.out.println(a);
10+
11+
a++;
12+
a++;
13+
System.out.println(a++);
14+
15+
++a;
16+
++a;
17+
System.out.println(++a);
18+
19+
System.out.println(a++);
20+
21+
System.out.println(++a);
22+
// the reason why it's 18 insted of 17 is cause a++ will print out the current value 16 first, only after that it will add up to +1. Hence, the output for ++a would be 17+1 = 18;
23+
24+
25+
26+
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /