Question

In: Computer Science

Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public...

Use BlueJ java to finish this PolkaDots class, class circle is included below as well.

public class PolkaDots
{
private ArrayList<Circle> dots;// an arrayList field to hold an ArrayList.

/**
* Create a new list of Circles, starts with 5 default circles.
*/
public PolkaDots()
{
dots = new ArrayList<Circle>(); // an arrayList to hold a bunch of circles

Circle circle1 = new Circle(30, 10, 10, "blue");//create the 1st circle
dots.add(circle1);// add the 1st circle to the arrayList   

Circle circle2 = new Circle(10, 50, 50, "red");
dots.add(circle2);

Circle circle3 = new Circle(50, 50, 150, "green");
dots.add(circle3);

Circle circle4 = new Circle(20, 100, 100, "yellow");
dots.add(circle4);

Circle circle5 = new Circle(30, 100, 150, "magenta");
dots.add(circle5);
}

/**
* Create a new list of Circles. Starts with the number of circles
* given by the integer parameter.
*/
//Write another constructor method that accepts a single integer
//parameter "dotNumber" and uses a while loop to add "dotNumber" circles
//to the "dots" ArrayList.
//The added circles should be created with the Circle class's default constructor
//(i.e. they will all be "blue" circles with a diameter of 30 & etc.)

/**
* Adds a new Circle to the "dots" arrayList.
*/
public void addDot(Circle newDot)
{
dots.add(newDot);
}

/**
* Make dots visible.
*/
public void makeVisible()
{
Circle thisDot;
int index = 0;
while(index < dots.size())
{
thisDot = dots.get(index);
thisDot.makeVisible();
index++;
}
}

/**
* Make all dots invisible.
*/
public void makeInvisible()
{
Circle thisDot;
int index = 0;
while(index < dots.size())
{
thisDot = dots.get(index);
thisDot.makeInvisible();
index++;
}
}

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 = true;
       draw();
}
  
/**
* Create a new circle with given values.
*/
public Circle(int diameter, int xPosition, int yPosition, String color)
{
       this.diameter = diameter;
       this.xPosition = xPosition;
       this.yPosition = yPosition;
       this.color = color;
       isVisible = true;
       draw();
}

   /**
   * 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;
   }
  
   /**
   * Change the position of the circle.
   */
   public void changePosition(int newX, int newY)
   {  
   xPosition = newX;
   yPosition = newY;   
   draw();
}
  
/**
* Change the size to the new size (in pixels). Size must be >= 0.
*/
public void changeSize(int newDiameter)
{
       diameter = newDiameter;
       draw();
}

/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
   * "magenta" and "black".
*/
public void changeColor(String newColor)
{
       color = newColor;
       draw();
}
  
/**
* Change the color by using a code number.
* Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(int newColorNumber)
{
if(newColorNumber > 0 && newColorNumber < 7)
{
if(newColorNumber == 1)
{
changeColor("red");
}
else if( newColorNumber == 2)
{
changeColor("yellow");
}
else if( newColorNumber == 3)
{
changeColor("green");
}
else if( newColorNumber == 4)
{
changeColor("blue");
}
else if( newColorNumber == 5)
{
changeColor("magenta");
}
else
{
changeColor("black");
}
}
}
  
   /*
   * 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);
       }
   }
  
/*
* get Diameter
*/
public int getDiameter()
{
return diameter;
}

/*
* get xPosition
*/
public int getXPosition()
{
return xPosition;
}

/*
* get yPosition
*/
public int getYPosition()
{
return yPosition;
}
}

Solutions

Expert Solution

Attached the complete code. If you get any queries, feel free to comment.

Program Screenshot for Indentation Reference:

Program code to copy:

import java.util.*;


public class PolkaDots {
    private ArrayList<Circle> dots;// an arrayList field to hold an ArrayList.

    /**
     * Create a new list of Circles, starts with 5 default circles.
     */
    public PolkaDots() {
        dots = new ArrayList<Circle>(); // an arrayList to hold a bunch of circles

        Circle circle1 = new Circle(30, 10, 10, "blue");// create the 1st circle
        dots.add(circle1);// add the 1st circle to the arrayList

        Circle circle2 = new Circle(10, 50, 50, "red");
        dots.add(circle2);

        Circle circle3 = new Circle(50, 50, 150, "green");
        dots.add(circle3);

        Circle circle4 = new Circle(20, 100, 100, "yellow");
        dots.add(circle4);

        Circle circle5 = new Circle(30, 100, 150, "magenta");
        dots.add(circle5);
    }

    /**
     * Create a new list of Circles. Starts with the number of circles given by the
     * integer parameter.
     */
    // Write another constructor method that accepts a single integer
    // parameter "dotNumber" and uses a while loop to add "dotNumber" circles
    // to the "dots" ArrayList.
    // The added circles should be created with the Circle class's default
    // constructor
    // (i.e. they will all be "blue" circles with a diameter of 30 & etc.)

    public PolkaDots(int dotNumber) {
        // create an empty array list
        dots = new ArrayList<Circle>();
        // add circles
        for (int i = 0; i < dotNumber; i++) {
            dots.add(new Circle());
        }
    }

    /**
     * Adds a new Circle to the "dots" arrayList.
     */
    public void addDot(Circle newDot) {
        dots.add(newDot);
    }

    /**
     * Make dots visible.
     */
    public void makeVisible() {
        Circle thisDot;
        int index = 0;
        while (index < dots.size()) {
            thisDot = dots.get(index);
            thisDot.makeVisible();
            index++;
        }
    }

    /**
     * Make all dots invisible.
     */
    public void makeInvisible() {
        Circle thisDot;
        int index = 0;
        while (index < dots.size()) {
            thisDot = dots.get(index);
            thisDot.makeInvisible();
            index++;
        }
    }

    public void randomizeSizes() {
        // get random class
        Random rand = new Random();

        // loop and set size

        for (Circle c : dots) {
            // 1 and 100 inclusive random number
            c.changeSize(rand.nextInt(100) + 1);
        }
    }

    public void randomizeColors() {
        // get random class
        Random rand = new Random();

        // loop and set color
        for (Circle c : dots) {
            // 1 and 6 inclusive random number
            c.changeSize(rand.nextInt(6) + 1);
        }
    }

    public void randomizePositions() {
        // get random class
        Random rand = new Random();
        // get number of items in dot
        int len = dots.size();
        // loop using while loop
        int i = 0; // current index
        while (i < len) {
            // set the random positions
          
            // OPTIONAL
            // get a random position between 1 and
            // 300 - circle diameter to partial
            // overflow
            int x = rand.nextInt(300 - dots.get(i).getDiameter()) + 1;
            int y = rand.nextInt(300 - dots.get(i).getDiameter()) + 1;
            dots.get(i).changePosition(x, y);
            // increment index
            i++;
        }
    }

}


Related Solutions

JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with specified width and height • A method name getWidth() return the value...
to be done in java Your task is to finish the StockItem class so that it...
to be done in java Your task is to finish the StockItem class so that it meets the following criteria • The StockItem class will have 4 attributes: a stock number; a name; the price of the item; the total number of items currently in stock • The first three of the above characteristics will need to be set at the time a StockItem object is created, with the total number of items set to 0 at this time. The...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the constructor for the class. Instantiate the testArrayList *and call the fillArrayList method to add objects to the testArrayList. * @param numElements The number of elements in the ArrayList */ public TestArrayLists(int numElements) { } /** * Complete the fillArrayList method. It should fill the testArrayList with * TestArrays objects consisting of 10 element int Array of random numbers. * @param numElements The number of...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT