You are on page 1of 115

Timings - Monday, Wed, Fri (9PM - 11PM)

Goal - Read the program very carefully, identify your mistake and
correct it

LinkedIn - Saksham Arora - Software Engineer - Microsoft | LinkedIn


Resume Saksham -
https://drive.google.com/file/d/16wcpmdqt1jcgA7yPIjZGIaktjIHQGn38/
view?usp=sharing
Online Java Compiler - https://www.onlinegdb.com/
Discord - Saksham#3230
Recording -
https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/calendar

Practice -
https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice

Day 1 PPT LINK - Introduction to Java.pptx

Drawing Notes link - Intro to Programming Drawing Notes August 2023

Questions List - Questions

7th August (Basic of program)


If you are not well in JAVA,
We will everything from the starting part

Computer understand binary language (011010101)


Programming language - English with some of rules

Humans cannot understand binary language

7th August (Basic of Java program)


// Any line which begins with a // will be ignored
// Java is based upon Object oriented programming (OOPs) ()
// Class and object in very detail in OOPs
// RULE 1 - Every program will begin with a class
// Classes are also having names
// RULE 2 - First letter for a class will be a capital letter

// RULE 3 - FileName should be as same as className

// Execution of program it always begin with main function


// RULE 4 - Every program will have a main function
// public will make class as public property so that
everyone(Multiple classes) can use it
// JAVA is case sensitive (it will treat uppercase and
lowercase differently)
public class Main
{
// public - will make function as public property so that
other functions can use it
// static - Main function will be static and not require
any object
// void - Main function will not return anything (We will
discuss in L9 Methods & Functions)
// main - name of the function is main
// String args[] - Command line arguments (We will
discuss in L8 arrays)
public static void main(String args[])
{
// Print anything in Java
// Any english sentence can be printed but it needs
to be written in "" (Double quotes)
System.out.print("Hello");

// System.out.print("AnythingYouCanPrint") - It will
print English sentence and not change the line
// System.out.println("AnythingYouCanPrint") - It
will print English sentence and change the line
}
}

7th August (Basic Java program)


public class Main
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

7th August (Commands for windows)

● Go to https://jdk.java.net/19/
● Download the Windows x64 version
● Extract the files (Right Click and click on Extract All)
● Copy this to C://Program Files/Java/
● (Type Environment Variables in Start) Open System
Variables (https://www.javatpoint.com/how-to-set-path-in-
java )
● Set the Path variables to C://Program
Files/Java/java-19/bin
● Close the command prompt and reopen it
● Restart the PC (Do it after the class)

7th August (Commands for Mac)

● Check if you have Brew installed


○ Run brew -v on your terminal
● Else install Home Brew
○ /bin/bash -c "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/inst
all/HEAD/install.sh)"
● Run brew install openjdk

7th August (Commands for Linux)

● sudo apt update


● sudo apt install default-jdk
7th August (Intellij Link - Free Version)

https://www.jetbrains.com/idea/download/

9th August (First Variety of Question)


public class Main // class is having name as Main
{
public static void main(String args[]) // main
function
{
// LOGIC - We will write the logic part
System.out.println("Hello, World");
}
}

9th August (Printing "Hello World")


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpprwqxxionk10jahij
class Solution {
public void helloWorld() {
// LOGIC
// We will not write the main function and main class

// We will just write the logic part


System.out.print("Hello World");
}
}

9th August (Print Two lines Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpppxo3rr8raz0r94mj
class Solution {
public void printTwoLines() {

// Only LOGIC is written


System.out.print("Hello there!");
System.out.print("Let's start");
}
}

9th August (Basic of Variables - 1)


// Keywords are the words which are fixed meaning in JAVA
public class Main
{
public static void main(String args[])
{
// Variable is just like a container or a box

// data type is the type of Variable we create

// RULE for variable -


// datatype variableName = valueStoredInVariable;

// Data Type - 1
// int - It is used for storing integer values
// Range -> -2*10^9 to +2*10^9 (FIXED)
// Memory used - 4 byte

// RULE - In java by default integer values are


represented in integers
// By writing L or l we tell the compiler treat value
as long

int a = 10;

// Data Type - 2
// long
// - It is storing big integer values
// - Range -> - 2*10^18 to 2*10^18
// - Memory - 8 bytes

long b = 150000000000000L;
double d = 4.5;

// - In java by default decimal values are


represented in double
// By writing F or f we tell the compiler treat value
as float
float e = 6.5f;

// Type Casting - Converting values of one datatype


into another
}
}

9th August (Basic of Variables - 2)


// Keywords are the words which are fixed meaning in JAVA

public class Main


{
public static void main(String args[])
{
// Variable is just like a container or a box

// data type is the type of Variable we create

// RULE for variable -


// datatype variableName = valueStoredInVariable;

// ASCII VALUES (L4) (String)


char s = '&';

// String is a class, L4 (String)


String a = "THis is a 2nd session. Got it";
}
}
9th August (Printing the Variable)
public class Main
{
public static void main(String args[])
{
int a = 5;

int b = 6;

System.out.println("Anything English Sentence");

// If we print the name of variable directly then


value of variable will be printed
System.out.println(b);

// Print a English sentence with a variable


System.out.println("a = " + a + " b = " + b);
}
}

9th August (Printing Variables Example)


public class Main
{
public static void main(String args[])
{
int a = 5;
int b = 6;

double c = 3.5;

float d = 5.7f;

long e = 150000000000000000l;

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}

9th August (Add two numbers)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp3his7a402l55t0lh
class Solution {
// We have created a functionality
// - We are giving two integer number as input
// - OUTPUT -> return the sum
public int addTwoNumbers(int a, int b) {

// INPUT - a & b
// OUTPUT - Return the sum of numbers
// LOGIC

// LOGIC - Addition -> + (Add the numbers)


int sum = a + b;

// return will return value to the function


return sum;
}
}

9th August (Add two numbers with Hidden program)


class Solution {
public int addTwoNumbers(int a, int b) {

// We have created a functionality


// - We are giving two integer number as input
// - OUTPUT -> return the sum

// INPUT - a & b
// OUTPUT - Return the sum of numbers
// LOGIC

// LOGIC - Addition -> + (Add the numbers)


int sum = a + b;

// return will return value to the function


return sum;
}
}

// HIDDEN PROGRAM (Object Oriented Programming - L11)


public class Main
{
public static void main(String args[])
{
Solution object = new Solution();
int sum = object.addTwoNumbers(5,6);
System.out.println(sum);
}
}

9th August (Divide Numbers NextLeap Practice)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppmy3zrpgveizk0i0s

class Solution {
public double divideNumbers(double a, double b) {
// Your code goes here
// Divide the numbers - / (division)
int c = a/b;
return c;
}
}

11th August (Type Casting - 1)


public class Main
{
public static void main(String arg[])
{
// Implicit Type casting
// smaller datatype to bigger datatype
// and there is no loss of data
// Automatically by the compiler

int b = 10;
long a = b;

System.out.println(a);
System.out.println(b);

// Explicit type casting


// Bigger datatype to Smaller datatype
// Or there is loss of data
// Compiler will not allow so we have to force
// we need to write data type again
long c = 11111111100000l;
int d = (int)c;

System.out.println(c);
System.out.println(d);

}
}

11th August (Type Casting - 2)


public class Main
{
public static void main(String arg[])
{
// Explicit type casting
// Bigger datatype to Smaller datatype
// Or there is loss of data
// Compiler will not allow so we have to force
// we need to write data type again

double a = 5.67;
int b = (int)a;

System.out.println(a);
System.out.println(b);

}
}

11th August (Arithmetic Operators)


public class Main
{
public static void main(String arg[])
{
// Operators
int a = 5;
int b = 9;

System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b); // If values are in
int then result will be in integer
System.out.println(a % b); // It will find the
remainder
}
}

11th August (Fahrenheit to Celsius Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppt5w11tph52vuraeg

class Solution {
public double convertFahrenheitToCelsius(double
tempF) {
double tempC = (tempF - 32.0) * (5.0/9.0);
return tempC;
}
}

11th August (Working of division operator - 1)


public class Main
{
public static void main(String arg[])
{
double temp = 35.0;
System.out.println((temp - 32)*(5/9));
}
}

11th August (Working of division operator - 2)


public class Main
{
public static void main(String arg[])
{
System.out.println(5.0/9.0);
System.out.println(5.0/9);
System.out.println(5/9.0);
System.out.println(5/9);
}
}

11th August (Relational Operators)


public class Main
{
public static void main(String arg[])
{
// Relational Operators

// == Equal to Equal to
// Compare if values are equal or not
// != Not equal to
// It will give true if values are different

boolean a = true;
boolean b = 5>6;

System.out.println(b); // false

System.out.println(5>6); // false
System.out.println(5<6); // true
System.out.println(5>5); // false
System.out.println(5>=5); // true
System.out.println(5==6); // false
System.out.println(5>=6); // false
System.out.println(5!=6); // true
System.out.println(5!=5); // false
}
}

11th August (Calculate Area of Circle)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppr0ppfx4x8em3yhiw

class Solution {
public double calculateCircleArea(double r) {
double pi = Math.PI;
double area = pi * r * r;
return area;
}
}

class Solution {
public boolean checkSameValue(int a, int b) {
boolean result = a==b;
return result;
}
}

class Solution {
public boolean checkLessThan(int a, int b) {
// Your code goes here
return a<b;
}
}

11th August (Logical Operators)


public class Main
{
public static void main(String arg[])
{
// Logical Operators

// && (AND)
// If all boolean values are true then only final
result is true
// If any one of them is false then result will
be false

boolean a = false;
boolean b = 5==5;
boolean c = 5>6;

System.out.println(a&&b&&c);

// || (OR)
// If any of boolean value is true then final
result will be true

System.out.println(a||b||c);
// ! (NOT)
// Reverse the boolean value given to it

System.out.println(!a);
}
}

class Solution {
public boolean checkNotEqual(int a, int b) {
return a!=b;
}
}

class Solution {
public boolean checkboolean(boolean a, boolean b) {
return (a && b);
}
}

class Solution {
public boolean checkGreater(int a, int b) {
return a>b;
}
}

11th August (Calculate Cylinder Volume)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppfjkp1tzq58qwefex
class Solution {
public double calculateCylinderVolume(double radius,
double height) {
double pi = Math.PI;
double volume = pi * radius * radius * height;
return volume;
}
}

11th August (Increment & Decrement Operators)


public class Main
{
public static void main(String arg[])
{
// Prefix - First increase the value then use it
// Postfix - First use the value and then
increase it
int a = 5;

int b = ++a;

System.out.println(a); // 6
System.out.println(b); // 6
}
}

11th August (Increment & Decrement Operators - 2)


public class Main
{
public static void main(String arg[])
{
int a = 5;

int b = ++a + a++ + ++a;


System.out.println(a); // 8
System.out.println(b); // 20
}
}

11th August (Increment & Decrement Operators - 30)


public class Main
{
public static void main(String arg[])
{
int a = 5;

int b = a--;

System.out.println(a);
System.out.println(b);
}
}

11th August (Integer to String and String to Integer Function - 1)


public class Main
{
public static void main(String arg[])
{
// Both are inbuilt function
// toString - It will take a integer and convert into
string
// parseInt - It will take a string and convert it
into integer

// Integer to string
int a = 10000;
String s = Integer.toString(a);
System.out.println(s);

// String to Integer
String b = "1235";
int d = Integer.parseInt(b);
System.out.println(d);
}
}

11th August (Integer to String and String to Integer Function - 2)


public class Main
{
public static void main(String arg[])
{
// Both are inbuilt function
// toString - It will take a integer and convert
into string
// parseInt - It will take a string and convert
it into integer

// Double to string
double a = 10000.445;
String s = Double.toString(a);
System.out.println(s);

// String to Double
String b = "1235.56";
double d = Double.parseDouble(b);
System.out.println(d);
}
}

11th August (Integer to String and String to Integer Function - 3)


public class Main
{
public static void main(String arg[])
{
// Both are inbuilt function
// toString - It will take a integer and convert
into string
// parseInt - It will take a string and convert
it into integer

// Boolean to string
boolean a = false
String s = Boolean.toString(a);
System.out.println(s);

// String to Boolean
String b = "false";
boolean d = Boolean.parseBoolean(b);
System.out.println(d);
}
}

11th August (Nth term of AP question)


class Solution {
public int nthTermInAnAP(int a, int d, int n) {
// Formula will always be given
// nth term = a + (n-1) * d

int result = a + (n-1) * d;


return result;
}
}

11th August (Shorthand Operators)


public class Main
{
public static void main(String arg[])
{
int a = 5;

a += 5; // a = a+5;
System.out.println(a);
a -= 10; // a = a-10;
System.out.println(a);

a *= 5; // a = a*5;
System.out.println(a);

a /= 5; // a = a/5;
System.out.println(a);
}
}

14th August (Backslash and Escape Sequence)


public class Main
{
public static void main(String args[])
{
System.out.println("Hello");
System.out.println("\"Hello\"");
System.out.println("\\n");

// "" (They are associated with strings)


// \ (Backslash - It will change internal
working of the next character)
String a = "\"Hello\"";

// Escape Sequence
// \n -> Change the New line
// \t -> Print the Tab space
System.out.println("\n123\t123\t456\nNextLeap");
}
}

14th August (String Functions)


public class Main
{
public static void main(String args[])
{
String a = "Virat";

// "NextLeap" -> Original String


// 01234567 -> Indexes

// Count Number of letters in the string


- .length()
System.out.println(a.length());

// Fetch indexed character in the string


- .charAt(index)
System.out.println(a.charAt(1));

String b = "Kohli";

// using + operator Strings can be combined


System.out.println(a + " " + b);

// .concat() -> Strings can combined with each


other
System.out.println(a.concat(" ").concat(b));

System.out.println(a.concat(" " + b));

// .equals() -> It will compare whether first


string is equal to second string
System.out.println(a.equals(b));
}
}

class Solution {
public boolean compare(String str1, String str2,
String str3) {
boolean result1 = str1.equals(str2);
boolean result2 = str2.equals(str3);

return result1 && result2;


}
}

14th August (Length of String Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpppjsntah0xw65r14t

class Solution {
public int length(String str) {
return str.length();
}
}

14th August (Maths Functions - 1)


public class Main
{
public static void main(String args[])
{
// Math class - Lot of inbuilt functionality are
present
int b = 15;

// Math.sqrt - It will find out square root of a


number
// It will give the output in double datatype
System.out.println(Math.sqrt(b));
int squareRoot = (int)Math.sqrt(b);
System.out.println(squareRoot);

int c = -9;
double d = 3.5;
// Math.abs - will give you the positive number
int e = Math.abs(c);
double f = Math.abs(d);

System.out.println(e);
System.out.println(f);

// Result of both of them is in double


// Math.floor - It will find out just smaller
closest integer number
// Math.ceil - It will find out just greater
closest integer number

int ceilResult = (int)Math.ceil(-7.1); //


Explicit Type Casting - 11th August
System.out.println(ceilResult);
System.out.println(Math.floor(-7.1));

// Math.cbrt - It will find the Cube root


System.out.println(Math.cbrt(125));

// Math.pow(a,b) - It will give us a^b


// Result will be in double datatype
System.out.println(Math.pow(3,3));
}
}

14th August (Maths Functions - 2)


public class Main
{
public static void main(String args[])
{
// Math class - Lot of inbuilt functionality are
present
int b = 15;

// Math.sqrt - It will find out square root of a


number
// It will give the output in double data type
System.out.println(Math.sqrt(b));
int squareRoot = (int)Math.sqrt(b);
System.out.println(squareRoot);

int c = -9;
double d = 3.5;

// Math.abs - will give you the positive number


int e = Math.abs(c);
double f = Math.abs(d);

System.out.println(e);
System.out.println(f);

// Result of both of them is in double


// Math.floor - It will find out just smaller
closest integer number
// Math.ceil - It will find out just greater
closest integer number

int ceilResult = (int)Math.ceil(-7.1); //


Explicit Type Casting - 11th August
System.out.println(ceilResult);
System.out.println(Math.floor(-7.1));

// Math.cbrt - It will find the Cube root


// Result will be in double datatype
System.out.println(Math.cbrt(125));

// Math.pow(a,b) - It will give us a^b


// Result will be in double datatype
System.out.println(Math.pow(3,3));

System.out.println(Math.pow(2.1,3.5));
}
}

14th August (Scanner Class - 1)


// Package - Group of classes and subclasses are called
as Packages
// Scanner is present inside java, inside util package
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// RULE for creating object -
// ClassName ObjectName = new ClassName();

Scanner in = new Scanner(System.in);

// Scanner Class - To input anything from the


user
// By default Scanner is not present so we need
to import
// Use scanner
// - Import scanner class
// - Create object of scanner class (Why ?
because all functions of
// scanner classs are non-static)
// - Functionality (Non - static)

int a = in.nextInt();
System.out.println(a);
double b = in.nextDouble();
System.out.println(b);

float c = in.nextFloat();
System.out.println(c);
}
}

14th August (Scanner Class - 2)


// Package - Group of classes and subclasses are called
as Packages
// Scanner is present inside java, inside util package
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// RULE for creating object -
// ClassName ObjectName = new ClassName();

Scanner in = new Scanner(System.in);

// Scanner Class - To input anything from the


user
// By default Scanner is not present so we need
to import
// Use scanner
// - Import scanner class
// - Create object of scanner class (Why ?
because all functions of
// scanner class are non-static)
// - Functionality (Non - static)

String word = in.nextLine();


System.out.println(word);
}
}

14th August (Scanner Class - 3)


// Package - Group of classes and subclasses are called
as Packages
// Scanner is present inside java, inside util package
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// RULE for creating object -
// ClassName ObjectName = new ClassName();

Scanner in = new Scanner(System.in);

// Scanner Class - To input anything from the


user
// By default Scanner is not present so we need
to import
// Use scanner
// - Import scanner class
// - Create object of scanner class (Why ?
because all functions of
// scanner classs are non-static)
// - Functionality (Non - static)

char letter = in.next().charAt(0);


System.out.println(letter);
}
}

14th August (Scanner Class - 4)


// Package - Group of classes and subclasses are called
as Packages
// Scanner is present inside java, inside util package
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// RULE for creating object -
// ClassName ObjectName = new ClassName();

Scanner in = new Scanner(System.in);

// Scanner Class - To input anything from the


user
// By default Scanner is not present so we need
to import
// Use scanner
// - Import scanner class
// - Create object of scanner class (Why ?
because all functions of
// scanner class are non-static)
// - Functionality (Non - static)

char letter = in.next().charAt(0);


