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

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)....
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT