Question

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)

(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)

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

code

import java.util.Scanner;
import java.lang.Math;
public class HalfArrow {
private int arrowBaseHeight;//create arrowBaseHeight data member
private int arrowBaseWidth;//create arrowBaseWidth data member
private int arrowHeadWidth;//create arrowHeadWidth data member
public static Scanner sc=new Scanner(System.in);//create scanner object to scan object
//setters
public void setArrowBaseHeight(int bh)
{
arrowBaseHeight=bh;
}
public void setArrowBaseWidth(int wh)
{
arrowBaseWidth=wh;
}
public void setArrowHeadWidth(int hw)
{
do
{//if head width is greater than base width then only accept it
if(hw>arrowBaseWidth)
{
arrowHeadWidth=hw;
break;
}
//otherwise ask user until he/she inputs no. greater than base width
System.out.println("Please enter arrow head width greater than arrow base width");
hw=sc.nextInt();
}while(true);

}
//draw rectangle
public void drawRectangle()
{
for(int i=0;i<arrowBaseHeight;i++)//draw for arrowBaseHeight height
{
for(int j=0;j<arrowBaseWidth;j++)
{
System.out.print("*");//print * arrowBaseWidth times
}
System.out.println();//new line
}
}
public void drawTriangle()
{
int x,y,spaces;
//as this trianle is right angle then by applying tan formula height comes out to be arrowHeadWidth/2
for(int i=0;i<arrowHeadWidth/2;i++)
{
// for i as row we print arrayHeadWidth/power(2,i) *'s
/*and total spaces before and after stars would be no. of stars printed in first row - no. of stars printed in current row but that is total spaces so divide it by 2 to get space which comes before stars.*/
x=(int)Math.pow(2,i);
y=arrowHeadWidth/x;
spaces=(arrowHeadWidth-y)/2;
for(int j=0;j<spaces;j++)
{
System.out.print(" ");//print spaces
}
for(int j=0;j<y;j++)
{
System.out.print("*");//print stars
}
System.out.println();
}
}
public static void main(String args[])
{
HalfArrow arr=new HalfArrow();//create object
System.out.println("Please enter arrow base width");
int bw=sc.nextInt();//take base width input
arr.setArrowBaseWidth(bw);
System.out.println("Please enter arrow base height");
arr.setArrowBaseHeight(sc.nextInt());//set base height
System.out.println("Please enter arrow head width");
arr.setArrowHeadWidth(sc.nextInt());//set head width
//as it is downfacing so first print rectangle then triangle
arr.drawRectangle();//draw rectangle
arr.drawTriangle();//draw triangle
}
}

Add a comment
Know the answer?
Add Answer to:
Program: Drawing a half arrow (Java) Instructor note: While you will be submitting this assignment through Zybooks, make...
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
  • 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...

  • Hello there, im trying to answer this question in c 4.22 LAB: Warm up: Drawing a...

    Hello there, im trying to answer this question in c 4.22 LAB: Warm up: Drawing a right triangle 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...

  • 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...

  • (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...

  • 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 *....

  • Write a Java program which implements both the while and for loop, as needed, to draw...

    Write a Java program which implements both the while and for loop, as needed, to draw the illustrations given below. Shape 1 123456789 12345678 1234567 123456 12345 1234 123 12 1 Shape 2 ************* ************* ************* ************* ************* Shape 3 ============ * * * * * * * * * * ============ Hint: To draw the shapes above, you can use nested for loops to accomplish the task. The outer for loop iterates on each row, and the inner for...

  • -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer...

    -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer to plan for retirement. Part 2: Generate a Retirement Planning Table: It's hard to decide how much you need to save for retirement. To help your customer visualize how long her nest egg will last, write a program that allows the user to generate a retirement planning table showing the number of months the savings will last for various combinations of starting account balance...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

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
ADVERTISEMENT