Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using text to create a picture is known as ASCII art. In section 2, we made an A

ID: 3866420 • Letter: U

Question

Using text to create a picture is known as ASCII art. In section 2, we made an ASCII art cat. This required us to type every character in the art we wanted to create. In this practice, you'll find a way to draw basic shapes programmatically in customizable sizes. Complete the following two methods in LoopShape.java: createRectangle (): This method accepts two arguments for width and height which should be used to print a rectangle createTriangle (): This method accepts one argument for the size of a leg, which should be used to print an isosceles right triangle Try changing the value of the arguments you're supplying these two methods from the main method. Make sure your program can successfully draw each shape to a custom size. Additionally, your program must: Refuse to draw shapes with any dimension less than 1 Be able to draw shapes with any dimension equal to 1 (a 1 times 1 shape should print just a single character) If the problem seems difficult, remember to break it into smaller challenges such as: How do I print a single line that is a variable number of "#"characters wide? How do I create a String that begins and ends with a "#", but has a variable number of spaces in between? Finishing each smaller challenge is an accomplishment. This problem is as much about understanding loops as it's about understanding how to break a big problem into smaller tasks. The knowledge you've gained in this section on loops will be very helpful in completing this program. You're free to use whichever type of loop statements you feel would be best. You'll also need to remember a few concepts from previous sections.

Explanation / Answer

import java.util.Scanner;
class shape
{
void createTriangle(int he) //triangle function with one paramete
{
int k,n;
n=he;
for(int i=0;i<=n;i++)
{
  
for(int j=0;j<i+1;j++)
{
if (j==0)
System.out.print("*");
else if(j==i)
System.out.print("*");
else if(i==n)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
  
}
}
void createRectangle(int a,int b)//rectangle function with one parameter
{
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
if(i==1 || i==a || j==1 || j==b){
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}

public static void main(String args[]) //main function
{
int ch;
shape obj=new shape();
Scanner sc=new Scanner(System.in); //accepting the choice from user
System.out.println("1.Create Rectangle");
System.out.println("2.Create Triangle");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1:
int l,b;
do{ //accepting the length and breadth from user
System.out.println("Enter the length");
l=sc.nextInt();
System.out.println("Enter the breadth");
b=sc.nextInt();
}while(l<1 || b<1);
obj.createRectangle(l,b); //calling the rectangle function
break;
case 2:
int h;
do{
System.out.println("Enter the height"); //accepting the height from user
h=sc.nextInt();
}while(h<1);
obj.createTriangle(h); //calling the triangle function
break;
default:
System.out.println("Invalid choice"); //for invalid option selection
break;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote