Response:
Here is the JAVA program:
import java.util.Scanner; // to accept user input
public class DrawHalfArrow{ // beginning of the half arrow class
public static void main(String[] args) { // starts main() function
Scanner scnr = new Scanner(System.in); // reads input
int arrowBaseHeight = 0; // variable for arrow base height
int arrowBaseWidth = 0; // variable for arrow base width
int arrowHeadWidth = 0; // variable for arrow head width
// prompts user for arrow base height, width, and head width
System.out.println("Enter arrow base height: ");
arrowBaseHeight = scnr.nextInt(); // scans input as an integer
System.out.println("Enter arrow base width: ");
arrowBaseWidth = scnr.nextInt();
/* while loop to keep asking for arrow head width until it's greater than the arrow base width */
while (arrowHeadWidth <= arrowBaseWidth) {
System.out.println("Enter arrow head width: ");
arrowHeadWidth = scnr.nextInt(); }
// begins the nested loop
// outer loop runs as many times as the arrow base height
for (int i = 0; i < arrowBaseHeight; i++) {
// inner loop prints the asterisks
for (int j = 0; j <arrowBaseWidth; j++) {
System.out.print("*"); } // shows asterisks
System.out.println(); }
// a temporary variable to store the width of the arrowhead
int k = arrowHeadWidth;
// outer loop to iterate the number of times equal to the arrowhead height
for (int i = 1; i <= arrowHeadWidth; i++)
{ for(int j = k; j > 0; j--) {// inner loop to print asterisks
System.out.print("*"); } // shows asterisks
k = k - 1;
System.out.println(); } } } // continues adding more asterisks for a new line
Clarification:
This program requests the user to input the arrow base height, base width, and head width. When the head width is asked, it checks that the width must exceed that of the base width. The while loop keeps prompting until a valid head width is entered.
The loop generates an arrow base matching the specified height. This satisfies the first condition.
A nested loop outputs the base with the specified width. The inner loop draws the asterisks to form the base, while the outer loop iterates according to the base height, thus meeting the second condition.
A temporary variable k stores the initial value of head width during modifications.
The final nested loop produces the arrow head with the specified width. The inner loop handles star printing for the arrow head, fulfilling the third condition.
The temporary variable k decreases by one for subsequent iterations to produce one less asterisk each time.
Attached is the output screenshot.