Question
import java.util.*; public class Triangle { public static Scanner kbd; public static void main(String[] args) { kbd = new Scanner(System.in); kbd.close(); } /** * Prints
import java.util.*;
public class Triangle { public static Scanner kbd;
public static void main(String[] args) { kbd = new Scanner(System.in);
kbd.close(); } /** * Prints a triangle to the screen * @param size The number of asterisks in the center point of the triangle */ public static void printTriangle(int size) {
}
/** * Prints a line of asterisks to the screen * @param numAsterisks The number of asterisks to be printed */ public static void printLine(int numAsterisks) { for(int i = 0; i < numAsterisks; ++i) { System.out.print("*"); } System.out.println(); } }
(Heres the starter code with my part 1.)
Create a new Java class called: Triangle
Write a program that asks the user to enter the size of a triangle to print on the screen. After getting this number, the program should then print a triangle to the screen by printing a series of lines consisting of asterisks. The first line will have one asterisk. The second line will have two asterisks, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. After reaching the number entered by the user, on the next line, print one less asterisk and continue by decreasing the number of asterisks by one for each successive line until only one asterisk is printed.
For example, if the user enters 3, then your program should display this:
* ** *** ** *
If the user enters 5, then your program should display this:
* ** *** **** ***** **** *** ** *
Program Requirements
Begin this part of the assignment by placing a copy of your printLine method from Part 1 into this program. You will use this method to help you with this part of the assignment. This method should be an EXACT COPY from Part 1. DO NOT change the printLine method when you copy it into your program for Part 2 of this assignment.
The main program should only do two major things:
Get the input from the user.
Use a method named printTriangle to display the output on the screen.
You must write and use a method called printTriangle. This method should NOT get any values from the user/keyboard that task should be performed by the main program. This method should be sent an integer value from the main program. This integer tells the method how many asterisks should be in the longest line of the triangle. The method, then, should use two loops to repeatedly call on the printLine method. The loops essentially keep changing the value of the integer that is sent to the printLine method so that each time the printLine method prints a different line length. You will need one loop to count up (making the lines longer and longer). When this loop ends, you will need to go into another loop which counts down (making the lines smaller and smaller).
The main program is NOT allowed to print any asterisks to the screen. It MUST call on the printTriangle method, and let that method take care of the printing the triangle to the screen.
The printTriangle method is NOT allowed to print any asterisks to the screen. It MUST use the printLine method repetively to print the lines of asterisks to the screen.
The keyboard object is declared as a class (global) variable as discussed in our class example. You are NOT allowed to use any other class (global) variables in this program. All other variables must be declared as local.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started