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 8653433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:32:31+00:00 2026-06-12T14:32:31+00:00

Right now I am doing the classic shape program involving shape classes. I can

  • 0

Right now I am doing the classic shape program involving shape classes. I can do the create circle or rectangle object without any problem. But when I get perimeter or area of all the objects, it turns out all the objects are null. Here is the code:

//Case menu selection actions

Here it the instance variables and arrays

private int menu_select;
private int i=0;
private Shape[] s = new Shape[10];

Here is the menu options

public static void display_menu()
{
    System.out.print("Choose an option:\n"+
            "1-Add a new circle\n"+
            "2-Add a new rectangle\n"+
            "3-Delete all shapes\n"+
            "4-Scale all shapes\n"+
            "5-Display perimeter of all shapes\n"+
            "6-Display the area of all shapes\n"+
            "7-Enter scale factor\n"+
            "8-Exit program\n");
}

Here is the the menu code

Here is the switch

//Case menu selection actions
    public void select_case()
    {

        if(i<=10)
        {
            switch (menu_select)
            {


                 case 1: Circle c = new Circle(1);
                         s[i]=c;
                         i++;
                         break;
                 case 2: Rectangle r = new Rectangle(1,1);
                         s[i]=r;
                         i++;
                         break;

                 case 3: s=null;
                         i=0;
                         break;
                 case 4: Scanner input = new Scanner(System.in);  
                         double d = input.nextDouble();
                         for(int i=0; i<s.length; i++)
                         {
                             s[i].setScaleFactor(d);
                         }
                         break;
                 case 5: for(int i=0; i<s.length; i++)
                         {
                            if(s[i] != null)
                            {   
                                System.out.println(s[i].getPerimeter());
                            }
                         }
                         break;
                 case 6: for(int i=0; i<s.length; i++)
                         {
                            System.out.println(s[i].getArea());
                         }
                         break;
                 case 7: //Enter scale factor

                        //No need for a case 8 since while loop terminates it.
                 default: System.out.println("Number must be 1-8");

            }
        }
    }

Here is the main method

public static void main(String args[])
    {
        Menu m;
        do
        {
            Menu.display_menu();
            m = new Menu(0);

        }
        while(m.getMenu_Select() != 8);



    }

}

I have tried giving the shape array indice a fixed number and I still get a null object. I have also tried removing the for loop with the fixed indice and still get null objects.

  • 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-12T14:32:33+00:00Added an answer on June 12, 2026 at 2:32 pm

    You’re creating a new Menu object with each iteration of the loop!

        {
            Menu.display_menu();
            m = new Menu(0); // here!
    
        }
    

    Don’t do that since any changes done on this object will have no effect or memory on the next object.

    Create one Menu object before the loop, and then call methods on it in the loop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can accomplish it with custom components (I'm doing it right now
How can I evaluate if DOMTXpath->query actually returns data. Right now I am doing
What I'm doing right now is to block hotlinking of any images in a
Right now I'm doing this: SELECT * FROM messages WHERE location_id = 7 AND
Right now I'm doing as follow: uint8_t ManualFlow = 40; // 0 -> 255
I have a problem here. Right now I'm doing my Web Based Application, my
What is the most efficient way to do this? Right now I am doing
I am coming from Java and try to learn Javascript right now. Doing that
I'd like to darken a UIView by a certain amount. Right now, I'm doing
Right now, I have: RewriteRule ^([^/\.]+)?$ index.php?id=$1 [L] to match any username at the

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.