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

  • SEARCH
  • Home
  • 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 8585893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:10:42+00:00 2026-06-11T22:10:42+00:00

I am trying to write a program like bouncingBall. but i generated N obstacles

  • 0

I am trying to write a program like bouncingBall. but i generated N obstacles in the screen. Each time the ball touch the obstacle, the obstacle disappears and shows up at another random place. i am trying to use 2 dimension array to store the random-gernerated obstacles’ point (x,y).

Right now if I input N>50, it gives me outofbound.
But what i want is to store point from (0,0) to (50,50)..what should I do achieve this with 2-dimentional array?

Thanks!

import java.util.ArrayList;

public class BouncingBall { 

    public static void main(String[] args) {

        if (args.length < 1) {
            System.out.println("Usage: java BouncingBall N");
            System.exit(0);
        }

        int N = Integer.parseInt(args[0]); 
        if (N > 2500) {
            System.out.println("Usage: java BouncingBall N<=2500");
            System.exit(0);
        }
        double[][] myArray = new double[50][50];

        // set the scale of the coordinate system
        StdDraw.setXscale(-1.0, 1.0);
        StdDraw.setYscale(-1.0, 1.0);

        // initial values
        double rx = 0.480, ry = 0.860;     // position
        double vx = 0.015, vy = 0.023;     // velocity
        double radius = 0.02;              // radius       
        double x;
        double y;
        double a[] = new double[2];

        StdDraw.setPenColor(StdDraw.WHITE);
        StdDraw.filledSquare(0, 0, 1.0);
        StdDraw.setPenColor(StdDraw.BLACK); 
        for(int i=0; i <= N; i++){
            x = 2.0*(double)Math.random()-1.0;
            y = 2.0*(double)Math.random()-1.0;
            for   (int  t=0;t <50;t++){ 
                for   (int   j=0;j <50;j++){ 
                      myArray[t][j]= x; 
                      myArray[j][t]= y; 
                } 
            } 
            StdDraw.filledSquare(x, y, 0.02);
            }

        // main animation loop
        while (true)  { 

            // bounce off wall according to law of elastic collision
            if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
            if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy;
            // clear the background   
            StdDraw.setPenColor(StdDraw.WHITE);
            StdDraw.filledSquare(0, 0, 1.0); 
            StdDraw.clear(); 

       StdDraw.setPenColor(StdDraw.BLACK);  

       for(int t=0; t <= N; t++){
           for   (int   j=0;j <50;j++){ 
                 x = myArray[t][j]; 
                 y = myArray[j][t]; 
           } 
        if ((Math.abs(rx + vx) > x - radius)||(Math.abs(ry + vy) > y - radius))
        {  //if the ball touch the square
            vx = -vx;
            vy = -vy;
            if (args.length == 2 && args[1].equals("-d")){
            x = 2.0*(double)Math.random()-1.0;  //new random x
            y = 2.0*(double)Math.random()-1.0;  //new random y
            }else{
                ;
            }
            StdDraw.filledSquare(x, y, 0.02);
            }
        else{
            StdDraw.filledSquare(x, y, 0.02); //if not touched, keep it.
            }
        }

       rx = rx + vx; 
       ry = ry + vy;

       StdDraw.filledCircle(rx, ry, radius); 
            // display and pause for 20 ms
        StdDraw.show(20); 
        }
    }  
}
  • 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-06-11T22:10:43+00:00Added an answer on June 11, 2026 at 10:10 pm

    Imagine that the user inputs -1 for N, then x and y won’t get a value because the loops bodies won’t run.

    Simple workaround: assign a default value to x and y (0 for example)

    double x = 0;
    double y = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C program like the one below. I am trying to write
I'm trying to link 10 computers together, the program I would like to write
i'm trying to write a php page that call for a server program like
I'm trying to write a program where users upload circular images like this deliciously
I am trying to write a program using python-fuse, but I can't get file
I am trying to write a program to analyze emotional expressions like tears. As
now, i'm trying to write program using php and sqlite, but i have problem.
I am trying to write a C++ program that works like the game 24.
I am trying to write a short program that acts like a client, like
I'm trying to write a program that would convert celcius to fahrenheit and visa-versa.

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.