Page 5 of 6 FirstFirst ... 3456 LastLast
Results 81 to 100 of 117
  1. #81
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Thanks Gary!

    Just looking at exercise 5 now...

    I guess I'd have a while statement while(x != SENTINEL) with a break statement in there somewhere. The user would input values and those values will be stored in int largestNumber, and int smallestNumber; so as they enter more values there will be some if statement checking to see whether the value is bigger/smaller and add it to the appropriate variable.

  2. #82
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    Thanks Gary!

    Just looking at exercise 5 now...

    I guess I'd have a while statement while(x != SENTINEL) with a break statement in there somewhere. The user would input values and those values will be stored in int largestNumber, and int smallestNumber; so as they enter more values there will be some if statement checking to see whether the value is bigger/smaller and add it to the appropriate variable.
    Exactly. (Not add it, but assign it, but I'm pretty sure that's what you meant.)

    -Gary-

  3. #83
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    This exercise wasn't as easy as I imagined. I thought it would only take about 10 lines of code max but somehow it took a lot more. I'm sure there's a much easier way of doing this but here's what I ended up with anyway...

    Java Code:
    /*
     * File: FindRange.java
     * Name: 
     * Section Leader: 
     * --------------------
     * This file is the starter file for the FindRange problem.
     */
    
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    
    	public static final int SENTINEL = 0;
    
    	public void run() {
    
    
    		println ("This program finds the largest and smallest numbers.");
    
    		int largestNumber;
    		int smallestNumber;
    
    		int a = readInt ("?");
    
    		if (a != SENTINEL) {
    
    			int b = readInt ("? ");
    
    			if (a > b) {
    
    				largestNumber = a;
    				smallestNumber = b;
    
    			}
    
    			else {
    
    				largestNumber = b;
    				smallestNumber = a; 
    
    			}
    
    			while (b != 0){
    
    				int x = readInt ("?");
    
    				if (x == SENTINEL) break;
    
    				if (x > largestNumber) {
    
    					largestNumber = x;
    
    				}
    
    				if (x < smallestNumber) {
    
    					smallestNumber = x;
    
    				}
    
    			}
    
    			if (smallestNumber == SENTINEL) smallestNumber = largestNumber;
    
    
    			println ("smallest: " + smallestNumber);
    			println ("largest: " + largestNumber);
    
    		}
    
    		else {
    
    			println ("No values have been entered.");
    
    		}
    
    	}
    }

  4. #84
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Think about that little problem a bit: you either haven't read any number(s) yet or you have. When you read your first number you have a value for your largestNumber and smallestNumber. When you read more numbers you adjust that largestNumber or lowestNumber accordingly. The key observation here is having read a number already. Have a look at this snippet:

    Java Code:
    boolean numbersRead= false;
    for (; (number= readInt("?")) != SENTINEL; numbersRead= true) {
       if (numbersRead) {
          if (largestNumber < number) largestNumber= number;
          if (smallestNumber > number) smallestNumber= number;
       }
       else
          largestNumber= smallestNumber= number;
    }
    if (numbersRead)
       System.out.println("highest: "+highestNumber+" lowest: "+smallestNumber);
    else
       System.out.println("no numbers read ...");
    kind regards,

    Jos
    Last edited by JosAH; 05-17-2010 at 02:09 PM.

  5. #85
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    This exercise wasn't as easy as I imagined. I thought it would only take about 10 lines of code max but somehow it took a lot more. I'm sure there's a much easier way of doing this...
    You're right about that. There are two approaches to this:

    1. Do one input outside the loop, and simply assign whatever is input to both smallestNumber and largestNumber. This is one form of the "loop and a half" pattern, and you'll run into it quite often.
    2. Strategically pre-initialize smallestNumber and largestNumber (hint: google Integer.MAX_VALUE and Integer.MIN_VALUE) so that the first entered number will certainly be assigned to both within the loop.

    In either case, there's no need to get a separate a and b and compare them. If there is only one value entered, it is by definition both the smallest and the largest.

    -Gary-

  6. #86
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    You're right about that. There are two approaches to this:
    Three; see my approach.

    kind regards,

    Jos

  7. #87
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Is this any better?

    Java Code:
    /*
     * File: FindRange.java
     * Name: 
     * Section Leader: 
     * --------------------
     * This file is the starter file for the FindRange problem.
     */
    
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    
    	public static final int SENTINEL = 0;
    
    	public void run() {
    
    		println ("This program finds the largest and smallest numbers.");
    
    		int x = readInt ("? ");
    
    		int largestNumber = x;
    		int smallestNumber = x;
    
    		while (x != SENTINEL) {
    
    			x = readInt("? ");
    
    			if (x > largestNumber) {
    
    				largestNumber = x;
    
    			}
    
    			if (x < smallestNumber) {
    
    				if (x == SENTINEL) break;
    
    				smallestNumber = x;
    
    			}
    
    		}
    
    		if (largestNumber != 0) {
    
    			println("smallest: " + smallestNumber);
    			println("largest: " + largestNumber);	
    
    		}
    
    		else {
    
    			println("No values given.");
    
    		}
    
    	}
    
    }
    Last edited by Learning Java; 05-17-2010 at 03:23 PM.

  8. #88
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    Is this any better?
    It's better. You've got a bug there, though. Try entering a bunch of negative numbers, and then your sentinel value of 0, and see if you get what you expect. Go slowly and carefully when you try to fix it. There's no need to make your program more complicated.

    -Gary-

  9. #89
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Didn't notice that hehe. Adding the break statement seems to fix it.

    Java Code:
    /*
     * File: FindRange.java
     * Name: 
     * Section Leader: 
     * --------------------
     * This file is the starter file for the FindRange problem.
     */
    
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    
    	public static final int SENTINEL = 0;
    
    	public void run() {
    
    		println ("This program finds the largest and smallest numbers.");
    
    		int x = readInt ("? ");
    
    		int largestNumber = x;
    		int smallestNumber = x;
    
    		while (x != SENTINEL) {
    
    			x = readInt("? ");
    
    			if (x > largestNumber) {
    
    				if (x == SENTINEL) break;
    
    				largestNumber = x;
    
    			}
    
    			if (x < smallestNumber) {
    
    				if (x == SENTINEL) break;
    
    				smallestNumber = x;
    
    			}
    
    		}
    
    		if (largestNumber != SENTINEL) {
    
    			println("smallest: " + smallestNumber);
    			println("largest: " + largestNumber);	
    
    		}
    
    		else {
    
    			println("No values given.");
    
    		}
    
    	}
    
    }

  10. #90
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    Didn't notice that hehe. Adding the break statement seems to fix it.
    Yeah, but that's more complicated. Now you're checking for your SENTINEL value three times, when you should really be checking for it once. You can either:
    1. Change your while(x != SENTINEL) to while(true), then immediately check for the SENTINEL value, and break if you have it, or,
    2. Move your readInt() to the bottom of the loop.

    -Gary-

  11. #91
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Question. I previously had:

    Java Code:
    if (largestNumber != 0)
    I guess that's not correct since you should be able to change the sentinel value. Is that correct?

  12. #92
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Java Code:
    /*
     * File: FindRange.java
     * Name: 
     * Section Leader: 
     * --------------------
     * This file is the starter file for the FindRange problem.
     */
    
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    
    	public static final int SENTINEL = 0;
    
    	public void run() {
    
    		println ("This program finds the largest and smallest numbers.");
    
    		int x = readInt ("? ");
    
    		int largestNumber = x;
    		int smallestNumber = x;
    
    		while (true) {
    
    			if (x == SENTINEL) break;
    
    			if (x > largestNumber) {
    
    				largestNumber = x;
    
    			}
    
    			if (x < smallestNumber) {
    
    				smallestNumber = x;
    
    			}
    
    			x = readInt("? ");
    
    		}
    
    		if (largestNumber >= smallestNumber) {
    
    			println("smallest: " + smallestNumber);
    			println("largest: " + largestNumber);	
    
    		}
    
    		else {
    
    			println("No values given.");
    
    		}
    
    	}
    
    }

  13. #93
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Please do continue to ignore my reply #84.

    kind regards,

    Jos

  14. #94
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I do apologize JosAH.

    Is this what you mean?

    Java Code:
    /*
     * File: FindRange.java
     * Name: 
     * Section Leader: 
     * --------------------
     * This file is the starter file for the FindRange problem.
     */
    
    import acm.program.*;
    
    public class FindRange extends ConsoleProgram {
    
    	public static final int SENTINEL = 0;
    
    	public void run() {
    
    		println ("This program finds the largest and smallest numbers.");
    
    		int number = readInt ("? ");
    
    		int largestNumber = number;
    		int smallestNumber = number;
    
    		boolean numbersRead= false;
    		for (; (number= readInt("?")) != SENTINEL; numbersRead= true) {
    			if (numbersRead) {
    				if (largestNumber < number) largestNumber= number;
    				if (smallestNumber > number) smallestNumber= number;
    			}
    			else
    				largestNumber= smallestNumber= number;
    		}
    		if (numbersRead)
    
    			println("highest: "+largestNumber+" lowest: "+smallestNumber);
    		else
    			println("no numbers read ...");
    
    	}
    
    }

  15. #95
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, that's fine. Either one of those solutions would have been enough. You did both, and that's not really wrong, but hardly anybody would do it like that. :)

    The one approach is like this:
    Java Code:
    		while (true) {
    			x = readInt("? ");
    			if (x == SENTINEL) break;
                            ...
    		}
    This is fine, but some developers hate this while (true) style, and believe that the while statement should always clearly define the exit condition for the loop. I'm not in that camp, personally -- I have no objection to while (true), as long as it's reasonably easy and clear to see where the break condition is.

    So if you're the type that hates while (true), you'll want do do the other thing, which is do your readInt() at the bottom of the loop, so it's immediately checked at the top of the loop.
    Java Code:
                    while (x != SENTINEL) {
                            ...
                            x = readInt("? ");
                    }
    The disadvantage of this approach is that the first time through the loop, your smallestNumber and largestNumber comparisons are done on the x you read in outside the loop. These comparisons are harmless, but unnecessary. It's also a bit counter-intuitive to have the read at the bottom of the loop, and may be confusing to some less experienced developers.

    So your code has all the stylistic disadvantages of both approaches. Again, it's not wrong, and it works just fine. It's just a style thing -- but style is important. For your own code, you can decide which approach you like best (or hate least), but if you're working with others, you should try to blend in to the style that they use.

    -Gary-

  16. #96
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    I must have messed something up with the code given by JosAH because there's a bug when you put in two negative numbers. My bad.

    I'm not progressing very fast am I? And I'm not using the more logical approach like you guysare. Seriously, this must be due to my lack of intelligence and/or Math right?

  17. #97
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    OK, that's fine.
    No it isn't; there is no need to read one number ahead before that loop is started. Read my reply #84 instead.

    kind regards,

    Jos

  18. #98
    Learning Java is offline Senior Member
    Join Date
    May 2010
    Location
    London
    Posts
    106
    Rep Power
    0

    Default

    Oh I got it to work. Thanks JosAH!

  19. #99
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Learning Java View Post
    Oh I got it to work. Thanks JosAH!
    Good; I hope you didn't just copy and paste it but you understand the code as well; it isn't rocket science.

    kind regards,

    Jos

  20. #100
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Learning Java View Post
    I must have messed something up with the code given by JosAH because there's a bug when you put in two negative numbers. My bad.

    I'm not progressing very fast am I? And I'm not using the more logical approach like you guysare. Seriously, this must be due to my lack of intelligence and/or Math right?
    You're actually doing very well, or else I wouldn't bother spending my time on you. Your variable names are excellent, your layout and style are consistently good, and your mistakes are exactly the mistakes you should be making at your level.

    JosAH is a more experienced and cleverer developer than I, and he uses idioms that I tend to avoid. Do you understand this line?
    Java Code:
    		for (; (number= readInt("?")) != SENTINEL; numbersRead= true) {
    There are a couple of things there that might be confusing to less experienced developers.

    -Gary-
    Last edited by gcalvin; 05-17-2010 at 06:04 PM.

Page 5 of 6 FirstFirst ... 3456 LastLast

Similar Threads

  1. Help for University Project
    By ja107 in forum Networking
    Replies: 3
    Last Post: 03-18-2010, 10:22 AM
  2. Help for University Project
    By ja107 in forum Networking
    Replies: 1
    Last Post: 03-04-2010, 12:22 AM
  3. University Of Nottingham Website Survey
    By MuslimCoder in forum Reviews / Advertising
    Replies: 1
    Last Post: 03-02-2009, 10:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •