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.
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!
create a new Java application called "RecursiveTriangle" (without the quotation marks) according to the following guidelines....
create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...
create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...
create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...
I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...
Write a complete Java program called "AtmSimDoLoop" (without the quotation marks) according to the following specifications. It should use a do-while loop to prompt the user with the following starting prompt (without the horizontal lines) until the user enters 4 to quit. The program should start with an initial account balance, which you can set to any legitimate double value. Prompt the user with the following prompt (without the horizontal lines). Enter the number of your desired transaction type. Deposit...
create a new Java application called "MinMax" (without the quotation marks) that declares an array of doubles of length 5, and uses methods to populate the array with user input from the command line and to print out the max (highest) and min (lowest) values in the array.
create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...
Within DrJava, create a class called StudentQuizScores and do the following using a do-while loop. 1. You program will read in a student’s first name. The same will be done for the student’s last name. Your program will use respective methods (described below) to accomplish this. 2. Your program will then read in three quiz scores, respectively. This should be done by using the same method three times. This method is described below. 3. Your program will then calculate the...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
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...