addDigits(123, 1)-> 0
addDigits(7645, -1)-> 0
addDigits(7769, 3)-> 9
addDigits(4678, 2)-> 15
addDigits(3340, 0)-> 10
the given method returns the correct output for the given examples
please drop a comment of there's an issue.
the required method is given as follows:
//---------------------------------------------------------
static int addDigits(int number, int index){
//converts the number to the string and checks the lenght of the string
if (Integer.toString(number).length()!=4){
return 0;
}
//checks if the index is negetive or greater than 3
if (index<0 || index>3){
return 0;
}
int sum=0;
//iterates from 3 to 0
for(int i=3; i>=0;i--){
//slices the last digit of the number into the digit variable
int digit=number%10;
//slices the last digit of the number
number=number/10;
//adds to the digit to the sum if the current "i" is greater than equal to the index
if(index<=i){
sum=digit+sum;
}
}
return sum;
}
In Java Write a method named addDigits which accepts two inputs, num and index. The first...
In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even digits with zero and returns a new integer. The new integer could have less digits. If the given integer is not a 4-digit integer or it is a negative integer the method returns zero. Examples: changeEvens(1000)-> 1000 changeEvens(-1000)-> 0 changeEvens(8764)-> 700 changeEvens(5829)-> 5009 changeEvens(1012)-> 1010
****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...
Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...
Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...
Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...
write in java 1. Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...
Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...
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...
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...
In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...