System.out.println(letter);
}
}

16th August (Conditionals Basic)


public class Main
{
public static void main(String[] args)
{
int n = 7;
// n%2 -> Remainder of n with 2
if(n%2 == 0) // n%2 == 0 (if condition is
true, then number is even)
{
System.out.println("Even");
}
else
{
System.out.println("Odd");
}
}
}

16th August (Even Odd Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppdm9wo4se9cogk8wj

class Solution {
public String checkNumber(int num) {

if(num%2 == 0)
{
return "even";
}
else
{
return "odd";
}
}
}

16th August (Array Basic for Questions)


Array
- Collection of different numbers of same datatype
together will be an array

int a = 5;
[1,2,3] - int array
[2.0, 2.5, 3.5, 7.8] - double array
['a', 'z', 'b', 's'] - char array
["NextLeap", "java", "c++"] - String array

// RULE - datatype arrayName[] = new


datatype[sizeOfArray];
// int arr[] = new int[2];
// int[] arr = new int[2];
// int []arr = new int[2];

// Change the value or set a value in array


// arrayName[index] = value;
arr[0] = 5;
arr[1] = 100;

16th August (Fill Elements in Array)


public class Main
{
public static void main(String[] args)
{
int arr[] = new int[5];
// int[] arr = new int[5];
// int []arr = new int[5];

arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;

System.out.println(arr[1]);
}
}
16th August (Increment and Decrement Question - 1)
https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpprzwj0wsa71ekwp6l

class Solution {
public int[] XandY(int X, int Y) {
int arr[] = new int[2];
X--;
Y++;

arr[0] = X;
arr[1] = Y;

return arr;
}
}

16th August (Increment and Decrement Question - 2)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpprzwj0wsa71ekwp6l

class Solution
{
public int[] XandY(int X, int Y)
{
// Return type was array that is why array is
created
int arr[] = new int[2];

// We need to decrease the value of X


// We need to increase the value of Y

// Store the numbers in array


// Return array

arr[0] = --X;
arr[1] = ++Y;

return arr;
}
}

16th August (Increment and Decrement Question - 3)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpprzwj0wsa71ekwp6l

class Solution
{
public int[] XandY(int X, int Y)
{
int arr[] = {--X, ++Y};
// int arr[] = new int[2];
// arr[0] = --X;
// arr[1] = ++Y;

return arr;
}
}

16th August (Ram Grades Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppdtcpvsvktw7cc3uk

class Solution {
public double percentage(int a, int b, int c, int d)
{
double sum = a+b+c+d;
double percentage = (sum/400.0)*100.0;
return percentage;
}
}

16th August (Greater of two numbers Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpphbuolg8k2fgrwzeo

class Solution {
public int greatest(int a, int b) {
if (a >= b)
{
return a;
}
else
{
return b;
}
}
}

16th August (Days of week Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppk7mx381t822bi55g

class Solution {
public String daysOfWeek(int num) {

if(num == 1)
{
return "Monday";
}
else if(num == 2)
{
return "Tuesday";
}
else if(num == 3)
{
return "Wednesday";
}
else if(num == 4)
{
return "Thursday";
}
else if(num == 5)
{
return "Friday";
}
else if(num == 6)
{
return "Saturday";
}
else if(num == 7)
{
return "Sunday";
}
else
{
return "Invalid Input";
}
}
}

16th August (Greatest of three numbers)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp97jwldf6bhv2ozpc

class Solution {
public int findGreatest(int num1, int num2, int
num3) {

if(num1 >= num2 && num1 >= num3)


{
// checking whether first number is biggest

return num1;
}
else if(num2 >= num1 && num2 >= num3)
{
// checking whether second number is
biggest
return num2;
}
else
{
// if first and second is not biggest
number then third will be the biggest
return num3;
}

}
}

16th August (Ternary Operators)


public class Main
{
public static void main(String[] args)
{
int a = 50;
int b = 600;
int c = 70;

// condition ? result1 : result2


// If condition is true then result1 will be
stored
// If condition is false then result2 will be
stored
int greater = a>b ? a : b;
System.out.println(greater);

String result = a%2 == 0 ? "Even" : "Odd";

int greatest = (a>=b && a>=c) ? a : (b>=a &&


b>=c ? b : c);
}
}

16th August (Nth term of GP)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppj6sl35y6bbpjjy1u
class Solution {
public int Nth_of_GP(int a, int r, int N) {
// Your code goes here

// Formula will be given in interview


// Nth Term of GP = a * r ^ (n-1)

int power = (int)Math.pow(r, N-1);


int result = a * power;
return result;
}
}

16th August (Months of year)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpprrjcajavv5n76uey
class Solution {
public String[] MonthsOfTheYear () {

String[] arr =
{"January","February","March","April","May",
"June","July","August","September","October","Novem
ber","December"};

return arr;
}
}

18th August (Check Positive or Negative Number)


class Solution {
public String CheckPositiveOrNegative(int number) {
// Your code goes here
if(number > 0)
{
return "positive";
}
else if(number < 0)
{
return "negative";
}
else
{
return "zero";
}
}
}

18th August (For Loop)


public class Main
{
public static void main(String[] args)
{
// Print Hello 5 times

// for counting how many times a loop is running


we use a variable
// initialization statement - We initialise or
set the counting variable
// test condition - Help us to stop the loop
// update statement - change the counter
variable depending upon question

// There is no difference if you write ++i, i++,


i=i+1, i+=1 in the update statement

// RULE - for(initialization statement; test


condition; update statement)

// STEP 1 - i=1, 1<=5 (true), Hello is printed,


i will be updated to 2
// STEP 2 - i=2, 2<=5 (true), Hello is printed,
i will be updated to 3
// STEP 3 - i=3, 3<=5 (true), Hello is printed,
i will be updated to 4
// STEP 4 - i=4, 4<=5 (true), Hello is printed,
i will be updated to 5
// STEP 5 - i=5, 5<=5 (true), Hello is printed,
i will be updated to 6
// STEP 6 - i=6, 6<=5 (false), THE loop will
STOP
// for(int i=1; i<=5; i=i+1)
// {
// System.out.println("Hello");
// }

// for(int i=0; i<=5; i=i+2)


// {
// System.out.println(i);
// }

// for(int i=1; i<=10; i=i*2)


// {
// System.out.println(i);
// }

//for(int i=5; i>=1; i--)


// {
// System.out.println(i);
// }

// Infinite Loop - It will never stop


// for(int i=5; i>=1; i++)
// {
// System.out.println(i);
// }

for(char i='a'; i<='z'; i++)


{
System.out.print(i + " ");
}
}
}

18th August (ASCII Values)


public class Main
{
public static void main(String[] args)
{
// ASCII VALUES -
// 'A' - 'Z' --> 65 - 90
// 'a' - 'z' --> 97 - 122

char a = 'z';
int d = a;

System.out.println(d);

int b = 65;
char c = (char)b; // bigger to smaller
System.out.println(c);
}
}

18th August (Sum of Natural Numbers Question)


class Solution {
public int sumOfNaturalNumbers(int n) {

int sum = 0; // 1 .... n

// STEP 1: i=1; 1<=5 (true), sum = 0 + 1 = 1, i


will become 2
// STEP 2: i=2; 2<=5 (true), sum = 1 + 2 = 3, i
will become 3
// STEP 3: i=3; 3<=5 (true), sum = 3 + 3 = 6, i
will become 4
// STEP 4: i=4; 4<=5 (true), sum = 6 + 4 = 10,
i will become 5
// STEP 5: i=5; 5<=5 (true), sum = 10 + 5 = 15,
i will become 6
// STEP 6: i=6; 6<=5 (false), The loop will
stop
for(int i=1; i<=n; i++)
{
sum = sum + i;
}

}
}

18th August (Sum of numbers in range)


class Solution {
public int sum_of_numbers_in_range(int[] interval) {
// Your code goes here

// Find first two elements of array


int start = interval[0]; // first element of
array ~ starting point
int end = interval[1]; // second element of
array ~ ending point

int sum = 0;

for(int i=start; i<=end; i++)


{
sum = sum + i;
}
return sum;
}
}

18th August (Factorial of Number)


class Solution {
public long factorial(int fact) {
// factorial of n = product of all numbers till
n(1 to N)

long answer = 1L;

for(int i=1; i<=fact; i++)


{
answer = answer * i;
}

return answer;
}
}

18th August (Fibonacci Series)


class Solution {
public int[] fibonacci(int n) {

// We need to find first n fibonacci numbers


// 0 1 1 2 3 5 8 13 21 .....

// n - 1 answer - [0]
// n - 2 answer - [0 1]
// n - 3 answer - [0 1 1]

// n = 5
int answer[] = new int[n];
// answer -> [0, 0, 0, 0, 0]
answer[0] = 0; // first number in series is 0

if(n == 1) // n can be 1 also so array size


will be 1 -> [0]
{
return answer;
}

answer[1] = 1; // second number in series is 1

// answer -> [0, 1, 1, 2, 3]


// [0, 1, 2, 3, 4]

// starting point = 2 (First two numbers are


known that is why we start from 2)
// ending point = n-1 (Why ?)

// STEP 1: i=2; 2<=4 (true), answer[2] =


answer[1]+answer[0]=1, i = 3
// STEP 2: i=3; 3<=4 (true), answer[3] =
answer[2]+answer[1]=2, i = 4
// STEP 1: i=4; 4<=4 (true), answer[4] =
answer[3]+answer[2]=3, i = 5

for(int i=2; i<=n-1; i++) // i is acting as


index
{
answer[i] = answer[i-1] + answer[i-2];
}

return answer;
}
}
18th August (Grades Question)
class Solution {
public char grades(double[] marks) {
// Loops & Conditionals

int size = marks.length;

// [40,80,80,40,60,60]
// size = 6

double sum = 0.0;

// STEP 1: i=0, 0<=5 (true), sum = 0 + marks[0]


= 40.0, i = 1
for(int i=0; i<=size-1; i++)
{
sum = sum + marks[i];
}

double average = sum / size;

if(average >= 80.0)


{
return 'A';
}
else if(average < 80.0 && average >= 60.0)
{
return 'B';
}
else if(average < 60.0 && average >= 40.0)
{
return 'C';
}
else
{
return 'D';
}
}
}

18th August (While Loop)


public class Main
{
public static void main(String[] args)
{
// Print Hello 5 times

// for counting how many times a loop is running


we use a variable
// initialization statement - We initialise or
set the counting variable
// test condition - Help us to stop the loop
// update statement - change the counter
variable depending upon question

// RULE FOR LOOP


// for(initialization statement; test condition;
update statement)
// {
// statement
// }

// RULE WHILE LOOP


// initialization statement
// while(test condition)
// {
// statements

// update statement
// }
int i = 1;
// STEP 1: i=1, 1<=5 (true), Hello will be
printed, i = 2
// STEP 2: i=2, 2<=5 (true), Hello will be
printed, i = 3
// STEP 3: i=3, 3<=5 (true), Hello will be
printed, i = 4
// STEP 4: i=4, 4<=5 (true), Hello will be
printed, i = 5
// STEP 5: i=5, 5<=5 (true), Hello will be
printed, i = 6
// STEP 6: i=6, 6<=5 (false), The loop will stop

while(i<=5)
{
System.out.println("Hello");

i=i+1; // i++, ++i, i+=1


}

int j = 1;
while(j<=5)
{
System.out.println("Hello");

j = j+3;
}
}
}

18th August (Do while Loop)


public class Main
{
public static void main(String[] args)
{
// Print Hello 5 times
// for counting how many times a loop is running
we use a variable
// initialization statement - We initialise or
set the counting variable
// test condition - Help us to stop the loop
// update statement - change the counter
variable depending upon question

// RULE FOR LOOP


// for(initialization statement; test condition;
update statement)
// {
// statement
// }

// RULE WHILE LOOP


// initialization statement
// while(test condition)
// {
// statements

// update statement
// }

// RULE DO WHILE LOOP


// initialization statement
// do
// {
//statement

// update statement
// } while(test condition);

// STEP 1: i=1, Hello will be printed, i=2, 2<=3


(true)
// STEP 2: i=2, Hello will be printed, i=3, 3<=3
(true)
// STEP 3: i=3, Hello will be printed, i=4, 4<=3
(false), The loop will stop
int i=1;
do
{
System.out.println("Hello");
i++;
}while(i<=3);
}
}

18th August (Odd Even using Switch Case)


public class Main
{
public static void main(String[] args)
{
int n = 5;
switch(n%2)
{
case 1:
System.out.println("Odd");
break;
case 0:
System.out.println("Even");
break;
}
}
}

18th August (Switch Case Example)


public class Main
{
public static void main(String[] args)
{
int n = 9;

switch(n)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
default:
System.out.println("Invalid Input");
case 5:
System.out.println("Friday");
case 10:
System.out.println("Anything");
}
}
}

