7 Replies - 2534 Views - Last Post: 13 January 2013 - 10:44 AM Rate Topic: -----

#1 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 09:13 AM

Hello,

I'm in the process of writing a .java program that reads in 3 integer command-line arguments (we'll call them a, b, c).

The purpose of this program is to determine (by using a boolean declaration) whether or not the three values passed are in strictly ascending/descending order.

e.g.

% java Test 49 17 10
true

% java Test 10 49 17
false

The portion that is a little puzzling is how to use an "if else" statement to detect if the values are in ascending/descending order.

Here's what I have, please critique my code so I can improve! Thanks.


public class Ordered {

	public static void main (String[] args) {
	
	int firstNum = Integer.parseInt(args[0]);
	int secondNum = Integer.parseInt(args[1]);
	int thirdNum = Integer.parseInt(args[2]);

	boolean isOrdered;
	
		if (values are ascending or descending) {
			isOrdered = true;
			System.out.println(isOrdered)
		
	    }	
		else {
			isOrdered = false;
			System.out.println(isOrdered)
	}
	
}




Is This A Good Question/Topic? 0
  • +

Replies To: Command-line arguments determining ascending or descending order.

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 09:16 AM

You have two cases:
1) a <= b <= c
2) a >= b >= c

You need to join these with a logical OR

Actually, Strictly indicates no "="
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 09:17 AM

If they're in strictly ascending order, a < b < c, strictly descending, the opposite.

What's the difficulty?
Was This Post Helpful? 1
  • +
  • -

#4 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 09:28 AM

View PostDogstopper, on 12 January 2013 - 09:16 AM, said:

You have two cases:
1) a <= b <= c
2) a >= b >= c

You need to join these with a logical OR

Actually, Strictly indicates no "="


Thanks, glad I was thinking along the same lines.

The syntax might be wrong, but this is what I was thinking:


if ((thirdNum >= secondNum) && (thirdNum >= firstNum) && (secondNum >= firstNum))  || (thirdNum <= secondNum) && (thirdNum <= firstNum) && (secondNum <= firstNum)); 

	isOrdered = true;
	System.out.println(isOrdered);




View PostGregBrannon, on 12 January 2013 - 09:17 AM, said:

If they're in strictly ascending order, a < b < c, strictly descending, the opposite.

What's the difficulty?


The difficulty for me personally, is to incorporate my ideas into actual syntax that the compiler can understand.

I think I have the right ideas, just a lack of background to implement them.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 09:32 AM

Get rid of the <= and >= and make them < and >.
The word strictly less than or strictly greater than means no =
Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1946
  • View blog
  • Posts: 4,047
  • Joined: 11-December 07

Re: Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 10:08 AM

That big long boolean is nasty. It might be right but it's difficult to read so unless it's perfect it's going to be difficult to fix. I would break it down like this:

// I have renamed your variables first second and third.
// The type int tells you they are numbers.

boolean isAscending = (first < second) && (second < third);
boolean isDescending = (third < second) && (second < first);
boolean isOrdered = isAscending || isDescending;



Now you can use the isOrdered variable in your if statament. How much more nicely does this read?

if (isOrdered) {
   System.out.println("Something about them being ordered.");
} else {
   System.out.println("Something about them being unordered.");
}

This post has been edited by cfoley: 12 January 2013 - 10:09 AM

Was This Post Helpful? 3
  • +
  • -

#7 JavaFrap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 11-September 12

Re: Command-line arguments determining ascending or descending order.

Posted 12 January 2013 - 10:23 AM

View Postcfoley, on 12 January 2013 - 10:08 AM, said:

That big long boolean is nasty. It might be right but it's difficult to read so unless it's perfect it's going to be difficult to fix. I would break it down like this:

// I have renamed your variables first second and third.
// The type int tells you they are numbers.

boolean isAscending = (first < second) && (second < third);
boolean isDescending = (third < second) && (second < first);
boolean isOrdered = isAscending || isDescending;



Now you can use the isOrdered variable in your if statament. How much more nicely does this read?

if (isOrdered) {
   System.out.println("Something about them being ordered.");
} else {
   System.out.println("Something about them being unordered.");
}


Yeah, I agree with you, but I couldn't think of any other way than to first attempt the brute-force approach.

Wow, that's a really clever way to do it. Never thought about using booleans like that before, thanks for the insight.

Much more cleaner and efficient.
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1946
  • View blog
  • Posts: 4,047
  • Joined: 11-December 07

Re: Command-line arguments determining ascending or descending order.

Posted 13 January 2013 - 10:44 AM

No problem. Glad I helped. :-)

Actually, writing it out the way I did highlights a problem with this approach. The isAscending line is almost identical to the isDescending line. Time to make a method:

public static boolean isAscending(int a, int b, int c) {
    boolean result = (a < B)/> && (b < c);
    return result;
}


Now you can simply:

boolean isAscending = isAscending(first, second, third);
boolean isDescending = isDescending(third, second, first);
boolean isOrdered = isAscending || isDescending;


We can keep going with this line of thinking:

public static boolean isOrdered(int a, int b, int c) {
   boolean result = isAscending(a, b, c) || isAscending(c, b, a);
   reurtn result;
}


The whole program now becomes:

public class Ordered {

	public static void main (String[] args) {
	
		int first = Integer.parseInt(args[0]);
		int second = Integer.parseInt(args[1]);
		int third = Integer.parseInt(args[2]);
	
		if (isOrdered(first, second, third)) {
			System.out.println("Something about them being ordered.");
		} else {
			System.out.println("Something about them being unordered.");
		}
	}
	
	public static boolean isOrdered(int a, int b, int c) {
		boolean result = isAscending(a, b, c) || isAscending(c, b, a);
		return result;
	}
	
	public static boolean isAscending(int a, int b, int c) {
		boolean result = (a < B)/> && (b < c);
		return result;
	}
	
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1