boolean numbers in order

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1031 Views - Last Post: 09 March 2012 - 04:10 PM Rate Topic: -----

#1 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

boolean numbers in order

Posted 09 March 2012 - 09:34 AM

public class Order {
    public static void main(String[] args) {
        System.out.println(consecutive(5, 6, 8));
        System.out.println(consecutive(9, 7, 8));
        System.out.println(consecutive(7, 8, 9));
    }
    public static boolean consecutive(int x, int y, int z) {
        if (x + 1 == y && y + 1 == z ){
        	return true;
        }
        else {
        	return false;
        }
    }
}



i have it so that if the numbers are in order it reads true but i cant get it to read if they are not arranged in the correct order but the numbers are there to get it to read true there for if you enter any numbers in the right order it will read true but if all the numbers are there and you don't enter them in order it will read false have trouble trying to figure out what im missing to have it work both ways the first one should read false and the next 2 should be true any help is appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: boolean numbers in order

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: boolean numbers in order

Posted 09 March 2012 - 09:51 AM

There are six ways this could be true for three integers (a, b, c): assuming a<b<c && a + 2 = c,
the three could be ordered (a, b, c) (a, c, B) (b, a, c), (b, c, a) (c, a, B) (c, b, a). All of these are to be true, you say.

You could do this with a complex if condition built up of logical ors, or with a series of if statements, elaborating each condition. That is a plausible but not a scalable solution. What happens if you want to answer the same question for 5, or 10, or n numbers? (assume n is a very big number)
If you want to do that, that requires little thought, just elaborate the possibilities as I've listed them.

For a more scalable solution... well, I'll start you off with this hint: get the numbers in an array, so you can refer to them dynamically. That'll save you a lot of repetitive rewriting of the same lines over and over and over again, redundantly and repetitiously.
Was This Post Helpful? 0
  • +
  • -

#3 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: boolean numbers in order

Posted 09 March 2012 - 02:13 PM


public class order1 {
	   public static void main(String[] args) {
	        System.out.println(test(5, 6, 8));
	        System.out.println(calc(10, 12, 11));
	        System.out.println(calc(7, 8, 9));
	        int min = 0;
	        int med = 0;
	        int max = 0;
	        
	    }
	        public static int calc( int x, int y, int z) {
	           int min = Math.min(x, Math.min(y, z));
	           return min;
	           int med = x + y + z - Math.min(x, Math.min(y, z)) - Math.max(x, Math.max(y, z));
	           return med;
	           int max = Math.max(x, Math.max(y, z));
	           return max;
	        }
	        public static boolean test(int min,int med, int max){
	        if (min + 1 == med && med + 1 == max ){
	        	return true ;
	        }
	        else {
	        	return false;
	        }
}
}


changed things around a little bit but i get an error for the method calc i only need to read in 3 numbers and just sort the min max and med number put them in order and see if the order is consecutive. i think i may have too much in my calc method
Was This Post Helpful? 0
  • +
  • -

#4 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: boolean numbers in order

Posted 09 March 2012 - 02:14 PM


public class order1 {
	   public static void main(String[] args) {
	        System.out.println(test(5, 6, 8));
	        System.out.println(calc(10, 12, 11));
	        System.out.println(calc(7, 8, 9));
	        int min = 0;
	        int med = 0;
	        int max = 0;
	        
	    }
	        public static int calc( int x, int y, int z) {
	           int min = Math.min(x, Math.min(y, z));
	           return min;
	           int med = x + y + z - Math.min(x, Math.min(y, z)) - Math.max(x, Math.max(y, z));
	           return med;
	           int max = Math.max(x, Math.max(y, z));
	           return max;
	        }
	        public static boolean test(int min,int med, int max){
	        if (min + 1 == med && med + 1 == max ){
	        	return true ;
	        }
	        else {
	        	return false;
	        }
}
}


changed things around a little bit but i get an error for the method calc i only need to read in 3 numbers and just sort the min max and med number put them in order and see if the order is consecutive. i think i may have too much in my calc method
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,770
  • Joined: 27-December 08

Re: boolean numbers in order

Posted 09 March 2012 - 02:16 PM

You could define your calc() method to accept int... numbers, utilizing the var-args feature. This will allow you to treat numbers as an array, and use the Arrays.sort() method. It also allows you to pass up to n numbers. You can also define your method to accept an array formally: int[] numbers, but then you will have to pass a formal array rather than the parameters as you currently do.
Was This Post Helpful? 1
  • +
  • -

