Page 1 of 1

Hello can anyone help me consolidate this code. it is written in eclipse, its split into 6 different segments denoted by

Posted: Sun Jul 10, 2022 11:30 am
by answerhappygod
Hello can anyone help me consolidate this code. it is written ineclipse, its split into 6 different segments denoted by a doubleslash and indention example
below is copy of my code
"
package app;
import java.util.Random;
public class DrawingApp {
public static void main(String[] args)
{
/*
* For every method remove the line with "throw ..." andimplement the method.
* We are using "throw..." so your code does not displayany compilation errors
* when you import the zip file. Also, if you don'timplement a method you will
* see a white square (instead of green) in the submitserver
*/
public static String getRectangle(int maxRows, intmaxCols, char symbol) {
if(maxRows<1 || maxCols<1){
returnnull;
}
else {
String result= "";
for (int i =0; i < maxRows; i++) {
for (int j = 0; j < maxCols; j++) {
result = result + symbol;
}
result = result + "\n";
}
returnresult;
}
{
public static void r(String[] args){
System.out.print(DrawingApp.getRectangle(6, 9, '*'));
}
//
public static String getFlag(int size, char color1, charcolor2, char color3) {
public static void flag(String[] args){
System.out.println(DrawingApp.getFlag(9, 'R', '.','Y')); //invoking the getFlag method
}
public static String getFlag(intsize, char color1, char color2, char color3) {
//if thesize < 3, then return null
if (size< 3)
returnnull;
//ifsize more than 2
StringBuilderbuilder = new StringBuilder(""); //output stringbuilder
//mainloop, for no:of rows (size x 2)
for(inti=1; i <= (size * 2); i++) {
//subloop, for no:of columns (size x 5)
for(intj=1; j <= (size * 5); j++) {
//forfirst & last lines
if(i==1|| i== (size * 2)) {
if(j==1) //for first character
builder.append(color1);
else //for all other characters in first & lastline
builder.append(color2);
}elseif( i == size || i == (size + 1)) { //for the middle 2lines
if(j<= size) //if column is less than size
builder.append(color1);
else //for all other column values
builder.append(color2);
}else{ //for all other rows other than first, last and middle 2lines
if(i<= size) { //for the first part
if(j <= i)
builder.append(color1);
else
builder.append(color3);
}else{ //for second part
if(j <= (size - (i % (size + 1))))
builder.append(color1);
else
builder.append(color3);
}
}
}
builder.append("\n"); //for each new row
}
returnbuilder.toString(); //return the string
}
}
}
//
public static String getHorizontalBars(int maxRows, intmaxCols, int bars, char color1, char color2, char color3){
int rows = maxRows/bars;
String result = "";
for(int j = 0;j<bars;j++){
for (int i = 0; i < rows;i++) {
for (int k =0; k < maxCols; k++) {
if (j % 3 == 0) {
result += color1;
} else if (j % 3 == 1) {
result += color2;
} else {
result += color3;
}
}
result +="\n";
}
}
return result;
}
//
public static String getVerticalBars(int maxRows, intmaxCols, int bars, char color1, char color2, char color3){
char[] colors = new char[3];
colors[0] = color1;
colors[1] = color2;
colors[2] = color3;
int currentColorIndex = 0;
char currentColor = colors[0];
int sizeOfBar = maxCols/bars;
String str = "";
String ans = "";
for(int i=0;i<maxCols;i++){
str += currentColor;
if(i%sizeOfBar==sizeOfBar-1){
currentColorIndex =(currentColorIndex+1)%3;
currentColor = colors[currentColorIndex];
}
}
for(int j=0;j<maxRows;j++){
ans += str;
ans += "\n";
}
return ans;
}
public static void vbar(String[] args) {
System.out.println(getVerticalBars(10,12,3,'R','G','B'));
System.out.println(getVerticalBars(5,12,4,'R','G','B'));
System.out.println(getVerticalBars(5,20,5,'R','G','B'));
}
//
public static char getRandomColor(Random random){
int choice =random.nextInt(6);
if (choice == 0){
return'R';
} else if (choice == 1){
return'G';
} else if (choice == 2){
return'B';
} else if (choice == 3){
return'Y';
} else if (choice == 4){
return'*';
} else {
return'.';
}
}
public static void rc(String[] args){
Random random = newRandom();
System.out.println("Randomcolor: " + getRandomColor(random));
System.out.println("Randomcolor: " + getRandomColor(random));
System.out.println("Randomcolor: " + getRandomColor(random));
System.out.println("Randomcolor: " + getRandomColor(random));
}
//
private static boolean isValidColor(charcolor) {
if (color == 'R' || color =='G' || color == 'B'|| color == 'Y' || color == '*' || color == '.'){
returntrue;
} else {
returnfalse;
}
}
}
}
thank you for any and all help.