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

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...
(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...
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 )...
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...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
true or false give reason language in java 1.A static inner class can access the instance...
true or false give reason language in java 1.A static inner class can access the instance variables and methods of its outer non-static class 2.executeQuery from statement may return more than one resultset object 3.Connection is java.sql interface that establishes a session with a specific database 4.Writable is an interface in Hadoop that acts as a wrapper class to almost all the primitive data type 5.Text is the wrapper class of string in Hadoop
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT