Package Name: hmwk05 Source File Name: (Submit this file) Pattern.java Test File
ID: 3889376 • Letter: P
Question
Package Name:
hmwk05
Source File Name:
(Submit this file)
Pattern.java
Test Files:
C2800_Ch05_Hmwk1_Pattern_Test.zip
This program will only have 1 class with a single method, just main(). We want to practice for and switch statements.
Your program will print a variety of patterns:
Prompt the user for the type of pattern and the size of the pattern
Use a switch statement (not if-else if…) to determine which pattern to print
Let N be the size of the pattern
Pattern type 1 – print N *’s
Pattern type 2 – print 1 to N (with a space after each number)
Pattern type 3 – print a right triangle of *’s. The first line has 1 *, the second line has 2 *’s, and so on, up to the Nth line with N *’s
Pattern type 4 – print a curve pattern (2 triangles, one on top of another). The first line is N *’s, the second line is 1 space and N-1 *’s until you get to a single *. Then reverse the printing. See sample run 4 for an example of N=3.
Use for loops to print the patterns
If you would like to make one of the patterns using a do..while instead of a for loop, you may. That would allow you to practice a do…while loop. But keep in mind that for this kind of problem, for loops would be the best choice.
Print a message If the user enters an invalid pattern choice
Requirements:
All numbers entered are integers
Put System.out.println() after the prompt line. This will help you match the Test Program and make it easier to use it.
Sample Run #1: (the highlighted text is what the user types)
Pattern(1=*, 2=nums, 3=triangle, 4=curve) size? 1 5
*****
Sample Run #2: (the highlighted text is what the user types)
Pattern(1=*, 2=nums, 3=triangle, 4=curve) size? 2 4
1 2 3 4
Sample Run #3: (the highlighted text is what the user types)
Pattern(1=*, 2=nums, 3=triangle, 4=curve) size? 3 6
*
**
***
****
*****
******
Sample Run #4: (the highlighted text is what the user types)
Pattern(1=*, 2=nums, 3=triangle, 4=curve) size? 4 3
***
**
*
*
**
***
Sample Run #5: (the highlighted text is what the user types)
Pattern(1=*, 2=nums, 3=triangle, 4=curve) size? 5 2
Invalid pattern
Extra Notes:
Did you correctly name the package/folder?
Did you correctly name the class/file?
Did you include comments?
Package Name:
hmwk05
Source File Name:
(Submit this file)
Pattern.java
Test Files:
C2800_Ch05_Hmwk1_Pattern_Test.zip
Explanation / Answer
import java.util.*;
package hmwk05;
class Pattern
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int pType,size,i,j,space;
System.out.println("Pattern(1=*, 2=nums, 3=triangle, 4=curve) size?");
//input pattern and size
pType = scan.nextInt();
size = scan.nextInt();
System.out.println();
//switch cases for different pattern types
switch(pType)
{
case 1: for(i=0;i<size;i++)
System.out.print("*"); //print * size times
break;
case 2: j = 1;
for(i=1;i<=size;i++)
{
System.out.print(j+" "); //print j size times after incrementing j
j++;
}
break;
case 3: for(i=0;i<size;i++)
{
for(j=0;j<=i;j++)
{
System.out.print("*"); // triangle pattern for rows i and columns j
}
System.out.println();
}
break;
case 4: for(i=0;i<size;i++)
{
for(space = 0;space <=i; space++) //add spaces before
System.out.print(" ");
for(j=size-1;j>=i;j--)
{
System.out.print("*"); // * pattern
}
System.out.println();
}
for(i=0;i<size;i++)
{
for(space = size;space > i; space--) //add spaces before
System.out.print(" ");
for(j=0;j<=i;j++)
{
System.out.print("*"); //* pattern
}
System.out.println();
}
break;
default: System.out.println(" Invalid Pattern");
break;
}
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.