Java How To Generate Random Numbers

How To Generate a Random Number

You can use Math.random() method to generate a random number.

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive): Continue reading Java How To Generate Random Numbers

Java How To Find the Square Root of a Number

Square Root of a Number

You can use Math.sqrt() to find the square root of a number:

Example

Find the square root of 64:

Math.sqrt(64);

Continue reading Java How To Find the Square Root of a Number

Java How To Find Positive or Negative Numbers

Find Out if a Number is Positive or Negative

Find out if a number is even or odd:

Example

int myNum = 10; // Is this a positive or negative number?

if (myNum > 0) {
  System.out.println("The value is a positive number.");
} else if (myNum < 0) {
  System.out.println("The value is a negative number.");
} else {
  System.out.println("The value is 0.");
}

Continue reading Java How To Find Positive or Negative Numbers

Java How To Find Even or Odd Numbers

Check Whether a Number is Even or Odd

Find out if a number is even or odd:

Example

int number = 5;

// Find out if the number above is even or odd
if (number % 2 == 0) {
  System.out.println(number + " is even.");
} else {
  System.out.println(number + " is odd.");
}

Continue reading Java How To Find Even or Odd Numbers

Java How To Get the Area of a Rectangle

Area of Rectangle

The area of a rectangle can be found by multiplying the length of the rectangle by the width:

Example

int length = 5; 
int width = 2; 
int area = length * width; 
System.out.println("Area of rectangle: " + area); 

Continue reading Java How To Get the Area of a Rectangle

Java How To Loop Through an Enum

Loop Through an Enum

The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum:

Example

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

public class Main { 
  public static void main(String[] args) { 
    for (Level myVar : Level.values()) {
      System.out.println(myVar);
    }
  } 
}

Continue reading Java How To Loop Through an Enum

Java How To Loop Through a HashMap

Loop Through a HashMap

Loop through the items of a HashMap with a for-each loop.

Note: Use the keySet() method if you only want the keys, and use the values() method if you only want the values: Continue reading Java How To Loop Through a HashMap

Java How To Loop Through an ArrayList

Loop Through an ArrayList

Loop through the elements of an ArrayList:

Example

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Continue reading Java How To Loop Through an ArrayList

Java How To Find the Smallest Element in an Array

How To Find the Smallest Element in an Array

Create a program that finds the lowest age among different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

// Create a 'lowest age' variable and assign the first array element of ages to it
int lowestAge = ages[0];

// Loop through the elements of the ages array to find the lowest age
for (int age : ages) {
  // Check if the current age is smaller than the current 'lowest age'
  if (lowestAge > age) {
    // If the smaller age is found, update 'lowest age' with that element
    lowestAge = age;
  }
}

// Output the value of the lowest age
System.out.println("The lowest age in the array is: " + lowestAge);

Continue reading Java How To Find the Smallest Element in an Array

Java How To Find the Average of Array Elements

How To Calculate the Average of Array Elements

Create a program that calculates the average of different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;

// Get the length of the array
int length = ages.length;

// Loop through the elements of the array
for (int age : ages) {
  sum += age;
}

// Calculate the average by dividing the sum by the length
avg = sum / length;

// Print the average
System.out.println("The average age is: " + avg);

 

Continue reading Java How To Find the Average of Array Elements