Java programming lanuage please help as soon as possible Your assignment is to w
ID: 3778187 • Letter: J
Question
Java programming lanuage
please help as soon as possible
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.
in the config.txt file is
int //size
int //number of logos
char //first letter
char //second letter
__________
// end of .txt file
Program Requirements
There exists many ways to build this code, but the requirements stay the same. Your software should:
Read in 4 data items from a sequential text file using Scanner.
Draw a picture composed of triangles, using the input data to determine:
How large each triangle will be (triangle size)
How many rows of triangles will be displayed (row count)
What character should be used to draw the triangles (char 1)
A second character used to draw the space around the triangles (char 2)
Your output should be compared against the sample output for accuracy.
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.
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)
Explanation / Answer
import java.util.Scanner; //can use Scanner class functions
import java.io.IOException; //used for the try-catch
import java.io.*; //file 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
}
}
/*
* @JessicaNguyen
* 5/19/15
*
* Use for loops to print out the specified triangles.
*
*/
import java.util.Scanner; //can use Scanner class functions
import java.io.IOException; //used for the try-catch
import java.io.*; //file 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
}
}
}
System.out.println(); //next line
}
}
} else if (triangleRows <= 0) {
System.out.println("Error."); //no negative #'s or 0's are allowed
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.