Question

Coding Requirements for Assignment2_1.java:

Based on Assignment1.java, re-do all 7 methods from triangle_0 to triangle_6 by storing characters (which could be symbols or digits) in a 2D "square" array, meaning it has equal number of rows and columns. Now, because each triangle is supposed to contain rows filled with different number of characters, you are required to fill each row with only required number of characters in this square array. For example, in an up-side-down 8-row triangle of "$", the 3rd row from top should be filled with only 6 "$" and leave two not assigned with anything (i.e., each is null).

This assignment also requires to change all while-loop statements to for-loop statements, or the other way around, depending which was used on Assignment1. For each method of triangle_x, you should comment out the entire block of statements of the method you did in Assignment1, and write new statements on top of the comment. This allows your old method to be compared and verified with the new one developed in this work.

Finally, because you won't print every character and line as in Assignment1, instead, you store them in arrays. So, to print the array to show the triangle at the end of each triangle_x method, you are particularly required to use only enhanced for-loop statements, i.e., the one using ":" with no counter or index variables.

//Assignment 1 code

public class Assignment1 {
   public static void main(String args[]) {
int totalLines = Integer.parseInt(args[0]);
  
triangle_0(totalLines);
  
triangle_1(totalLines, args[1]);
triangle_2(totalLines);
triangle_3(totalLines);
triangle_4(totalLines, args[1]);
triangle_5(totalLines);
triangle_6(totalLines);
   }
  
   private static void triangle_0(int max){
int lineCount = 0, widthCount = max;
  
System.out.println("\ntriangle_0\n");
while (lineCount <= max){
while (widthCount >= 1){
if (widthCount == 1)
System.out.println("*");//move to new line after print, if it is the last "*" of the line
else
System.out.print("*");
widthCount = widthCount - 1;
}
lineCount = lineCount + 1;
widthCount = max - lineCount;
}
   }
  
   private static void triangle_1(int max,String displayvalue){
       int lineCount = 0, widthCount = max;
  
System.out.println("\ntriangle_1\n");
while (lineCount <= max){
while (widthCount >= 1){
if (widthCount == 1)
System.out.println(displayvalue);//move to new line after print, if it is the last "*" of the line
else
System.out.print(displayvalue);
widthCount = widthCount - 1;
}
lineCount = lineCount + 1;
widthCount = max - lineCount;
}
   }
  
   private static void triangle_2(int max){
       int lineCount = 0, widthCount = max;
  
System.out.println("\ntriangle_2\n");
while (lineCount <= max){
while (widthCount >= 1){
if (widthCount == 1)
System.out.println(max-lineCount);//move to new line after print, if it is the last "*" of the line
else
System.out.print(max-lineCount);
widthCount = widthCount - 1;
}
lineCount = lineCount + 1;
widthCount = max - lineCount;
}
   }
  
   private static void triangle_3(int max){
       int lineCount = 0, widthCount = max;
  
System.out.println("\ntriangle_3\n");
while (lineCount <= max){
while (widthCount >= 1){
if (widthCount == 1)
System.out.println(widthCount);//move to new line after print, if it is the last "*" of the line
else
System.out.print(widthCount);
widthCount = widthCount - 1;
}
lineCount = lineCount + 1;
widthCount = max - lineCount;
}
   }
  
   private static void triangle_4(int max,String displayvalue){
       int lineCount , widthCount;
       System.out.println("\ntriangle_4\n");
       for(lineCount=1;lineCount<=max;lineCount++){
           for(widthCount=1;widthCount<lineCount;widthCount++){
               System.out.print(displayvalue);
           }
           System.out.println(displayvalue);
       }
   }
  
   private static void triangle_5(int max){
       int lineCount , widthCount;
       System.out.println("\ntriangle_5\n");
       for(lineCount=1;lineCount<=max;lineCount++){
           for(widthCount=1;widthCount<lineCount;widthCount++){
               System.out.print(lineCount);
           }
           System.out.println(lineCount);
       }
   }
  
   private static void triangle_6(int max){
       int lineCount , widthCount;
       System.out.println("\ntriangle_6\n");
       for(lineCount=1;lineCount<=max;lineCount++){
           for(widthCount=1;widthCount<lineCount;widthCount++){
               System.out.print(widthCount);
           }
           System.out.println(widthCount);
       }
   }
  
}

CCWindowsSystem32cmd.exe Microsoft Windows [Version 18.0.17134.765] (c) 2018 Microsoft Corporation. All rights reserved. E:\>

CCAWindows System32cmd.exe 4444 1 triangle_3 4321 321 21 1 triangle_4 $$ $$$$ triangle_5 333 4444 triangle_6 1 12 123 1234 E:

CCWindowsSystem32cmd.exe Microsoft Windows [Version 18.0.17134.765] (c) 2018 Microsoft Corporation. All rights reserved. E:\>javac Assignment4.java E: \>java Assignment4 4$ triangle_e triangle_1 $$$$ triangle_2 4444 333 22 1 triangle_3 4321 321 1 triangle_4 $ $$$$ 01:27 ENG O Type here to search A INTL 05-06-2019
CCAWindows System32cmd.exe 4444 1 triangle_3 4321 321 21 1 triangle_4 $$ $$$$ triangle_5 333 4444 triangle_6 1 12 123 1234 E:\>. 01:27 ENG O Type here to search O INTL 05-06-2019
0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA PROGRAM :- NOTE :- I have modified the code such that functions don't print the values but store in a 2d array i.e a square matrix and return that matrix to the caller function. There I print the matrix in-order to show what is stored. I have attached the output to this answer and also switched while loop into for loop

public class Assignment1 {
  
static void print_t_d_arr(char[][] arr){
for(int i = 0;i<arr.length;i++){
for(int j = 0;j<arr.length;j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
  
public static void main(String args[]) {
  
int totalLines = Integer.parseInt(args[0]);
  
char[][] matrix = new char[totalLines][totalLines];
  
matrix = triangle_0(totalLines);
System.out.println("\ntriangle_0\n");
print_t_d_arr(matrix);
  
matrix = triangle_1(totalLines, args[1]);
System.out.println("\ntriangle_1\n");
print_t_d_arr(matrix);
  
matrix = triangle_2(totalLines);
System.out.println("\ntriangle_2\n");
print_t_d_arr(matrix);
  
matrix = triangle_3(totalLines);
System.out.println("\ntriangle_3\n");
print_t_d_arr(matrix);

matrix = triangle_4(totalLines, args[1]);
System.out.println("\ntriangle_4\n");
print_t_d_arr(matrix);
  
matrix = triangle_5(totalLines);
System.out.println("\ntriangle_5\n");
print_t_d_arr(matrix);
  
matrix = triangle_6(totalLines);
System.out.println("\ntriangle_6\n");
print_t_d_arr(matrix);
}
  
private static char[][] triangle_0(int max){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount <= max; lineCount++){
for(int widthCount = max - lineCount; widthCount >= 1; widthCount--){
td_arr[lineCount][widthCount-1] = '*';
}
}
  
return td_arr;
}
  

private static char[][] triangle_1(int max,String displayvalue){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount <= max; lineCount++){
for(int widthCount = max - lineCount; widthCount >= 1; widthCount--){
td_arr[lineCount][widthCount-1] = displayvalue.charAt(0);
}
}
  
return td_arr;
}
  
private static char[][] triangle_2(int max){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount <= max; lineCount++){
for(int widthCount = max - lineCount; widthCount >= 1; widthCount--){
td_arr[lineCount][widthCount-1] = Character.forDigit(max-lineCount,10);
}
}
  
return td_arr;
}

private static char[][] triangle_3(int max){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount <= max; lineCount++){
for(int widthCount = max - lineCount; widthCount >= 1; widthCount--){
td_arr[lineCount][widthCount-1] = Character.forDigit(widthCount,10);
}
}
  
return td_arr;
}
  
  
private static char[][] triangle_4(int max,String displayvalue){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount < max; lineCount++){
for(int widthCount = 0; widthCount <= lineCount ; widthCount++){
td_arr[lineCount][widthCount] = displayvalue.charAt(0);
}
}
  
return td_arr;
}

private static char[][] triangle_5(int max){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount < max; lineCount++){
for(int widthCount = 0; widthCount <= lineCount ; widthCount++){
td_arr[lineCount][widthCount] = Character.forDigit(lineCount+1,10);
}
}
  
return td_arr;
}

private static char[][] triangle_6(int max){
char[][] td_arr = new char[max][max];
  
for(int lineCount = 0; lineCount < max; lineCount++){
for(int widthCount = 0; widthCount <= lineCount ; widthCount++){
td_arr[lineCount][widthCount] = Character.forDigit(widthCount+1,10);
}
}
  
return td_arr;
}

Output:-

Interactive mode CommandLine Argunents OFF Focus View: 4 $ OFF Version: JDK 18.e.1 Stdin Inputs... External Libraries O Add E  
}

Add a comment
Know the answer?
Add Answer to:
Coding Requirements for Assignment2_1.java: Based on Assignment1.java, re-do all 7 methods from triangle_0 to triangle_6 by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • We learn arrays (and Arrays class) in Chapter 10 and ArrayList collection in Chapter 14. This...

    We learn arrays (and Arrays class) in Chapter 10 and ArrayList collection in Chapter 14. This assignment asks you to re-do Assignment 4 by using arrays and ArrayList collections to deliver exactly the same output. The input also stays the same, which are two argument values entered at the command line when 'java' command is issued. With the use of these data structures that help to store data in certain way, you are not going to print each character immediately...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • I need a java program that prints the following: 1* 2** 3*** 4**** 5***** 6****** 7*******...

    I need a java program that prints the following: 1* 2** 3*** 4**** 5***** 6****** 7******* 8******** 9********* This is what I have so far, but I cannot figure out how to fix it. public class HelloWorld{ public static void main(String []args){ System.out.println("Pattern Two");    for (int line=1; line<10; line++){ System.out.println(); for (int j=1; j System.out.print(line); for (int i=1; i System.out.print("*"); } } } } }

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • Need help debugging. Create an application that keeps track of the items that a wizard can...

    Need help debugging. Create an application that keeps track of the items that a wizard can carry. console application which has no errors: import java.util.Scanner; public class Console {        private static Scanner sc = new Scanner(System.in);     public static String getString(String prompt) {         System.out.print(prompt);         String s = sc.nextLine();         return s;     }     public static int getInt(String prompt) {         int i = 0;         boolean isValid = false;         while (!isValid) {             System.out.print(prompt);...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket....

    The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient {       private static final String SERVER = "dict.org";    private static final int PORT = 2628;    private static final int TIMEOUT = 15000;    public static void main(String[] args) {        Socket   ...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT