Print Different Asterisk Shapes in java


//main method
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk2(n);
}
}
//java class
public class Asterisk1 {
private int count = 0;
public void asterisk1(int n) {
if(n==0) {
System.out.println();
}
else {
System.out.print("*");
asterisk1(n-1); } }
public void asterisk2( int n) {
if(n==0){
return; }
else {
asterisk1(n);
asterisk2(n-1);
}
}
}
This program will output the asterisk in this form. If the number 4 is entered the form will be like this.
****
***
**
*
2. A Java source code for another Asterisk Form
/*The following code will have the same method as the first but different in implementation*/ //main method
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk2(n,1);
}
}
// java class
public class Asterisk1 {
private int count = 0;
public void asterisk1(int n) {
if(n==0) {
System.out.println();
}
else {
System.out.print("*");
asterisk1(n-1);
}
}
public void asterisk3(int count,int m) {
if( count == 0) {
System.out.println();
} else {
asterisk1(m);
asterisk3(n-1,m+1);
}
}
}
The output of this code if number 4 is entered will be like this.
*
**
***
****

To get a form that would look like this,
****
***
**
*
*
**
***
****
3. Here is the other code.

//main method
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 n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk2(n); access.asterisk3(n,1);
}
}
// java class
public class Asterisk1 {
private int count = 0;
public void asterisk1(int n) {
if(n==0) { System.out.println();
} else {
System.out.print("*");
asterisk1(n-1);
}
}
public void asterisk2( int n) {
if(n==0) {
return;
} else {
asterisk1(n);
asterisk2(n-1);
}
}
public void asterisk3( int n,int m)
{ if( n == 0) {
System.out.println();
} else {
asterisk1(m);
asterisk3(n-1,m+1);
}
}
}
4. Lastly, to acquire a shape like this,
*
**
***
****
****
***
**
*
here is the code:
//main method
package prog_proj2;
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 n = input.nextInt();
Asterisk1 access = new Asterisk1();
access.asterisk3(n); access.asterisk2(n);
}
}
//java class
public class Asterisk1 {
private int count = 0;
public static void asterisk1(int n) {
if(n==0) { System.out.println();
} else {
System.out.print("*");
asterisk1(n-1);
}
}
public void asterisk2( int n) {
if(n==0) { return;
} else {
asterisk1(n);
asterisk2(n-1);
}
}
public void asterisk3( int n) {
count++;
if( n == 0) {
return;
} else {
asterisk1(count);
asterisk3(n-1);
}
}
}

1 comments:

Anonymous said...

please show how to obtain full shapes please, please , please: oval, diamond, arrow and box

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|