How do I create a GETTER function?

I am doing some schoolwork for computer science.

I have done most of the assessment but I just need help with creating the GETTER function. Here are the questions.

Problem 1 Star Class: Now let's say you want to decide how many stars are shown instead of hard- coding 25 in the array initialization (the first line of the program), as well as the maximum radius. You're only in charge of the Star class, so you want to add a variable called starCount and a variable called maxRadius. Main Program: - 25 = Star.starCount - 20 = Star.maxRadius

Problem 2 What happens if X & Y are made static

Problem 3: Static Methods Public variables are a security concern. Change the starCount and maxRadius to private variables and Create GETTER Functinos for both

Problem 4: Question: What would happen if getStarCount() was made static?

Here is my code Question 1 is completed, and the answer to question 2 is that the circles will clump together and overlap.

Note the code is in two separate tabs the first one is Star_Start

private Star[] stars = new Star [Star.starCount];

public void setup() {
  size(500, 500); 

  ellipseMode(CENTER);

  for (int i = 0; i < stars.length; i++) {
    stars[i] = new Star(random(500), random(500), random(Star.maxRadius));

  }
}

public void draw() {

  background(0);

  for (int i = 0; i < stars.length;  i++) {
    ellipse(stars[i].getX(), stars[i].getY(), stars[i].getRadius(), stars[i].getRadius());
  }
}
public void mouseClicked() {
  for (int i = 0; i < stars.length; i++) {
    stars[i] = new Star(random(500), random(500), random(Star.maxRadius));
  }
}

2nd Tab

Star.Java

class Star { 
  private float x; 
  private float y; 
  private float radius;
  public static int starCount = 250;
  public static int maxRadius =  20;

  public Star (float x, float y, float radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;

  }

  public float getX () {
    return x;

  }

  public float getY() {
    return y; 

  }

  public float getRadius() {
    return radius; 

  }


}

If someone could tell me what he means by GETTER function and how to add it to my code that would be wonderful. My friends and I cannot seem to figure it out and since we are on a long weekend and our teacher is busy, he is not available at the moment.

Tagged:

Answers

  • edit post, highlight code, press ctrl-o to format.

    you can do the same with the spec text in order to preserve the formatting.

    this is a getter method. search for 'java encapsulation' to see why you would want to do this.

    public float getX () {
      return x;
    }
    
Sign In or Register to comment.