Results 1 to 6 of 6
  1. #1
    gatehrdy is offline Member
    Join Date
    Aug 2010
    Location
    I live in Arizona
    Posts
    13
    Rep Power
    0

    Default bluej challenge exercise 1.16

    I am looking for help with this challenge exercise 1.16. If you have done it and know what it is, I would appreciate some help with it. I am very new to java and am trying to teach myself with the help of this book that doesn't give solutions. I have tried and have not come up with the solution. Thank you for your time.
    John 15:7 "If ye abide in me, and my words abide in you, ye shall ask what ye will, and it shall be done unto you.":)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    You know, if you're too lazy to post some code and ask a specific question, most members that might be otherwise inclined to help won't bother to even try to find out what exactly you mean by
    challenge exercise 1.16
    Recommended reading: How To Ask Questions The Smart Way

    db
    Last edited by DarrylBurke; 09-03-2010 at 07:21 AM.

  3. #3
    gatehrdy is offline Member
    Join Date
    Aug 2010
    Location
    I live in Arizona
    Posts
    13
    Rep Power
    0

    Default Code

    Hi there. I didn't put the code up, but instead asked if anyone else has been through the same book. ("If you have done it and know what it is") If they have done it, and know what it is, then this would certainly reduce the confusion, but because you were so inclined to comment about the code, I will post it. The challenge calls for the student to add a function to a picture class. "Exercise 1.16 Challenge exercise If you added your sunset to the end of the draw method (so the sun goes down automatically when the picture is drawn), change this now. We now want the sunset in a separate method, so that we can call draw and see the picture with the sun up and then call sunset (a separate method!) to make the sun go down." There it is as it is written out of the book.

    Here is the code for the picture class:

    public class Picture
    {
    private Square wall;
    private Square window;
    private Triangle roof;
    private Circle sun;


    /**
    * Constructor for objects of class Picture
    */
    public Picture()
    {
    // nothing to do... instance variables are automatically set to null
    }

    /**
    * Draw this picture.
    */
    public void draw()
    {
    wall = new Square();
    wall.moveVertical(80);
    wall.changeSize(100);
    wall.makeVisible();

    window = new Square();
    window.changeColor("black");
    window.moveHorizontal(20);
    window.moveVertical(100);
    window.makeVisible();

    roof = new Triangle();
    roof.changeSize(50, 140);
    roof.moveHorizontal(60);
    roof.moveVertical(70);
    roof.makeVisible();

    sun = new Circle();
    sun.changeColor("blue");
    sun.moveHorizontal(180);
    sun.moveVertical(-10);
    sun.changeSize(60);
    sun.makeVisible();
    }



    /**
    * Change this picture to black/white display
    */
    public void setBlackAndWhite()
    {
    if(wall != null) // only if it's painted already...
    {
    wall.changeColor("black");
    window.changeColor("white");
    roof.changeColor("black");
    sun.changeColor("black");
    }
    }

    /**
    * Change this picture to use color display
    */
    public void setColor()
    {
    if(wall != null) // only if it's painted already...
    {
    wall.changeColor("red");
    window.changeColor("black");
    roof.changeColor("green");
    sun.changeColor("yellow");
    }
    }
    }

    Here is the code for the circle class:

    import java.awt.*;
    import java.awt.geom.*;

    /**
    * A circle that can be manipulated and that draws itself on a canvas.
    *
    * @author Michael Kolling and David J. Barnes
    * @version 2006.03.30
    */

    public class Circle
    {

    private int diameter;
    private int xPosition;
    private int yPosition;
    private String color;
    private boolean isVisible;

    /**
    * Create a new circle at default position with default color.
    */
    public Circle()
    {

    diameter = 30;
    xPosition = 20;
    yPosition = 60;
    color = "blue";
    isVisible = false;
    }

    /**
    * Make this circle visible. If it was already visible, do nothing.
    */
    public void makeVisible()
    {
    isVisible = true;
    draw();
    }

    /**
    * Make this circle invisible. If it was already invisible, do nothing.
    */
    public void makeInvisible()
    {
    erase();
    isVisible = false;
    }

    /**
    * Move the circle a few pixels to the right.
    */
    public void moveRight()
    {
    moveHorizontal(20);
    }

    /**
    * Move the circle a few pixels to the left.
    */
    public void moveLeft()
    {
    moveHorizontal(-20);
    }

    /**
    * Move the circle a few pixels up.
    */
    public void moveUp()
    {
    moveVertical(-20);
    }

    /**
    * Move the circle a few pixels down.
    */
    public void moveDown()
    {
    moveVertical(20);
    }

    /**
    * Move the circle horizontally by 'distance' pixels.
    */
    public void moveHorizontal(int distance)
    {
    erase();
    xPosition += distance;
    draw();
    }

    /**
    * Move the circle vertically by 'distance' pixels.
    */
    public void moveVertical(int distance)
    {
    erase();
    yPosition += distance;
    draw();
    }

    /**
    * Slowly move the circle horizontally by 'distance' pixels.
    */
    public void slowMoveHorizontal(int distance)
    {
    int delta;

    if(distance < 0)
    {
    delta = -1;
    distance = -distance;
    }
    else
    {
    delta = 1;
    }

    for(int i = 0; i < distance; i++)
    {
    xPosition += delta;
    draw();
    }
    }

    /**
    * Slowly move the circle vertically by 'distance' pixels.
    */
    public void slowMoveVertical(int distance)
    {
    int delta;

    if(distance < 0)
    {
    delta = -1;
    distance = -distance;
    }
    else
    {
    delta = 1;
    }

    for(int i = 0; i < distance; i++)
    {
    yPosition += delta;
    draw();
    }
    }

    /**
    * Change the size to the new size (in pixels). Size must be >= 0.
    */
    public void changeSize(int newDiameter)
    {
    erase();
    diameter = newDiameter;
    draw();
    }

    /**
    * Change the color. Valid colors are "red", "yellow", "blue", "green",
    * "magenta" and "black".
    */
    public void changeColor(String newColor)
    {
    color = newColor;
    draw();
    }

    /**
    * Draw the circle with current specifications on screen.
    */
    private void draw()
    {
    if(isVisible) {
    Canvas canvas = Canvas.getCanvas();
    canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
    diameter, diameter));
    canvas.wait(10);
    }
    }

    /**
    * Erase the circle on screen.
    */
    private void erase()
    {
    if(isVisible) {
    Canvas canvas = Canvas.getCanvas();
    canvas.erase(this);
    }
    }
    }


    there are 3 more classes. It's probably not necessary to post them, but if needed, I will. Thanks for your tactfulness, Oh I mean thoughtfulness... Oh yeah, I'm not lazy. Thanks for your time.
    Last edited by gatehrdy; 09-03-2010 at 09:06 AM.
    John 15:7 "If ye abide in me, and my words abide in you, ye shall ask what ye will, and it shall be done unto you.":)

  4. #4
    kgbrown247 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: Code

    Quote Originally Posted by gatehrdy View Post
    Hi there. I didn't put the code up, but instead asked if anyone else has been through the same book. ("If you have done it and know what it is") If they have done it, and know what it is, then this would certainly reduce the confusion, but because you were so inclined to comment about the code, I will post it. The challenge calls for the student to add a function to a picture class. "Exercise 1.16 Challenge exercise If you added your sunset to the end of the draw method (so the sun goes down automatically when the picture is drawn), change this now. We now want the sunset in a separate method, so that we can call draw and see the picture with the sun up and then call sunset (a separate method!) to make the sun go down." There it is as it is written out of the book.

    Here is the code for the picture class:

    public class Picture
    {
    private Square wall;
    private Square window;
    private Triangle roof;
    private Circle sun;


    /**
    * Constructor for objects of class Picture
    */
    public Picture()
    {
    // nothing to do... instance variables are automatically set to null
    }

    /**
    * Draw this picture.
    */
    public void draw()
    {
    wall = new Square();
    wall.moveVertical(80);
    wall.changeSize(100);
    wall.makeVisible();

    window = new Square();
    window.changeColor("black");
    window.moveHorizontal(20);
    window.moveVertical(100);
    window.makeVisible();

    roof = new Triangle();
    roof.changeSize(50, 140);
    roof.moveHorizontal(60);
    roof.moveVertical(70);
    roof.makeVisible();

    sun = new Circle();
    sun.changeColor("blue");
    sun.moveHorizontal(180);
    sun.moveVertical(-10);
    sun.changeSize(60);
    sun.makeVisible();
    }



    /**
    * Change this picture to black/white display
    */
    public void setBlackAndWhite()
    {
    if(wall != null) // only if it's painted already...
    {
    wall.changeColor("black");
    window.changeColor("white");
    roof.changeColor("black");
    sun.changeColor("black");
    }
    }

    /**
    * Change this picture to use color display
    */
    public void setColor()
    {
    if(wall != null) // only if it's painted already...
    {
    wall.changeColor("red");
    window.changeColor("black");
    roof.changeColor("green");
    sun.changeColor("yellow");
    }
    }
    }

    Here is the code for the circle class:

    import java.awt.*;
    import java.awt.geom.*;

    /**
    * A circle that can be manipulated and that draws itself on a canvas.
    *
    * @author Michael Kolling and David J. Barnes
    * @version 2006.03.30
    */

    public class Circle
    {

    private int diameter;
    private int xPosition;
    private int yPosition;
    private String color;
    private boolean isVisible;

    /**
    * Create a new circle at default position with default color.
    */
    public Circle()
    {

    diameter = 30;
    xPosition = 20;
    yPosition = 60;
    color = "blue";
    isVisible = false;
    }

    /**
    * Make this circle visible. If it was already visible, do nothing.
    */
    public void makeVisible()
    {
    isVisible = true;
    draw();
    }

    /**
    * Make this circle invisible. If it was already invisible, do nothing.
    */
    public void makeInvisible()
    {
    erase();
    isVisible = false;
    }

    /**
    * Move the circle a few pixels to the right.
    */
    public void moveRight()
    {
    moveHorizontal(20);
    }

    /**
    * Move the circle a few pixels to the left.
    */
    public void moveLeft()
    {
    moveHorizontal(-20);
    }

    /**
    * Move the circle a few pixels up.
    */
    public void moveUp()
    {
    moveVertical(-20);
    }

    /**
    * Move the circle a few pixels down.
    */
    public void moveDown()
    {
    moveVertical(20);
    }

    /**
    * Move the circle horizontally by 'distance' pixels.
    */
    public void moveHorizontal(int distance)
    {
    erase();
    xPosition += distance;
    draw();
    }

    /**
    * Move the circle vertically by 'distance' pixels.
    */
    public void moveVertical(int distance)
    {
    erase();
    yPosition += distance;
    draw();
    }

    /**
    * Slowly move the circle horizontally by 'distance' pixels.
    */
    public void slowMoveHorizontal(int distance)
    {
    int delta;

    if(distance < 0)
    {
    delta = -1;
    distance = -distance;
    }
    else
    {
    delta = 1;
    }

    for(int i = 0; i < distance; i++)
    {
    xPosition += delta;
    draw();
    }
    }

    /**
    * Slowly move the circle vertically by 'distance' pixels.
    */
    public void slowMoveVertical(int distance)
    {
    int delta;

    if(distance < 0)
    {
    delta = -1;
    distance = -distance;
    }
    else
    {
    delta = 1;
    }

    for(int i = 0; i < distance; i++)
    {
    yPosition += delta;
    draw();
    }
    }

    /**
    * Change the size to the new size (in pixels). Size must be >= 0.
    */
    public void changeSize(int newDiameter)
    {
    erase();
    diameter = newDiameter;
    draw();
    }

    /**
    * Change the color. Valid colors are "red", "yellow", "blue", "green",
    * "magenta" and "black".
    */
    public void changeColor(String newColor)
    {
    color = newColor;
    draw();
    }

    /**
    * Draw the circle with current specifications on screen.
    */
    private void draw()
    {
    if(isVisible) {
    Canvas canvas = Canvas.getCanvas();
    canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
    diameter, diameter));
    canvas.wait(10);
    }
    }

    /**
    * Erase the circle on screen.
    */
    private void erase()
    {
    if(isVisible) {
    Canvas canvas = Canvas.getCanvas();
    canvas.erase(this);
    }
    }
    }


    there are 3 more classes. It's probably not necessary to post them, but if needed, I will. Thanks for your tactfulness, Oh I mean thoughtfulness... Oh yeah, I'm not lazy. Thanks for your time.
    public void sunset()
    {
    sun.slowMoveVertical(300);
    }
    then call sunset after you call the drawing

  5. #5
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Code

    Hey mate,

    it would be better if you posted your code in the proper format so we all can read it properly. If your going to post chunks of code - do it in the advanced form and there is a button that you can paste your code between.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: bluej challenge exercise 1.16

    The question was asked 2 years ago. Closing the thread.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 04:15 AM
  2. Need Help in BlueJ... Cannot figure this out.
    By ERICAMAUVE4 in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 12:34 AM
  3. BlueJ help
    By Cid17 in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 07:38 PM
  4. help using BlueJ
    By zoe in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:07 AM
  5. BlueJ 2.2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-07-2007, 02:28 PM

Tags for this Thread

Posting Permissions

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