Question

In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

input code:

output:

code:

public class Main
{
public static int changeEvens(int n)
{
/*declare the variables*/
int newnumber=0,m=0,multiply=10,i=0;
/*check number is less than zero or greter than 10000*/
if(n<0 || n<1000 || n>9999)
{
return 0;
}
else
{
/*take one array for store the digits*/
int array[]=new int[4];
/*store all the digits in the array*/
for(i=0;i<4;i++)
{
array[i]=n%10;
n=n/10;
}
/*make new number with zeros*/
for(i=3;i>=0;i--)
{
/*check first digit is even or not*/
if(i==3 && array[i]%2==0)
{
newnumber=newnumber*1;
}
/*check after first even or not*/
else if(array[i]%2==0)
{
array[i]=0;
newnumber=newnumber*multiply;
}
/*do this for odd digits*/
else
{
newnumber=newnumber*multiply+array[i];
}
}
return newnumber;/*return newnumber*/
}
}
   public static void main(String[] args)
   {
   /*call method*/
       System.out.println(changeEvens(1000));
       System.out.println(changeEvens(-1000));
       System.out.println(changeEvens(8764));
       System.out.println(changeEvens(5829));
       System.out.println(changeEvens(1012));
   }
}

Add a comment
Know the answer?
Add Answer to:
In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even...
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
  • In Java Write a method named addDigits which accepts two inputs, num and index. The first...

    In Java Write a method named addDigits which accepts two inputs, num and index. The first input parameter num is a 4-digit positive integer and index is an int value which refers to a digit that the summation should start from. For example if the given num is 2345 and the index is 1, starting from the second digit, the result of 12 is returned which is the sum of 3, 4, and 5. If the num is not a...

  • Java Programming Write a method named isEven that accepts an integer argument. The method should return...

    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 factorial that accepts an integer n as a parameter and returns the...

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

  • Write in Java Write code which checks validity of a 3-digit positive integer entered by the...

    Write in Java Write code which checks validity of a 3-digit positive integer entered by the user. The number is considered valid (true) if the sum of the first two digits is less than the last. Otherwise it is invalid (false).

  • Write a function named factorCount that accepts an integer (assumed to be positive) as its parameter...

    Write a function named factorCount that accepts an integer (assumed to be positive) as its parameter and returns a count of its positive factors. For example, the eight factors of 24 are 1, 2, 3, 4, 6, 8, 12, and 24, so the call of factorCount(24) should return 8. Program is c++

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

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

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

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

  • java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter...

    java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter n contains only odd digits (1, 3, 5, 7, or 9), and returns false otherwise. Zero is counted as an even digit whenever it appears inside the number. Remember that for a positive integer n, the expression (n % 10) gives its last digit, and the expression (n / 10) gives its other digits. Correct answer true false false false 357199 7540573 97531000

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

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

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