Write a recursive method called drawTriangle() that outputs lines of '*' to form an upside down isosceles triangle. Method drawTriangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the last line for correct formatting. Hint: The number of '*' decreases by 2 for every line drawn. Ex: If the input of the program is: 3 the method drawTriangle() outputs: *** * Ex: If the input of the program is: 19 the method drawTriangle() outputs: ******************* ***************** *************** ************* *********** ********* ******* ***** *** * Note: No space is output before the first '*' on the first line when the base length is 19.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a recursive method called drawTriangle() that outputs lines of '*' to form an upside down isosceles triangle. Method drawTriangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the last line for correct formatting.

Hint: The number of '*' decreases by 2 for every line drawn.

Ex: If the input of the program is:

3

the method drawTriangle() outputs:

*** *

Ex: If the input of the program is:

19

the method drawTriangle() outputs:

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

Note: No space is output before the first '*' on the first line when the base length is 19.

### Lab Activity: Drawing an Upside Down Triangle

#### Java Programming Example

In this lab, the task is to draw an upside-down triangle using a recursive method in Java. Below is a partial implementation where you'll need to complete the missing `drawTriangle()` method.

```java
import java.util.Scanner;

public class LabProgram {

    /* TODO: Write recursive drawTriangle() method here. */

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int baseLength;

        baseLength = scnr.nextInt();
        drawTriangle(baseLength);
    }
}
```

#### Explanation

- **Import Statement**: The `Scanner` class is imported to allow taking inputs from the user.
- **Class Definition**: The `LabProgram` class encapsulates the logic for this exercise.
  
- **TODO Comment**: A placeholder is provided for the recursive method `drawTriangle()`. You will need to implement this method to draw the pattern.
  
- **Main Method**: 
  - Initializes the `Scanner` object to read input from the console.
  - Reads an integer value `baseLength` which determines the base length of the triangle.
  - Calls the `drawTriangle()` method with `baseLength` as an argument.

This lab requires you to write a recursive function to display an upside-down triangle pattern based on the user-provided `baseLength`.

Make sure to implement the logic for the `drawTriangle()` function to complete the lab activity.
Transcribed Image Text:### Lab Activity: Drawing an Upside Down Triangle #### Java Programming Example In this lab, the task is to draw an upside-down triangle using a recursive method in Java. Below is a partial implementation where you'll need to complete the missing `drawTriangle()` method. ```java import java.util.Scanner; public class LabProgram { /* TODO: Write recursive drawTriangle() method here. */ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int baseLength; baseLength = scnr.nextInt(); drawTriangle(baseLength); } } ``` #### Explanation - **Import Statement**: The `Scanner` class is imported to allow taking inputs from the user. - **Class Definition**: The `LabProgram` class encapsulates the logic for this exercise. - **TODO Comment**: A placeholder is provided for the recursive method `drawTriangle()`. You will need to implement this method to draw the pattern. - **Main Method**: - Initializes the `Scanner` object to read input from the console. - Reads an integer value `baseLength` which determines the base length of the triangle. - Calls the `drawTriangle()` method with `baseLength` as an argument. This lab requires you to write a recursive function to display an upside-down triangle pattern based on the user-provided `baseLength`. Make sure to implement the logic for the `drawTriangle()` function to complete the lab activity.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education