How do you find the outline of a square and the outline of a right triangle when
ID: 641397 • Letter: H
Question
How do you find the outline of a square and the outline of a right triangle when you put in a negative number for the input? Here is what i have. I need help creating the output when you put in a negative number. When the negative number is put in, it should outline the shape of a square (might look like a rectangle) and an outline of a right triangle.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String input;
String option = null;
System.out.print("Enter the Figure type (SQUARE or TRIANGLE): ");
input = keyboard.next();
while (!input.equalsIgnoreCase("square") && !input.equalsIgnoreCase("triangle"))
{
System.out.print("Enter the Figure type (SQUARE or TRIANGLE): ");
input = keyboard.next();
}
if (input.equalsIgnoreCase("square")){
drawSquare();
} else if (input.equalsIgnoreCase("triangle")){
drawTriangle();
}
System.out.println("");
System.out.print("Enter YES to print another figure: ");
option = keyboard.next();
if(option.equalsIgnoreCase("no")){
System.out.println("Goodbye.");
}
}
public static void drawSquare() {
Scanner keyboard = new Scanner(System.in);
int start = 0;
System.out.print("Enter the character to be used to draw the SQUARE: ");
String character = keyboard.next();
System.out.print("What is the base and height of the SQUARE: ");
int size;
size = keyboard.nextInt();
for (int i = 0; i < size; i++) {
System.out.println();
for (int j = 0; j < size; j++) {
System.out.print(character);
}
}
}
public static void drawTriangle() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the character to be used to draw the TRIANGLE: ");
char letter;
letter = keyboard.next().charAt(0);
System.out.print("Enter the base and height of the TRIANGLE: ");
int size;
size = keyboard.nextInt();
for (int i = 0; i <= size; i++) {
for (int j = 0; j < i ; j++) {
System.out.print(letter);
}
System.out.println();
}
}
}
Explanation / Answer
import java.util.Scanner;
public class DrawFigures {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value;
String input;
String option = null;
do {
System.out.print("Enter the Figure type (SQUARE or TRIANGLE): ");
input = keyboard.next();
while (!input.equalsIgnoreCase("square")
&& !input.equalsIgnoreCase("triangle")) {
System.out.print("Enter the Figure type (SQUARE or TRIANGLE): ");
input = keyboard.next();
}
if (input.equalsIgnoreCase("square")) {
System.out.print("Enter a negative number if you want only outline: ");
value = keyboard.nextInt();
if(value < 0)
drawSquareOutline();
else
drawSquare();
} else if (input.equalsIgnoreCase("triangle")) {
System.out.print("Enter a negative number if you want only outline: ");
value = keyboard.nextInt();
if(value < 0)
drawTriangleOutline();
else
drawTriangle();
}
System.out.println("");
System.out.print("Enter YES to print another figure: ");
option = keyboard.next();
if (option.equalsIgnoreCase("no")) {
System.out.println("Goodbye.");
}
} while (!option.equalsIgnoreCase("no"));
}
public static void drawSquare() {
Scanner keyboard = new Scanner(System.in);
int start = 0;
System.out.print("Enter the character to be used to draw the SQUARE: ");
String character = keyboard.next();
System.out.print("What is the base and height of the SQUARE: ");
int size;
size = keyboard.nextInt();
for (int i = 0; i < size; i++) {
System.out.println();
for (int j = 0; j < size; j++) {
System.out.print(character);
}
}
}
public static void drawTriangle() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the character to be used to draw the TRIANGLE: ");
char letter;
letter = keyboard.next().charAt(0);
System.out.print("Enter the base and height of the TRIANGLE: ");
int size;
size = keyboard.nextInt();
for (int i = 0; i <= size; i++) {
for (int j = 0; j < i; j++) {
System.out.print(letter);
}
System.out.println();
}
}
public static void drawSquareOutline(){
Scanner keyboard = new Scanner(System.in);
int start = 0;
System.out.print("Enter the character to be used to draw the SQUARE: ");
String character = keyboard.next();
System.out.print("What is the base and height of the SQUARE: ");
int size;
size = keyboard.nextInt();
for (int i = 0; i < size; i++) {
System.out.println();
for (int j = 0; j < size; j++) {
if(i == 0 || i == size - 1 || j == 0 || j == size - 1)
System.out.print(character);
else
System.out.print(" ");
}
}
}
public static void drawTriangleOutline(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the character to be used to draw the TRIANGLE: ");
char letter;
letter = keyboard.next().charAt(0);
System.out.print("Enter the base and height of the TRIANGLE: ");
int size;
size = keyboard.nextInt();
for (int i = 0; i <= size; i++) {
for (int j = 0; j < i; j++) {
if(i == 0 || j == 0 || i == size || j == i - 1)
System.out.print(letter);
else
System.out.print(" ");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.