Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java


static String Q3(int a, int b, int c, double lowerBound, double targetValue, double upperBound) {

        // TODO: If the average of a, b, and c is less than lower bound, return "too low".
        //       If the average of a, b, and c is greater than upper bound, return "too high".
        //       If the average of a, b, and c is within 2.5 of the target value, return "just right".
        //       If the average of a, b, and c is between the upper and lower bounds,
        //          but not within 2.5 of the target value, return "almost".
        //



If I run this
System.out.println(Q3(74, 9, 48, 18.4, 38.4, 112.1));
		System.out.println(Q3(22, 8, 16, -9.0, 38.3, 124.5));
		System.out.println(Q3(31, 32, 35, 20.7, 35.1, 88.2));
		System.out.print(Q3(22, 8, 16, -9.0, 38.3, 124.5));

I have to get almost, just right, just right, and almost . I keep getting
almost
null
null
null


What I have tried:

static String Q3(int a, int b, int c, double lowerBound, double targetValue, double upperBound) {

        // TODO: If the average of a, b, and c is less than lower bound, return "too low".
        //       If the average of a, b, and c is greater than upper bound, return "too high".
        //       If the average of a, b, and c is within 2.5 of the target value, return "just right".
        //       If the average of a, b, and c is between the upper and lower bounds,
        //          but not within 2.5 of the target value, return "almost".
        //

        // 22, 8, 16, -9.0, 38.3, 124.5
        // 31, 32, 35, 20.7, 35.1, 88.2 -- avg=36.667
    	// 74, 9, 48, 18.4, 38.4, 112.1 == 43.6666667
    	// 22, 8, 16, -9.0, 38.3, 124.5 == 15.23 35.8  40.8
    	 double avg = (a+b+c)/3;
    	 String result = null;
    	 if(avg < lowerBound){
    		 result = "too low";
    	 }
    	 else if(avg > upperBound){
    		 result = "too high";
    	 }
    	 else if(targetValue - 2.5 <= avg && avg <= targetValue + 2.5){
    		 result = "just right";
    	 }
    	 else if(lowerBound < avg && avg < upperBound && targetValue - 2.5 < avg && targetValue + 2.5 < avg){
    		 result = "almost";
    	 }
    	 else{
    		 return result;
    	 }
		return result;
    	 
    }
Posted
Updated 6-Mar-17 13:45pm
Comments
[no name] 6-Mar-17 17:27pm    
What help is it that you need? What is the question? What did the debugger reveal to you when you stepped through your code?
Member 13042098 6-Mar-17 17:31pm    
My question is why do I keep getting different output values I'm supposed to get almost, just right, just right, and almost. But my question is why do I keep getting null and if the control statement is correct considering the given condition's.
[no name] 6-Mar-17 18:13pm    
You are getting the output you are because your inputs are incorrect. For example, the average of 22, 8, 16 using your integer math is 15 not 15.23 (and 15.23 is not correct either) and it's not even close to your targetValue of 38.3. Because of that, it fails all of your if conditions and you return the default value of null. Learn to use the debugger and you can find these sorts of issues yourself.

1 solution

Java
else{
    return result;

When this return is executed, result contain NULL, That(s your logic.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900