Create a program that will use nested loops to create the following 3 star desig
ID: 3781254 • Letter: C
Question
Create a program that will use nested loops to create the following 3 star designs. Use the attached skeleton as a template.
Star Design #1:
*
**
***
****
*****
Star Design #2:
*****
****
***
**
*
Star Design #3:
*
***
*****
*******
public class SolvingStarDesigns {
public static void main(String[] args) {
//Instantiate the driver class, so you can call instance methods (& not static methods)
SolvingStarDesigns myStars = new SolvingStarDesigns();
myStars.makeDesign1();
myStars.makeDesign2();
myStars.makeDesign3();
myStars.makeDesign4();
}
/**
* Use javadoc tags to document each method:
*/
public void makeDesign1()
{
//put your code here
}
/**
* Use javadoc tags to document each method:
*/
public void makeDesign2()
{
}
/**
* Use javadoc tags to document each method:
*/
public void makeDesign3()
{
}
/**
* Use javadoc tags to document each method:
*/
public void makeDesign4()
{
}
}
Explanation / Answer
public class SolvingStarDesigns {
public static void main(String[] args)
{
//Instantiate the driver class
SolvingStarDesigns myStars = new SolvingStarDesigns();
myStars.makeDesign1();
myStars.makeDesign2();
myStars.makeDeisgn3();
}
/**
* makeDesign1() prints star design 1
*/
public void makeDesign1()
{
System.out.println(" Star Design #1:");
for(int i=0;i<5;i++)
{
for(int j=0;j<=i;j++)
System.out.print('*');
System.out.println();
}
}
/**
* makeDesign2() prints star design 2
*/
public void makeDesign2()
{
System.out.println(" Star Design #2:");
int k=0;
for(int i=5;i>=1;i--)
{
for(int l=0;l<k;l++)
System.out.print(' ');
for(int j=i;j>=1;j--)
System.out.print("* ");
System.out.println();
k++;
}
}
/**
* makeDesign3() prints star design 3
*/
public void makeDeisgn3()
{
System.out.println(" Star Design #3:");
int k=3;
for(int i=0;i<4;i++)
{
for(int l=k;l>=1;l--)
System.out.print(' ');
for(int j=0;j<2*i+1;j++)
System.out.print('*');
System.out.println();
k--;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.