I need to write a Java program that prints out the following triangles exactly.
ID: 3884628 • Letter: I
Question
I need to write a Java program that prints out the following triangles exactly.
Here is the parameters of the Java program:
package assignment1;
public class Assignment1 {
private final int HEIGHT = 11;
public enum Patterns{
bottomLeftTriangle,
bottomRightTriangle,
topLeftTriangle,
topRightTriangle,
centerTriangle,
};
// Write details of your algorithm
void printCenter(char ch){
}
// Write details of your algorithm
void printBottomLeft(char ch){
}
// Write details of your algorithm
void printTopLeft(char ch){
}
// Write details of your algorithm
void printBottomRight(char ch){
}
// Write details of your algorithm
void printTopRight(char ch){
}
/*
* This function according to the pattenId calls the appropriate print function
* with the input ch. Default printTopLeft is called.
*
*/
public void printPattern(Patterns patternId, char ch){
switch (patternId){
case bottomLeftTriangle:
this.printBottomLeft(ch);
break;
case bottomRightTriangle:
this.printBottomRight(ch);
break;
case topLeftTriangle:
this.printTopLeft(ch);
break;
case topRightTriangle:
this.printTopRight(ch);
break;
case centerTriangle:
this.printCenter(ch);
break;
default:
this.printTopLeft(ch);
break;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Assignment1 ass1 = new Assignment1();
ass1.printPattern(Assignment1.Patterns.bottomLeftTriangle, '*');
ass1.printPattern(Assignment1.Patterns.bottomRightTriangle, '#');
ass1.printPattern(Assignment1.Patterns.topLeftTriangle, '@');
ass1.printPattern(Assignment1.Patterns.topRightTriangle, '%');
ass1.printPattern(Assignment1.Patterns.centerTriangle, '^');
}
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class Assignment1 {
private final int HEIGHT = 11;
public enum Patterns{
bottomLeftTriangle,
bottomRightTriangle,
topLeftTriangle,
topRightTriangle,
centerTriangle,
};
// Write details of your algorithm
void printCenter(char ch){
int i,j,k=1;
for(i=0;i<HEIGHT;i++)
{
//printing space
for(j=0;j<HEIGHT-i;j++)
{
System.out.print(' ');
}
//printing char
for(j=0;j<k;j++)
{
System.out.print(ch);
}
k=k+2;
System.out.println();
}
}
// Write details of your algorithm
void printBottomLeft(char ch){
int i,j,k=1;
for(i=0;i<HEIGHT;i++)
{
//printing char
for(j=0;j<k;j++)
{
System.out.print(ch);
}
k=k+1;
System.out.println();
}
}
// Write details of your algorithm
void printTopLeft(char ch){
int i,j,k=0;
for(i=0;i<HEIGHT;i++)
{
//printing char
for(j=0;j<HEIGHT-k;j++)
{
System.out.print(ch);
}
k=k+1;
System.out.println();
}
}
// Write details of your algorithm
void printBottomRight(char ch){
int i,j,k=1;
for(i=0;i<HEIGHT;i++)
{
//printing space
for(j=0;j<HEIGHT-i;j++)
{
System.out.print(' ');
}
//printing char
for(j=0;j<k;j++)
{
System.out.print(ch);
}
k=k+1;
System.out.println();
}
}
// Write details of your algorithm
void printTopRight(char ch){
int i,j,k=0;
for(i=0;i<HEIGHT;i++)
{
//printing space
for(j=0;j<i;j++)
{
System.out.print(' ');
}
//printing char
for(j=0;j<HEIGHT-k;j++)
{
System.out.print(ch);
}
k=k+1;
System.out.println();
}
}
/*
* This function according to the pattenId calls the appropriate print function
* with the input ch. Default printTopLeft is called.
*
*/
public void printPattern(Patterns patternId, char ch){
switch (patternId){
case bottomLeftTriangle:
this.printBottomLeft(ch);
break;
case bottomRightTriangle:
this.printBottomRight(ch);
break;
case topLeftTriangle:
this.printTopLeft(ch);
break;
case topRightTriangle:
this.printTopRight(ch);
break;
case centerTriangle:
this.printCenter(ch);
break;
default:
this.printTopLeft(ch);
break;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Assignment1 ass1 = new Assignment1();
ass1.printPattern(Assignment1.Patterns.bottomLeftTriangle, '*');
ass1.printPattern(Assignment1.Patterns.bottomRightTriangle, '#');
ass1.printPattern(Assignment1.Patterns.topLeftTriangle, '@');
ass1.printPattern(Assignment1.Patterns.topRightTriangle, '%');
ass1.printPattern(Assignment1.Patterns.centerTriangle, '^');
}
}
output:-
run:
*
**
***
****
*****
******
*******
********
*********
**********
***********
#
##
###
####
#####
######
#######
########
#########
##########
###########
@@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@
@@@@@@@@
@@@@@@@
@@@@@@
@@@@@
@@@@
@@@
@@
@
%%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%
%%%%%%%%
%%%%%%%
%%%%%%
%%%%%
%%%%
%%%
%%
%
^
^^^
^^^^^
^^^^^^^
^^^^^^^^^
^^^^^^^^^^^
^^^^^^^^^^^^^
^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^
BUILD SUCCESSFUL (total time: 0 seconds)