├── Strings ├── StringBuil.java ├── StringBuff.java ├── Strings.java └── String Tokenizers │ ├── StringFirst.java │ ├── StringSecond.java │ └── StringThird.java ├── Basic Programs ├── HelloWorld.java ├── TpeCast.java ├── StudentImp.java ├── Factorial.java ├── Arithmatic.java ├── Armstrong.java ├── TryWithResources.java ├── Primitive.java └── InputMethods.java ├── Java Programs ├── Main.java ├── ThreadClass.java ├── MethodOverRid.java ├── SingleLevel.java ├── PreventInheritanceExample.java ├── FactorialExample.java ├── Final.java ├── Comparision.java ├── ArrayIndexExample.java ├── MyRun.java ├── Concatenation.java ├── MyRunnable.java ├── ExceptionHandling.java ├── StringConcatenationExample.java ├── ClassObject.java ├── ExceptionHandlingExample.java ├── SuperClass.java ├── OneDimensionalArrayExample.java ├── CustomException.java ├── TwoDArray.java ├── MultiLevel.java ├── Armstrong.java ├── InputMethods.java ├── ArithmeticOperations.java ├── VariableTypesExample.java ├── MethodOverLoad.java ├── TypeCasting.java ├── ScannerMethods.java ├── Mutiple.java ├── Arithmetic.java └── StringMethods.java ├── Control Statements ├── Looping Statments │ ├── DoWhileLoop.java │ ├── ForLoop.java │ ├── WhileLoop.java │ └── README.md ├── Conditional Statements │ ├── Conditional.java │ ├── SwitchCase.java │ ├── ConditionalScanner.java │ └── README.md ├── Jumping Statements │ ├── README.md │ └── JumpingStatementsDemo.java └── README.md ├── Exception Handling ├── ChekExcep.java └── DefExcepHandler.java ├── Arrays ├── ComArray.java ├── IterateArray.java └── MultiDimArray.java ├── Inheritance ├── Overrid.java ├── Area.java ├── TransMul.java └── Hierarchichal.java ├── README.md └── LICENSE /Strings/StringBuil.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Strings/StringBuff.java: -------------------------------------------------------------------------------- 1 | public class StringBuff { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /Basic Programs/HelloWorld.java: -------------------------------------------------------------------------------- 1 | public class HelloWorld { 2 | public static void main(String[] args) { 3 | System.out.println("Hello, World!"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Java Programs/Main.java: -------------------------------------------------------------------------------- 1 | final class BaseClass { 2 | 3 | public void display() { 4 | System.out.println("This is a final class."); 5 | } 6 | } 7 | 8 | 9 | public class Main { 10 | public static void main(String[] args) { 11 | BaseClass obj = new BaseClass(); 12 | obj.display(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Control Statements/Looping Statments/DoWhileLoop.java: -------------------------------------------------------------------------------- 1 | public class DoWhileLoop { 2 | public static void main(String[] args) { 3 | int i = 10; 4 | do { // This block will execute first before checking the condition. 5 | System.out.println("This is do Block"); 6 | } while (i != 10); // Condition becomes False. 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Basic Programs/TpeCast.java: -------------------------------------------------------------------------------- 1 | class TypeCast{ 2 | public static void main(String[] args){ 3 | int marks = 80; 4 | long lmv = marks; 5 | System.out.println("integer value"+marks); 6 | System.out.println("long value"+lmv); 7 | 8 | float d = 12.5 ; 9 | double inm = (double)12.5; 10 | System.out.println("After Type Casting"+inm); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /Exception Handling/ChekExcep.java: -------------------------------------------------------------------------------- 1 | class ChekExcep { 2 | public static void main(String[] args){ 3 | String className = "ChekdExcep"; 4 | 5 | try { 6 | Class clz = Class.forName(className); 7 | System.out.println("Class loaded successfully: " +clz.getName()); 8 | } 9 | catch(ClassNotFoundException e){ 10 | System.err.println("class not found: "+ className); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Exception Handling/DefExcepHandler.java: -------------------------------------------------------------------------------- 1 | class DefExcepHandler{ 2 | public static void main(String[] args){ 3 | try{ 4 | 5 | int data=50/0; 6 | System.out.println(data); 7 | } 8 | catch(ArithmeticException ae){ 9 | System.out.println(ae); 10 | } 11 | catch(Exception e){ 12 | System.out.println(e); 13 | System.out.println("parent class exceptionhandles"+e); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Basic Programs/StudentImp.java: -------------------------------------------------------------------------------- 1 | public class StudentImp { 2 | public static void main(String[] args){ 3 | Student s = new Student("Yethish","97"); 4 | s.display(); 5 | } 6 | } 7 | 8 | class Student{ 9 | String nam,rn; 10 | Student(String name,String rno){ 11 | nam = name; 12 | rn = rno; 13 | } 14 | } 15 | 16 | public void display(){ 17 | System.out.printf("Welcome %s rno is %s \n",nam,rn); 18 | } -------------------------------------------------------------------------------- /Basic Programs/Factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Factorial{ 4 | public static void main(String[] args){ 5 | int f = 1; 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the number to find the factorial: "); 8 | int n = sc.nextInt(); 9 | for(int i = 1;i<=n;i++){ 10 | f *= i; 11 | } 12 | System.out.printf("Factorial of %d is %d",n,f); 13 | } 14 | } -------------------------------------------------------------------------------- /Arrays/ComArray.java: -------------------------------------------------------------------------------- 1 | public class ComArray { 2 | int[] arr3 = {14,7,3,9,2}; 3 | public static void main(String[] args) { 4 | int[] arr1 = {14,7,3,9,2}; 5 | int[] arr2 = {3,89,5,78,7,3,9,2}; 6 | for (int i=0; i num2 && num1 > num3) { 7 | System.out.printf(" %d is Greater", num1); 8 | } else if (num2 > num1 && num2 > num3) { 9 | System.out.printf(" %d is Greater", num2); 10 | } else { 11 | System.out.printf(" %d is Greater", num3); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Control Statements/Looping Statments/WhileLoop.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class WhileLoop { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | System.out.println("Enter a number for which you want a table: "); 7 | int number = input.nextInt(); 8 | int i = 1; 9 | while (i <= 10) { 10 | System.out.println(number + "*" + i + "=" + number * i); 11 | i += 1; 12 | } 13 | input.close(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Strings/String Tokenizers/StringFirst.java: -------------------------------------------------------------------------------- 1 | //Java Program using first Constructor(using Single parameter). 2 | 3 | import java.util.StringTokenizer; 4 | class StringFirst { 5 | public static void main(String[] args) { 6 | StringTokenizer st = new StringTokenizer("This statement is passed directly inside the class."); 7 | System.out.println("Tokens extracted from the string using StringTokenizer: "); 8 | while (st.hasMoreTokens()) { 9 | System.out.println(st.nextToken()); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Basic Programs/Armstrong.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Armstrong{ 4 | public static void main(String[] args){ 5 | int sum=0,n,org; 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the Number"); 8 | n=sc.nextInt(); 9 | org = n; 10 | while(org!=0){ 11 | int rem = org%10; 12 | sum+=rem*rem*rem; 13 | org/=10; 14 | } 15 | if(sum==n){ 16 | System.out.println("Armstrong Number"); 17 | } 18 | else{ 19 | System.out.println("Not an Armstrong Number"); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Java Programs/Comparision.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Comparision{ 3 | public static void main(String[] args){ 4 | Scanner sc = new Scanner(System.in); 5 | System.out.println("Enter a programming language that you know"); 6 | String input_string = sc.next(); 7 | String predefined_string = "Java"; 8 | if(input_string.equals(predefined_string)) 9 | { 10 | System.out.println("You strings are matched"); } 11 | else 12 | { 13 | System.out.println("Sorry Your strings aren't matched"); 14 | } 15 | 16 | sc.close(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Java Programs/ArrayIndexExample.java: -------------------------------------------------------------------------------- 1 | public class ArrayIndexExample { 2 | public static void main(String[] args) { 3 | int[] numbers = {1, 2, 3, 4, 5}; 4 | 5 | try { 6 | // Trying to access an index beyond the array length 7 | System.out.println("Accessing element at index 10: " + numbers[10]); 8 | } catch (ArrayIndexOutOfBoundsException e) { 9 | // Catching and handling the exception 10 | System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage()); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Inheritance/Area.java: -------------------------------------------------------------------------------- 1 | public class Area{ 2 | public void calculate(){ 3 | System.out.println("Nothng to calculate"); 4 | } 5 | public void calculate(int x){ 6 | System.out.println("Circle area: "+Math.PI*Math.pow(x,2)); 7 | } 8 | 9 | public void calculate(double x){ 10 | System.out.println("Circle Perimeter"+2*Math.PI*x); 11 | } 12 | 13 | public void calculate(int x,int y){ 14 | System.out.println("Rectangle Area"+x*y); 15 | } 16 | public static void main(String[] args){ 17 | Area a1=new Area(); 18 | a1.calculate(); 19 | a1.calculate(10,20); 20 | a1.calculate(10.5); 21 | a1.calculate(10); 22 | } 23 | } -------------------------------------------------------------------------------- /Java Programs/MyRun.java: -------------------------------------------------------------------------------- 1 | // Implementing the Runnable interface 2 | class MyRunnable implements Runnable { 3 | public void run() { 4 | System.out.println("Thread using Runnable is running..."); 5 | } 6 | } 7 | 8 | public class MyRun { 9 | public static void main(String[] args) { 10 | // Creating an instance of MyRunnable 11 | MyRunnable myRunnable = new MyRunnable(); 12 | 13 | // Creating a Thread object with MyRunnable instance 14 | Thread thread = new Thread(myRunnable); 15 | 16 | // Starting the thread 17 | thread.start(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Java Programs/Concatenation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Concatenation { 3 | public static void main(String[] args){ 4 | String str1 = "My name is "; 5 | String str2 = "I am studying in "; 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter Your name:"); 8 | String name = sc.next(); 9 | String my_name = str1.concat(name); 10 | System.out.println("Enter your college name:"); 11 | String college = sc.next(); 12 | String result = str2 + " " + college; 13 | System.out.printf("Here is %s Details",+name); 14 | System.out.println(my_name); 15 | System.out.println(result); 16 | } 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Java Programs/MyRunnable.java: -------------------------------------------------------------------------------- 1 | // Implementing the Runnable interface 2 | class MyRunnable implements Runnable { 3 | public void run() { 4 | System.out.println("Thread using Runnable is running..."); 5 | } 6 | } 7 | 8 | public class MyRunnable { 9 | public static void main(String[] args) { 10 | // Creating an instance of MyRunnable 11 | MyRunnable myRunnable = new MyRunnable(); 12 | 13 | // Creating a Thread object with MyRunnable instance 14 | Thread thread = new Thread(myRunnable); 15 | 16 | // Starting the thread 17 | thread.start(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Control Statements/Conditional Statements/SwitchCase.java: -------------------------------------------------------------------------------- 1 | public class SwitchCase { 2 | public static void main(String[] args) { 3 | int choice = 2; 4 | 5 | switch (choice) { 6 | case 1: 7 | System.out.println("You chose option 1"); 8 | break; 9 | case 2: 10 | System.out.println("You chose option 2"); 11 | break; 12 | case 3: 13 | System.out.println("You chose option 3"); 14 | break; 15 | default: 16 | System.out.println("Invalid choice"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Arrays/MultiDimArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class MultiDimArray { 3 | public static void main(String[] args) { 4 | int marks[][] = new int[10][2]; 5 | Scanner sc=new Scanner(System.in); 6 | System.out.println("Enter roll numbers and marks: "); 7 | for (int i=0; i num2 && num1 > num3) { 16 | System.out.printf("%d is Greatest", num1); 17 | } else if (num2 > num1 && num2 > num3) { 18 | System.out.printf("%d is Greatest", num2); 19 | } else { 20 | System.out.printf("%d is Greatest", num3); 21 | } 22 | input.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Basic Programs/InputMethods.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class InputMethods { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | // Basic Input methods 8 | System.out.println("Enter your name: "); 9 | String name = scanner.next(); 10 | scanner.nextLine(); 11 | System.out.println("Enter your college name: "); 12 | String college = scanner.nextLine(); // Read the whole line 13 | System.out.println("Enter your Roll number: "); 14 | int rollNumber = scanner.nextInt(); 15 | System.out.println("Enter your 1st Year marks Percentage: "); 16 | float marks = scanner.nextFloat(); // variable declaration 17 | scanner.close(); 18 | 19 | System.out.println("--------- Your Details ---------\n"); 20 | System.out.println("Name :" + name); 21 | System.out.println("\nCollege : " + college); 22 | System.out.println("\nRoll No : " + rollNumber); 23 | System.out.println("\nPercentage : " + marks); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Java Programs/InputMethods.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class InputMethods { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | // Basic Input methods 8 | System.out.println("Enter your name: "); 9 | String name = scanner.next(); 10 | scanner.nextLine(); 11 | System.out.println("Enter your college name: "); 12 | String college = scanner.nextLine(); // Read the whole line 13 | System.out.println("Enter your Roll number: "); 14 | int rollNumber = scanner.nextInt(); 15 | System.out.println("Enter your 1st Year marks Percentage: "); 16 | float marks = scanner.nextFloat(); // variable declaration 17 | scanner.close(); 18 | 19 | System.out.println("--------- Your Details ---------\n"); 20 | System.out.println("Name :" + name); 21 | System.out.println("\nCollege : " + college); 22 | System.out.println("\nRoll No : " + rollNumber); 23 | System.out.println("\nPercentage : " + marks); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Control Statements/Conditional Statements/README.md: -------------------------------------------------------------------------------- 1 | # Java Conditional Statements 2 | 3 | In Java programming, conditional statements are used to perform different actions based on different conditions. There are mainly three types of conditional statements in Java: `if`, `else if`, and `else`. 4 | 5 | ## 1. `if` Statement 6 | 7 | The `if` statement checks a condition and executes a block of code if the condition is true. 8 | 9 | ### Syntax: 10 | ```java 11 | if (condition) { 12 | // Code to be executed if condition is true 13 | } 14 | 15 | ## 2. `else if` Statement 16 | 17 | The `else if` statement allows you to check multiple conditions after the initial `if` statement. If the first `if` condition is false, it evaluates the next condition. 18 | 19 | ### Syntax: 20 | ```java 21 | else if (condition) { 22 | // Code to be executed if condition is true 23 | } 24 | 25 | ## 3. `else` Statement 26 | 27 | The `else` statement is used to execute a block of code if none of the preceding conditions are true. 28 | 29 | ### Syntax: 30 | ```java 31 | else { 32 | // Code to be executed if no condition is true 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Madavaram Yethishwar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Java Programs/ArithmeticOperations.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ArithmeticOperations { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | // Input first number 8 | System.out.print("Enter the first number: "); 9 | double num1 = scanner.nextDouble(); 10 | 11 | // Input second number 12 | System.out.print("Enter the second number: "); 13 | double num2 = scanner.nextDouble(); 14 | 15 | // Perform arithmetic operations 16 | double sum = num1 + num2; 17 | double difference = num1 - num2; 18 | double product = num1 * num2; 19 | double quotient = num1 / num2; 20 | 21 | // Display results 22 | System.out.println("Sum: " + num1 + " + " + num2 + " = " + sum); 23 | System.out.println("Difference: " + num1 + " - " + num2 + " = " + difference); 24 | System.out.println("Product: " + num1 + " * " + num2 + " = " + product); 25 | System.out.println("Quotient: " + num1 + " / " + num2 + " = " + quotient); 26 | 27 | // Close the scanner 28 | scanner.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Java Programs/VariableTypesExample.java: -------------------------------------------------------------------------------- 1 | public class VariableTypesExample { 2 | 3 | // Instance variable (or non-static variable) 4 | int instanceVar = 10; 5 | 6 | // Class variable (or static variable) 7 | static String classVar = "Hello"; 8 | 9 | public void methodWithLocalVariable() { 10 | // Local variable 11 | double localVar = 3.14; 12 | 13 | System.out.println("Instance Variable: " + instanceVar); 14 | System.out.println("Class Variable: " + classVar); 15 | System.out.println("Local Variable: " + localVar); 16 | } 17 | 18 | public static void main(String[] args) { 19 | VariableTypesExample obj = new VariableTypesExample(); 20 | 21 | // Accessing instance variable through object 22 | System.out.println("Instance Variable accessed through object: " + obj.instanceVar); 23 | 24 | // Accessing class variable directly using class name 25 | System.out.println("Class Variable accessed directly: " + VariableTypesExample.classVar); 26 | 27 | // Calling method with local variable 28 | obj.methodWithLocalVariable(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Java Programs/MethodOverLoad.java: -------------------------------------------------------------------------------- 1 | class MethodOverLoad { 2 | // Method to add two integers 3 | public int add(int a, int b) { 4 | return a + b; 5 | } 6 | 7 | // Method to add three integers 8 | public int add(int a, int b, int c) { 9 | return a + b + c; 10 | } 11 | 12 | // Method to add two floats 13 | public float add(float a, float b) { 14 | return a + b; 15 | } 16 | 17 | // Method to add two doubles 18 | public double add(double a, double b) { 19 | return a + b; 20 | } 21 | 22 | // Method to add three doubles (return type should be double) 23 | public double add(double a, double b, double c) { 24 | return a + b + c; 25 | } 26 | 27 | public static void main(String[] args) { 28 | MethodOverLoad math = new MethodOverLoad(); 29 | System.out.println("Sum of 2 and 3: " + math.add(2, 3)); 30 | System.out.println("Sum of 2, 3 and 4: " + math.add(2, 3, 4)); 31 | System.out.println("Sum of 2.5 and 3.5: " + math.add(2.5, 3.5)); 32 | System.out.println("Sum of 2.5, 3.5, and 4.5: " + math.add(2.5, 3.5, 4.5)); 33 | System.out.println("Sum of 2.0f and 3.0f: " + math.add(2.0f, 3.0f)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Control Statements/Jumping Statements/README.md: -------------------------------------------------------------------------------- 1 | ## Jumping Statements in Java 2 | 3 | Jumping statements in Java control the flow of execution by allowing you to jump to different parts of your code. The primary jumping statements are `break`, `continue`, and `return`. 4 | 5 | ### `break` Statement 6 | 7 | The `break` statement is used to exit a loop or switch statement prematurely. When a `break` statement is encountered, the control flow jumps out of the current loop or switch block. 8 | 9 | **Example:** 10 | 11 | ```java 12 | for (int i = 1; i <= 10; i++) { 13 | if (i == 5) { 14 | break; // exit the loop when i is 5 15 | } 16 | System.out.println(i); 17 | } 18 | // Output: 1 2 3 4 19 | ``` 20 | 21 | ### `continue` Statement 22 | 23 | The `continue` statement is used to skip the current iteration of a loop and proceed with the next iteration. When a `continue` statement is encountered, the remaining code inside the loop is skipped for the current iteration. 24 | 25 | **Example:** 26 | 27 | ```java 28 | for (int i = 1; i <= 10; i++) { 29 | if (i % 2 == 0) { 30 | continue; // skip the rest of the loop body for even numbers 31 | } 32 | System.out.println(i); 33 | } 34 | // Output: 1 3 5 7 9 35 | ``` 36 | -------------------------------------------------------------------------------- /Control Statements/README.md: -------------------------------------------------------------------------------- 1 | In Java, there are generally three types of control statements: 2 | 3 | Control statements are programming constructs that dictate the flow of execution within a program. They allow you to make decisions, repeat blocks of code, and alter the normal sequence of program execution. In essence, control statements enable you to control the flow of your program based on various conditions and requirements# Java Control Statements 4 | 5 | ## Selection Statements/Conditional Statements 6 | 7 | Conditional statements are used to make decisions based on certain conditions. 8 | 9 | - **if statement** 10 | - **if-else statement** 11 | - **if-else if-else statement** (also known as nested if-else) 12 | - **switch statement** 13 | 14 | ## Looping Statements (Iteration) 15 | 16 | Iteration statements are used to repeatedly execute a block of code. 17 | 18 | - **for loop** 19 | - **while loop** 20 | - **do-while loop** 21 | 22 | ## Jump Statements 23 | 24 | Jumping statements are used to alter the normal flow of control. 25 | 26 | - **break statement** 27 | - **continue statement** 28 | - **return statement** 29 | - **throw statement** 30 | 31 | These control statements provide various ways to control the flow of execution in Java programs. 32 | -------------------------------------------------------------------------------- /Control Statements/Jumping Statements/JumpingStatementsDemo.java: -------------------------------------------------------------------------------- 1 | public class JumpingStatementsDemo { 2 | public static void main(String[] args) { 3 | // Using break statement 4 | System.out.println("Using break statement:"); 5 | for (int i = 1; i <= 10; i++) { 6 | if (i == 5) { 7 | break; // exit the loop when i is 5 8 | } 9 | System.out.println(i); 10 | } 11 | 12 | // Using continue statement 13 | System.out.println("\nUsing continue statement:"); 14 | for (int i = 1; i <= 10; i++) { 15 | if (i % 2 == 0) { 16 | continue; // skip the rest of the loop body for even numbers 17 | } 18 | System.out.println(i); 19 | } 20 | 21 | // Using return statement 22 | System.out.println("\nUsing return statement:"); 23 | printNumbersUntil(5); // method will return when i equals 5 24 | } 25 | 26 | public static void printNumbersUntil(int limit) { 27 | for (int i = 1; i <= 10; i++) { 28 | if (i == limit) { 29 | return; // exit the method when i equals the limit 30 | } 31 | System.out.println(i); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Java Programs/TypeCasting.java: -------------------------------------------------------------------------------- 1 | public class TypeCasting { 2 | public static void main(String[] args) { 3 | // Implicit Typecasting (Widening Conversion) 4 | int myInt = 100; 5 | long myLong = myInt; // Implicit casting from int to long 6 | float myFloat = myInt; // Implicit casting from int to float 7 | double myDouble = myFloat; // Implicit casting from float to double 8 | 9 | System.out.println("Implicit Typecasting:"); 10 | System.out.println("int to long: " + myLong); 11 | System.out.println("int to float: " + myFloat); 12 | System.out.println("float to double: " + myDouble); 13 | 14 | // Explicit Typecasting (Narrowing Conversion) 15 | double myDouble2 = 123.456; 16 | float myFloat2 = (float) myDouble2; // Explicit casting from double to float 17 | long myLong2 = (long) myDouble2; // Explicit casting from double to long 18 | int myInt2 = (int) myDouble2; // Explicit casting from double to int 19 | 20 | System.out.println("\nExplicit Typecasting:"); 21 | System.out.println("double to float: " + myFloat2); 22 | System.out.println("double to long: " + myLong2); 23 | System.out.println("double to int: " + myInt2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Java Programs/ScannerMethods.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ScannerMethods { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.println("Enter input suitable for nextLine() method"); 8 | String my_string = sc.nextLine(); 9 | 10 | System.out.println("Enter input suitable for next() method"); 11 | String my_word = sc.next(); 12 | 13 | System.out.println("Enter input suitable for nextInt() method"); 14 | int my_integer = sc.nextInt(); 15 | 16 | System.out.println("Enter input suitable for nextFloat() method"); 17 | float my_float = sc.nextFloat(); 18 | 19 | System.out.println("Enter input suitable for nextDouble() method"); 20 | double my_double = sc.nextDouble(); 21 | 22 | // Consume the leftover newline 23 | 24 | 25 | System.out.println("Your word is " + my_word); 26 | System.out.println("Your integer is " + my_integer); 27 | System.out.println("Your float is " + my_float); 28 | System.out.println("Your double is " + my_double); 29 | System.out.println("Your string is " + my_string); 30 | 31 | sc.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Java Programs/Mutiple.java: -------------------------------------------------------------------------------- 1 | // Interface defining methods related to a vehicle 2 | interface Vehicle { 3 | void start(); 4 | void stop(); 5 | } 6 | 7 | // Interface defining methods related to a camera 8 | interface Camera { 9 | void capturePhoto(); 10 | void recordVideo(); 11 | } 12 | 13 | // Class implementing both Vehicle and Camera interfaces 14 | 15 | class Car implements Vehicle, Camera { 16 | // Vehicle interface methods 17 | public void start() { 18 | System.out.println("Car started."); 19 | } 20 | 21 | public void stop() { 22 | System.out.println("Car stopped."); 23 | } 24 | 25 | // Camera interface methods 26 | public void capturePhoto() { 27 | System.out.println("Photo captured."); 28 | } 29 | 30 | public void recordVideo() { 31 | System.out.println("Video recording started."); 32 | } 33 | 34 | // Additional method specific to Car class 35 | void drive() { 36 | System.out.println("Car is being driven."); 37 | } 38 | } 39 | 40 | public class Mutiple { 41 | public static void main(String[] args) { 42 | Car myCar = new Car(); 43 | 44 | // Using methods from Vehicle interface 45 | myCar.start(); 46 | myCar.stop(); 47 | 48 | // Using methods from Camera interface 49 | myCar.capturePhoto(); 50 | myCar.recordVideo(); 51 | 52 | // Using method specific to Car class 53 | myCar.drive(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Control Statements/Looping Statments/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Java Looping Statements 3 | 4 | In Java, looping statements are used to execute a block of code repeatedly based on a specified condition. There are mainly three types of looping statements in Java: `for`, `while`, and `do-while`. 5 | 6 | 7 | ## 1. `for` Loop 8 | 9 | The `for` loop is used when the number of iterations is known beforehand. 10 | 11 | ### Syntax: 12 | ```java 13 | for (initialization; condition; update) { 14 | // Code to be executed 15 | } 16 | ``` 17 | 18 | ### Example: 19 | ```java 20 | for (int i = 1; i <= 10; i++) { 21 | System.out.println(number + "*" + i + "=" + number * i); 22 | } 23 | ``` 24 | 25 | ## 2. `while` Loop 26 | 27 | The `while` loop is used when the number of iterations is not known beforehand, and the loop continues as long as a specified condition is true. 28 | 29 | ### Syntax: 30 | ```java 31 | while (condition) { 32 | // Code to be executed 33 | } 34 | ``` 35 | 36 | ### Example: 37 | ```java 38 | int i = 1; 39 | while (i <= 10) { 40 | System.out.println(number + "*" + i + "=" + number * i); 41 | i += 1; 42 | } 43 | ``` 44 | 45 | ## 3. `do-while` Loop 46 | 47 | The `do-while` loop is similar to the `while` loop, but the condition is checked after executing the code block, so it always executes at least once. 48 | 49 | ### Syntax: 50 | ```java 51 | do { 52 | // Code to be executed 53 | } while (condition); 54 | ``` 55 | 56 | ### Example: 57 | ```java 58 | int i = 1; 59 | do { 60 | System.out.println("This is Do block: "); 61 | i++; 62 | } while (i <= 10); 63 | ``` 64 | 65 | These looping statements provide flexibility in controlling the flow of execution in Java programs and are essential for repetitive tasks. 66 | ``` 67 | -------------------------------------------------------------------------------- /Java Programs/Arithmetic.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Arithmetic { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | System.out.print("Enter first number: "); 8 | double num1 = sc.nextDouble(); 9 | System.out.print("Enter second number: "); 10 | double num2 = sc.nextDouble(); 11 | 12 | System.out.println("Choose operation:"); 13 | System.out.println("1. Addition (+)"); 14 | System.out.println("2. Subtraction (-)"); 15 | System.out.println("3. Multiplication (*)"); 16 | System.out.println("4. Division (/)"); 17 | System.out.print("Enter your choice (1-4): "); 18 | int choice = sc.nextInt(); 19 | 20 | 21 | double result = 0; 22 | switch (choice) { 23 | case 1: 24 | result = num1 + num2; 25 | System.out.println(num1 + " + " + num2 + " = " + result); 26 | break; 27 | case 2: 28 | result = num1 - num2; 29 | System.out.println(num1 + " - " + num2 + " = " + result); 30 | break; 31 | case 3: 32 | result = num1 * num2; 33 | System.out.println(num1 + " * " + num2 + " = " + result); 34 | break; 35 | case 4: 36 | if (num2 != 0) { 37 | result = num1 / num2; 38 | System.out.println(num1 + " / " + num2 + " = " + result); 39 | } else { 40 | System.out.println("Error: Division by zero is not allowed."); 41 | } 42 | break; 43 | default: 44 | System.out.println("Invalid choice. Please choose a number between 1 and 4."); 45 | } 46 | 47 | sc.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Java Programs/StringMethods.java: -------------------------------------------------------------------------------- 1 | public class StringMethods { 2 | 3 | public static void main(String[] args) { 4 | // Creating a string 5 | String str = "Hello, World!"; 6 | 7 | // Length of the string 8 | int length = str.length(); 9 | System.out.println("Length of the string: " + length); 10 | 11 | // Character at specific index 12 | char charAt5 = str.charAt(5); 13 | System.out.println("Character at index 5: " + charAt5); 14 | 15 | // Substring from index 7 to end 16 | String substring = str.substring(7); 17 | System.out.println("Substring from index 7: " + substring); 18 | 19 | // Concatenation with another string 20 | String concatStr = str.concat(" Welcome!"); 21 | System.out.println("Concatenated string: " + concatStr); 22 | 23 | // Index of a character or substring 24 | int indexOfWorld = str.indexOf("World"); 25 | System.out.println("Index of 'World': " + indexOfWorld); 26 | 27 | // Replace characters or substring 28 | String replacedStr = str.replace("Hello", "Hi"); 29 | System.out.println("String after replacement: " + replacedStr); 30 | 31 | // Convert to uppercase 32 | String uppercase = str.toUpperCase(); 33 | System.out.println("Uppercase: " + uppercase); 34 | 35 | // Convert to lowercase 36 | String lowercase = str.toLowerCase(); 37 | System.out.println("Lowercase: " + lowercase); 38 | 39 | // Check if starts with a substring 40 | boolean startsWithHello = str.startsWith("Hello"); 41 | System.out.println("Starts with 'Hello': " + startsWithHello); 42 | 43 | // Check if ends with a substring 44 | boolean endsWithWorld = str.endsWith("World!"); 45 | System.out.println("Ends with 'World!': " + endsWithWorld); 46 | } 47 | } 48 | --------------------------------------------------------------------------------