Tuesday, April 16, 2013

A Recursive Asterisk Diamond Shape : JAVA OOP

Write Java Source Code: A Recursive Asterisk Diamond Shape

Sample Output 1:
Enter a number: 3
The shape for this is:
---*--
--*-*--
-*-*-*--
--*-*--
---*--
Sample Output 2:
Enter a number: 4
The shape for this is:
----*--
---*-*--
--*-*-*--
-*-*-*-*--
--*-*-*--
---*-*--
----*--

Sample Output 3:
Enter a number: 5
The shape for this is:
-----*--
----*-*--
---*-*-*--
--*-*-*-*--
-*-*-*-*-*--
--*-*-*-*--
---*-*-*--
----*-*--
-----*--

Source Code
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//java class
public class Diamond
{
    public String Diamond_Asterisk(int num) //method1
    {
        if(num>0)
        {
            return "*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }
     public String Diamond_Asterisk2(int num)//method2
    {
        if(num>0)
        {
            return "-*-" + Diamond_Asterisk(num-1);//access method1
        }
        else
        {
            return "-";
        }
    }
    public String Space(int num) //method3
    {
        if(num>0)
        {
            return "-" + Space(num-1);
        }
        else
        {
            return "-";
        }
    }
    public void DiamondResult(int num)//method4
    {
        for(int i=1; i<num; i++)
        {
            System.out.print(Space(num-i));//access method3
             System.out.println(Diamond_Asterisk(i));//access method1
             
        }
         for(int i=0; i<num; i++)
        {
            System.out.println(Diamond_Asterisk2(num-i));//access method2
            System.out.print(Space(i));//access method3
        }
      
    }
     
}
//main class
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = input.nextInt();
        Diamond access = new Diamond();
        System.out.println("The shape for this is: ");
        access.DiamondResult(num);
         
    }
}

3 comments:

  1. *
    **
    ***
    ****
    *****

    WHAT IS THE CODE OF THIS?

    ReplyDelete
  2. Mohammad Ashraful Hasan Sobuj
    My Batch- 46th
    My Department :CSE
    My ID 201420656


    public class Green
    {
    public String Green_Asterisk(int num)
    {
    if(num>0)
    {
    return "*-" + Green_Asterisk(num-1);
    }
    else
    {
    return "-";
    }
    }
    public String Green_Asterisk2(int num)
    {
    if(num>0)
    {
    return "-*-" + Green_Asterisk(num-1);
    }
    else
    {
    return "-";
    }
    }
    public String Space(int num)
    {
    if(num>0)
    {
    return "-" + Space(num-1);
    }
    else
    {
    return "-";
    }
    }
    public void GreenResult(int num)
    {
    for(int i=1; i<num; i++)
    {
    System.out.print(Space(num-i));
    System.out.println(Green_Asterisk(i));
    }
    for(int i=0; i<num; i++)
    {
    System.out.println(Green_Asterisk2(num-i));
    System.out.print(Space(i));
    }
    }
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int num = input.nextInt();
    Green access = new Green();
    System.out.println("The shape for this is: ");
    access.GreenResult(num);
    }
    }

    ReplyDelete