Question

II. Answer part A, B, and C. Remember that 0 is not a positive value! 2a)...

II. Answer part A, B, and C. Remember that 0 is not a positive value!

2a)

Return true if at least one parameter value is positive, and false otherwise.


atLeastOnePositive(1, 0, 3) → true
atLeastOnePositive(-10, 0, -4) → false
atLeastOnePositive(3, -2, -4) → true

2b)

Return true if exactly one parameter value is positive, and false otherwise.

exactlyOnePositive(1, 0, 3) → false
exactlyOnePositive(-10, 0, -4) → false
exactlyOnePositive(3, -2, -4) → true

2c)

Return the middle of three integer values. This is the value such that it is less than or equal one value, and greater than or equal the other.

midOfThree(1, 2, 3) → 2
midOfThree(200, 100, 300) → 200
midOfThree(33, 11, 22) → 22

Homework Answers

Answer #1

// 2a)
public static boolean atLeastOnePositive(int n1, int n2, int n3) {
    return n1 > 0 || n2 > 0 || n3 > 0;
}

// 2b)
public static boolean exactlyOnePositive(int n1, int n2, int n3) {
    int count = 0;
    if (n1 > 0) ++count;
    if (n2 > 0) ++count;
    if (n3 > 0) ++count;
    return count == 1;
}

// 2c)
public static int midOfThree(int n1, int n2, int n3) {
    int min = n1, max = n1;
    if (n2 > max) max = n2;
    if (n3 > max) max = n3;
    if (n2 < min) min = n2;
    if (n3 < min) min = n3;
    return (n1 + n2 + n3) - (min + max);
}
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 C++ 1) Write the function definition that has 1 pass by value integer parameter. The...
In C++ 1) Write the function definition that has 1 pass by value integer parameter. The function contains logic to return true if the parameter is even and false if it is not. 2 ) Write a main function that prompts the user for a value, calls the function that you created in step one with the value entered by the user, display "even" if the function return true and otherwise displays "odd"
You need to answer each of the following questions. You need to show your work. Your...
You need to answer each of the following questions. You need to show your work. Your answer should be submitted in an attachment that is in Microsoft or PDF format. 1. Suppose a zero coupon security has a face value of $1000 and a maturity of 5 years. If the return on comparable securities is 6%, calculate the market price of this security. 2. Suppose a coupon security has a face value of $1000, a maturity of 4 years, a...
Python: Write a program that prompts the user to enter a positive integer value, and compute...
Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message containing the word Error and exit the program....
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Find the value of C > 0 such that the function ?C sin2x, if0≤x≤π, f(x) =...
Find the value of C > 0 such that the function ?C sin2x, if0≤x≤π, f(x) = 0, otherwise is a probability density function. Hint: Remember that sin2 x = 12 (1 − cos 2x). 2. Suppose that a continuous random variable X has probability density function given by the above f(x), where C > 0 is the value you computed in the previous exercise. Compute E(X). Hint: Use integration by parts! 3. Compute E(cos(X)). Hint: Use integration by substitution!
True or false. If your answer is false, you must justify your answer with a one...
True or false. If your answer is false, you must justify your answer with a one sentence explanation to receive any credit. An asset depreciated in the 3-years MACRS class will have book value of 0 at the end of 3 years. The discounted payback period will always be no shorter than the undiscounted payback period. If the internal rate of return on an investment is positive, then the investment’s net present value must be positive. You are purchasing a...
Given the following prediction Sample ID Predicted Value Ground Truth 1 0.9 1 2 0.3 1...
Given the following prediction Sample ID Predicted Value Ground Truth 1 0.9 1 2 0.3 1 3 0.7 1 4 0.8 -1 5 0.4 -1 6 0.2 -1 (a) (18 Pts) Using 0.5 as the cutoff value, namely, the sample is predicted as 1 if predicted value >=0.5, otherwise -1. Please compute true positive rate (tpr, also called recall/sensitivity), false positive rate (fpr), true negative rate (tnr), false negative rate (fnr), accuracy and precision, respectively.
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the...
{- Alexa loves movies and maintains a list of negative and/or positive integer ratings for the n movies in her collection. She's getting ready for a film festival and wants to choose some subsequence of movies from her collection to bring such that the following conditions are satisfied: The collective sum of their ratings is maximal. She must go through her list in order and cannot skip more than one movie in a row. In other words, she cannot skip...
(In Python) Complete the isPrime() function to take an int and return True if it is...
(In Python) Complete the isPrime() function to take an int and return True if it is a prime number and False if it is not. Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2). For example, if num is 7, then you could test: if 7 remainder 6 is not 0, then if 7 remainder 5 is not 0, then if 7 remainder 4 is not...
2. You are facing two investment opportunities. The first one will return you with an uneven...
2. You are facing two investment opportunities. The first one will return you with an uneven stream of cash flows. The second one is annuity payments. Assume a 4.9% discount rate (your opportunity cost for money) and payments are in the end-of-period. (2a) How much you are willing to pay for this uneven stream of cash flows? Round to the nearest whole dollar. Year Cash Flow 1 $3,300 2 $3,300 3 $4,500 4 $5,300 (2b) If the investment will provide...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT