Question

Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

Lab 7: Methods

1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.

2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 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:

o 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”.

• 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Here are the methods for all the problems.
thanks and do give a thumbs up from your end : )

=========================================================================

import java.util.Random;

public class Numbers {

    /*
    Write a method called cubeIt that accepts one integer parameter
    and returns the value raised to the third power as an integer.
     */
    public static int cubeIt(int number) {

        return number * number * number;

    }

    /*
    Write a method called randomInRange that accepts two integer parameters representing a range.
    The method returns a random integer in the specified range inclusive.
     */

    public static int randomInRange(int start, int end) {

        Random random = new Random();
        return start + random.nextInt(end - start);
    }

    /*
    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.
     */
    public static boolean larger(double firstNumber, double secondNumber) {
        return firstNumber > secondNumber;
    }

    public static void main(String[] args) {

        System.out.println(cubeIt(5));
        System.out.println(randomInRange(10,100));
        System.out.println(larger(7,6.99999));
    }
}

================================================================

public class Characters {

    /*
    Write a method called countA that accepts a String parameter and
    returns the number of times the character ‘A’ is found in the string.
     */
    public static int countA(String sentence){

        int countA=0;
        for(char letter:sentence.toCharArray()){
            countA+=letter=='A'?1:0;
        }
        return countA;
    }

    /*
    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.
     */

    public static String multiConcat(String word, int count){
        String multiWord="";
        for(int time=1;time<=count;time++){
            multiWord+=word;
        }
        return multiWord;
    }

    public static void main(String[] args) {
        System.out.println(countA("At times there is A"));
        System.out.println(multiConcat("hi",3));
    }

}

======================================================================

Add a comment
Know the answer?
Add Answer to:
Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...
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
  • 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...

    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 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...

  • 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%...

  • 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.

  • 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...

  • 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...

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • write a program in java that asks the user for a sentence, and then returns that...

    write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by...

  • I am trying to create a program called Count.java that contains a single method called compute...

    I am trying to create a program called Count.java that contains a single method called compute with a string parameter called value. The method should count the number of newlines, words (any sequence of characters separated with whitespace), and characters in a given string and return an array of the three integer values. Use a Scanner to count the number of words in a string. The Scanner.next() method returns the next word, ignoring whitespace.

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

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