#6 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: boolean numbers in order

Posted 09 March 2012 - 02:20 PM

im looking to not use arrays if possible
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,770
  • Joined: 27-December 08

Re: boolean numbers in order

Posted 09 March 2012 - 02:21 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: boolean numbers in order

Posted 09 March 2012 - 02:26 PM

View Postmarcb1387, on 09 March 2012 - 04:14 PM, said:

changed things around a little bit but i get an error for the method calc


Probably the unreachable code problem. After a return, the method ends, so none of the code after your first return can ever execute.

I like the way you're approaching it now, though. That's a very good way of making it more dynamic while still accepting ints x, y, and z.

Quote

i think i may have too much in my calc method


This is a useful insight. Do you see anything that you could usefully extract to a helper method? I think it's probably a reasonable length as it is, but any time you get the urge to simplify, it's worth taking it seriously.

This post has been edited by jon.kiparsky: 09 March 2012 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: boolean numbers in order

Posted 09 March 2012 - 02:34 PM

how can i pass x y and z through 3 separate methods and store the values to pass through my test method or can i combine and find the min med and max of the 3 numbers to one method the pass it to test i think i might be getting about my skill level
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7891
  • View blog
  • Posts: 13,417
  • Joined: 19-March 11

Re: boolean numbers in order

Posted 09 March 2012 - 02:54 PM

I'd think in terms of dividing the functionality you're using into discrete chunks. Like I say, this doesn't seem to be desperately complicated to my eye, but what if you could do something like

public static int calc(int x, int y, int z)
{
  if (max(x,y,z))-min(x,y,z) !=2) return false;
  if (max(x,y,z))-med(x,y,z) !=1) return false;
  return true;
}


I think this is the logic you're going for, at least.
Now if you implement the three helper methods, min, max, and med, then you're set, right?
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: boolean numbers in order

Posted 09 March 2012 - 03:11 PM

Still think that macosxnerd101's approach is the best

	public static void main(String[] args) {
		System.out.println(calc(1,2,5,4,6,3));
		System.out.println(calc(2,1));
		System.out.println(calc(5,4,3,1,0));
		
	}
	static boolean calc(int... number) {
		Arrays.sort(number);
		for(int i = 0; i < number.length - 1; ++i) {
			if(number[i] + 1 != number[i+1])
				return false;
		}
		return true;
	}



This post has been edited by pbl: 09 March 2012 - 03:12 PM

Was This Post Helpful? 1
  • +
  • -

#12 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: boolean numbers in order

Posted 09 March 2012 - 03:37 PM


public class order1 {
	   public static void main(String[] args) {
	        System.out.println(calc(5, 6, 8));
	        System.out.println(calc(10, 12, 11));
	        System.out.println(calc(7, 8, 9));
	        
	    }
	        public static boolean calc( int x, int y, int z) {
	        	if (Math.max(x, Math.max(y, z)) - Math.min(x, Math.min(y, z)) == 2 ){
	        	return true;	        	
	        }
	        	else {
	        		return false;
	        	}
	        }
}
	           



got it thanks for the help guys
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: boolean numbers in order

Posted 09 March 2012 - 03:42 PM

The code you just post won't work with

System.out.println(calc(7, 7, 9));

though. Will return true

:)

You will need jon.kiparsky's med() method

This post has been edited by pbl: 09 March 2012 - 03:45 PM
Reason for edit:: jon not john

Was This Post Helpful? 0
  • +
  • -

#14 marcb1387  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 12

Re: boolean numbers in order

Posted 09 March 2012 - 03:44 PM

your right i did not account for duplicate numbers thank you

This post has been edited by marcb1387: 09 March 2012 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5883
  • View blog
  • Posts: 12,764
  • Joined: 16-October 07

Re: boolean numbers in order

Posted 09 March 2012 - 04:06 PM

Because pbl seems amused when I use recursion for silly things.

And, because with only three choices, the cost of a simple sort is trivial, I'd go with:
public static boolean consecutive(int x, int y, int z) {
	if (x>y) { return consecutive(y, x, z); }
	if (y>z) { return consecutive(x, z, y); }
	return x==(y-1) && x==(z-2);
}


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2