Question

I need java code for the following problem.

Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: WritePlan each method before coding. Write down what values(s) are sent to the method and what is returned. For example, the cubel

Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method called larger that accepts two double parameters and returns true if the first parameter is greater and false otherwise. Use the Boolean data type. 3. Write a Java program called Characters that calls the following methods and displays the returned value: Write a method called countA that accepts a String parameter and returns the number of times the character 'A' is found in the string. o Write a method called multiConcat that takes a String and integer as parameters. Return a String that consists of the string parameter concatenated with itself count times, where the count is the integer parameter. For example, if the parameter values are "hi" and 3, the return value is "hihihi". o .Display the returned values in main There is no user input. . Set up variables and change the values to test the methods. Submit only one .java file
Plan each method before coding. Write down what values(s) are sent to the method and what is returned. For example, the cubelt method: * receives one integer from main. returns an integer to main The method header should look like this: public static int cubeltlint x) *you don't have to use the name x. The randomlnRange method receives two integers from main. returns one integer to main. The method header should look like this: public static int randonlnRange(int start, int ena) you don't have to use the name start and end.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below, I have mentioned all the details in the comments:

Main.java

public class Main {
    public static void main(String[] args) {
        //test for all the methods
        System.out.println("Test for cubeIt");
        System.out.println("return value of cubeIt(2): " + Numbers.cubeIt(2));
        System.out.println("return value of cubeIt(3): " + Numbers.cubeIt(3));
        System.out.println();
        System.out.println("Test for randomInRange");
        System.out.println("return value of randomInRange(2): " + Numbers.randomInRange(10,20));
        System.out.println("return value of randomInRange(2): " + Numbers.randomInRange(1,5));

        System.out.println();
        System.out.println("Test for countA");
        System.out.println("return value of countA(\"RANDOM\"): " + Characters.countA("RANDOM"));
        System.out.println("return value of countA(\"ABCABCAA\"): " + Characters.countA("ABCABCAA"));

        System.out.println();
        System.out.println("Test for countA");
        System.out.println("return value of multiConcat(\"hi\",3): " + Characters.multiConcat("hi",3));

    }
}

class Numbers{
    public static int cubeIt(int x){
        //returnt the x^3 using pow method
        return (int)Math.pow(x,3);
    }

    public static int randomInRange(int min,int max){
        int val;
        //get the value using Math.Random()
        val = min + (int)(Math.random() * ((max - min) + 1));
        return val;
    }
}

class Characters{
    public static int countA(String val){
        int count = 0;

        //count each character by comparing it with the 'A'
        for (int i=0; i<val.length(); i++)
        {
            // checking character in string
            if (val.charAt(i) == 'A')
                count++;
        }
        return count;
    }

    public static String multiConcat(String s, int count){
        //get the string builder object
        StringBuilder sb = new StringBuilder();
        while(count-- > 0){
            //conacat for count times
            sb.append(s);
        }
        //return string value
        return sb.toString();
    }
}

Output:

C:\Program Files Java jdk1.8.0_151 binljava .. . Test for cubeIt return value of cubeIt (2): 8 11 |료 return value of cube I

Add a comment
Know the answer?
Add Answer to:
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • By using Java public static methods solve following problem Write a method called vowelCount that accepts...

    By using Java public static methods solve following problem Write a method called vowelCount that accepts a String as its only parameter, and returns an array int[5] containing the count of vowels a,e,i,o,u in that String, using ignoreCase. For example, vowelCount("") returns {0,0,0,0,0}, and for the callvowelCount("Bill Iverson") your method returns {0,1,2,1,0} because there is one 'e' and two 'i' vowels (ignore case) and one 'o' with zero counts for 'a' and 'u' vowels.

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Write a java Program Initialize Array. Write a Java method initArray() with the following header to...

    Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • Write four overloaded methods called randomize. Each method will return a random number based on the...

    Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT