Page 6 of 6 FirstFirst ... 456
Results 101 to 117 of 117
  1. #101
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    JosAH is a more experienced and cleverer developer than I, and he uses idioms that I tend to avoid.
    And I am much less of a teacher than Gary is but I am curious about those idioms; at least in the C and C++ world they are well accepted. I don't think that crippling a language by not using those idioms is a good thing.

    kind regards,

    Jos

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

    Default

    Quote Originally Posted by JosAH View Post
    And I am much less of a teacher than Gary is but I am curious about those idioms; at least in the C and C++ world they are well accepted. I don't think that crippling a language by not using those idioms is a good thing.
    I don't disagree, as long as clarity is preserved. And of course, clarity is to some extent a function of experience.

    The advantage of your approach is that you have just one readInt() call. And that's a significant advantage -- I don't mean to minimize it. (A maintenance coder wanting to change the prompt is likely to miss one of the calls if there are more than one.) The disadvantage, if there is one, is the creative use of the for () statement, and the simultaneous assignment and comparison, both of which may be confusing to someone working on their second assignment in an introductory Java course. There's also the extra if/else in every iteration of the loop, but that's a matter of taste. Personally, I'd probably initialize smallestNumber and largestNumber to Integer.MAX_VALUE and Integer.MIN_VALUE, respectively, but I understand the disadvantage to that approach as well (it's a lie -- those values were never the smallest and largest values entered -- and lying with data is a dangerous habit).

    My version:
    Java Code:
    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 = Integer.MIN_VALUE;
    		int smallestNumber = Integer.MAX_VALUE;
    
    		while (true) {
    			int x = readInt("? ");
    			if (x == SENTINEL) break;
    
    			if (x > largestNumber) largestNumber = x;
    			if (x < smallestNumber) smallestNumber = x;
    		}
    
    		if (largestNumber >= smallestNumber) {
    			println("smallest: " + smallestNumber);
    			println("largest: " + largestNumber);	
    		} else {
    			println("No values given.");
    		}
    	}
    
    }
    -Gary-

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

    Default

    JosAH is a more experienced and cleverer developer than I, and he uses idioms that I tend to avoid. Do you understand this line?
    Code:

    for (; (number= readInt("?")) != SENTINEL; numbersRead= true)
    Actually I have never come across this before. I'm guessing that he assigned the value from readInt to the variable "number" and done the test in one, hence there is nothing before the first semicolon but it's still required syntax. And I'm guessing that "numbersRead = true" after the loop ends? Not entirely sure.
    Last edited by Learning Java; 05-17-2010 at 06:57 PM.

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

    Default

    Quote Originally Posted by Learning Java View Post
    Actually I have never come across this before. I'm guessing that he assigned the value from readInt to the variable "number" and done the the test in one, hence there is nothing before the first semicolon but it's still required syntax. And I'm guessing that "numbersRead = true" after the loop ends? Not entirely sure.
    A for statement is
    Java Code:
            for ([I]initialization[/I]; [I]termination[/I]; [I]increment[/I]) { }
    • initialization -- a statement performed before the first iteration of the loop
    • termination -- a boolean that ends the loop when it returns false
    • increment -- a statement that runs at the conclusion of each iteration of the loop

    Any of these can be an empty statement (an empty termination statement means the loop will never terminate, except with a break statement in the body). The statements need not relate to one another in any way. JosAH does not use an initialization statement, because his numbersRead boolean is initialized outside the loop (because he needs its value after the loop terminates). His termination statement also performs the readInt() call and assigns its return value to an int variable number. Note that an assignment statement returns the value being assigned. This means you can assign multiple variables the same value, like this:
    Java Code:
            int a = int b = int c = 5;
    But the Sun Java Code Conventions document recommends avoiding this, because it's "hard to read."

    http://java.sun.com/docs/codeconv/ht....doc9.html#547

    The numbersRead = true statement runs at the conclusion of each loop iteration. You only need it to run once, but it certainly does no harm running each time through the loop.

    -Gary-

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

    Default

    So the numbersRead = true statement can't really be considered an increment; or can it? Then again n-- isn't a increment either it's a decrement. So the word increment is meant in the sense that it's done after the loop is finished - right? Ok cool, I understand now.

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

    Default

    Okaaaay looking at exercise 6 now. I'm thinking that while (x > 1) with an if and else statement should do the job?

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

    Default

    Java Code:
    /*
     * File: Hailstone.java
     * Name: 
     * Section Leader: 
     * --------------------
     * This file is the starter file for the Hailstone problem.
     */
    
    import acm.program.*;
    
    public class Hailstone extends ConsoleProgram {
    
    	public void run() {
    
    		int counter = 0;
    		int x = readInt("Enter a number: ");
    
    
    		while (x > 1) {
    
    			if (x % 2 == 0) {
    
    				println (x +" is even, so I take half: " + x / 2);
    
    				x = x / 2;
    
    				counter++;
    
    			}
    
    			else {
    
    				println (x + " is odd, so I make 3n+1: " + ((x * 3) + 1));
    
    				x = (x * 3) + 1;
    
    				counter++;
    
    			}
    
    		}
    
    		println("The process took " + counter + " to reach 1");
    
    	}
    }
    Last edited by Learning Java; 05-18-2010 at 01:26 PM.

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

    Default

    Think I could have the counter++ at the end of the while statement actually and it'd do the same thing.

  9. #109
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up WELL DONE Assignment two complete.

    Quote Originally Posted by Learning Java View Post
    Think I could have the counter++ at the end of the while statement actually and it'd do the same thing.
    Absolutely right, no point in repeating code unnecessarily..

    @Jos
    talk about creative use of the for loop! you certainly never fail to give a guy something to really think about !
    i'm gonna have a good scan at that now:)

    well done LJ on completing assignment two. Are you also doing the exercises at the end of each chapter of the book as well? they are good practice.
    also the mark dexter tutorials would be well worth a look at this point.

    This was the assignment that methods really started to click into place for me. (thanks to help from Gary).

    Because methods was the subject of the assignment and the chapter, I did however get a bit method happy with a method for every thing! but it was good practise passing parameters and returning values.

    here was my original find range which is a loop and half basically. i had the idea at that time of keeping each method simple, just doing one thing, i find it makes it easier to refine the whole thing once you have finished and got it work.
    and theres still room for some further simplification. it's not necesarily a good example Just a slightly different one.

    Java Code:
    public class FindRange extends ConsoleProgram{
    	
    	/* Specifies the value of the sentinel */
    	private static final int SENTINEL = 0;
    	
    	public void run(){
    		intro();
    		enterValue();
    	}
    		
    	
    	private void intro() {
    		println("This program determines the maximum and minimum values.");
    		println("Enter values, one per line, using " + SENTINEL);
    		println("to signal the end of the list.");
    	}
    
             private void enterValue() {
    		int val = readInt(" ? ");
    		if(val == SENTINEL) println("No values were entrerd");
    		if (val != SENTINEL) enterNextValue (val);
    	}
    	
    	private void enterNextValue(int val) {
    		int min=val;
    		int max=val;
    		while(true){
    			val = readInt(" ? ");
    			if(val == SENTINEL)break;
    			max=max(val,max);
    			min=min (val,min);
    		}
    		printResults(max, min);
    	}
    
    	private void printResults(int max, int min) {
    		println("The Maximum is " + max + ".");
    		println("The Minimum is "+ min +".");
    		
    	}
    
    	private int max(int val, int max) {
    		if(val>max)max=val;
    		return max;
    	}
    
    	private int min(int val, int min) {
    		if (val<min)min=val;
    		return min;
    	}
    }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

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

    Default

    Thanks!

    Yes I can see you're quite comfortable with your use of methods :D I think I might try some of the exercises again using methods just for practice (although I guess it might overcomplicate some of the exercises a little).

    Regarding the exercises in the book; I've been doing the ones I can do, but most of the time I feel as if I'm not ready to do them. I don't feel as if the chapter prepares me for the exercise, but maybe that's just me. Now, I wasn't doing too bad until I got up to Chapter 5 and then the book started getting all Mathy on me so I didn't get any further. And I don't want to further bother people here with another 10 exercises haha.

    As for Mark Dexters tutorials I have seen the first and second videos.

    I'm only on video 7 on CS106A.

  11. #111
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Smile

    Quote Originally Posted by Learning Java View Post
    Thanks!

    Yes I can see you're quite comfortable with your use of methods :D I think I might try some of the exercises again using methods just for practice (although I guess it might overcomplicate some of the exercises a little).

    Regarding the exercises in the book; I've been doing the ones I can do, but most of the time I feel as if I'm not ready to do them. I don't feel as if the chapter prepares me for the exercise, but maybe that's just me. Now, I wasn't doing too bad until I got up to Chapter 5 and then the book started getting all Mathy on me so I didn't get any further. And I don't want to further bother people here with another 10 exercises haha.

    As for Mark Dexters tutorials I have seen the first and second videos.

    I'm only on video 7 on CS106A.
    I found the same thing to be honest, and like I said earlier although this is foundation course, the enrolled stanford students get a lot more 1 to 1 support than you are able to achieve yourself following online.
    I am only four videos ahead, partly because i've been looking at other stuff as well. I learned about enums and arrayLists by myself (with help here) because they were a much better (more conventional)way of solving the random card exercise at the end of Chapter 6 in the book.


    I often find myself going back to earlier exercises- sometimes to see how i did something the last time i did it, (because i'm human and i forget stuff) and sometimes to rewrite it completely because i found a better way to do it.(or just think I have)

    there is quite a lot of mathy looking stuff in chap 5 I agree, but you dont need to be good at maths in order to solve these problems, just be able to think in a logical way..

    eg. the perfect square exercise
    once you realise that if
    1. you take any number (positive)
    2. find its square root.
    3. if its square root is is a whole number then its a perfect square.
    simple eh. just put that into code

    another way (brute force) to do it, might be to say,
    the number is say 23
    2*2 = 4, is 4 the answer i'm looking for, NO, is 4 bigger than the answer i,m looking for NO,
    3*3 = 9 is 9 the answer i'm looking for, NO, is 9 bigger than the answer i,m looking for NO
    4*4 = 16 is 16 the answer i'm looking for, NO, is 16 bigger than the answer i,m looking for NO
    5*5 = 25 is 25 the answer i'm looking for, NO, is 25 bigger than the answer i,m looking for YES

    23 is not a perfect square.

    The hardest part of any exercise is determining the pattern/algorithm that will work. or sometimes working out which end to start from.
    Sometimes there are many ways like in euclids algorithm vs brute force or recursive method vs iterative methods.
    I think I spent whole weekend with pen and paper just trying to find a pattern other than brute force that would work to find prime numbers after i got one to work, within notime i had about four alternative methods to find prime numbers.

    the perfect number exercise.. I got to over 120 lines of code, and then i realised something, (which would probably be blindingly obvious to an experienced developer) and then i solved the problem in about 8 lines.

    I dont think it matters, weather your just starting out, or you're senior programmer with 20 years experience and fluent in 10 programming languages. there is a certain amount of frustration within any programing task.
    if you get stuck, just open a new thread.. and while your waiting think about the next task... cos sometimes when thinking about the next one you discover something about the last exercise you hadnt thought of..

    you can always find the answer to mathematical questions, via google or yahoo or wherever. eg i wanted to determine all the co-ordinates of the pixels around the circumference of a circle/ ball given that you only know its radius and its x and y co-ordinates in an acm.graphics program, i'm not great at algebra, its been over 20 years since i did it it at poly. and this is not an algebra forum, so no point in asking in here. But i knew there would be a way to do it mathematically. so after about two hours of googling i found the formula.
    Can you then apply that formula and code it to make it work in a program. that took me about four hours. just to get a pesky little ball to bounce whenever any part of its circumference came into contact with another object.

    its not about knowing maths its about applying logic,
    break it down and use pseudo code..

    don't worry about bothering people in here, they'll always help out if they can.
    some people are really helpful and provide lots of support, like Gary, these people really help you comprehend the problem and how to arrive at a solution.
    some people will bend your brain in half till its oozing out of your ears, like Jos, from these people will learn a lot a very clever and useful stuff.

    a lot of people like you and I are at various stages of learning, and you will soon find yourself seeing new threads where you think, ooh i know about that.. and you'll soon be chipping in with your own two penneth worth..
    i think its a great way to learn.

    have a break and do the dexter stuff, its about four hours long, but took me about three evenings to work through properly and type up all the code.

    kind regards
    sonny
    Last edited by sonny; 05-19-2010 at 02:38 PM. Reason: punctuation
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  12. #112
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sonny View Post
    this is not an algebra forum, so no point in asking in here.
    Aaaw, but I like algebra; information systems (CS) are just the simple algebraic problems. I'd like to call for a revolt; we want more algebra in here! ;-)

    Seriously, about those methods: a good start is to read the problem and the requirements. Mark every noun, it can be a class and mark every verb, it can be a method. Next visualize all the classes in your head (I don't have much imagination so I visualize them as blobs, with no particular shape at all). All I can say to those blobs are the verbs from the story and those blobs can do just the same to other blobs (or themselves). If you can work out the story you're more than halfway there and you immediately understand that stupid error "blahblahblah can not be called in a static context", (i.e. you're saying something but you're not saying it to any blob in particular).

    kind regards,

    Jos

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

    Default

    Thanks guys, I really do appreciate the help I've had here. Hopefully one day I'll be at a decent standard and can help others out who are new. Because let's face it, I think most of us need help at some point in the learning process; I don't think it's as easy as just picking up a book or watching a video and memorizing things for most people.

    I'm going to start watching the Mark Dexter videos from the beginning right now. Also, I don't need to worry about the "companion tutorial document" do I? It's just there to show the code used in the video, right?

    Thanks again!

  14. #114
    kubus_ is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Woohhaa, nice teaching work you guys!! The pyramid didn't let me sleep for last few days :) and I'm not a CS student, just a guy watching Merhan's lecturers! first I've tried do it with three nested for loops but instead of centered pyramid I got only staircase :] in the bottom center screen!
    Cheers!

  15. #115
    YLTO is offline Member
    Join Date
    Jun 2011
    Location
    Jakarta
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Learning Java View Post
    How does this look?

    Java Code:
    /*
     * File: ProgramHierarchy.java
     * Name: 
     * Section Leader: 
     * ---------------------------
     * This file is the starter file for the ProgramHierarchy problem.
     */
    
    import acm.graphics.*;
    import acm.program.*;
    
    
    public class ProgramHierarchy extends GraphicsProgram {
    
    	public static final double BOX_WIDTH = 200.0;
    	public static final double BOX_HEIGHT = 80.0;
    
    
    	public void run() {
    
    		double horizontalMargin = (getWidth() - BOX_WIDTH * 3) / 4;
    		double verticalMargin = (getHeight() - BOX_HEIGHT * 2) / 3;
    
    		double x = horizontalMargin * 2 + BOX_WIDTH;
    		double y = verticalMargin;
    
    		GRect box1 = drawBox(x, y, "Program");
    
    		x = horizontalMargin;
    		y = verticalMargin * 2 + BOX_HEIGHT;
    
    		GRect box2 = drawBox(x, y, "GraphicsProgram");
    
    		x = horizontalMargin * 2 + BOX_WIDTH;
    		y = verticalMargin * 2 + BOX_HEIGHT;
    
    		GRect box3 = drawBox(x, y, "ConsoleProgram");
    
    		x = horizontalMargin * 3 + BOX_WIDTH * 2;
    		y = verticalMargin * 2 + BOX_HEIGHT;
    
    		GRect box4 = drawBox(x, y, "DialogProgram");
    
    		drawConnectingLine(box1, box2);
    		drawConnectingLine(box1, box3);
    		drawConnectingLine(box1, box4);
    
    	}
    
    	public GRect drawBox (double x, double y, String text) {
    
    		GRect box = new GRect (x, y, BOX_WIDTH, BOX_HEIGHT);
    		add(box);
    
    		GLabel label = new GLabel (text, x, y);
    
    		x = x  + BOX_WIDTH / 2 - label.getWidth() / 2;
    		y = y + BOX_HEIGHT / 2 + label.getAscent() / 2 ;
    
    		label.setLocation (x, y);
    		add (label);
    
    		return box;
    
    	}
    
    	public void drawConnectingLine(GRect fromBox, GRect toBox) {
    
    		double fromX = fromBox.getX() + BOX_WIDTH / 2;
    		double fromY = fromBox.getY() + BOX_HEIGHT;
    
    		double toX = toBox.getX() + BOX_WIDTH / 2;
    		double toY = toBox.getY();
    
    		GLine line = new GLine (fromX, fromY, toX, toY);
    		add(line);
    
    	}
    
    }
    wow your code is so simple. My code is messy and buggy. I wonder how to decompose it effectively especially the parameter is so much confusing

    Java Code:
    /*
     * File: ProgramHierarchy.java
     * Name: 
     * Section Leader: 
     * ---------------------------
     * This file is the starter file for the ProgramHierarchy problem.
     */
    
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
    
    public class ProgramHierarchy extends GraphicsProgram {	
    
    /*	Dimension of the box */
    	private static final double BOX_WIDTH = 200;
    	private static final double BOX_HEIGHT = 50;
    
    /* 	Width of space between boxes */
    	private static final double BOX_SPACE = 25;
    	
    /*	The height of space between boxes */
    	private static final double BOX_CONNECTOR = 50;
    	
    		double x = (getWidth()/2) - (BOX_WIDTH/2);
    		double y = (getHeight()/2) - (BOX_HEIGHT/2);
    		double dx = BOX_SPACE + BOX_WIDTH;
    		double dy = BOX_CONNECTOR;
    	
    		
    	public void run(){
    		drawProgram(x, y);
    		drawConsole(x, y + dy);
    		drawGraphics(x - dx , y + dy);
    		drawDialog(x + dx , y + dy);
    	}
    
    	private void drawProgram(double x, double y){
    		drawBox(x, y);
    		drawLabel("Program" , x, y );
    	}
    	
    	private void drawConsole(double x, double y){
    		drawBox(x, y);
    		drawLine(x + BOX_WIDTH/2 , y + BOX_HEIGHT, x, y + dy/2 );
    		drawLabel("ConsoleProgram" ,x ,y);	
    	}
    
    	private void drawGraphics(double x, double y){
    		drawBox(x,y);
    		drawLine(x + BOX_WIDTH/2, y + BOX_HEIGHT, x + BOX_WIDTH - dx, y + BOX_HEIGHT + dy);
    		drawLabel("GraphicsProgram" ,x, y);
    	}
    	
    	private void drawDialog(double x, double y ){
    		drawBox(x,y);
    		drawLine(x + BOX_WIDTH/2, y + BOX_HEIGHT, x + BOX_WIDTH + dx, y + BOX_HEIGHT + dy );
    	}
    
    	private void drawLine(double x1, double x2, double y1, double y2){
    		add(new GLine(x1, x2, y1, y2));
    	}
    	
    	private void drawLabel(String text, double x, double y) {
    		GLabel label = new GLabel(text, x, y);
    		x = BOX_WIDTH/2 - label.getWidth()/2;
    		y = BOX_HEIGHT/2 - label.getAscent()/2;
    		add(label);
    	}
    	
    	private void drawBox(double x, double y){
    		add(new GRect(x, y));
    	}
    }
    Last edited by YLTO; 07-07-2011 at 08:54 AM.

  16. #116
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    This is an extremely old thread, please avoid bumping or reviving dead threads.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  17. #117
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Page 6 of 6 FirstFirst ... 456

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
  •