21st August (Basic of nested Loop)


public class Main
{
public static void main(String[] args)
{

// Basic Loop -
// for(int i=1; i<=5; i++)
// {
// System.out.println("Hello");
// }

// Nested Loop - If we write one loop inside another


then it will become nested loop
// STEP 1: i=1, 1<=3 (true), j loop will start
running (3 times), i = 2
// STEP 2: i=2, 2<=3 (true), j loop will start
running (3 times), i = 3
// STEP 3: i=3, 3<=3 (true), j loop will start
running (3 times), i = 4
// STEP 4: i=4, 4<=3 (false), i loop will stop

for(int i=1; i<=3; i++) // Outer Loop (3 times)


{
// STEPs: j=1, 1<=3 (true), Hello will be
printed, j = 2
// STEPs: j=2, 2<=3 (true), Hello will be
printed, j = 3
// STEPs: j=3, 3<=3 (true), Hello will be
printed, j = 4
// STEPs: j=4, 4<=3 (false), j loop will stop
for(int j=1; j<=3; j++) // Inner Loop (3 times)
{
System.out.println("Hello");
}
}

}
}

21st August (Nested Loop Example - 2)


public class Main
{
public static void main(String[] args)
{
// i = 1, 1<=5 (true), j loop will run 1 time (1
star will be printed), i = 2
// i = 2, 2<=5 (true), j loop will run 2 time (2
star will be printed), i = 3
// i = 3, 3<=5 (true), j loop will run 3 time (3
star will be printed), i = 4
// i = 4, 4<=5 (true), j loop will run 4 time (4
star will be printed), i = 5
// i = 5, 5<=5 (true), j loop will run 5 time (5
star will be printed), i = 6
for(int i=1; i<=5; i++) // Outer Loop - Number
of Rows printed
{
// When i = 2
// j = 1, 1<=2 (true), * will printed, j = 2
// j = 2, 2<=2 (true), * will printed, j = 3
// j = 3, 3<=2 (false), The loop will stop
for(int j=1; j<=i; j++) // Inner Loop - What
is printed in that row
{
System.out.print("*");
}
System.out.println(); // Changing the row
}
}
}

21st August (Extra variables Method)


import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// EXTRA VARIABLES (EASYYY)
int numberOfSpaces = n-1; // how many spaces
will be printed in that row/ line
int numberOfStars = 1; // how many stars will be
printed in that row/ line

// i = 1, 1<=5 (true), numberOfSpaces = 4, 4


spaces & 1 stars will be printed in the same line, i = 2
// i = 2, 2<=5 (true), numberOfSpaces = 3, 3
spaces & 2 stars will be printed in the same line, i = 3
// i = 3, 3<=5 (true), numberOfSpaces = 2, 2
spaces & 2 stars will be printed in the same line, i = 4
// i = 4, 4<=5 (true), numberOfSpaces = 1, 1
spaces & 2 stars will be printed in the same line, i = 5
// i = 5, 5<=5 (true), numberOfSpaces = 0, 0
spaces & 2 stars will be printed in the same line, i = 6
for(int i=1; i<=n; i++) // Outer Loop - Number
of Rows printed
{
// First inner loop for number of spaces
for(int j=1; j<=numberOfSpaces; j++)
{
System.out.print(" ");
}

// Second inner loop for number of stars


for(int j=1; j<=numberOfStars; j++)
{
System.out.print("*");
}

numberOfSpaces = numberOfSpaces - 1;
numberOfStars = numberOfStars + 1;

System.out.println(); // Changing the row


}
}
}

21st August (Relation Method)


import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();

// EXAMPLE when n is 5
// i = 1, 1<=5 (true), n-i = 4, 4 spaces & 1
stars will be printed in the same line, i = 2
// i = 2, 2<=5 (true), n-i = 3, 3 spaces & 2
stars will be printed in the same line, i = 3
// i = 3, 3<=5 (true), n-i = 2, 2 spaces & 3
stars will be printed in the same line, i = 4
// i = 4, 4<=5 (true), n-i = 1, 1 spaces & 4
stars will be printed in the same line, i = 5
// i = 5, 5<=5 (true), n-i = 0, 0 spaces & 5
stars will be printed in the same line, i = 6
for(int i=1; i<=n; i++) // Outer Loop - Number
of Rows printed
{
// First inner loop for number of spaces
for(int j=1; j<=n-i; j++)
{
System.out.print(" ");
}

// Second inner loop for number of stars


for(int j=1; j<=i; j++)
{
System.out.print("*");
}

System.out.println(); // Changing the row


}
}
}

21st August (Print Pyramid Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppqx2ejmnrkxk35hex

class Solution {
public void printPyramid(int n) {

for(int i=1; i<=n; i++) // Outer Loop - number


of rows
{
for(int j=1; j<=i; j++) // in every row i
things need to be printed
{
System.out.print(i);
}
System.out.println();
}

}
}

21st August (Triangle Pattern Question - Extra Variables Method)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppq7tuap7sa6dz7tuz

class Solution {
public void trianglePattern(int n) {

// EXTRA VARIABLES METHOD


int numbersOfStars = 1;
int numberOfSpaces = n-1;

// N = 4
// i = 1, 1<=4 (true), numbersOfStars = 1,
numberOfSpaces = 3
// i = 2, 2<=4 (true), numbersOfStars = 3,
numberOfSpaces = 2
// i = 3, 3<=4 (true), numbersOfStars = 5,
numberOfSpaces = 1
// i = 4, 4<=4 (true), numbersOfStars = 7,
numberOfSpaces = 0
for(int i=1; i<=n; i++) // number of rows
{
// Print spaces
for(int j=1; j<=numberOfSpaces; j++)
{
System.out.print(" ");
}

// Print stars
for(int j=1; j<=numbersOfStars; j++)
{
System.out.print("*");
}

numbersOfStars = numbersOfStars + 2;
numberOfSpaces = numberOfSpaces - 1;

System.out.println();
}

}
}
21st August (Triangle Pattern Question - Relation Method)
https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppq7tuap7sa6dz7tuz

class Solution {
public void trianglePattern(int n) {

// N = 4
// i = 1, 1<=4 (true), n-i = 3, 2*i-1 = 1,
numbersOfStars = 1, numberOfSpaces = 3
// i = 2, 2<=4 (true), n-i = 2, 2*i-1 = 3,
numbersOfStars = 3, numberOfSpaces = 2
// i = 3, 3<=4 (true), n-i = 1, 2*i-1 = 5,
numbersOfStars = 5, numberOfSpaces = 1
// i = 4, 4<=4 (true), n-i = 0, 2*i-1 = 7,
numbersOfStars = 7, numberOfSpaces = 0
for(int i=1; i<=n; i++) // number of rows
{
// Print spaces
for(int j=1; j<=(n-i); j++)
{
System.out.print(" ");
}

// Print stars
for(int j=1; j<=2*i-1; j++)
{
System.out.print("*");
}

System.out.println();
}

}
}

21st August (Number Pattern Printing)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppattzpeav8i0zzrjs
class Solution {
public void TimesTable (int n) {

// N = 3
for(int i=1; i<=n; i++)
{
// Example for i = 2, n = 3
// n = 3, i = 2, j = 1, i*j = 2*1
// n = 3, i = 2, j = 2, i*j = 2*2
// n = 3, i = 2, j = 3, i*j = 2*3
for(int j=1; j<=n; j++)
{
System.out.print(i*j + " ");
}

System.out.println(); // Changing the line


}
}
}

21st August (Star Pattern Question - 1)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppi5b5exbjyjw8c3jr
class Solution {
public void StarPattern (int n) {

int numberOfStars = n;
for(int i=1; i<=n; i++) // Outer Loop - number
of rows
{
for(int j=1; j<=numberOfStars; j++)
{
System.out.print("*");
}
numberOfStars = numberOfStars - 1;
System.out.println();
}

}
}

21st August (Star Pattern Question - 2)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppi5b5exbjyjw8c3jr
class Solution {
public void StarPattern (int n) {

// n = 5
// i = 1, j = 5 - 4 - 3 - 2 - 1 (5 times)
// i = 2, j = 5 - 4 - 3 - 2 (4 times)
// i = 3, j = 5 - 4 - 3 (3 times)
// i = 4, j = 5 - 4 (2 times)
// i = 5, j = 5 (1 time)
for(int i=1; i<=n; i++) // Outer Loop - number
of rows
{
for(int j=n; j>=i; j--)
{
System.out.print("*");
}

System.out.println();
}

}
}

21st August (Leap Year Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppe7hm4zy7amyk8vo6
class Solution {
public boolean leapyear(int year) {

if(year%4 == 0)
{
if(year%100 == 0)
{
// now it has to be divisible by 400
for being a leap year
if(year%400 == 0) // divisible by 4,
divisible by 100 and 400, Example - 2000, 1600, 1200
{
return true;
}
else // Example - 1900, 1700, 1500
(They are not leap)
{
return false;
}
}
else // Example - 2008, 2012, 2004, 2016,
2020
{
return true;
}
}
else // not divisible by 4 (Not a leap year) //
Example - 1997, 2003, 2007, 2005
{
return false;
}
}
}
23rd August (1D array Theory)
public class Main
{
public static void main(String[] args)
{
// int, long, float, double, char, boolean
variables are stored in stack

// array values and String value are stored in


Heap memory
// their variables are stored in stack memory

// RULE -
// datatype arrayName[] = new
datatype[sizeOfArray];
// datatype[] arrayName = new
datatype[sizeOfArray];
// datatype []arrayName = new
datatype[sizeOfArray];

int a[] = new int[3];


a[0] = 10;
a[1] = 20;
a[2] = 30;

// If you make changes in either of array then


change will be reflected in both arrays
int b[] = a;

b[0] = 100;

System.out.println(a[0]);
System.out.println(b[0]);
}
}
23rd August (Variable Example)
public class Main
{
public static void main(String[] args)
{
// int, long, float, double, char, boolean
variables are stored in stack

// array values and String value are stored in


Heap memory
// their variables are stored in stack memory

// RULE -
// datatype arrayName[] = new data
type[sizeOfArray];
// datatype[] arrayName = new data
type[sizeOfArray];
// datatype []arrayName = new data
type[sizeOfArray];

int a = 5;

int b = 10;
b = a;

b = 20;

System.out.println(a);
System.out.println(b);
}
}

23rd August (Iterate in the array Example)


public class Main
{
public static void main(String[] args)
{
// int, long, float, double, char, boolean
variables are stored in stack

// array values and String value are stored in


Heap memory
// their variables are stored in stack memory

// RULE -
// datatype arrayName[] = new data
type[sizeOfArray];
// datatype[] arrayName = new data
type[sizeOfArray];
// datatype []arrayName = new data
type[sizeOfArray];

int a[] = new int[5];


a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 50;

// print all elements of array


// index is going from 0 to size-1

// i = 0, a[0] will be printed


// i = 1, a[1] will be printed
// i = 2, a[2] will be printed
// i = 3, a[3] will be printed
// i = 4, a[4] will be printed
for(int i=0; i<=4; i++)
{
System.out.print(a[i] + " ");
}
System.out.println(a);
}
}

23rd August (Types of Array)


public class Main
{
public static void main(String[] args)
{
// int, long, float, double, char, boolean
variables are stored in stack

// array values and String value are stored in


Heap memory
// their variables are stored in stack memory

// RULE -
// datatype arrayName[] = new
datatype[sizeOfArray];
// datatype[] arrayName = new
datatype[sizeOfArray];
// datatype []arrayName = new
datatype[sizeOfArray];

int z[] = {1,2};

int b[] = new int[1000000];


float c[] = new float[3];
double a[] = new double[3];
char d[] = new char[3];
boolean e[] = new boolean[3];
String f[] = new String[3];
f[1] = "12345";
f[2] = "67890";

System.out.println(b[0]);
System.out.println(c[0]);
System.out.println(a[1]);
System.out.println(d[0]);
System.out.println(e[0]);
System.out.println(f[0]);
}
}

23rd August (Type of Array and Print Array)


public class Main
{
public static void main(String[] args)
{
// int, long, float, double, char, boolean
variables are stored in stack

// array values and String value are stored in


Heap memory
// their variables are stored in stack memory

// RULE -
// datatype arrayName[] = new
datatype[sizeOfArray];
// datatype[] arrayName = new
datatype[sizeOfArray];
// datatype []arrayName = new
datatype[sizeOfArray];

int a[] = new int[3];

System.out.println(a); // address of variable a


is printed

int n = a.length; // size of the array

// i = 0, 0<3 (true)
// i = 1, 1<3 (true)
// i = 2, 2<3 (true)
// i = 3, 3<3 (false)
for(int i=0; i<3; i++)
{
System.out.println(a[i]);
}
}
}

23rd August (Sum of numbers Array Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppdyuti99r6jcdqv01

class Solution {
public int sumNumbers(int[] arr) {

// Size is not given find size of array


int size = arr.length;

// Sum of all elements


int sum = 0;

// i<size (This is also correct)


for(int i=0; i<=size-1; i++)
{
sum = sum + arr[i];
}

return sum;
}
}

23rd August (Max of Array Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpppjimjfnn4x7cdgo8

class Solution {
public int findMax(int[] arr) {
// Size is not given find size of array
int size = arr.length;

int maximum = arr[0];


// arr = [-1, 7, -2]
// maximum = -1

// STEP 1: i=0 i<=2 (true), arr[0] = -1, (arr[i]


> maximum) (false), i = 1, maximum = -1
// STEP 2: i=1 1<=2 (true), arr[1] = 7, (arr[i] >
maximum) (true), i = 2, maximum = 7
// STEP 1: i=2 2<=2 (true), arr[2] = -2, (arr[i]
> maximum) (false), i = 3, maximum = 7
for(int i=0; i<=size-1; i++)
{
if(arr[i] > maximum)
{
maximum = arr[i];
}
}

return maximum;
}
}

23rd August (Count Even Numbers in Array Question)


class Solution {
public int countEvens(int[] arr) {
// Your code goes here

// STEP 1 - Find the size of array

// STEP 2 - Iterate and count even numbers


// Even - If divisible by 2 (arr[i]%2 == 0)
}
}

23rd August (Find Average in Array Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppux20d3n9x9yyq758

class Solution {
public double findAverage(int[] arr) {
// Size is not given find size of array
int size = arr.length;

// Sum of all elements


double sum = 0.0;

// i<size (This is also correct)


for(int i=0; i<=size-1; i++)
{
sum = sum + arr[i];
}

double average = sum/size;


return average;
}
}

23rd August (Replace Even by 0 and Odd by 1)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppaqmizihsv1zvkwrl

class Solution {
public int[] modifyArray(int[] arr) {
// Your code goes here

int size = arr.length;

for(int i=0; i<size; i++)


{
if(arr[i]%2 == 0) // even number
{
// since it is even change it to 0
// make change in the original array
arr[i] = 0;
}
else
{
// since it is even change it to 1
arr[i] = 1;
}
}

return arr;
}
}

23rd August (Sum of all positive numbers in Array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp8e03mqkzaov1wywh

class Solution {
public int findSumOfPositives(int[] arr) {
// Your code goes here

// Size is not given find size of array


int size = arr.length;

// Sum of all elements


int sum = 0;

// i<size (This is also correct)


for(int i=0; i<=size-1; i++)
{
if(arr[i] > 0)
{
sum = sum + arr[i];
}
}

return sum;

}
}

23rd August (Pair with a Given Sum)


class Solution {
public int[] findPairWithGivenSum(int[] arr, int
targetSum) {
// Your code goes here

int result[] = new int[2];


int n = arr.length;

for(int i=0; i<n; i++) // fixed element


{
for(int j=i+1; j<n; j++) // other element
options
{
int sum = arr[i] + arr[j];

if(sum == targetSum)
{
result[0] = arr[i];
result[1] = arr[j];
return result; // both the loops
will stop and we result the result
}
}
}

return result;
}
}
25th August (2D array Basic)
public class Main
{
public static void main(String[] args)
{
// RULE for creating 1D array -
// datatype arrayName[] = new datatype[size];
int a[] = new int[3];

// RULE for creating 1D array -


// datatype arrayName[][] = new
datatype[rowSize][columnSize];
// datatype[][] arrayName = new
datatype[rowSize][columnSize];
// datatype [][]arrayName = new
datatype[rowSize][columnSize];

int arr[][] = new int[2][3];


arr[0][0] = 1;
arr[0][1] = 5;
arr[0][2] = 2;

arr[1][0] = 5;
arr[1][1] = 3;
arr[1][2] = 7;

for(int i=0; i<2; i++) // rows


{
for(int j=0; j<3; j++) // columns
{
System.out.print(arr[i][j] + " ");
}
System.out.println(); // Changing the row
}
}
}
25th August (Input the array using Scanner Class)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();

int a[] = new int[n];


// n = 5
// i = 0, 1, 2, 3, 4
// i = 5, 5<5 (false)
// i<=(n-1) (This is correct)
for(int i=0; i<n; i++)
{
a[i] = in.nextInt();
}

// a -> [7, 8, 9, 1, 2]
for(int i=0; i<n; i++)
{
System.out.print(a[i] + " ");
}
}
}

25th August (2D array Indexing)


import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[][] = new int[3][3];

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;

a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;

a[2][0] = 7;
a[2][1] = 8;
a[2][2] = 9;

for(int i=0; i<3; i++) // rows


{
for(int j=0; j<3; j++) // columns
{
// i == j (LEFT Diagonal)
// i+j == n-1 (RIGHT Diagonal)
if(i+j == 2)
{
System.out.print(a[i][j] + " ");
}
}
}
}
}

