Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA LANGUAGE - Follow the instructions as closely as possible. Comments are wel

ID: 3918546 • Letter: J

Question

JAVA LANGUAGE - Follow the instructions as closely as possible. Comments are welcomed

Require .txt file: "config.txt"

int //size

int //number of logos

char //first letter

char //second letter

Summary

Extend your understanding of Repetition Control Structures by building a picture of triangles. This picture will be configurable with respect both triangle size and the number of triangle rows, as specified by a configuration file your program will parse.

Introduction – Sample Executions

Your assignment is to write a program that draws rows and rows of triangles. It may be easier to view sample executions of such a program, and so I’ve included some below. Your program will read from an input file (“config.txt”) the size of each triangle and the number of rows of triangles using the Scanner technique covered in class or lab. In addition, your software will read in two characters from the input file, and use those characters to draw the triangle picture.

For added challenge, try using JOptionPane.showMessageDialog(null, new JTextArea(String)) to display the triangle string pattern you have created

Empirical & Black Box Analysis

Black Box testing focuses on the external behaviours of a system while specifically ignoring the internal design details (which may not be available, either, as it is in this case). Studying the output in the figures above will reveal these invariants that should be built into your software:

(1) The number of triangles for a given row is proportional to the depth of the row. a. So row 1 has 1 triangle, row 2 has two triangles, etc.

(2) The number of rows internal to a triangle is proportional to the size of the triangle.

(3) Ultimately, you’re building one big box where every line (or row) is composed of (nested) looping patterns similar to the following pseudocode: Loop(on number of rows)

Loop(draw left border(s)) (0 to many times)

Loop(draw triangle(s)) (0 to many times) //note this one step expands to a similar pattern

Loop(draw right border(s)) (0 to many times)

Note that the “draw triangle(s)” step bolded above actually represents multiple sub-steps, just as a method call actually represents multiple lines of code (although one line methods do exist). Every triangle is composed of a left border of stars, the spaces that compose the internal triangle, and then the right border. Determining just how many stars, spaces, and then stars to print is up to you. Using the pattern above as a template, you should add the additional sub-steps omitted above before the pseudocode is detailed enough to be simply translated into code.

Program Requirements

There exists many ways to build this code, but the requirements stay the same. Your software should:

(1) Read in 4 data items from a sequential text file using Scanner.

(2) Draw a picture composed of triangles, using the input data to determine:

a. How large each triangle will be (triangle size)

b. How many rows of triangles will be displayed (row count)

c. What character should be used to draw the triangles (char 1)

d. A second character used to draw the space around the triangles (char 2)

(3) Your output should be compared against the sample output for accuracy.

a. Note that the number of triangles drawn per row is directly proportional to the row count itself. So, for the first row, one triangle is drawn, and for the second row, two, and so on.

(4) Break up this program into smaller functions and call those

a. You can call a function inside of a loop

Use as many Comments as possible

E ? config.txt 1 int //size 2 int //number of logos 3 char //first letter 4 char //second letter

Explanation / Answer

import java.util.Scanner;
import java.io.IOException;
import java.io.*;

public class TriangleBuild
{
    public static void main(String[] args) throws IOException {

        Scanner keyboard = new Scanner(new File("config.txt"));
        //create scanner object, and a new file, and define the txt file to read input from

        while (keyboard.hasNext()){
            //while there is a next token in the txt file to be read (keeps going until end)

            int triangleSize = keyboard.nextInt(); //store first int
            int triangleRows = keyboard.nextInt(); //store second int
            String junk = keyboard.nextLine(); //get rid of next line
            char ch1 = keyboard.next().charAt(0); //store first char
            char ch2 = keyboard.next().charAt(0); //store second char

            System.out.println("size: "+ triangleSize + " rows: " + triangleRows +
                    " char 1: " + ch1 + " char 2: " + ch2 + " ");
            //print out size, rows, chars 1 and 2 to check if read correctly

            triangleDraw(triangleSize, triangleRows, ch1, ch2); //call method with new parameters
            System.out.println();
        }


        //triangleDraw(3,2,'*','!'); //test
    }

    public static void triangleDraw (int triangleSize, int triangleRows, char ch1, char ch2) {

        int size = triangleSize; // size of triangle
        int boxLength = ((triangleSize*2)*(triangleRows))-(triangleRows*2);
        if (size == 0) { //if there are 0 rows, then just print boxes. no triangles.
            for (int i = 0; i < triangleRows*2; i++) { //columns
                for (int j = 0; j < triangleRows*2; j++) { //rows
                    System.out.print(ch1); //character 1 used to print out the box
                }
                System.out.println();
            }
        } else if (triangleRows == 1) {
            for (int y = size; y >= 0; y--) { //columns

                for (int x = size; x >= 0; x--) { //right triangle rows
                    if (x>(y-1)) {
                        System.out.print(ch1);
                    } else {
                        System.out.print(ch2);
                    }
                }

                for (int x = 1; x<= size+1; x++) {//upside down right triangle rows
                    if (y>=x) {
                        System.out.print(ch2);
                    } else {
                        System.out.print(ch1);
                    }
                }
                System.out.println(); //new line
            }
        } else if (triangleRows > 1) {
            for (int rows = 1; rows <= triangleRows; rows++) { //iterates over rows
                for (int y = size; y >= 0; y--) { //columns


                    //whole rows of boxes+triangles
                    int count = 1;
                    if (rows < triangleRows) { //print boxes on rows that are NOT the last row
                        for (int box = 1; box <= boxLength/(Math.pow(2,rows)); box++){
                            //figure out size of box
                            //print box
                            System.out.print(ch1);
                        }
                    }


                    for (int numTriangle = 1; numTriangle <= rows; numTriangle++){
                        //number of triangles in a row
                        //row 1 = 1 tri; row 2 = 2 tri; etc

                        for (int x = size; x >= 0; x--) { //right triangle
                            if (x>(y-1)) {
                                System.out.print(ch1);
                            } else {
                                System.out.print(ch2);
                            }
                        }

                        for (int x = 1; x<= size+1; x++) {//upside down right triangle
                            if (y>=x) {
                                System.out.print(ch2);
                            } else {
                                System.out.print(ch1);
                            }
                        }


                    }


                    if (rows < triangleRows) {
                        //print box on either side of triangles on all rows except last row
                        for (int box = 1; box <= boxLength/(Math.pow(2,rows)); box++){ //size of box
                            //print box
                            System.out.print(ch1); //using character 1
                        }
                    }

                    System.out.println(); //next line
                }


            }
        } else if (triangleRows <= 0) {
            System.out.println("Error."); //no negative #'s or 0's are allowed
        }
    }
}