Skip to content

Commit de743d4

Browse files
Add files via upload
1 parent 7c13fe5 commit de743d4

File tree

67 files changed

+1696
-0
lines changed

Some content is hidden

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

67 files changed

+1696
-0
lines changed

_01_basics/Datatypes.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com._01_basics;
2+
3+
public class Datatypes {
4+
public static void main (String[] args) {
5+
int myNum = 5; // Integer (whole number)
6+
float myFloatNum = 5.99f; // Floating point number
7+
char myLetter = 'D'; // Character
8+
boolean myBool = true; // Boolean
9+
String myText = "Hello"; // String
10+
11+
12+
System.out.println("These below are the datatypes : ");
13+
System.out.println("Integer Value = "+myNum);
14+
System.out.println("Integer Value = "+myFloatNum);
15+
System.out.println("Integer Value = "+myLetter);
16+
System.out.println("Integer Value = "+myBool);
17+
System.out.println("Integer Value = "+myText);
18+
19+
}
20+
}
21+
22+
//Primitive Datatypes in Java
23+
24+
/*
25+
26+
27+
Data Type Size Description
28+
byte 1 byte Stores whole numbers from -128 to 127
29+
short 2 bytes Stores whole numbers from -32,768 to 32,767
30+
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
31+
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
32+
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
33+
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
34+
boolean 1 bit Stores true or false values
35+
char 2 bytes Stores a single character/letter or ASCII values
36+
37+
*/

_01_basics/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com._01_basics;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
System.out.println("Hello World");
7+
}
8+
}
9+
// This is a single line comment in java
10+
11+
/*
12+
This is
13+
a multiline comment
14+
in java
15+
*/

_01_basics/Operators.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com._01_basics;
2+
import java.util.Scanner;
3+
public class Operators {
4+
public static void main (String[] args) {
5+
int x,y,sum,sub,mul,div,mod;
6+
Scanner inp = new Scanner(System.in);
7+
System.out.println("Enter value of x");
8+
x = inp.nextInt();
9+
System.out.println("Enter value of y");
10+
y = inp.nextInt();
11+
sum = x + y;
12+
sub = x - y;
13+
mul = x * y;
14+
div = x / y;
15+
mod = x % y;
16+
System.out.println("Sum = "+sum);
17+
System.out.println("Sum = "+sub);
18+
System.out.println("Sum = "+mul);
19+
System.out.println("Sum = "+div);
20+
System.out.println("Sum = "+mod);
21+
}
22+
}

_01_basics/StringMethods.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com._01_basics;
2+
3+
public class StringMethods {
4+
public static void main (String[] args) {
5+
String greeting = "Hello";
6+
System.out.println(greeting+" Sahil");
7+
8+
//Function to get string length
9+
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
10+
System.out.println("The length of the txt string is: " + txt.length());
11+
12+
//String to UPPERCASE
13+
String words = "Hello World";
14+
System.out.println(words.toUpperCase());
15+
//String to lowercase
16+
System.out.println(words.toLowerCase());
17+
18+
//Finding a Character in a String
19+
String text = "Please locate where 'sb' occurs";
20+
System.out.println("sb is at "+text.indexOf("sb")+"th location");
21+
22+
//String Concatenation +
23+
String firstName = "Sahil";
24+
String lastName = "Batra";
25+
System.out.println("My name is "+firstName+" "+lastName);
26+
27+
//String Concatenation using concat() method
28+
System.out.println(firstName.concat(lastName)+" is my name.");
29+
30+
//Special Characters
31+
/*
32+
Escape character Result Description
33+
\' ' Single quote
34+
\" " Double quote
35+
\\ \ Backslash
36+
*/
37+
String desc = "We are the so-called \"Punjabi\" from the north.";
38+
System.out.println(desc);
39+
String text1 = "It\'s alright";
40+
System.out.println("Dont worry "+text1);
41+
42+
//Six other escape sequences are valid in Java:
43+
/*
44+
Code Result
45+
\n New Line
46+
\r Carriage Return
47+
\t Tab
48+
\b Backspace
49+
\f Form Feed
50+
Try above yourself
51+
*/
52+
53+
//WARNING!
54+
//Java uses the + operator for both addition and concatenation.
55+
//Numbers are added. Strings are concatenated.
56+
}
57+
}

_01_basics/Typecasting.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com._01_basics;
2+
3+
public class Typecasting {
4+
public static void main(String[] args) {
5+
6+
//Widening Casting
7+
8+
int myInt = 9;
9+
double myDouble = myInt; // Automatic casting: int to double
10+
11+
System.out.println(myInt); // Outputs 9
12+
System.out.println(myDouble); // Outputs 9.0
13+
14+
//Narrowing Casting
15+
16+
double myDoublen = 9.78d;
17+
int myIntn = (int) myDouble; // Manual casting: double to int
18+
19+
System.out.println(myDouble); // Outputs 9.78
20+
System.out.println(myInt); // Outputs 9 but still its a wrong practise
21+
}
22+
}

