Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Beginner in Java. This is probably remarkably easy to answer

SiflSifl Posts: 1Member
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:

[color=Green]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:PersonalCosc1010Labs"). 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.[/color]

Here's the code (Circle class):

[code]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);
}
}
}[/code]

Comments

  • TalanTalan Posts: 4Member
    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;
    }
  • TalanTalan Posts: 4Member
    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
  • TalanTalan Posts: 4Member
    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
  • TalanTalan Posts: 4Member
    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
Sign In or Register to comment.