Question

Task 6: Create a function in Java that counts the number of even digits in a...

Task 6: Create a function in Java that counts the number of even digits in a positive integer.
public static int countEvenDigits(int a); // Ex, countEvenDigits(1234) => 2

Task 7: Create a generic function that returns digits in a given range. The ones place corresponds to power 0, the tens place corresponds to power 1, the hundreds place corresponds to power 2, and so on. Think 10^3 => 1000 => thousands place. (Do not use exponents in your code.)
// Ex, getDigits(12345,0,0) => 5

Homework Answers

Answer #1


public class CountEvenDigits {

    public static int countEvenDigits(int a) {
        int count = 0;
        if (a == 0) {
            count = 1;
        } else {
            while (a > 0) {
                if ((a % 10) % 2 == 0) {
                    count++;
                }
                a /= 10;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(countEvenDigits(1234));
    }
}

Know the answer?
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
Similar Questions
In Java a function can call itself(we may explore more about this later in the course).  This...
In Java a function can call itself(we may explore more about this later in the course).  This is called a “recursive function”. In order for this to work there must be a situation where the function finally stopscalling itself and simply returns a value (or just returns). We can use a Java ifstatement to make a recursive function return as required to make it work properly. This special case where the function finally returns is called the recursive function base case....
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in...
Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to...
Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to the user The number should be between 2 and 10 inclusive Prompt the user to enter the value of half that number You can assume the user will enter valid numbers, not letters or gibberish They only get one chance to answer per number Tell the user if they are right or wrong Ask the user if they want to try another even number...
Using your favorite IDE (e.g., Eclipse or NetBeans), create a new Java application (project) named CPS151_Lab3,...
Using your favorite IDE (e.g., Eclipse or NetBeans), create a new Java application (project) named CPS151_Lab3, or, if you are using DrJava, create a folder named CPS151_Lab3. Then, to either your project or folder, add: a main class (if one is not automatically created), a VotingMachine class that models a voting machine. Make sure your new class is in the same package as the main class. Add to the VotingMachine class: private integer data fields for the number of votes...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of frogs were sitting together on one block when they had a terrible quarrel. Now they want to jump away from one another so that the distance between them will be as large as possible. The distance between blocks numbered J and K, where JK, is computed as K-J+1. The frogs can only jump up, meaning that they can move from one block to another...
python programming Question #4: # Years ago the Romans used a different system to represent numbers....
python programming Question #4: # Years ago the Romans used a different system to represent numbers. # Instead of using the digits (0, 1, 2, 3, 4, 5, 6, etc.), the Romans # formed numbers by joining combinations of the characters # (I, V, X, L, C, D, and M). # Roman Numeral characters and their integer values are: # I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M...