25th August (2D array Indexing)


import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();

int a[][] = new int[3][3];

// rowsize = a.length (Number of rows)


// ColumnSize = a[0].length (Number of columns)

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;

a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;

a[2][0] = 7;
a[2][1] = 8;
a[2][2] = 9;

}
}

25th August (Maximum in 2D Array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppbff1l99hw12vpkgs

class Solution {
public int max(int[][] args) {
// Your code goes here

int rows = args.length;


int columns = args[0].length;
int maximum = args[0][0];

for(int i=0; i<rows; i++)


{
for(int j=0; j<columns; j++)
{
if(args[i][j] > maximum)
{
maximum = args[i][j];
}
}
}

return maximum;
}
}

25th August (Sum of all Elements in Array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppw79b986psj2xknzz

class Solution {
public int sumOfElements(int[][] arr) {
// Your code goes here

int rows = arr.length;


int columns = arr[0].length;

int sum = 0;

for(int i=0; i<rows; i++)


{
for(int j=0; j<columns; j++)
{
sum = sum + arr[i][j];
}
}

return sum;
}
}

25th August (Sum of Diagonal Elements)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppw79b986psj2xknzz
class Solution {
public int findDiagonalSum(int[][] arr) {
// Your code goes here

int rows = arr.length;


int columns = arr[0].length;

int sum = 0;

for(int i=0; i<rows; i++)


{
for(int j=0; j<columns; j++)
{
if(i==j)
{
sum = sum + arr[i][j];
}
}
}

return sum;
}
}

25th August (Sum of Anti-Diagonals in 2D array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp8d3l9t3p0oypbbi1

class Solution {
public int sumAntiDiagonal(int[][] arr) {
int n = arr.length;
int sum = 0;

for(int i=0; i<n; i++)


{
for(int j=0; j<n; j++)
{
if(i+j == (n-1)) // RIGHT DIAGONAL or
anti diagonal
{
sum = sum + arr[i][j];
}
}
}

return sum;
}
}

25th August (Maximum in Each Row in 2D Array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp7jna9pdhwwkktfta

class Solution {
public int[] findRowMaxima(int[][] arr) {

int rows = arr.length;


int columns = arr[0].length;

int answer[] = new int[row];

// i = 0, maximumInRow = 1
// j = 0, i = 0, arr[i][j] = 1, 1>1 (false)
// j = 1, i = 0, arr[i][j] = 2, 2>1 (true)
// j = 2, i = 0, arr[i][j] = 3, 3>1 (true)

// i = 1, maximumInRow = 4
// j = 0, i = 0, arr[i][j] = 1, 1>1 (false)
// j = 1, i = 0, arr[i][j] = 2, 2>1 (true)
// j = 2, i = 0, arr[i][j] = 3, 3>1 (true)
for(int i=0; i<rows; i++)
{
int maximumInRow = arr[i][0];
for(int j=0; j<columns; j++)
{
if(arr[i][j] > maximumInRow)
{
maximumInRow = arr[i][j];
}
}

answer[i] = maximumInRow;
}

return answer;
}
}

25th August (Find Average in 2D array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppfd28n80oo2jonaoh

class Solution {
public double findAverage(int[][] arr) {
int rows = arr.length;
int columns = arr[0].length;

int totalElements = rows * columns;

// sum
double sum = 0.0;

for(int i=0; i<rows; i++)


{
for(int j=0; j<columns; j++)
{
sum = sum + arr[i][j];
}
}

return sum/totalElements;
}
}

25th August (Maximum One’s in 2D Array)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppjg2ktbbglwt0g8rn

class Solution {
public int maximumOnesRow(int[][] arr, int r, int c)
{

int maximumOnes = 0;
int indexOfRow = 0;

for(int i=0; i<r; i++) // Rows


{
// number of one's in the current row
int numberOfOnesInRow = 0;

for(int j=0; j<c; j++) // Columns


{
if(arr[i][j] == 1)
{
numberOfOnesInRow++;
}
}

// If current row one's, are they greater


than the current maximumOnes
if(numberOfOnesInRow > maximumOnes)
{
maximumOnes = numberOfOnesInRow;
indexOfRow = i;
}
}

return indexOfRow;
}
}

25th August (Reverse Array Using Two Pointer)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp98795pehes3ti2jh

class Solution {
public int[] reverseArray(int[] arr) {
// Your code goes here

int n = arr.length;
int i = 0;
int j = n-1;

while(i<j)
{
// interchange the elements
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

// i is moving in forward direction


i++;

// j is moving in backward direction


j--;
}

return arr;
}
}
28th August (Basic of Function and Method)
class Solution
{
// We have created a functionality
// - We are giving two integer number as input
// - OUTPUT -> return the sum
public int addTwoNumbers(int a, int b) // METHOD
{

// INPUT - a & b
// OUTPUT - Return the sum of numbers
// LOGIC

// LOGIC - Addition -> + (Add the numbers)


int sum = a + b;

// return will return value to the function


return sum;
}
}

class Main
{
public static void main(String arg[]) // FUNCTION
{
// RULE for creating object -
// ClassName objectName = new ClassName();
// Solution object = new Solution();
int c = Solution.addTwoNumbers(5,7);
System.out.println(c);
}
}

28th August (Basic of functions)


class Solution
{
// We have created a functionality
// - We are giving two integer number as input
// - OUTPUT -> return the sum
public int addTwoNumbers(int a, int b) // METHOD
{

// INPUT - a & b
// OUTPUT - Return the sum of numbers
// LOGIC

// LOGIC - Addition -> + (Add the numbers)


int sum = a + b;

// return will return value to the function


if(sum > 0)
{
return sum;
}
else
{
return 0;
}
}
}

class Main
{
public static void main(String arg[]) // FUNCTION
{
// RULE for creating object -
// ClassName objectName = new ClassName();
// Solution object = new Solution();
int c = Solution.addTwoNumbers(5,7);
System.out.println(c);
}
}
28th August (Add two numbers void function Example)
class Solution
{
// We have created a functionality
// - We are giving two integer number as input
// - OUTPUT -> return the sum
public void addTwoNumbers(int a, int b) // METHOD
{

// INPUT - a & b
// OUTPUT - Return the sum of numbers
// LOGIC

// LOGIC - Addition -> + (Add the numbers)


int sum = a + b;

System.out.println(sum);

return; // optional in nature (You don't return


anything in case of void)
}
}

class Main
{
public static void main(String arg[]) // FUNCTION
{

}
}

class Solution
{
// We have created a functionality
// - We are giving two integer number as input
// - OUTPUT -> return the sum
public void addTwoNumbers(int a, int b) // METHOD
{

// INPUT - a & b
// OUTPUT - Return the sum of numbers
// LOGIC

// LOGIC - Addition -> + (Add the numbers)


int sum = a + b;

System.out.println(sum);
return; // optional in nature (You don't return
anything in case of void)
}
}

class Main
{
public static void main(String arg[]) // FUNCTION
{
Solution object = new Solution();
object.addTwoNumbers(5, 6); // now line 6 is
running
}
}

28th August (Swap Two Number Question)


class Solution {
public void swap(int a, int b) {

int temp = a;
a = b;
b = temp;

// return type is void, we cannot return


anything
System.out.println(a);
System.out.println(b);
}
}

28th August (Prime Number Question Approach - 1)


class Solution {
public boolean isPrime(int num) {

if(num == 1)
{
return false;
}

// PRIME numbers are not divisible by any


number from 2 to num - 1
for(int i=2; i<= num-1; i++)
{
if(num%i == 0)
{
return false;
}
}

return true;
}
}

28th August (Prime Number Question Approach - 2)


class Solution {
public boolean isPrime(int num) {

int countOfDivisors = 0;

// PRIME numbers are not divisible by any


number from 2 to num - 1
for(int i=1; i<=num; i++)
{
if(num%i == 0)
{
countOfDivisors++;
}
}

if(countOfDivisors == 2)
{
return true;
}
else
{
return false;
}
}
}

28th August (Count Vowels Question)


class Solution {
public int countVowels(String s) {
// Your code goes here

int countOfVowels = 0;
int l = s.length();

for(int i=0; i<l; i++)


{
if(s.charAt(i) == 'a' || s.charAt(i) == 'e'
|| s.charAt(i) == 'i' ||
s.charAt(i) == 'o' || s.charAt(i) == 'u' ||
s.charAt(i) == 'A' ||
s.charAt(i) == 'E' || s.charAt(i) == 'I' ||
s.charAt(i) == 'O' ||
s.charAt(i) == 'U')
{
countOfVowels++;
}
}

return countOfVowels;
}
}

28th August (Basic of OOPs)


class Car
{
int speed; // global variable
int brake; // they are not shared by objects (These
values will be different for different object)
static int tyre; // they are shared by all the
objects

public void speedUp()


{
speed = 120;
int nitro = 5; // local variable
speed = speed * nitro;
}

public void printSpeed()


{
System.out.println(speed);
}

public void applyBrake()


{
brake = 1;
speed = 0;
}
}

public class Main


{
public static void main(String arg[])
{
// RULE - className objectName = new className();
Car obj1 = new Car();
obj1.speed = 100;
obj1.brake = 2;
obj1.tyre = 4;

Car obj2 = new Car();


obj2.speed = 200;
obj2.brake = 4;
obj2.tyre = 6;

System.out.println(obj1.tyre);
System.out.println(obj2.tyre);

System.out.println(obj1.speed);
System.out.println(obj1.brake);

System.out.println(obj2.speed);
System.out.println(obj2.brake);
}
}

28th August (Basic of OOPs - 2)


class Student
{
String name;
int rollNumber;
char section;
int classNumber;
// it will not do anything (DEFAULT CONSTRUCTOR)
// It is empty and won't do anything
public Student()
{

public Student()
{
name = "";
rollNumber = 0;
section = '';
classNumber = 0;
}

// PARAMETERIZED CONSTRUCTOR
public Student(String Name, int RollNumber, char
Section, int ClassNumber)
{
name = Name;
rollNumber = RollNumber;
section = Section;
classNumber = ClassNumber;
}

// - It is having same name as that of class name


// - It is helping to initialise the variables
directly
// - When object is created, constructor is
automatically
// - They don't return anything (They don't have
return type)
// - They cannot be static
}
public class Main
{
public static void main(String arg[])
{
Student firstStudent = new Student();
firstStudent.name = "Sandip";
firstStudent.rollNumber = 1;
firstStudent.section = 'A';
firstStudent.classNumber = 12;

Student secondStudent = new Student("ABIR", 2,


'A', 11);
}
}

30th August (Constructors - 1)


class Student
{
String name;
int rollNumber;
String schoolName;

public Student()
{
System.out.println("Default Constructor called");
}

public Student(String Name, int RollNumber, String


SchoolName)
{
System.out.println("Parameterized Constructor
called");
name = Name;
rollNumber = RollNumber;
schoolName = SchoolName;
}
public String getSchoolName()
{
return schoolName;
}
}

public class Main


{
public static void main(String[] args)
{
// Default Constructor will be called
Student obj1 = new Student();
obj1.schoolName = "ABC";

System.out.println(obj1.schoolName);
System.out.println(obj1.getSchoolName());

// Parameterized Constructor will be called


Student obj2 = new Student("A", 12, "XYZ");

System.out.println(obj2.schoolName);
System.out.println(obj2.getSchoolName());
}
}

30th August (Inheritance - 1)


class A
{
public void Method1()
{
System.out.println("Class A");
}
}

class B extends A
{
// If parent class has same method name as that of
child class
// Child class method will dominate
// Method overriding - Child class method dominates
over parent class method
public void Method1()
{
System.out.println("Class B");
}
}

public class Main


{
public static void main(String[] args)
{
A obj1 = new A();
obj1.Method1();

B obj2 = new B();


obj2.Method1();
}
}

30th August (Encapsulation - 1)


class Bank
{
// no other class expect the bank class can change
the property
private String userName;
private String password;

// no one can use it expect the class in which they


are present
private void Method() // non static
{
System.out.println("Method");
}

// no one can use it expect the class in which they


are present
private static void Function() // static
{
System.out.println("Function");
}
}

public class Main


{
public static void main(String[] args)
{
Bank obj = new Bank();
obj.userName = "123456789";
System.out.println(obj.userName);
obj.Method();
Bank.Function();
}
}

30th August (Encapsulation - 2)


class Bank
{
// no other class expect the bank class can change
the property
private String userName = "123456789";
private String password = "ABDCFEEDF";

public String getUserName()


{
return userName;
}
public String getPassword()
{
return password;
}

public void setUserName(String UserName)


{
if(UserName.length() >= 8) // other checks are
also present
{
userName = UserName;
}
}

public void setPassword(String Password)


{
if(Password.length() >= 8) // other checks are
also present
{
password = Password;
}
}
}

public class Main


{
public static void main(String[] args)
{
Bank obj = new Bank();
// System.out.println(obj.userName); userName
and password is private
System.out.println(obj.getUserName());
System.out.println(obj.getPassword());
}
}
30th August (Students Question - 1)
https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppff42tu6zwxtwjotz

class Student {

// here getter and setter are there so we are


creating properties
// private in nature
private String name;
private int age;
private String gender;

// this.something -> properties of the class


// this keyword will help us to identify the
properties of class

public Student(String name, int age, String gender)


{
this.name = name;
this.age = age;
this.gender = gender;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}

public String getGender() {


return gender;
}

public void setName(String name) {


this.name = name;
}

public void setAge(int age) {


this.age = age;
}

public void setGender(String gender) {


this.gender = gender;
}
}

30th August (Dog Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactpp9op9p0zfam8omgmk

class Dog {

private String name;


private String species;
private String breed;

public Dog(String name, String species, String


breed) {

this.name = name;
this.species = species;
this.breed = breed;
}

public String getBreed() {


return breed;
}
}

30th August (Animal and Constructor Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppr9lvpmwa22elv0c5
class Animal {

private String name;


private String species;

public Animal(String name, String species) {

this.name = name;
this.species = species;
}

public String getName() {


return name;
}

public String getSpecies() {


return species;
}
}

30th August (Animal Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppq7hce01vwnmbjt4z

// Create a class called "Animal" with the following


method:
// makeSound: a method that returns a string "Some
generic animal sound"

class Animal
{
public String makeSound()
{
return "Some generic animal sound";
}
}
// create two subclasses of Animal called "Dog" and "Cat"
with the following methods:
// subclasses - Means child class (Inheritance needs to
be used)
// makeSound: a method in the Dog class that returns a
string "Bark"
// makeSound: a method in the Cat class that returns a
string "Meow"

class Dog extends Animal


{
public String makeSound()
{
return "Bark";
}
}

class Cat extends Animal


{
public String makeSound()
{
return "Meow";
}
}

class Solution {

Animal animalObject;
Dog dogObject;
Cat catObject;

public Solution()
{
// RULE - objectName = new classname();
animalObject = new Animal();
dogObject = new Dog();
catObject = new Cat();
}

public String makeAnimalSound() {


return animalObject.makeSound();
}

public String makeDogSound() {


return dogObject.makeSound();
}

public String makeCatSound() {


return catObject.makeSound();
}
}

30th August (Animal I Question)


https://nextleap.app/course-dashboard/nlcfs4d3puzyj9tduradz/practice/
nlactppmvcn5vxn9f1z4p6x

// Create a class in Java called "Animal" with the


following properties:
// name: a string representing the animal's name
// species: a string representing the animal's species
class Animal
{
String name;
String species;

public Animal()
{

// a constructor that takes name and species and sets


the same to its properties
public Animal(String name, String species)
{
this.name = name;
this.species = species;
}

// getName: a method that returns the animal's name


public String getName()
{
return name;
}
}

// Then, create a subclass of Animal called "Dog" with


the following property: breed: a string representing the
dog's breed
// The Dog class should also have the following
constructor:
// Dog: a constructor that takes in the name, species,
and breed as parameters and sets
// them as the properties of the dog object

class Dog extends Animal


{
String breed;

public Dog()
{

public Dog(String name, String species, String breed)


{
this.name = name;
this.species = species;
this.breed = breed;
}
}

class Solution {

Animal animalObject;
Dog dogObject;

public Solution() {
animalObject = new Animal();
dogObject = new Dog();
}

public String AnimalName(String name, String


species) {
animalObject.name = name;
animalObject.species = species;

return animalObject.getName();

// animalObject = new Animal(name, species);


}

public String DogName(String name, String species,


String breed) {
dogObject = new Dog(name, species, breed);

return dogObject.getName();
}
}

30th August (Method Overriding Example)


class A
{
public static void print()
{
System.out.println("Class A");
}
}

class B extends A
{
// B class print function overrides A class function
public static void print()
{
System.out.println("Class B");
}
}

public class Main


{
public static void main(String arg[])
{
B.print();
}
}

30th August (Method Overloading Example)


class Calculator
{
public int sum(int a, int b)
{
return a+b;
}

public int sum(int a, int b, int c)


{
return a+b+c;
}
}

public class Main


{
public static void main(String arg[])
{
Calculator obj = new Calculator();
System.out.println(obj.sum(4, 5));

System.out.println(obj.sum(4, 5, 1));
}
}

1st Sept (Integer Wrapper Class)


public class Main
{
public static void main(String[] args)
{
String b = Integer.toString(5); // "5"

int a = 5;
int b = 6;

System.out.println(a < b);

System.out.println(Integer.compare(a,b));
}
}

1st Sept (ArrayList Data structure - 1)


import java.util.ArrayList;
// import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE -
// ArrayList<datatype> arrayListName = new
ArrayList<datatype>();
// USE of ArrayList - Dynamic size array

ArrayList<Integer> arr = new ArrayList<Integer>();

System.out.println(arr.size());

// add is used for inserting element into arraylist

arr.add(5);

int b = 10;
arr.add(b);

Integer c = 100;
arr.add(c);

// .size() - Find out number of elements in


arrayList
System.out.println(arr.size());

// .get(index) - Fetch the ith element


// System.out.println(arr.get(2));

// i = 0, 1, 2,
// i = 3 (STOP)
for(int i=0; i<arr.size(); i++)
{
System.out.print(arr.get(i) + " ");
}
}
}

1st Sept (ArrayList Data structure - 2)


import java.util.ArrayList;
// import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE -
// ArrayList<datatype> arrayListName = new
ArrayList<datatype>();
// ArrayList<datatype> arrayListName = new
ArrayList<>();

// USE of ArrayList - Dynamic size array

ArrayList<Integer> arr = new ArrayList<>();

System.out.println(arr.size());

// add is used for inserting element into


arraylist
arr.add(5);

int b = 10;
arr.add(b);

Integer c = 100;
arr.add(c);

// .size() - Find out number of elements in


arrayList
System.out.println(arr.size());

// .get(index) - Fetch the ith element


// System.out.println(arr.get(2));

System.out.println(arr);

// i = 0, 1, 2,
// i = 3 (STOP)
for(int i=0; i<arr.size(); i++)
{
System.out.print(arr.get(i) + " ");
}

}
}

1st Sept (Print ArrayList Question)


class Solution {
public void arrayListAddExercise() {
// Your code goes here

ArrayList<String> list = new ArrayList<>();

// Inserting the elements


list.add("Apple");
list.add("Banana");
list.add("Orange");
list.add("Mango");

for(int i=0; i<list.size(); i++)


{
System.out.println(list.get(i));
}

}
}

1st Sept (Print ArrayList Size Question)


class Solution {
public void arrayListSize() {
// Your code goes here

ArrayList<Integer> list = new ArrayList<>();


list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

System.out.println(list.size());

}
}

1st Sept (ArrayList - 3)


import java.util.ArrayList;
// import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE -
// ArrayList<datatype> arrayListName = new
ArrayList<datatype>();
// ArrayList<datatype> arrayListName = new
ArrayList<>();

// USE of ArrayList - Dynamic size array

ArrayList<Integer> arr = new ArrayList<>();

System.out.println(arr.size());

// add is used for inserting element into


arraylist
arr.add(5);

int b = 10;
arr.add(b);
Integer c = 100;
arr.add(c);

System.out.println(arr);

// .remove(index) - Element at that index will


be removed

arr.remove(2);
arr.remove(0);

// .size() - Find out number of elements in


arrayList
System.out.println(arr.size());

// .get(index) - Fetch the ith element


// System.out.println(arr.get(2));

System.out.println(arr);

// i = 0, 1, 2,
// i = 3 (STOP)
for(int i=0; i<arr.size(); i++)
{
System.out.print(arr.get(i) + " ");
}
}
}

1st Sept (LinkedList - 1)


import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE -
// LinkedList<datatype> linkedListName = new
LinkedList<>();

LinkedList<Integer> a = new LinkedList<>();

System.out.println(a.size());

a.add(1);
a.add(3);
a.add(5);

System.out.println(a.size());

System.out.println(a);

a.remove(1);

for(int i=0; i<a.size(); i++)


{
System.out.print(a.get(i) + " ");
}
}
}

