Question

In: Computer Science

Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...

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”

Solutions

Expert Solution

The given java methods will be defined as follows:

  • Create a class VLPUtility.
  • Define method concatStrings. This method will take variable number arguments of type String. In the method body, initialize an empty string variable. Use for each loop to traverse the variable length array of arguments and append each string and a space to the string variable. Then return the string.
  • Define overloaded method concatStrings. This method will take a boolean variable also as a parameter. If the variable is true, change the strings to upper and add this and a space to the string variable and return it. Otherwise, only concatenate the strings and return the result.
  • Define the makeWord method. This method will take variable length arguments of type char. Define the string variable as empty. Use loop to traverse the array of arguments and append the characters to the string. Return the result.
  • Define the overloaded method makeWord. It will take a boolean variable and an int variable also as parameters. The int variable will define the number of times the string will be repeated. The boolean variable defines whether the string has to be reversed or not.

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:


Related Solutions

in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
The language is java Write a class called Tablet that stores information about a tablet's age,...
The language is java Write a class called Tablet that stores information about a tablet's age, capacity (in GB), and current usage (in GB). You should not need to store any more information Write actuators and mutators for all instance data Write a toString method When you print a tablet, the info should be presented as such: This tablet is X years old with a capacity of Y gb and has Z gb used. There is A gb free on...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Coding in Java Assignment Write the following static methods. Assume they are all in the same...
Coding in Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT