Starting from:
$30

$24

Lab Assignment 04 Solution

PURPOSE: The purpose of this lab is to demonstrate the ability to write a program that satisfies a specification.




ASSIGNMENT A: [6 pts.] You are to write a program with several methods. The first part of the lab requires you to write down the design/logic of the program. The program is to print out a square or a right triangle of a size that is prompted for and input by the user. The size that is input will be used as a length and height of a square or right triangle that is to be printed. The string input of the figure type should be case insensitive. If the figure input type is incorrect a message to the user is to be printed stating there is an error and requesting new input. The request for good input is repeated until a correct figure type is entered. If the input number is negative only a right triangle outline will be printed otherwise a filled right triangle will be printed. A prompt will also be given for the character that is to be used to print the right triangle or square. Any character may be used to construct the right triangle or square.




Your program should ask the user to enter YES if they want to print another figure. This yes comparison should be case insensitive. The program may exit on any other input.




You are to sketch out your logic in either pseudo code or with flow chart symbols. Your design should include the calling of methods to describe the program, its operation, to get and check inputs, and finally to do the actual figure printing. Remember: A method is to do one thing, and should be named appropriately. A method may call another (smaller) method to help it in its task. For example, a method called printTriangle might call a method called printLineOfCharacters multiple times and with multiple arguments. Think about what you want your methods to do, name them appropriately, and decide what arguments need to be sent to the methods in order for them to perform their designated task.




Hint: (You need to make use of this in your program!)




Many situations call for the creation of complex output. Rather than outputting a single line at a time, it can be more efficient to build the entire output using a StringBuilder object, and then do a single output operation on the string contained in the StringBuilder object. Later on when you start working with GUI components, you will always need to create the output string before you actually place it into a GUI component. String objects are immutable (content cannot change after their creation). StringBuilder objects are mutable, meaning, you can easily and efficiently add any type of values to the object’s existing content. For example:




StringBuilder sb = new StringBuilder();




sb.append(“Bob is ”);

sb.append(34);

sb.append(“years old\n”);




System.out.print(sb.toString());




You can also add characters individually to the StringBuilder object. See the Java 8 documentation for all the details on the StringBuilder class.




sb.append(‘x’);




I would recommend using a StringBuilder object in this lab to build up the output string for the triangle or square.




Also, you should have a method for making a square and a different one for making a triangle. Your methods will need to have arguments for things that they need and/or return items that they get or their results. You are to show your logic (flow chart or pseudo code) to your TA. After your TA agrees with your logic you can proceed.




ASSIGNMENT B: [14 pts.] You are to code the program described above. You are to write documentation which describes your program logic as well as javadoc for all methods.




Below is an example of input/output. (The text outside the box below is to be printed only once)

This program will use methods to display the program




instructions and to print out the figures. The program

will be fully documented with javadoc statements.




You are to enter in the type of figure to be printed:




SQUARE or TRIANGLE followed by the character to be used and the size of the figure's base and height. After the program prints the figure the program will ask if there is another figure to be displayed.







Enter the Figure type (SQUARE OR TRIANGLE): Circle Input error




Enter the Figure type (SQUARE OR TRIANGLE): SqUaRe

Enter the character to be used to draw the SQUARE: X

What is the base and height of the SQUARE: 4

XXXX

XXXX

XXXX

XXXX




Enter YES to print another figure: YeS




Enter the Figure type (SQUARE OR TRIANGLE): TRIangle




Enter the character to be used to draw the TRIANGLE: A

What is the base and height of the TRIANGLE: 5

A

AA

AAA

AAAA

AAAAA

Enter YES to print another figure: No Good Bye






Save your lab assignment by creating a zip file. Upload your zipped lab project file and a notepad (.txt) file of your program to your Pilot drop box before the lab due date. Ask your TA for help if you have any questions.




Rubric:




Part A – Design Documentation (6)




Pseudo code or flow chart plus sufficient written explanation to fully describe all the parts of the program.




Part B – Program implementation (14)




Method to introduce the program (1)
Method to get and validate input (2)
Method to print triangle – output generated is correct (3)



Method to print square – output generated is correct (3)
Use of StringBuilder to create an output string (1)
Main method to allow user to repeatedly request a figure of some size to be output (2)
Style and commenting – for every function! (2)

More products