Question

This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The...

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)

while (arrowHeadWidth <= arrowBaseWidth) {
    // Prompt user for a valid arrow head value
}

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

Enter arrow base height:
5
Enter arrow base width:
2
Enter arrow head width:
4

**
**
**
**
**
****
***
**
*

This is what I have:

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;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

The entire code was perfectly correct except at this condition [ while (arrowHeadWidth <= arrowBaseWidth) ]

You have to use '<=' instead of using '>='.

Updated code is as follows.

*/

import java.util.*;

public class Main
{
   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;
    }
}

Add a comment
Know the answer?
Add Answer to:
This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program: Drawing a half arrow (Java) Instructor note: While you will be submitting this assignment through Zybooks, make...

    Program: Drawing a half arrow (Java) Instructor note: While you will be submitting this assignment through Zybooks, make sure you are still applying appropriate formatting and in-line commenting. 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)...

  • in Java This program will output a right triangle based on user specified height triangleHeight and...

    in Java This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a character. Modify the given program to output a right triangle that instead uses the user-specified trianglechar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or* Each subsequent line will...

  • 5.13 Program: Drawing a half arrow (Python 3) This zyLab activity is the traditional programming assignment,...

    5.13 Program: Drawing a half arrow (Python 3) This zyLab activity is the traditional programming assignment, typically requiring a few hours over a week. The previous section provides warm up exercises intended to help a student prepare for this programming assignment. 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...

  • 4.30 Draw a Custom Full Arrow This program outputs an upwards facing arrow composed of a...

    4.30 Draw a Custom Full Arrow This program outputs an upwards facing arrow composed of a rectangle and a triangle. The arrow characters used to draw the base and head are characters specified by the user. The dimensions are defined by user as well, specified as arrow base height, arrow base width, and arrow head width. Create a program to output an arrow that reads in the respective characters and values in a java format. Perform sanity checking on numeric...

  • In C programming language: This program will output a right triangle based on user specified height...

    In C programming language: This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *....

  • (In C++) The program will output a right triangle based on user specified height triangleHeight and...

    (In C++) The program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. Modify the program to use a nested loop to output a right triangle of height triangleHeight. The 1st line will have one user-specified character, such as %or * Each subsequent line will have 1 additional user-specified character until the number in the triangle"s base reaches triangleHeight. Output a space after each user-specified character, including after the line"s last user-specified character. Example output...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program...

    CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program that reads grades from the user, so that it has a method that checks if a particular input is valid i.e. as long as the user types invalid input, the user should be given another chance to enter input (it would also be good to let the user know that their input is invalid). Moreover, only a valid grade should be used in computing...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT