0

At this point I'm just trying to get something to come out of what little I have and nothing is working.

So I start out with a driver class:

    class TheDriverClass
    {
      public static void main(String[] args)
      {
        Phone p = new Phone(5);
        System.out.println(p); 
  // Here it's supposed to return the values of an array with size 5, 
  //so it should print out 00000
       }
     }

Next I have the class Phone:

   class Phone
   {
       private Phone[] arr; //I'm supposed to keep the array private
       public Phone(int theLength){
          arr = new Phone[theLength];
          return Arrays.toString(arr);
       }
   }

Now it looks a little ridiculous because I've become desperate to get at least SOMETHING out and I've exhausted any and all ideas.

What is supposed to happen is that the driver class gives Phone a number which will be used as theLength of the array (arr), and the array is initialized with that length (all integers of the array defaulted to 0) and returns as many 0's as the array is long (or if I were to assign different values at different locations of the array, it will go through and print every value in the order it is placed in the array).

2
  • You write you can't return the array cause it's private. Why not copy it and return a copy? Commented Oct 9, 2018 at 0:05
  • 1
    And a "getter" to your Phone class - public Phone[] getNumber() { return arr; } or something simular Commented Oct 9, 2018 at 0:05

1 Answer 1

1

Your constructor will not be returning anything

public Phone(int theLength){
      arr = new Phone[theLength];
}

But you will need a getter

public Phone[] getPhones () {
    return arr;
}

Usually I would have separate classes that hold the Phone array and the Phone Object itself only deals with the functionality of the actual phone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.