Java Source Code: A Recursive Asterisk Diamond Shape Program

Excellent Condition Iphone 4 8gb LIKE NEW! Cheap Price!!
Current Bid: \$46.00

Below is the Java source code on getting a diamond shape of asterisks using recursion. Recursion is way that repeat the codes over and over until the condition is met. It is like the for, while and do-while loops. This program I made on outputting a diamond shape asterisk prompts for the user to enter the number and the program will output the asterisks in diamond shape. The size of the diamond depends on the number that the user will enter. You can study and trace the code below.

Java Source Code: A Recursive Asterisk Diamond Shape

//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; iJava Tips and Tutorials

Java Tutorial Examples
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Simple Codes for Beginners
How to Program in Java: Complete Simple Easy Steps
Basic Knowledge Required in Programming
5 Important Tips to Learn Java Programming and Other Programming Languages

Note: You can revise the program by changing the dashes into spaces, I just use the dash in order to show the diamond shape generated by the program.

Sample Output 1:

Enter a number: 3

The shape for this is:

—*–

–*-*–

-*-*-*–

–*-*–

—*–

Sample Output 2:

Enter a number: 4

The shape for this is:

—-*–

—*-*–

–*-*-*–

-*-*-*-*–

–*-*-*–

—*-*–

—-*–

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: \$4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: \$4.99

Sample Output 3:

Enter a number: 5

The shape for this is:

—–*–

—-*-*–

—*-*-*–

–*-*-*-*–

-*-*-*-*-*–

–*-*-*-*–

—*-*-*–

—-*-*–

—–*–

Java Books

Java Programming : From Problem Analysis to Program Design by D. S. Malik…
Current Bid: \$2.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: \$50.80

Other Java Source Code Example

Java Simple Codes for Beginners
A Java source code: How to Output Asterisk in Different Forms Using Recursion
Java Source code: How to Add Numbers inside an Array using For Loop
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code: How to Sort Numbers using Selection Sort
Java Source Code: How to Sort Numbers in Bubble Sort
Java Source code: How to Print a String Backward using Recursion
Java Source Code: Binary Search in Recursion
Java Source Code on a Recursive Linear Search
Java source code: Recursive function for X to the power Y
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source Code: How to make a Program that will determine a Person’s Salutation and Current Age
Java Source Code: Recursive Snow Flakes

Do You Like Java Programming?

Yes
No
See results without voting