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

Drawing a half arrow (Java) Make sure your source files are encoded in UTF-8. So

ID: 3593089 • Letter: D

Question

Drawing a half arrow (Java)

Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct.

This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by user specified arrow base height, arrow base width, and arrow head width.

(1) Modify the given program to use a loop to output an arrow base of height arrowBaseHeight. (1 pt)

(2) Modify the given program to use a loop to output an arrow base of width arrowBaseWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow base. (1 pt)

(3) Modify the given program to use a loop to output an arrow head of width arrowHeadWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow head. (2 pts)

(4) Modify the given program to only accept an arrow head width that is larger than the arrow base width. Use a loop to continue prompting the user for an arrow head width until the value is larger than the arrow base width. (1 pt)

Example output for arrowBaseHeight = 5, arrowBaseWidth = 2, and arrowHeadWidth = 4:

The example run is shown as it looks in the Eclipse Console window which includes both output and input. Each input ends with newline which means there is an extra newline before the triangle begins. The last line of the half triangle ends with a newline.

Explanation / Answer

import java.util.Scanner;

public class DrawHalfArrow {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

int arrowBaseHeight = 0;

int arrowBaseWidth = 0;

int arrowHeadWidth = 0;

System.out.println("Enter arrow base height: ");

arrowBaseHeight = scnr.nextInt();

System.out.println("Enter arrow base width: ");

arrowBaseWidth = scnr.nextInt();

while (arrowHeadWidth <= arrowBaseWidth) {

System.out.println("Enter arrow head width: ");

arrowHeadWidth = scnr.nextInt();

}

// Draw arrow base (height = 3, width = 2)

for(int i=0; i<arrowBaseHeight; i++){

for(int j=0; j<arrowBaseWidth; j++){

System.out.print("*");

}

System.out.println();

}

// Draw arrow head (width = 4)

for(int i=0; i<arrowHeadWidth; i++){

for(int j=0; j<arrowHeadWidth-i; j++){

System.out.print("*");

}

System.out.println();

}

return;

}

}

Output:

Enter arrow base height:
3
Enter arrow base width:
2
Enter arrow head width:
4
**
**
**
****
***
**
*

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