+ -

Thursday, 29 May 2014

Print Star( * ) Patterns

Here are some tricks to print * pattern

If you can print pattern like this then you are genius

NOTE : Now all you need to print any other pattern is just satisfy a condition before printing *

Java code to generate above pattern is given below.
save it with name Pattern.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package com.trickyjava.pattern;

public class Pattern {

 public static void main(String[] args) {
  for (int i = 1; i <= 5; i++) {
   for (int j = 1; j <= 5; j++) {
    System.out.print(" * ");
   }
   System.out.println();
  }
 }

}

In above pattern first loop for (int i = 1; i <= 5; i++)  used for rows
and second loop for (int j = 1; j <= 5; j++) is used for columns

Now if we want to print pattern like this
for this we need to put a condition before printing *

So now we will learn how to make condition to print pattern

Here is explanation for the above pattern 
NOTE : we will make condition based on ( j + i ) and ( j - i )  for any pattern.
Now java code to print the above pattern is


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package com.trickyjava.pattern;

public class Pattern {

 public static void main(String[] args) {
  for (int i = 1; i <= 5; i++) {
   for (int j = 1; j <= 5; j++) {
    if (i + j <= 6) {             
     System.out.print(" * ");
    } else {                        
     System.out.print("   ");
    }                               
   }
   System.out.println();
  }
 }

}

Here are some other patterns you can try.

Change the condition for the required pattern




Some more patterns

Change no. of rows or columns to print required pattern





5 Tricky {Java}: May 2014 Here are some tricks to print * pattern If you can print pattern like this then you are genius NOTE : Now all you need to print any...

Tuesday, 27 May 2014

Stack Implementation without using Array

Here is simple code to implement Stack without using array.

This is Stack.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.trickyjava;

public class Stack<T> {
     private StackElement<T> top;

     public T pop() throws Exception {
          if (null == top) {
             throw new Exception("No element in stack");
          }
          T element = top.getElement();
          top = top.getPrevious();
          return element;
     }

     public void push(T element) {
          if (null == top) {
             top = new StackElement<T>(null, element);
             return;
          }
          top = new StackElement<T>(top, element);
     }

     public void empty() {
          top = null;
     }
  
     public boolean hasMoreElements() {
          return null != top;
     }
  
     private class StackElement<T> {
  
          private StackElement<T> previous;
          private T element;
    
          public StackElement(StackElement<T> previous, T element) {
             this.previous = previous;
             this.element = element;
          }
    
          public T getElement() {
             return element;
          }
    
          public StackElement<T> getPrevious() {
             return previous;
          }
    }
}


This is code how you will use it.
This is Application.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.trickyjava;

public class Application {

     public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("This");
        stack.push("is");
        stack.push("stack");
        stack.push("implementation");
        stack.push("without");
        stack.push("using");
        stack.push("array");

        while (stack.hasMoreElements()) {
           try {
              System.out.println(stack.pop());
           } catch (Exception e) {
              e.printStackTrace();
           }
        }
    }

}

This is Output
array
using
without
implementation
stack
is
This
5 Tricky {Java}: May 2014 Here is simple code to implement Stack without using array. This is   Stack.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

Welcome to www.TrickyJava.com

Hi This is my very first post on www.TrickyJava.com . So basically this blog is for tricky codes in java and java code related problems and solutions. Thanks for support and contribution. 

5 Tricky {Java}: May 2014 Hi This is my very first post on www.TrickyJava.com  . So basically this blog is for tricky codes in java and java code related problems a...
<

Followers