Menu

Generate a random number between 1 to n

Generate a lucky number(random number), between 1 to n using Java. 

Below is the Java code to choose a lucky number between two numbers.

import java.util.Random;

class Main {
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(6) + 1 ; // Generates number between 1 and 6
        System.out.println("Lucky number is: " + randomNumber);
    }
}

What is Waste | Classification and Definition of Waste

Waste refers to any material, substance, or activity that is no longer useful, needed, or productive, and is typically discarded. Waste can come from households, industries, nature, or even digital systems.


How Do We Identify Waste?

You can identify waste by asking yourself following questions:

  • Is it adding value?

  • Is it being used efficiently?

  • Can it be reused, recycled, or avoided?

  • Does it lead to unnecessary cost, pollution, or effort?

If the answer is no value, no use, or negative impact, it is likely waste.


Types of waste

Type of Waste Description Examples
Solid Waste Tangible, physical waste from homes, offices, and industries Food scraps, plastic, paper, glass, packaging
Liquid Waste Waste in liquid form from households and industries Sewage, chemicals, oils, wastewater
Organic Waste Biodegradable waste that comes from plants or animals Food waste, garden waste, manure
Recyclable Waste Materials that can be processed and reused Paper, cardboard, metals, glass, certain plastics
Hazardous Waste Harmful to health or environment; needs special handling Batteries, chemicals, pesticides, medical waste
Electronic Waste (E-waste) Discarded electronic items and components Phones, computers, TVs, chargers, printers
Biomedical Waste Waste generated by healthcare facilities Syringes, surgical tools, infected dressings
Industrial Waste By-products of industrial processes Slag, chemical solvents, factory scraps
Construction & Demolition Waste Debris from building or tearing down structures Bricks, wood, concrete, metal rods
Radioactive Waste Waste from nuclear power or research Nuclear fuel rods, isotopes, contaminated tools
Digital Waste Useless or outdated digital data consuming space and resources Spam emails, unused files, inactive apps
Time/Process Waste (Lean) Activities that do not add value in a workflow Waiting time, rework, overproduction


Why it matters?

  1. Environmental ProtectionProper waste disposal prevents pollution of air, water, and soil, protecting ecosystems and wildlife.
  2. Public Health & Safety: Poorly managed waste (especially biomedical and hazardous) can spread diseases, contaminate water sources, and harm sanitation workers.
  3. Economic Efficiency: Reducing, reusing, and recycling waste helps save production and disposal costs and creates opportunities for sustainable industries.
  4. Resource ConservationRecycling preserves natural resources like metals, water, timber, and minerals, reducing the need for raw material extraction.
  5. Climate Change MitigationWaste in landfills generates methane, a potent greenhouse gas. Reducing and recycling waste lowers emissions.
  6. Regulatory ComplianceFollowing proper waste management practices helps businesses and municipalities meet legal and environmental regulations. 
  7. Cleaner and Safer CommunitiesWell-managed waste systems result in cleaner streets, reduced litter, and improved urban living conditions.
  8. Infrastructure EfficiencyReduces burden on landfills, sewage systems, and waste processing facilities—making city infrastructure more sustainable.
  9. Green Job CreationRecycling and upcycling industries generate employment, supporting circular economy models. 
  10. Awareness and EducationUnderstanding waste helps people make more conscious consumption decisions and engage in responsible behavior.

Cognitive Complexity

Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.

A developer can reduce the Cognitive Complexity in following ways.

  • Deep nesting: Use early returns or guard clauses
  • Repeated logic: Extract into helper functions
  • Multiple concerns: Break the method into smaller methods
  • Verbose conditions: Use descriptive variable/method names


A Java code example with high cognitive complexity

This is a Java code example, that is nested, hard-to-read method that checks prime numbers, counts them, and handles edge cases.

public int countPrimes(int[] numbers) {
    int count = 0;
    for (int i = 0; i < numbers.length; i++) {
        if (numbers[i] > 1) {
            boolean isPrime = true;
            for (int j = 2; j < numbers[i]; j++) {
                if (numbers[i] % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                count++;
            }
        } else {
            if (numbers[i] == 0) {
                System.out.println("Zero found");
            } else {
                System.out.println("Negative or One found");
            }
        }
    }
    return count;
}


Refactored the above java code (low congitive complexity)


public int countPrimes(int[] numbers) {
    int count = 0;
    for (int num : numbers) {
        if (isPrime(num)) {
            count++;
        } else {
            handleNonPrime(num);
        }
    }
    return count;
}

private boolean isPrime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i < num; i++) {
        if (num % i == 0) return false;
    }
    return true;
}

private void handleNonPrime(int num) {
    if (num == 0) {
        System.out.println("Zero found");
    } else {
        System.out.println("Negative or One found");
    }
}