_01_basics/Variables.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com._01_basics;
2+
3+
public class Variables {
4+
public static void main (String[] args) {
5+
String myName = "Sahil";
6+
int Num = 15; // declaration with initialization
7+
Num = 20; // initialization afterwards
8+
System.out.println(Num);
9+
final int myConst = 30;
10+
System.out.println(myConst);
11+
//myNum = 25; // will generate an error: cannot assign a value to a final variable
12+
13+
// other data types
14+
15+
int myNum = 5;
16+
float myFloatNum = 5.99f;
17+
char myLetter = 'D';
18+
boolean myBool = true;
19+
String myText = "Hello";
20+
21+
System.out.println("Integer Value = "+myNum);
22+
System.out.println("Integer Value = "+myFloatNum);
23+
System.out.println("Integer Value = "+myLetter);
24+
System.out.println("Integer Value = "+myBool);
25+
System.out.println("Integer Value = "+myText);
26+
27+
// For numeric values, the + character works as a mathematical operator
28+
int x = 5;
29+
int y = 6;
30+
System.out.println(x + y); // Print the value of x + y
31+
32+
// Declare Many Variables
33+
int a = 5, b = 6, c = 50;
34+
System.out.println(a + b + c);
35+
System.out.println(a+ "" +b+ "" +c);
36+
}
37+
}
38+
39+
// variable declaration :
40+
// data type variable_name = value;
41+
/* you can add the final keyword if you don't want others
42+
(or yourself) to overwrite existing values
43+
(this will declare the variable as "final" or "constant",
44+
which means unchangeable and read-only */
45+
// Here x. y , a,b myNum and more of these are called java identifiers
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com._02_control_statements;
2+
3+
public class BreakAndContinue {
4+
public static void main(String[] args) {
5+
System.out.println("Breaking at 4 but loop is till 10");
6+
for (int i = 0; i <= 10; i++) {
7+
if (i == 4) {
8+
break;
9+
}
10+
System.out.println(i);
11+
}
12+
System.out.println("\nContinuing at 4 but loop is till 10");
13+
for (int j = 0; j < 10; j++) {
14+
if (j== 4) {
15+
continue;
16+
}
17+
System.out.println(j);
18+
}
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com._02_control_statements;
2+
3+
import java.util.Scanner;
4+
// Shorthand Switch Statement
5+
public class Enhanced_Switch {
6+
public static void main(String[] args) {
7+
Scanner inp = new Scanner(System.in);
8+
String fruit = inp.next();
9+
10+
switch (fruit) {
11+
case "Mango" -> System.out.println("King of fruit");
12+
case "Apple" -> System.out.println("A sweet red fruit");
13+
case "Orange" -> System.out.println("A round fruit");
14+
case "Grapes" -> System.out.println("Small green fruit");
15+
default -> System.out.println("Please enter a valid food");
16+
}
17+
}
18+
}

_02_control_statements/For.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com._02_control_statements;
2+
3+
public class For {
4+
public static void main(String[] args) {
5+
System.out.println("Loop from 0 to 5");
6+
for (int i = 0; i <= 5; i++) {
7+
System.out.println(i);
8+
}
9+
System.out.println("Loop for even numbers 0 to 10");
10+
for (int j = 0; j <= 10; j = j + 2) {
11+
System.out.println(j);
12+
}
13+
//Using for loop with array
14+
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
15+
for (String k : cars) {
16+
System.out.println(k);
17+
}
18+
}
19+
}

_02_control_statements/IfElse.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com._02_control_statements;
2+
3+
public class IfElse {
4+
public static void main (String[] args) {
5+
//if statement
6+
if (20 > 18) {
7+
System.out.println("20 is greater than 18");
8+
}
9+
int x = 20;
10+
int y = 18;
11+
if (x > y) {
12+
System.out.println("x is greater than y");
13+
}
14+
//if else statement
15+
int time = 20;
16+
if (time < 18) {
17+
System.out.println("Good day.");
18+
} else {
19+
System.out.println("Good evening.");
20+
}
21+
//else if statement
22+
int t = 22;
23+
if (t < 10) {
24+
System.out.println("Good morning.");
25+
} else if (t < 20) {
26+
System.out.println("Good day.");
27+
} else {
28+
System.out.println("Good evening.");
29+
}
30+
//Short Hand if..else (Ternary Operator)
31+
int timezone = 20;
32+
String result = (timezone < 18) ? "Good day." : "Good evening.";
33+
System.out.println(result);
34+
}
35+
}

0 commit comments

Comments
 (0)