Question

In: Computer Science

Write a complete static method named addUp that accepts, in order, an integer number and an...

Write a complete static method named addUp that accepts, in order, an integer number and an integer thisMany as parameters and that prints a complete line of output reporting all the numbers that result from adding number to itself thisMany number of times (starting at 1 and ending at thisMany). For example, the following calls:

addUp(3, 5);

addUp(7, 3);

should produce this output:

Adding up 3 for 5 times gives: 3, 6, 9, 12, 15

Adding up 7 for 3 times gives: 7, 14, 21

Notice that the answers are separated by commas and a space. You must exactly reproduce this format. You may assume that the number of add ups you will be asked to do (thisMany) is greater than or equal to 1. Your method must work with any value of number including negative numbers and zero.

Write a main method that tests your addUp method.

Solutions

Expert Solution

public class temp {
    public static void main(String[] args) {
        addUp(3, 5);
        System.out.println();
        addUp(7, 3);
    }

    // method to display the sum of numbers this many times
    public static void addUp(int number, int thisMany) {
        System.out.print("Adding up " + number + " for " + thisMany + " times gives: ");
        int sum = number;

        // loop to sum and print the value
        for (int i = 0; i < thisMany; i++) {
            System.out.print(sum);
            // display comma and space
            if (i != thisMany-1) {
                System.out.print(", ");
            }
            // calculate sum
            sum = sum + number;
        }
    }

}

FOR HELP PLEASE COMMENT.

THANK YOU


Related Solutions

In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT