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
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
You can use Math.sqrt()
to find the square root of a number:
Find the square root of 64:
Math.sqrt(64);
Continue reading Java How To Find the Square Root of a Number
Find out if a number is even or odd:
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
Find out if a number is even or odd:
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.");
}
The area of a rectangle can be found by multiplying the length of the rectangle by the width:
int length = 5;
int width = 2;
int area = length * width;
System.out.println("Area of rectangle: " + area);
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:
enum Level {
LOW,
MEDIUM,
HIGH
}
public class Main {
public static void main(String[] args) {
for (Level myVar : Level.values()) {
System.out.println(myVar);
}
}
}
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
Loop through the elements of an ArrayList
:
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);
}
}
}
Create a program that finds the lowest age among different ages:
// 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
Create a program that calculates the average of different ages:
// 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