1st Sept (LinkedList - 2)


import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE -
// LinkedList<datatype> linkedListName = new
LinkedList<>();

LinkedList<Integer> a = new LinkedList<>();


System.out.println(a.size());

a.add(1);
a.add(3);
a.add(5);

System.out.println(a.getLast());
System.out.println(a.get(a.size()-1));

System.out.println(a.size());

System.out.println(a);

a.remove(1);

for(int i=0; i<a.size(); i++)


{
System.out.print(a.get(i) + " ");
}
}
}

1st Sept (TreeSet/HashSet - 1)


import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE - HashSet<datatype> setName = new
HashSet<datatype>();

TreeSet<Integer> s = new TreeSet<>();

// TreeSet - arranging the element in ascending


order
// HashSet - elements are arranged in random
order

s.add(1);
s.add(2);
s.add(2);
s.add(2);
s.add(3);
s.add(3);
s.add(-100);
s.add(0);
s.add(100);

System.out.println(s.size());

// Collections.reverse(s); --> Reverse


arrayList or Linked List

for(Integer i: s) // forEach (There is no


concept of indexing )
{
System.out.print(i + " ");
}

}
}

1st Sept (LinkedHashMap - 1)


import java.util.*;
public class Main
{
public static void main(String[] args)
{
// RULE -
// Key - Value (Both can have different data
types)
// datatype1 - datatype of Key
// datatype2 - datatype of Value

// LinkedHashMap<datatype1, datatype2>
hashmapName = new LinkedHashMap<>();

LinkedHashMap<Integer,String> hmap = new


LinkedHashMap<>();

hmap.put(1, "ABC");
hmap.put(2, "DEF");
hmap.put(5, "XYZ");

System.out.println(hmap.get(50));

hmap.put(5, "ZZZ");
hmap.put(5, "ABCD");

// keys are unique in Hashmap


System.out.println(hmap.size());

// get(key) -> value associated with the key


System.out.println(hmap.get(1));

System.out.println(hmap.keySet());

// hmap.keySet() -> It will give us all the keys

for(Integer key : hmap.keySet())


{
System.out.println(key + " -> " +
hmap.get(key));
}
}
}
1st Sept (LinkedHashMap Question)
class Solution {
public String getValueAssociatedWithKey(String key)
{
// Your code goes here

LinkedHashMap<String, String> hmap = new


LinkedHashMap<>();

hmap.put("Key1", "Value1");
hmap.put("Key2", "Value2");
hmap.put("Key3", "Value3");

return hmap.get(key);
}
}

