In: Computer Science
Programming Assignment No. 5
Write a program that asks the user to enter a positive odd number
less than 20. If the number is 5, 11 or 15, draw a square whose
width is the same as the number entered, if 3, 9, or 17, draw a box
(a box is a hollowed out square) with the width the same as the
number entered, otherwise just present an message saying "To be
determined". Write a method for the square that accepts an integer
as a parameter and returns a string (use for loops), the same for
the box( use while loops). No output should be done in those
methods. Write a method that will ask for the number and does not
return until the number entered satisfies the conditions above. Use
a do while loop in that method. The method main cannot be used as
one of the methods to be written. Use a switch statement to
determine which shape to draw. Use the methods from the IO class
used in Program 4 for input and output. Modify showMessage so that
it uses a fixed-width font otherwise IO should be the same as for
Program 4. The main program should be in the default package. Good
program structure, adherarnce to Java conventions, and indentation
are required. There will be 2 files for this assignment
Program5.java which is the default package and IO.java which is in
the util package. IO.java will be the same as it was in Program 4.
Do not write any more methods than the ones specified. The program
above is just a starting point, there are a number of changes that
need to be made before. If you are not sure how to get started, a
more detailed starting point is in the video.
Problem5.java
import java.util.Scanner;
class Problem5
{
public static void main(String[] args)
{
// Get input from user
int n = getInput();
switch(n)
{
// Square
case
case 5:
case 11:
case 19:
System.out.println(square(n));
break;
// Box
square
case 3:
case 9:
case 17:
System.out.println(box(n));
break;
// Final
case
default:
System.out.println("To be
determined");
}
}
public static int getInput()
{
// Create object for Scanner class
to get input from user
Scanner input = new
Scanner(System.in);
int n;
do
{
// Promt user
for input
System.out.print("Enter odd number less than 20: ");
// Get input
from user
n =
input.nextInt();
}while(n>20 || n%2==0);//Iterate
until given condition satisified
// Return when n satisified given
condition
return n;
}
public static String square(int n)
{
// Initialize String square with
empty string
String square = "";
for(int i=0; i<n; i++)
{
for(int j=0;
j<n; j++)
{
// Add * to square for each iteration
square += "*";
}
// Add a new
line to square after each row
square +=
"\n";
}
// Return square
return square;
}
public static String box(int n)
{
// Initialize String box with empty
string
String box = "";
// Initialize i to 0
int i=0;
while(i<n)
{
// Row starts
with *
box +=
"*";
int j=1;
while(j<n-1)
{
// Add * if row it is first or last row, add
space otherwise
box += (i==0||i==n-1)?"*":" ";
// Increment j by 1
j++;
}
// Row ends with
*
box +=
"*\n";
// Increment i
by 1
i++;
}
return box;
}
}
output screenshot: