Skip to main content

Posts

Showing posts from October, 2018

How i+=j works in Java?It will surprise you?

According to the specification of Java for mathematical operations ,  If the left-hand operand expression is not an array access expression, then:
First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.Otherwise, the result of the binary operation is converted to the…

Java : Most Frequently Asked Star Patterns in Coding Interview

Today we are you going to learn some coding tricks to show the patterns on console using stars (*). Let jump into the sections.
1. Christmas Tree : 
public class ChristmasTree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
        System.out.println("Christmas tree drawing program.");
        System.out.print("Enter the height: ");
        int n = sc.nextInt();
        System.out.print("Enter the levels: ");
        int level = sc.nextInt();
if (n < 1) {
System.out.println("Height must be positive number.");
} else if (level < 1) {
System.out.println("Number of levels must be positive");
} else {
for (int i = 0; i < level; i++) {
printStars(n);
}
}
        sc.close();
    }
private static void printStars(int height) {
for (int j = 0; j < height; j++) { // Number of rows!!!
for (int k = j; k < height - 1; k++) { // For printing the spaces!!!
System.out.print(" ");
}
for (int m = 0; m <= (j * 2); m++) {…