1st Sept (Abstract Class Example)


abstract class Instagram
{
// It will not have any Implementation in abstract
class
// abstract in nature
abstract void Post();

// non - abstract in nature


public void UploadPhoto()
{
// logic
System.out.println("Post on Instagram Logic");
}
}

// Implement abstract class - extends


// Implement interface - implements
class Implementation extends Instagram
{
public void Post()
{
// logic
System.out.println("Post on Instagram Logic");
}
}

public class Main


{
public static void main(String[] args)
{
Implementation obj = new Implementation();
}
}

1st Sept (Interface Example)


interface Instagram
{
void Post();

void UploadPhoto();
}
// Implement abstract class - extends
// Implement interface - implements

class Implementation implements Instagram


{
public void Post()
{
// logic
System.out.println("Post on Instragram Logic");
}

public void UploadPhoto()


{
// logic
}
}

public class Main


{
public static void main(String[] args)
{
Implementation obj = new Implementation();
Instagram obj1 = new Implementation();
obj.Post();
}
}

1st Sept (Animal/ Interface Question)


// Create an interface in Java called "Animal" with the
following
// methods: getName: a method that returns the name of
the animal as a string
// speak: a method that returns the sound the animal
makes as a string

interface Animal
{
String getName();

String speak();
}

// Then, create a class called "Dog" that implements the


Animal
// interface and has the following property: breed: a
string representing
// the breed of the dog
class Dog implements Animal
{
String breed;
String name;

Dog(String name, String breed)


{
this.name = name;
this.breed = breed;
}

// The Dog class should have the following methods:


// getBreed: a method that returns the breed of the
dog as a string
public String getBreed()
{
return breed;
}

// Implementing the interface


public String getName()
{
return name;
}

public String speak()


{
return "Woof!";
}
}

class Solution {

Dog dogObject;
// Animal animalInterface;
public Solution(String name, String breed) {
dogObject = new Dog(name, breed);
// animalInterface = new Dog(name, breed);
}

public String getName()


{
// return animalInterface.getName();
return dogObject.getName();
}

public String getBreed()


{
return dogObject.getBreed();
}

public String speak()


{
// return animalInterface.speak();
return dogObject.speak();
}
}

You might also like