Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Beginner in Java. This is probably remarkably easy to answer Posted by Sifl on 23 Jan 2010 at 7:22 PM
Hi all,

I'm taking an introductory computer science class. I know HTML (4) so I guess I kind of have a basic idea of codes. Java's stumping me. This is of the BlueJ variety, and I'm stuck on the first part of the second lab assignment of the class. I'm not looking for an entire walkthrough of my assignment, just a little help on the first instruction (or two :D), at least at this point in time. I plan on figuring out the rest of the assignment, I've just been stuck on this particular step for quite a while (my book is of no help). Any assistance is welcomed!

Here's my lab instructions:

Copy the "Lab2" folder (the entire folder and contents) from the "downloads" folder on Lamont and paste a copy inside your "Labs" folder on arthur ("H:\Personal\Cosc1010\Labs"). Using BlueJ open the "shapes" project found in the "Lab2" folder and do the following:

Change the constructor of the class Circle so that it allows you to specify the color during construction.
Add an accessor method to Circle that returns the diameter of a circle.
Add an accessor method to Circle that returns the color of a circle.
Add a mutator method to Circle that will change a circle's x position to a new x.
Add a mutator method to Circle that will change a circle's y position to a new y.
Add a mutator method to Circle that moves a circle to a new location: (x, y).
Add an accessor method to Circle that returns the distance of the circle from (0, 0): the origin.

Use: (int)(Math.sqrt(z)) to get the square root of z.
For example, the following code stores the value 10 in the variable x:
int num = 100;
int numSquareRoot = (int)(Math.sqrt(num));

Add a method that displays a circle's diameter, color, and coordinates exactly as in the following example:

The red circle at (55, 120) has a diameter of 5.


Here's the code (Circle class):

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

/**
 * A circle that can be manipulated and that draws itself on a canvas.
 * 
 * @author yourname
 * @version date
 * @section 13
 */

/*
 * kjshfklajhfkljhfdlkzsjhdflkajhdsf
 * 
 */



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;
    }
    
    
    //*******************************************************************
    
    //Your code goes here.

    
    //*******************************************************************
    
    /**
     * 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);
        }
    }
}


Report
Re: Beginner in Java. This is probably remarkably easy to answer Posted by Talan on 9 Feb 2010 at 7:52 PM
Hi there,

first question;
Change the constructor of the class Circle so that it allows you to specify the color during construction.
would be ;

public Circle(String color)
{
diameter = 30;
xPosition = 20;
yPosition = 60;
color = color;
isVisible = false;
}
Report
Re: Beginner in Java. This is probably remarkably easy to answer Posted by Talan on 9 Feb 2010 at 8:25 PM
Add an accessor method to Circle that returns the diameter of a circle.

public int getDiameter(){
return this.diameter;
}

Add an accessor method to Circle that returns the color of a circle.

public String getColorCircle(){
return this.color;
}

hope that answers your question a bit
Report
Re: Beginner in Java. This is probably remarkably easy to answer Posted by Talan on 9 Feb 2010 at 8:25 PM
Add an accessor method to Circle that returns the diameter of a circle.

public int getDiameter(){
return this.diameter;
}

Add an accessor method to Circle that returns the color of a circle.

public String getColorCircle(){
return this.color;
}

hope that answers your question a bit
Report
Re: Beginner in Java. This is probably remarkably easy to answer Posted by Talan on 9 Feb 2010 at 8:25 PM
Add an accessor method to Circle that returns the diameter of a circle.

public int getDiameter(){
return this.diameter;
}

Add an accessor method to Circle that returns the color of a circle.

public String getColorCircle(){
return this.color;
}

hope that answers your question a bit



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.