In: Computer Science
Write a class called VLPUtility with the following static methods: Java Language
1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it.
2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as is.
3. makeWord: accepts a variable length parameter list of char values and make a word from these character values, return the result as a String.
4. Overload this method so that it accepts a variable length parameter list of char values, a boolean variable named reverse, and an int variable named repeat.
a. If the boolean variable is true, the combined char values will be reversed.
b. Based on the value of the int variable, the combined char values will be repeated.
c. The result will be returned. For example, if ‘a’, ‘l’, ‘l’, ‘o’, ‘w’ are passed, it will be combined into “allow”. If the boolean variable is true, it will be reversed to “wolla”; if the int variable value is 2, it will be either “allowallow” or “wollawolla”
The given java methods will be defined as follows:
The java program:
//class VLPUtility
class VLPUtility{
//method to concate strings
public String concatStrings(String ... str){
String result="";
//add each string in the array
for(String i:str){
result += i + " ";
}
//return the result
return result;
}
//overloaded method concatStrings
public String concatStrings(boolean upper, String ... str){
String result = "";
//upper is true
if (upper){
for(String i:str){
//change string to upper case and add
result += i.toUpperCase() + " ";
}
}
//otherwise just add the strings
else{
for(String i:str){
result += i;
}
}
//return the result
return result;
}
//method to make a word using given characters
public String makeWord(char ... letters){
String result = "";
//add char to empty string
for(char ch:letters){
result += ch;
}
//return the result
return result;
}
//overloaded method makeWord
public String makeWord(boolean reverse, int repeat, char ... letters){
int s = letters.length; //find the length of arguments
String result = "";
//if reverse is true
//append words in reverse
if (reverse){
for(int i=1;i<=s;i++){
result += letters[s-i];
}
}
//otherwise add as it is
else {
for(char ch:letters){
result += ch;
}
}
//for repeating words
String result2 = "";
for(int i=0;i<repeat;i++)
result2 += result;
result = result2;
//return the result
return result;
}
}
class Main{
public static void main(String[] args) {
//create object of VLPUtility class
VLPUtility V= new VLPUtility();
//call method concatStrings
System.out.println(V.concatStrings("abc","def","geh"));
//cal overloaded method concatStrings
System.out.println(V.concatStrings(true,"abc","def","geh"));
//call overloaded method concatStrings
System.out.println(V.concatStrings(false,"abc","def","geh"));
//call method makeWord
System.out.println(V.makeWord('c','a','t'));
//call overloaded method makeWord
System.out.println(V.makeWord(true,3,'c','a','t'));
}
}
Sample output: