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

Problem # 1: Write a function that will generate a set of stars based on given s

ID: 3545464 • Letter: P

Question

Problem # 1:

Write a function that will generate a set of stars based on given shape and sizes. You need to keep on asking the user for shapes and sizes until the user enters -1 for the size or no for the shape. Note that the function only draws based on sizes and shapes anything else should be in main.

public static void drawShape(int s1, int s2, char shape)

Sample run:   

What shape you want (s: square, r: rectangle, t: triangle, n: no)? s

Enter side (-1: stop): 4

* * * *

* * * *

* * * *

* * * *

What shape you want (s: square, r: rectangle, t: triangle, n: no)? r

Enter height (-1: stop): 4

Enter width (-1: stop): 3

* * *

* * *

* * *

* * *

What shape you want (s: square, r: rectangle, t: triangle, n: no)? t

Enter height (-1: stop): 4

    *

  * * *

* * * *

* * * * *

What shape you want (s: square, r: rectangle, t: triangle, n: no)? n

Thank you !!!

Explanation / Answer

import java.util.*;

public class calculate {

public static void drawShape(int s1, int s2, char shape)

{int i,j;

if(shape=='s'||shape=='r')

{

for(i=0;i<s1;i++)

{for(j=0;j<s2;j++)

System.out.print("* ");

System.out.println();

}

}

else if(shape=='t')

{

for(i=1;i<=s1;i++)

{for(j=1;j<=i;j++)

System.out.print("* ");

System.out.println();

}

}

}


public static void main(String[] args) {

int s1,s2,i;char n;

while(true)

{

System.out.println("What shape you want (s: square, r: rectangle, t: triangle, n: no)?" );

Scanner sc=new Scanner(System.in);

String str=sc.nextLine();

n=str.charAt(0);

if(n=='n')

break;

else if(n=='s')

{

System.out.println("Enter side (-1: stop):");

str=sc.nextLine();

s1=Integer.parseInt(str);

if(s1==-1)

break;

else

calculate.drawShape(s1,s1,n);

}

else if(n=='r')

{

System.out.println("Enter height (-1: stop):");

str=sc.nextLine();

s1=Integer.parseInt(str);

if(s1==-1)

break;

System.out.println("Enter width (-1: stop):");

str=sc.nextLine();

s2=Integer.parseInt(str);

if(s2==-1)

break;

calculate.drawShape(s1,s2,n);

}

else if(n=='t')

{

System.out.println("Enter height (-1: stop):");

str=sc.nextLine();

s1=Integer.parseInt(str);

if(s1==-1)

break;

calculate.drawShape(s1,s1,n);

}

}


}

}

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