Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 72
  1. #41
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ccie007: I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck!

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

    Default

    Just found out that you can in fact concatenate a integer with an empty String... didn't know that until now. I guess that's the best way of doing it?
    Last edited by Learning Java; 05-22-2010 at 10:13 PM.

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    That's the easiest way to convert an int to a String. See also the Integer class for many static methods that convert ints to String.

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

    Default

    Cool :) It's a shame that something like that can get in the way of progressing in an exercise... I mean, I could sit here for hours trying to come up with a way of doing an exercise only to find I didn't have the tools required to do the task. I certainly don't remember the book mentioning this; but I could be wrong.

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

    Default

    Quote Originally Posted by Learning Java View Post
    Just found out that you can in fact concatenate a integer with an empty String... didn't know that until now. I guess that's the best way of doing it?
    That's not the best way at all, really, but fine if you're concatenating to an existing String. If you really just want a conversion, then you should use Integer.toString(myInt) or String.valueOf(myInt).

    @OP:
    For this exercise, you'll need to figure out the left and top margins before you start putting GRects up, or they'll all be jammed into the top left corner. You should do a little decomposition -- break the job down into simpler methods. I would do at least a drawDay() and probably a positionDay() too. The job of positionDay(int day) would be to figure out where the GRect should go, and then drawDay(double x, double y, int day) would actually draw the GRect and the GLabel. If you do it that way, you can start by writing your drawDay() method, and get that looking nice (experiment with GLabel offsets from GRect coordinates, and also the GLabel font) before you move on to the rest.

    -Gary-

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

    Lightbulb Strictly in the Context of the cs106a course

    Quote Originally Posted by gcalvin View Post
    @OP:
    -- break the job down into simpler methods. I would do at least a drawDay() and probably a positionDay() too. The job of positionDay(int day) would be to figure out where the GRect should go, and then drawDay(double x, double y, int day) would actually draw the GRect and the GLabel. If you do it that way, you can start by writing your drawDay() method, and get that looking nice (experiment with GLabel offsets from GRect coordinates, and also the GLabel font) before you move on to the rest.

    -Gary-

    I think part of the problem with doing it this way (which is the way that makes the most sense) is that at this point in the course the student has not really been introduced to the idea of methods (exepting Karel the Robot), or passing and returning parameters, and the course is still focussed on control statements and expressions at this point. methods is coming up next.

    far beit for me to differ from an excellent teacher like Gary, but to my mind this exercise is just a development of the checkerboard exercise. only the objective here is to control which sqaure to start numbering from and possibly How many rows to draw aswell. and done in one singular run method.
    so it goes
    public void run(){
    Java Code:
     //firstly calculate the number of rows 
    int rows = something or maybe just set to 6 for now
    for (int i = 0; i < rows; i++) {	// vertical setup
    			for (int j = 0; j < DAYS_IN_WEEK; j++) {   //horizontal setup
    				int x = j * DAY_WIDTH;
    				int y = i * DAY_HEIGHT;
    				GRect square = new GRect(x, y, DAY_WIDTH, DAY_HEIGHT);
    				add(square);
    
    }
    up to here this is identical to checkerboard. but the number of rows needs to be calculated ( or maybe just assume that it will be 6 to begin with you can figure out what happens if february starts on a sunday at a later point))

    after this point (but still inside the braces of the j loop)
    the date (or integer value) of the square must be determined.
    Java Code:
    int date =  something
    BUT, the first square has no date ???? or does it ???
    if you are going to start numbering (in a for loop)you have to start on the first iteration of the loop (at the first square).
    HINT you might not add a GLabel to the first square, but every square will have a integer value. which you will calculate from the given constants and the I and J indexes. this is the really tricky bit of this exercise.
    draw it with pencil and paper and write in the index values of I and J in each square. then use your constants to calculate what the date should be in that square. will the formula(expression) you used work the same for every square

    next....(still inside the braces of the J loop)
    So how do you get the program to start numbering (adding a GLable of the date) from the required square

    Java Code:
    if (date.......){
    your going to (add) something to the canvass
    }
    finally your going to add the days of the week to the top row only.
    if you can solve the date, the days are a sinch, with a switch statement

    EDIT
    to clarify: the above is strictly limited to the context of the cs106a course
    Last edited by sonny; 05-23-2010 at 03:08 AM.
    :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

  7. #47
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    BUT, the first square has no date ???? or does it ???
    First square is empty.Dates only starts from the 6th and 7th square of the first row.Hence first 5 squares of the first row are empty.

    By the way any idea why the program needs to have constant

    /* The day of the week on which the month starts */
    /* (Sunday = 0, Monday = 1, Tuesday = 2, and so on) */
    private static final int DAY_MONTH_STARTS = 5;

    The question says

    Your program should use the following named constants to control the format of the
    calendar display:
    /* The number of days in the month */
    private static final int DAYS_IN_MONTH = 31;
    /* The day of the week on which the month starts */
    /* (Sunday = 0, Monday = 1, Tuesday = 2, and so on) */
    private static final int DAY_MONTH_STARTS = 5;
    /* The width in pixels of a day on the calendar */
    private static final int DAY_WIDTH = 40;
    /* The height in pixels of a day on the calendar */
    private static final int DAY_HEIGHT = 30;

  8. #48
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    DAY_MONTH_STARTS is the column where the "1" should appear.

  9. #49
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    okay.By the way what is that device on your pic Norm.

  10. #50
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I am trying to display dates on the sqaures

    [code##]public void run(){

    for (int i=0; i < NROWS; i++){
    for (int j=0; j < NCOLUMN; j++){

    int x = j * DAY_WIDTH;

    int y = i * DAY_HEIGHT;

    GRect sq = new GRect(x,y,DAY_WIDTH, DAY_HEIGHT);

    for(int k=0;k<DAYS_IN_MONTH;k++){ }
    [code##/]
    I believe hopefull this for loop with k index will help me to display 1 to 31 days of the month.But how can we display that on the canvas.I think we can't display variable k with GLabel like println(k).Any idea mates?

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    That's a sextant from my sailing days.

    how can we display that on the canvas
    Earlier on you posted code that displayed using the GLabel class. Go back and look at that. You need to have the position it displayed at be variables so that it moves across the screen and down the screen.

  12. #52
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I have written code which put circles in the right sqaures where the dates number should be ie 6th and 7th squares at the front row and so.
    check out the image/pic [IMG]http://http://i.domaindlx.com/cobainia/cale.jpg[/IMG]
    But my objective is to put numbers instead of circles.Ideally numbers from a loop

    for (int k=1;k<DAYS_IN_MONTH;k++)

    which should display number from 1 to 31.Any ideas how we can display values of variable k on the canvass using GLabeL.I believe GLabel only allows you to write string like GLabel day = new GLabel(("hi world"),17,17).How can we do like

    GLabel day = new GLabel((k),17,17) where k display values from 1 to 31 days.
    Last edited by ccie007; 05-23-2010 at 06:23 PM.

  13. #53
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    You need to have the position it displayed at be variables so that it moves across the screen and down the screen.
    Add to Norm's Reputation
    displayed at be variables
    Norm could you clarify this for me plz.

  14. #54
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Default

    Here is how I did the exercise. There is probably a better way of doing it, though.

    Java Code:
    import acm.program.*;
    import acm.graphics.*;
    
    
    public class Calendar extends GraphicsProgram {
    
    	/* The number of days in the month */
    	private static final int DAYS_IN_MONTH = 31;
    	/* The day of the week on which the month starts */
    	/* (Sunday = 0, Monday = 1, Tuesday = 2, and so on) */
    	private static final int DAY_MONTH_STARTS = 5;
    	/* The width in pixels of a day on the calendar */
    	private static final int DAY_WIDTH = 50;
    	/* The height in pixels of a day on the calendar */
    	private static final int DAY_HEIGHT = 40;
    	/* Number of columns in the calendar */
    	private static final int NUMBER_OF_COLUMNS = 7;
    	/* Space between box x coordinate and label x coordinate on the calendar */
    	private static final int LABEL_X_SPACE = 5;
    	/* Space between box y coordinate and label y coordinate on the calendar */
    	private static final int LABEL_Y_SPACE = 15;
    
    	public void run() {
    
    		int DAY = 1;	
    
    		for (int row = 0; DAY <= DAYS_IN_MONTH; row++)
    			for (int collumn = 0; collumn < NUMBER_OF_COLUMNS ; collumn++) {
    
    				GRect box = new GRect (collumn * DAY_WIDTH, row * DAY_HEIGHT, DAY_WIDTH, DAY_HEIGHT);			
    
    				add(box);
    
    				if (collumn >= DAY_MONTH_STARTS || row > 0) {
    
    					GLabel label = new GLabel (DAY  + "", collumn  * DAY_WIDTH + LABEL_X_SPACE, row  * DAY_HEIGHT + LABEL_Y_SPACE);
    
    					if (DAY <= DAYS_IN_MONTH) {
    
    						add(label);
    
    						DAY++;
    
    					}
    
    				}
    
    			}
    
    	}
    
    }

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

    Post

    constructing a Glabel is covered in the lectures on you tube. and the book.

    there are a few ways you can do it but the most relevant way for this exercise is as follows

    Java Code:
    GLabel [I]name[/I] = new GLabel (" " + k + " ", x co-ordinate, y co-ordinate)..
    However, a third k loop! inside your J loop? so how many times are you going to be counting to 31? your code suggests that you will be counting up to 31 a total of 42 times.

    you have not correctly added code tags, if you want people to be able to understand where you are up to using code tags is a big help

    think again about your nested for loops
    forget the third loop for now.. and the circles

    paste in this bit of code to your program after you add your square
    Java Code:
    add( new GLabel("( "+i+" , "+j+" )", x+5, y+15) );
    pause(200);
    now run the program.

    what do you see now?
    does this help you see what the loops are doing?

    in each square you should be able to see the value of i index and the value of the j index when that square was added.

    lets forget about adding the date for now, it is only two lines of code but lets just try to get the days of the week added its about 8 lines of code but its much more obvious to see how to control adding the labels.

    look at the top row of squares, what do they all have in common in terms of the loop variables. and what is different?
    i = 0
    j is incrementing

    you can do a switch statement yes? (chap 4 page 78)
    so
    Java Code:
    if (i==0){
    		switch (j){
    		
    		case(0):
    			add( new GLabel("Sun", x+25, y+15) ); break;
    		
    		case(1):
    			add( new GLabel("Mon", x+25, y+15) );break;
    			
    		
    
    you can complete the rest to case 6
    }
    run that. days of the week on the top row yes?

    so what happens if you take out
    Java Code:
    if (i==0){
    remember to take out the closing curly brace so just have the switch statement inside the J Loop.

    the if(i==0) is the control statement. it is within the second loop so it is executed on every iteration of the J loop, (and the J loop goes all the way through on each iteration of the I Loop.) the if(i==0) controls when to add the label. in this case only add the labels while the i loop is zero

    okay if you have that working, post what you have using code tags your only two lines of code from completing the task.

    what you need to do next inside the Jloop is
    declare an integer variable,
    date would be a good name for it
    date is going to be declared on every iteration of JLoop (ie 42 times)
    you need to calculate what the date is using the DAYS_IN_WEEK, i index, J index, and DAY_MONTH_STARTS in each square

    HINT: the date in the first square will be negative, the date in the last square is going to be bigger than 31.
    but we will have a control statement to determine if we add the date as a label or not.


    heres another little tip for posting code.
    before you copy your code to the clipboard.
    in eclipse.
    press CTRL A
    press CTRL I
    press CTRL SHIFT F

    your code should now be corectly formatted and indented.

    regards
    sonny
    :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

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

    Thumbs up just some key differences highlighted

    Quote Originally Posted by Learning Java View Post
    Here is how I did the exercise. There is probably a better way of doing it, though.
    theres nothing wrong with that it looks to work fine.:cool:
    just add a switch for days of the week.

    the key differences are that LJ has initialised a variable outside the first for loop that will control the first for loop. and the variable is incremented later in the code.
    although maybe i would have called this variable square just to avoid any confusion between actual days. and convention dictates that we usually start counting from 0. this simplifies the nested if control statements inside the j loop
    also note that drawing the GRect and GLabels. LJ has instead of declaring variables like x = row*DAY_Hieght to use in the constructor of the GRect, he has used the expression directly in the GRects constructor. saving a few lines of code.
    so like this
    Java Code:
    int square = 0;	
    
    		for (int row = 0; square < DAYS_IN_MONTH; row++)
    			for (int collumn = 0; collumn < NUMBER_OF_COLUMNS ; collumn++) {
    
    				GRect box = new GRect (collumn * DAY_WIDTH, row * DAY_HEIGHT, DAY_WIDTH, DAY_HEIGHT);			
    
    				add(box);
    
    				if (collumn > DAY_MONTH_STARTS || row > 0) {
    
    					GLabel label = new GLabel (square  + "", collumn  * DAY_WIDTH + LABEL_X_SPACE, row  * DAY_HEIGHT + LABEL_Y_SPACE);
    
    					if (square < DAYS_IN_MONTH) {
    
    						add(label);  square++;
    
    
    					}
    
    				}
    				
    
    			}
    
    	}
    
    }
    Last edited by sonny; 05-23-2010 at 11:25 PM. Reason: correction
    :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

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

    Default

    Quote Originally Posted by sonny View Post
    just add a switch for days of the week.
    My book doesn't mention anything about adding days of the week :confused: Have you a different version of the book? Or was this something you thought would make the program better? Does sound like a good idea though actually! :)

  19. #59
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Well done LJ you solved the problem.How long have u been programming.

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

    Default

    I haven't been "programming" (if you can call it that) for very long at all. I'm only on video 7 on CS106a so I'm new like you are. Although as far as I can tell my code does solve the problem, that doesn't mean it's the best way to do the exercise.

    Do you understand how the code works?

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM
  2. Need help with java question
    By ccie007 in forum New To Java
    Replies: 23
    Last Post: 05-18-2010, 06:32 PM
  3. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  4. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  5. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM

Posting Permissions

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