Consider the following Java method, which is written incorrectly: Under what cases will the method print the
Question:
Consider the following Java method, which is written incorrectly:
Under what cases will the method print the correct answer, and when will it print an incorrect answer? What should be changed to fix the code? Can you think of a way to write the code correctly without any if/else statements?
Transcribed Image Text:
// This method should return how many of its three // arguments are odd numbers. public static void printNumodd (int ni, int n2, int n3) { int count = 0; if (nl % 2 != 0) { count++; } else if (n2 % 2 != 0) { count++; } else if (n3 % 2 != 0) { count++; System.out.println (count + " of the 3 numbers are odd.");
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 62% (8 reviews)
The code fails when more than one number is odd because it uses else if rather than if The t...View the full answer
Answered By
Ann Wangechi
hey, there, paying attention to detail is one of my strong points, i do my very best combined with passion. i enjoy researching since the net is one of my favorite places to be and to learn. i am a proficient and versatile blog, article academic and research writing i possess excellent English writing skills, great proof-reading. i am a good communicator and always provide feedback in real time. i'm experienced in the writing field, competent in computing, essays, accounting and research work and also as a Database and Systems Administrator
4.90+
151+ Reviews
291+ Question Solved
Related Book For
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp
Question Posted:
Students also viewed these Computer science questions
-
Can you think of a marketing research technique that could not be supported online? Explain your answer.
-
Can you think of any circumstances under which an open-loop protocol, (e.g., a Hamming code) might be preferable to the feedback-type protocols discussed throughout this chapter?
-
Can you think of any reasons why not-for-profit firms would benefit less from doing strategic planning than for-profit companies?
-
Michael Mayfield, the president of Mendo Mill and Lumber Co., in California, received a notice of a legal claim from Edward Starski. The claim alleged that a stack of lumber had fallen on a customer...
-
Steam enters an adiabatic turbine at 10 MPa and 500oC and leaves at 10 kPa with a quality of 90 percent. Neglecting the changes in kinetic and potential energies, determine the mass flow rate...
-
Write a static method isStrictlyIncreasing(double[] in) that returns true if each value in the given array is greater than the value before it, or false otherwise.
-
For a certain fluid flow problem it is known that both the Froude number and the Weber number are important dimensionless parameters. If the problem is to be studied by using a 1:15 scale model,...
-
In February 2012, Packard Corporation purchased the following securities. Prior to these purchases, Packard had no portfolio of investment securities. During 2012, Packard received $2,400 in interest...
-
Who was the founder of developmental psychology? What are key theories that make up the study of developmental psychology?
-
Procter & Gamble has been the leading soap manufacturer in the United States since 1879, when it introduced Ivory soap. However, late in 1991, its major rival, Lever Bros. (Unilever), overtook it by...
-
One of the exercises in Chapter 3 asked you to write a method that would find the roots of a quadratic equation of the form ax 2 + bx + c = 0. The quadratic method was passed a , b , and c and then...
-
Describe a problem with the following code: Scanner console = new Scanner (System.in); System.out.print ("What is your favorite color?"); String name = console.next (); if (name == "blue") {...
-
If he drove the Ford today, what are the probabilities for the cars that he will drive 2 days from today? A man owns an Audi, a Ford, and a VW. He drives every day and never drives the same car two...
-
Explain the relationship between the long-run average cost curve and the concept of economies of scale.
-
List out the purposes of using design patterns in OOD.
-
Which analysis uses stories to derive the classes and objects from the problem domain? (a) Abstraction analysis (b) Behavioral analysis (c) Operations analysis (d) Scenario-based analysis
-
How is research and development related to a firms long-run profit in a monopolistically competitive market?
-
The operations performed in a system depicts (a) state (b) behavior (c) abstraction (d) None of the above
-
The drawing shows three point charges fixed in place. The charge at the coordinate origin has a value of q1 = +8.00 C; the other two charges have identical magnitudes, but opposite signs: q2 = -5.00...
-
U.S. households have become smaller over the years. The following table from the 2010 GSS contains information on the number of people currently aged 18 years or older living in a respondent's...
-
An ISBN-13 code, a new version of ISBN-10, is another example of a weighted checksum with 13 digits, in which there are 12 decimal digits defining the book and the last digit is the checksum digit....
-
One of the examples of a weighted checksum is the ISBN-10 code we see printed on the back cover of some books. In ISBN-10, there are 9 decimal digits that define the country, the publisher, and the...
-
Manually simulate the Adler algorithm (Figure 10.19) to calculate the checksum of the following words: (FBFF) 16 and (EFAA) 16 . Also show that the result is a weighted checksum. Figure 10.19 Start...
-
You are planning for retirement in 20 years and currently have $85,000 in a savings account and $25,000 in a mutual fund. Assuming your savings account returns 4% compounded semi-annually, while your...
-
An Italian company is expecting to receive Kuwait dinars in 9 months' time. The spot rate is Lire/dinar 5.467.Your Company could borrow in dinar at 9% per annum or in Lire at 14% per Annum No forward...
-
Gold coast organics has a total assets turnover of 2.5 and a net profit margin of 3.5 percent.the total debt ratio for the firm is 50 percent.calculate the gold coast organics return On equity.
Study smarter with the SolutionInn App