Triangles
Due: September 9
Objective
To gain experience with variables, conditions, loops and I/O in Java.Task
An equilateral triangle is one that has all three sides of equal length. Your task for this is to write a program that can print out equilateral triangles of different sizes. You will print them out using a ASCII characters.
Details
- You should get two pieces of input from the user: the size of the triangle
and the character to use for drawing it (called the "fill" character).
The Scanner has no "nextChar" function. Instead, you can just read a String,
and use the first character of it:
in.next( ).charAt(0); - You should make sure that the size is in the range 1-50. If the user enters zero, a negative number, or one greater than 50, you should repeatedly ask them for a new size.
- To make the triangles look more balanced, you should put a space between each fill character.
- After running the program, it should ask the user if they want to run it again. If so, get input and draw a new triangle. If not, the program should exit.
General Requirements
- Your code should be readable and reasonably indented.
- You must provide comments in your program.
- You must include a comment at the top with your name.
- You should not have a "package" line in your program.
- You should not have the comments NetBeans puts in by default e.g. the "license header", "@author" or "TODO" lines.
- Your file name/class name should be something descriptive, not something like "JavaApplication4".
Example Run
Here is an example run of this program. User input is shown in bold face:
Enter size: 7
Enter fill: #
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
Would you like another triangle (Y/N)?
: y
Enter size: 15
Enter fill: +
+
+ +
+ + +
+ + + +
+ + + + +
+ + + + + +
+ + + + + + +
+ + + + + + + +
+ + + + + + + + +
+ + + + + + + + + +
+ + + + + + + + + + +
+ + + + + + + + + + + +
+ + + + + + + + + + + + +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
Would you like another triangle (Y/N)?
: y
Enter size: -12
Size must be between 1-50!
Enter size: 88
Size must be between 1-50!
Enter size: 0
Size must be between 1-50!
Enter size: 3
Enter fill: ~
~
~ ~
~ ~ ~
Would you like another triangle (Y/N)?
: y
Enter size: 2
Enter fill: #
#
# #
Would you like another triangle (Y/N)?
: y
Enter size: 1
Enter fill: Q
Q
Would you like another triangle (Y/N)?
: y
Enter size: 50
Enter fill: %
%
% %
% % %
% % % %
% % % % %
% % % % % %
% % % % % % %
% % % % % % % %
% % % % % % % % %
% % % % % % % % % %
% % % % % % % % % % %
% % % % % % % % % % % %
% % % % % % % % % % % % %
% % % % % % % % % % % % % %
% % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
Would you like another triangle (Y/N)?
: N