Question

create a new Java application called "RecursiveTriangle" (without the quotation marks) according to the following guidelines....

create a new Java application called "RecursiveTriangle" (without the quotation marks) according to the following guidelines.

Modify the example in Horstmann Section 5.9, pp. 228-230 so that the triangle displays “in reverse order” as in the example below, which allows the user to set the number of lines to print and the String used for printing the triangle.

Use a method to prompt the user for the number of lines (between 1 and 10) to print. This method should take no parameters and return an int value.

Use a method to prompt the user for the string to be used when printing the pattern. This method should take no parameters and return a String value.

Use a method called printTriangle that takes an int and a String as parameters (i.e., the return values from the two methods mentioned above) and has no return value. That method should use recursion to print the “reversed triangle” as in the following example.

EXAMPLE: The call to printTriangle(numLines, pattern) should print the following, assuming numLines has the value 4 and pattern has the value “[]”.

[][][][]
[][][]
[][]
[]

Thoughts

You could write and test each method as you go. Alternatively, you could write the whole program with “everything in main” and then decompose it into methods.

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

Please find the Java code below:-

1. RecursiveTriangle.java

import java.util.Scanner;

public class RecursiveTriangle {

Scanner sc = new Scanner(System.in);

public int getNumberLines(){

System.out.println("Please enter number of lines (between 1 and 10) to print: ");

int numLines = Integer.parseInt(sc.nextLine());

if (numLines < 1 || numLines > 10){

System.out.println("Invalid Input!!!, Please enter number of lines (between 1 and 10) to print");

return 0;

}

return numLines;

}

public String getPattern(){

System.out.println("Please enter pattern string to print: ");

String pattern = sc.nextLine();

return pattern;

}

public void printTriangle(int numLines, String pattern) {

if (numLines < 1)

return;

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

System.out.print(pattern);

}

System.out.println();

printTriangle(numLines - 1, pattern);

}

public static void main(String[] args) {

RecursiveTriangle r = new RecursiveTriangle();

int numLines = r.getNumberLines();

String pattern = r.getPattern();

r.printTriangle(numLines, pattern);

}

}

The outputs are attcahed below:-

Please let me know in case of any clarifications required. Thanks!

Add a comment
Know the answer?
Add Answer to:
create a new Java application called "RecursiveTriangle" (without the quotation marks) according to the following guidelines....
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
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