Question

In: Computer Science

Please don't just copy another solution. Write the following Java program: public static String getFlag(int size,...

Please don't just copy another solution.

Write the following Java program:

public static String getFlag(int size, char color1, char color2, char color3) - This method returns a string where a triangle appears on the left size of the diagram, followed by horizontal lines. For example, calling DrawingApp.getFlag(9, 'R', '.', 'Y'); will generate the string:

R............................................

RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRRRR....................................

RRRRRRRRR....................................

RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

R............................................

The diagram has a number of rows that corresponds to size * 2 and a number of colums that corresponds to size * 5. The first and last row will use color2 (except for the first character that will use color1). The center two rows will use color2 and the rest color3. The triangle will rely on color1 and will have a height corresponding to size * 2. If the size parameter is less than three, the method will return null and will not generate any diagram. For this method you can assume the colors are valid. The method MUST not rely on System.out.println().

Solutions

Expert Solution

Main.java
public class Main {

   public static void main(String[] args) {
       //calling the function and printing the output
       String output = DrawingApp.getFlag(9, 'R', '.', 'Y');
       System.out.println(output);

   }

}

DrawingApp.java
public class DrawingApp {
   public static String getFlag(int size, char color1, char color2, char color3) {
       //return and temporary string
       String returnString = "";
       String temp1 = "";
       String temp2 = "";
       //for rows
       for(int i=0;i<size;i++) {
           temp1 = "";
           //for columns
           for(int j=0;j<size*5;j++) {
               //for color 1
               if(j<=i){
                   temp1 += color1;
               }else if(i==0 || i==size-1) { //1st and last rows is color 2
                   temp1 += color2;
               }else { //other rows is color 3
                   temp1 += color3;
               }
           }
           returnString+=temp1+"\n"; //first half with correct order
           temp2=temp1+"\n"+temp2; //reversed order string for 2nd half
       }
       returnString+=temp2+"\n"; //combining two halves
       return returnString;
   }
}

Sample Output:


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
write a java program that allows main to work public static void main(String[] args) { /*...
write a java program that allows main to work public static void main(String[] args) { /* Start with the empty list. */ LinkedList list = newLinkedList(); // // ******INSERTION****** // // Insert the values deleteFront(list); deleteBack(list); list = insert(list, 1); list = insert(list, 2); list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); list = insert(list, 6); list = insert(list, 7); list = insert(list, 8);    // Basic Operations on the LinkedList printList(list); insertFront(list,0); printList(list); insertBack(list,999); printList(list);...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT