Create a Triangles class. It must have the following methods: Draw(int num) – Pr
ID: 3661462 • Letter: C
Question
Create a Triangles class. It must have the following methods:
Draw(int num) – Prints num asterisks
Space(int num) – Prints num spaces
Endl() – Prints an end-of-line character
Right(int num) – Prints a num x num Right triangle
Left(int num) – Prints a num x num Left triangle
Pyramid(int num) – Prints a num-sized pyramid
Diamond(int num) – Prints a num-sized diamond
Create a Lab01 class. It must do the following (output to a command/cmd window):
Read num from the command line.
Instantiate a Triangles class.
Draw num Spaces followed by num asterisks all on one line.
Draw a num sized Right triangle
Draw a num-sized Left Triangle
Draw a num-sized Pyramid
Draw a num-sized Diamond
There must be a separator line between each of the objects. It must have 5 carets (^^^^^).
*
**
***
****
---*
--**
-***
****
---*
--***
-*****
*******
---*
--***
-*****
*******
-*****
--***
---*
Right Triangle
Left Triangle
Pyramid
Diamond
*
**
***
****
---*
--**
-***
****
---*
--***
-*****
*******
---*
--***
-*****
*******
-*****
--***
---*
Right Triangle
Left Triangle
Pyramid
Diamond
Explanation / Answer
/**
* The java Triangle that has methods to Draw, Space,
* Endl, Right, Left and Pyramid and Diamond.
* */
//Triangle.java
public class Triangle
{
//Print the asteriks
public void Draw(int num)
{
for (int i = 1; i <=num; i++)
System.out.print("*");
}
//Print spaces
public void Space(int num)
{
for (int i = 1; i <=num; i++)
System.out.print(" ");
}
//Prints new line
public void Endl()
{
System.out.println();
}
//Prints right triangle
public void Right(int num)
{
for (int i = 1; i <=num; i++)
{
//Calls Draw to draw asterik
Draw(i);
//Calls Endl to print a new line
Endl();
}
}
//Prints a left triangle
public void Left(int num)
{
int space=num;
for (int i = 1; i <=num; i++)
{
//Calls Space
Space(space-1);
//Calls Draw
Draw(i);
//Calls Endl
Endl();
space=space-1;
}
}
//Prints Pyramid shape
public void Pyramid(int num)
{
int space=num-1;
int count=1;
for (int i = 1; i <=num; i++)
{
//Calls Space
Space(space);
//Calls Draw
Draw(count);
//Calls Endl
Endl();
space=space-1;
count=count+2;
}
}
//Prints Diamond
public void Diamond(int num)
{
int space=num-1;
int count=1;
//Top diamond shape
for (int i = 1; i <=num; i++)
{
Space(space);
Draw(count);
Endl();
space=space-1;
count=count+2;
}
space=1;
//Bottom diamond shape
for (int i = count-num;
i >=1; i=i-2)
{
Space(space);
Draw(i);
Endl();
space++;
}
}//end of Diamond
}//end of the Triangle
------------------------------------------------------------------------------------------
/*The java program that creates an instance of Triangle
* class and calls the Right method that prints the right
* triangle , calls the Left method that prints the left
* triangle. calls the Pyramid method that prints the pyramid
* and calls the Diamond that prints the diamond shape on console.*/
//Lab01.java
public class Lab01
{
public static void main(String[] args)
{
//Create an Triangle instance
Triangle triangle=new Triangle();
System.out.println("Right Triangle");
//Call Right method with num=5
triangle.Right(4);
System.out.println("^^^^^");
System.out.println("Left Triangle");
//Call Left method with num=5
triangle.Left(4);
System.out.println("^^^^^");
System.out.println("Pyramid");
//Call Pyramid method with num=5
triangle.Pyramid(4);
System.out.println("^^^^^");
System.out.println("Diamond");
//Call Pyramid method with num=5
triangle.Diamond(4);
}//end of main
}//end of Lab01
---------------------------------------------------------------------------------
Sample Output:
Right Triangle
*
**
***
****
^^^^^
Left Triangle
*
**
***
****
^^^^^
Pyramid
*
***
*****
*******
^^^^^
Diamond
*
***
*****
*******
*****
***
*