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
When an architect designs a building, what visual design elements could be used to help capture a viewer’s attention? A) The use
Salsk061 [2.6K]
The correct answer is A
6 0
3 years ago
Read 2 more answers
What does limited access to a document mean?
OverLord2011 [107]
Limited access to a document usually means that the creator of the document is able to limit how much access one person is able to get.
8 0
3 years ago
Read 2 more answers
. If you have written the following source code:
taurus [48]

Answer:

SpringBreak.java

Explanation:

Java classes are saved in files having the same name as the class name.

So if the given class structure is:

public class SpringBreak{

// lots of code here

}

It needs to be saved in a file called SpringBreak.java.

The physical file should follow the package structure as provided in the class.

The java file will be compiled by the java compiler to generate the corresponding class file.

8 0
3 years ago
When numbers are changed in cells that are involved in formula is the formulas are automatically
Sonbull [250]

Excel automatically produces a new output.

7 0
3 years ago
Consider an environment in which there is a one-to-one mapping between user-level threads and kernel-level threads that allows o
DENIUS [597]

Explanation:

According to the situation mentioned in the question about the model that can make the multi threaded execution faster as compared with the single threaded counterparts programs because multi-threaded programs have the capability to execute simultaneously in multiple processes.

In this type of system ,the blocking system cannot block the complete execution because kernel thread is present for every user thread .So, if one kernel thread gets blocked , others will still keep running .

Whereas, for uni-processor system , the program has to wait for the input -output operation completion for most of the time.Thus, multi-threaded program can execute faster than uni-processor.

 

5 0
3 years ago
Other questions:
  • The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program sh
    9·1 answer
  • https://brainly.com/app/ask?entry=top&amp;q=What+did+you+learn+during+this+course+that+reinforces+your+belief+in+your+technology
    5·1 answer
  • Refer to the exhibit. the gigabit interfaces on both routers have been configured with subinterface numbers that match the vlan
    11·1 answer
  • What is a Photojournalist
    5·1 answer
  • A database administrator (DBA) must have a clear understanding of the fundamental business of an organization, be proficient in
    11·1 answer
  • What are some difficulties in synchronizing audio and video during telecine transfer? (Select all that apply.)
    13·1 answer
  • An airline has found about 7% of its passengers request vegetarian meals. On a flight with 166 passengers the airline has 16 veg
    11·1 answer
  • Write a class called Person that has two data members - the person's name and age. It should have an init method that takes two
    12·1 answer
  • What are some best practices for file management
    8·1 answer
  • How can she change that value so it is reflected in the chart in her presentation?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!