1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
blagie [28]
3 years ago
9

In Java, write a AllDigitsOdd program that has a method called allDigitsOdd that takes an integer and returns whether every digi

t of an integer is odd (boolean).
returns true if the integer consists entirely of odd digits (1,3,5,7,9)
returns false if any of its digits are even (0,2,4,6,8)
Consider the use of mod (%).
You should complete the method with integers only. You should not convert the integer to a String
Method call examples:
allDigitsOdd(135319) would return true
allDigitsOdd(-9145293) would return false
Your main method should:

//Prompt the user for an integer.
//You can assume the user always types an integer.
//Then print whether or not the number has all odd digits.
Computers and Technology
1 answer:
yan [13]3 years ago
8 0

Answer:

Hi there! The solution for this problem can be implemented a number of ways. Please find my solution below along with the explanation.

Explanation:

The prompts are fairly easily implemented using the Scanner class in Java for user input. In the allDigitsOdd function, we can perform a Math operation on the number being passed into the function to determine the number of digits in the number. We use the log10 function to do this and convert the double value returned to int. This is calculated in the code below as:  "(int)(Math.log10(number) + 1); ". Once we have the number of digits, we write a simple loop to iterate over each digit and perform a modulus function on it to determine if the digit is odd or even. I have declared 2 variables call odd and even that get incremented if the mod function (%) returns a 1 or a 0 respectively. Finally, the result is displayed on the screen.

AllDigitsOdd.java

import java.util.Scanner;

import java.lang.Math;

public class AllDigitsOdd {

 public static void main(String args[]) {

   // print main menu for user selection

   System.out.println("Please enter a number");

   Scanner scan = new Scanner(System.in);

   int selection = scan.nextInt();

   if (allDigitsOdd(selection)) {

     System.out.println("All numbers are odd");

   } else {

     System.out.println("All numbers are not odd");

   }

 }

 public static boolean allDigitsOdd(int number) {

   int digits = (int)(Math.log10(number) + 1);

   int even = 0;

   int odd = 0;

   for (int index = 1; index <= digits; index++) {

       if ((int)(number / Math.pow(10, index - 1)) % 10 % 2 == 0) {

         even += 1;

       } else {

         odd += 1;

       }

   }

   if (even == 0 && odd > 0) {

     return true;

   } else {

     return false;

     //if (odd > 0 && even == 0) {

     //  return true;

     //}

   }

 }

}

You might be interested in
Who is the person responsible for creating the original website content?
Romashka [77]

Answer: D) Content creator

Explanation:

  • Content creator are responsible for creating the original content of the website. They contribute writing the blog posts about various topics and promote the content with the help of digital and social media.
  • Content creator are basically responsible for writing blog post on the industry based topics, graphic design and video editing.

Content editor uses data and evaluation from the users for analysis. It basically includes design and development to enhanced the material on the website.

Static and dynamic information are related to computer terminologies.

Therefore, (D) option is correct.

8 0
3 years ago
A must decode this code. I guess is not a Cesar. The last line is a email adress.
Pie
It is a text in Polish language.
Powiedz nam w czym jesteś dobry

Magento
Drupal
DjangoSymfony
Python
PHP
Zaznaczone?
Teraz wyślij formularz z konsoli przeglądarki:

require('jquery')[WON'T TRANSLATE THIS LINE BECAUSE IT IS IMMORAL-> YES, I AM A PROGRAMMER :)) ]



3 0
3 years ago
How many generations of computers languages have there been since the middle of the 20th century
Molodets [167]
<span>There are 4 computer language generations. First is the first generation language or 1GL, second is the second-generation languages or the 2GL, next is the third-generation languages or the 3GL, and the last is fourth-generation languages or the 4GL.</span>
4 0
2 years ago
Read 2 more answers
Which set of symbols can be used to determine which calculations to perform first?
Monica [59]

Answer:

parentheses

Explanation: hope i helped <3 °ω°

5 0
3 years ago
Read 2 more answers
Question # 1 Multiple Select Which features are important when you plan a program? Select 4 options. Knowing what information is
Black_prince [1.1K]

Answer:

c

Explanation:

7 0
3 years ago
Other questions:
  • Gina's teacher has sent her a Word document that contains the names of all the students who are participating in the
    11·2 answers
  • You have a large dataset that will print on several pages. You want to ensure that related records print on the same page with c
    11·1 answer
  • Python provides a special version of a decision structure known as the ________ statement, which makes the logic of the nested d
    13·1 answer
  • What is an individual section machine?
    14·1 answer
  • When compared to defender and analyzer firms, early adopters of new technologies tend to be?
    13·2 answers
  • Exercise#3: Write a program that performs the following: Declare a two arrays called arrayA and arrayB that holds integer and th
    14·2 answers
  • JUST NEED TO KNOW WHO ALL DOSE EDGINUITY
    12·2 answers
  • If you misspell a word in your Java program it may be true that I. the program will not compile II. the program may compile, but
    6·1 answer
  • Create a Flowchart and write pseudocode for a program that allows the user to enter two integer values: a and b.
    8·1 answer
  • What is the definition of a nested function?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!