Solved

JAVA Concept Question: Finding the Larger Value

Posted on 2013-01-02
3
426 Views
Last Modified: 2013-01-03
Hello, I am fairly new to the Java Programming Language and have a question regarding a practice problem that I have completed. The problem that I finished is as follows.

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.

max1020(11, 19) ¿ 19
max1020(19, 11) ¿ 19
max1020(11, 9) ¿ 11

Below is the code that I have written followed by one of the solutions that the website provided.

My Code:

public int max1020(int a, int b) {
  if ((a >= 10) && (a <= 20)){
    if ((b >= 10) && (b <= 20)){
      if (a > b){
        return a;
      }
      else return b;
    }
    else return a;
  }
  else {
    if ((b >= 10) && (b <= 20)){
      return b;
    }
    return 0;
  }
}


Solution Code:

public int max1020(int a, int b) {
  // First make it so the bigger value is in a
  if (b > a) {
    int temp = a;
    a = b;
    b = temp;
  }
 
  // Knowing a is bigger, just check a first
  if (a >= 10 && a <= 20) return a;
  if (b >= 10 && b <= 20) return b;
  return 0;
}

The question that I would like to ask is why does the solution code use the first if statement? I understand that the comment says the statement is checking for the larger value, but do not understand how it does that.

Thank you for your help.
0
Comment
Question by:Paul_ATL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 130 total points
ID: 38738068
This might be neater

import java.util.Scanner;
import java.lang.Math.*;

class ReturnLargest
{
      public static void main (String [] args)
      {
            Scanner keyIn = new Scanner(System.in);
            
            int numA = 0;
            int numB = 0; 
	
            int largest =0;
            int smallest = 0;                      
            
                System.out.print("Enter an integer: \n");
                numA = keyIn.nextInt( );
	System.out.print("Enter an integer: \n");
                numB = keyIn.nextInt( );
                largest = Math.max(numA,numB);
	smallest = Math.min(numA,numB);
               if((largest>=10&&largest<=20)||(smallest>=10&&smallest<=20)){System.out.println("\n"+largest);}else{System.out.println("\n0");}
            	
      }
}

Open in new window


re: Your comment : if you mean this :  if (b > a) {
Both ints need to be checked for range, and you then need to know which is the largest.
0
 
LVL 16

Accepted Solution

by:
imladris earned 170 total points
ID: 38740692
In this piece:

// First make it so the bigger value is in a
  if (b > a) {
    int temp = a;
    a = b;
    b = temp;
  }

It checks a and b. If b is bigger than a it swaps the values in a and b, so that a is now bigger. So at the end of this clause, a is always bigger than b.

Then, in the next part, it only has to check for range.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38741299
Just wondering where I or imladris or both went off the rails to get only a 'good'. The 'excellent' category doesn't cost you any extra points. But more importantly, it would be interesting to hear what you were not 100% happy with.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question