Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 647069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:40:56+00:00 2026-05-13T21:40:56+00:00

I’m learning Java, and I know one of the big complaints about newbie programmers

  • 0

I’m learning Java, and I know one of the big complaints about newbie programmers is that we make really long and involved methods that should be broken into several. Well here is one I wrote and is a perfect example. :-D.

public void buildBall(){

        /* sets the x and y value for the center of the canvas */
        double i = ((getWidth() / 2));
        double j = ((getHeight() / 2));

        /* randomizes the start speed of the ball */
        vy = 3.0;
        vx = rgen.nextDouble(1.0, 3.0);
        if (rgen.nextBoolean(.05)) vx = -vx;    

        /* creates the ball */
        GOval ball = new GOval(i,j,(2 *BALL_RADIUS),(2 * BALL_RADIUS));     
        ball.setFilled(true);
        ball.setFillColor(Color.RED);
        add(ball);


        /* animates the ball */ 
        while(true){
            i = (i + (vx* 2));
            j = (j + (vy* 2));  
            if (i > APPLICATION_WIDTH-(2 * BALL_RADIUS)){
                vx = -vx;       
            }

            if (j > APPLICATION_HEIGHT-(2 * BALL_RADIUS)){
                vy = -vy;
            }

            if (i < 0){
                vx = -vx;
            }

            if (j < 0){
                vy = -vy;
            }

            ball.move(vx + vx, vy + vy);
            pause(10);

            /* checks the edges of the ball to see if it hits an object */
            colider = getElementAt(i, j);

            if (colider == null){
                colider = getElementAt(i + (2*BALL_RADIUS), j); 
                }
            if (colider == null){
                colider = getElementAt(i + (2*BALL_RADIUS), j + (2*BALL_RADIUS));   
                }
            if (colider == null){
                colider = getElementAt(i, j + (2*BALL_RADIUS)); 
                }

            /* If the ball hits an object it reverses direction */
            if (colider != null){
                vy = -vy;


             /* removes bricks when hit but not the paddle */
                if (j < (getHeight() -(PADDLE_Y_OFFSET + PADDLE_HEIGHT))){
                remove(colider);
                }

            }

        }

You can see from the title of the method that I started with good intentions of “building the ball”.

There are a few issues I ran up against:

The problem is that then I needed to move the ball, so I created that while loop. I don’t see any other way to do that other than just keep it “true”, so that means any other code I create below this loop won’t happen. I didn’t make the while loop a different function because I was using those variables i and j.

So I don’t see how I can refactor beyond this loop.

So my main question is:

How would I pass the values of i and j to a new method: “animateBall” and how would I use
ball.move(vx + vx, vy + vy); in that new method if ball has been declared in the buildBall method?

I understand this is probably a simple thing of better understanding variable scope and passing arguments, but I’m not quite there yet…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:40:56+00:00Added an answer on May 13, 2026 at 9:40 pm

    This can be refactored into three methods
    a> build ball : create the ball object and set initial location: buildBall()
    b> animate the entire while loop except the collider part animate(Ball ball, vx, vy)
    c> get the collider getCollider()

    Since ball is an object and you are already setting i,j as its fields they will be passed. Java passes all parameters by value. Objects live on the heap; object references are passed by value as method parameters.

    Edit, added pseudo code

        class Animator{
        void animateBall(){
        Ball ball = buildBall(); //Ball will have i,j,radius etc set by this method
        int vx = randomNumber();
        int vy = randomNumber();
        moveIt(vx,vy, ball);
        }
        void moveIt(int vx, int vy, Ball ball){
            while(true){
                 //move the ball, change i,j fields of ball
                 //check for collission etc
                 Collider collider = getCollider(ball);
                 //change direction based on collider etc.
            }
        }
        Collider getCollider(Ball ball){
        //collision code here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.