Question

In: Computer Science

. Write a program to print * in the following order using 2d array in java...

. Write a program to print * in the following order using 2d array in java

                                             *             *             *             *             *

                                             *             *             *             *

                                             *             *             *

                                             *             *            

                                             *

Solutions

Expert Solution

JAVA CODE:

// printClass class
public class printClass {
        public static void main(String[] args) {
                // 2D array is initialized
                String[][] array = new String[5][5];
                // empty string " " is assigned to all the values in the array
                for (int i = 0; i < 5; i++) {
                        for (int j= 0; j < 5; j++) {
                                array[i][j] = " ";
                        }
                }
                // '*' is assigned to first upper half of the triangle in the array
                for (int i = 0; i < 5; i++) {
                        for (int j = 0; j < 5-i; j++) {
                                array[i][j] = "*";
                        }
                }
                // values stored in the array are printed
                for (int i = 0; i < 5; i++) {
                        for (int j = 0; j < 5; j++) {
                                System.out.print(array[i][j] + "        ");
                        }
                        System.out.print("\n");
                }
        }
}

OUTPUT:


Related Solutions

Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
Program to implement in c++ dont forget to implement the 2D array and print whether the...
Program to implement in c++ dont forget to implement the 2D array and print whether the vehicle is available or not! This is the main part that I dont know how to implement! no good rating if this isnt done. Define a class Vehicle that has the following data members:  Model of the vehicle (e.g., Ford, Toyota) as a standard library string.  The date that vehicle has joined to the fleet use the class Date that has the...
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
In java coding, write a program that will print out a representation of a “W” using...
In java coding, write a program that will print out a representation of a “W” using a character that a user provides.
write a program which will prompt an array of 12 integers then print the array as...
write a program which will prompt an array of 12 integers then print the array as an array of 4 columns on 3 rows
Write a method that will accept a 2D character array. The 2D array represents a grid...
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT