Different Types of shapes printing in java… .. .

For ┬ádoing below stuff there’s no need of going into Java 2D graphics et c..advanced topics. U can implement more user Interface attractive shapes by using 2D graphics.

But these stuff is quite useful to crack system test somewhat ,Hope it will assist you … .. .



1. square:
========

package p;

public class p
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if((i==2&&(j==2||j==3))||(i==3&&(j==2||j==3)))
{
System.out.print(“\t”);
}
else
{
System.out.print(“*”);
System.out.print(“\t”);}
}
System.out.println(“\n”);
}
}
}
output:
=====

op3

————————–THE_END——————————————


2.Triangle:
=======

code:
—–

package p;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class p3 {
public static void main ( String arg[] ){
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader read = new BufferedReader(istream) ;
System.out.print(“Enter Triangle Size : “);
int num=0;
try{
num=Integer.parseInt( read.readLine() );
} catch(Exception Number){
System.out.println(“Invalid Number!”);
}
for(int i=1;i<=num;i++){
for(int j=1;j<num-(i-1);j++){
System.out.print(” “);
}
for(int k=1;k<=i;k++){
System.out.print(“*”);
for(int k1=1;k1<k;k1+=k){

System.out.print(“*”);
}
}
System.out.println();
}
}
}
output:
——-
Enter Triangle Size : 5
*
***
*****
*******
*********

ii)

code:
====

package p;

public class p5 {

public static void main(String[] args) {
int c=0;
for (int i =1;i<5;i++) {
for (int j =5;j>i;j–) {
System.out.print(” “);
}
for (int j =1; j <=i; j++) {
System.out.print(++c);
}

for (int j =1; j <i; j++) {
System.out.print(–c);
}
System.out.println();
}
}
}

output:
=====

1
232
34543
4567654

iii)

code:
—–
package p;
class p6
{
public static void main(String[] args)
{
//Scanne in = new Scanner(System.in);
int h=10;
// int h=in.nextInt(); //height <- read input
for(int i=0;i<h;i++){
for(int s=1;s<h-i;s++) //spacing
System.out.print(“-“);
for(int j=0;j<=i*2;j++){
if(j==i*2 || j==0)
System.out.print(“X”); //first and last pos
else if(i==h-1)
System.out.print(“X”); //last line
else
System.out.print(“-“); //middle
}
System.out.println(); //new line
}
}
}

output:
——-
———X
——–X-X
——-X—X
——X—–X
—–X——-X
—-X———X
—X———–X
–X————-X
-X—————X
XXXXXXXXXXXXXXXXXXX

iv)

code:
——

package p;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class p8 {
public static void main ( String arg[] ){
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader read = new BufferedReader(istream) ;
System.out.print(“Enter Triangle Size : “);
int num=0;
try{
num=Integer.parseInt( read.readLine() );
} catch(Exception Number){
System.out.println(“Invalid Number!”);
}
for(int i=1;i<=num;i++){
for(int j=1;j<=num-(i-1);j++){
System.out.print(“*”);
if(j==num-(i-1)){
for(int k=1;k<=i;k+=1){
System.out.print(” “);
}
for(int s=1;s<i;s++){
System.out.print(” “);
}
for(int l=1;l<=num-(i-1);l++){
System.out.print(“*”);
}
}
}
System.out.println();
}
}
}
output:
——-

Enter Triangle Size : 7
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
v)

code:
—–
package p;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class p9
{
int count = 0;
public static void asterisk1(int n)
{
if(n==0)
{
System.out.println();
}
else
{
System.out.print(“*”);
asterisk1(n-1);
}
}
public static void asterisk2( int n)
{
if(n==0){
return; }
else
{
asterisk1(n);
asterisk2(n-1);
}
}
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Please enter size : “);
String no = null;
try
{
no = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(“You entered : ” + no);
int n1=Integer.parseInt(no);
asterisk1(n1);
asterisk2(n1);

}
}
output:
——-

Please enter size : 9
You entered : 9
*********
*********
********
*******
******
*****
****
***
**
*

vi)

code:
====

package p;
import java.util.Scanner;
public class p9
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter a number: “);
int n = input.nextInt();
p91 access = new p91();
access.asterisk3(n); access.asterisk2(n);
}
}
class p91
{
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);
}
}
}

output:
=====

Enter a number: 10
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*

————————–THE_END——————————————


3.circle:
——-

package p;
public class p9 {

public static void main(String[] args) {
DrawMeACircle(3, 3, 3); // should print to the screen an ellipse looking circle
}

public static void DrawMeACircle(double posX, double posY, double radius) {

double xaxis = 20; // scanning the coordinates
double yaxis = 20; // ” ”

for (double x = 0; x < xaxis; x++) {
for (double y = 0; y < yaxis; y++) {

//using the formula for a cicrle
double a = Math.abs((posX – x) * (posX – x));
double b = Math.abs((posY – y) * (posY – y));

double c = Math.abs(a + b);
double d = Math.abs(radius * radius);

// checking if the formula stands correct at each coordinate scanned
double arbitraryNumber = 2;
if ( Math.abs(c – d) < arbitraryNumber)
{

System.out.print(‘@’);
}
else {
System.out.print(‘ ‘);
}
}
System.out.println();
}
}
}

output:
=====

op2

————————THE_END——————————————

package p;

class p11
{
public static void main(String ar[])
{
int i,j,n=4,n1=2;

for(i=1;i<=4;i++)
{
for(int sp=0;sp<=n;sp++)
{
System.out.print(” “);
}

for(j=1;j<=i;j++)
{
System.out.print(“* “);
}

n–;
System.out.print(“\n”);
}

for(i=3;i>=1;i–)
{
for(int sp=0;sp<=n1;sp++)
{
System.out.print(” “);
}

for(j=i;j>=1;j–)
{
System.out.print(“* “);
}

n1++;
System.out.print(“\n”);
}
}
}
(or)
public class StarPattern
{
public static void main(String[] args)
{
System.out.print(“Enter no of row u want in star pattern : “);
Scanner in = new Scanner(System.in);
int num=in.nextInt();
for(int i=1;i<=num;i++)
{
for(int j=num;j>=i;j–)
{
System.out.print(” “);
}
for(int m=1;m<=i;m++)
{
System.out.print(” *”);
}
System.out.print(“\n”);
}
for(int i=1;i<=num;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(” “);
}
for(int m=num;m>=i;m–)
{
System.out.print(” *”);
}
System.out.print(“\n”);
}
}

}
output:

op

————————————THE_END——————