Hi everyone,

I'm trying to do exercise from the book I'm reading at the moment. But, I don't seem to have understood the last part of the question. I'd greatly appreciate anyone explaining the question to me. I have done all parts of the question except the last one which I am not really sure of. The bold lines that I don't really understand.

I don't want any answers please. I'd like to solve the problem myself after having a good understanding of what I should be doing and someone explains the part that I don't understand.

Here is the question:

Exercise 8: (4) Following the form of the example Lunch.java, create a class called ConnectionManager that manages a fixed array of Connection objects. The client programmer must not be able to explicitly create Connection objects, but can only get them via a static method in ConnectionManager. When the ConnectionManager runs out of objects, it returns a null reference. Test the classes in main( ).

// Class called Soup1

class Soup1 {
	
		private Soup1(){  }
		
		public static Soup1 makeSoup(){
			return new Soup1();
		}
		
}


// Class called Soup2

class Soup2 {
	
	private Soup2(){	}
	
	private static Soup2 ps1 = new Soup2();
	
	public static Soup2 access(){
		return ps1;
	}
	
	public void f(){	}

}



// Class called lunch
public class lunch {

	void testStatic(){
		Soup1 soup = Soup1.makeSoup();
	}
	void testSingleton(){
		Soup2.access().f();
	}
}



// Class called ConnectionManager
class ConnectionManager {
	
	Object[] connecter = new Object[5];
	
	private ConnectionManager(){
		
	}
	
	static ConnectionManager connecter(){
		return new ConnectionManager();
	}
	
}

Thanks,

Recommended Answers

All 13 Replies

keep a static int, in which you keep your index.
if that int < the length of the array
array[yourInt] = new Connection(); // NOT connectionManager, according to your assignment
and then return array[yourInt];
if ( int >= length of the array) return null

// Class called ConnectionManager
class ConnectionManager {
 
	Object[] connecter = new Object[5]; //make it a Connection array (create a Collection class that user can't instanciate.
 
	private ConnectionManager(){
               
	}
 
	static ConnectionManager connecter(){//dont call the method the same name your variable is called, just bad practice
		return new ConnectionManager();//not what you should be returning, at all
	}
 
}

Here the assignement didnt tell you the manage had an array of objects , it said an array of Connection objects, which the user cannot directly instanciate.

like stultuske said : create a static int , then in your static method that hands out connections , return Collections from your array , at the position of that static int, and increase it everytime.

before handing out the Connection at connecter[iStaticInt], verify that iStaticInt is < than connecter.length, otherwise return null.

I ain't sure how to create connection objects from Array. The below code is working for me.

Fields as follows:

	static Object[] connecter = new Object[5];
	static int i=0;


static ConnectionManager connecter(){
		if(ConnectionManager.i <= connecter.length){
			return new ConnectionManager();
		}else if(ConnectionManager.i > connecter.length){
			return null;
		}
		return null;
	}

@Philippe.... Is that what you're trying to tell me in your post?

The fields should look like these? 


static ConnectionManager[] connecter = new ConnectionManager[5];
static int i=0;

And then, I loop through the connection objs in the array inside my method?

you don't create them from the array:
you instantiate them and store them in the array.

@Philippe.... Is that what you're trying to tell me in your post?

The fields should look like these? 


static ConnectionManager[] connecter = new ConnectionManager[5];
static int i=0;

And then, I loop through the connection objs in the array inside my method?

No, What im saying is , it should not be an array of "Objects" nor an array of "ConnectionManager". Your assignement states that the ConnectionManager class has an array of "Collection". So you will need to create two classes, Connection and ConnectionManager.

The Connection class isnt that important as it doesnt need to be functional.
Since the user should not be able to create a Connection on his own tho , i'm guessing it should be private or protected, not entirely sure how you will manage to hide the constructor ( or not have a public constructor which i'm not sure is allowed to be honest)

private class Connection{
    private int iNotImportantConnectionNumber;

     public Connection(int num){
          iNotImportantConnectionNumber = num;
     }

     public int getConnectionNumber(){
         return iNotImportantConnectionNumber;
     }
}

now the important class is ConnectionManager, it's the one that needs to be functional.
It needs to create 2 variables, an array of Connections, and a static int Count.
Lets make it more dynamic and set the constructor to reset the size of the array.

public class ConnectionManager{
     private Static int iCount;
     private Connection[] myPrivateConnections;

     //constructor
     public ConnectionManager( int size ){
        myPrivateConnections = new Connection[size]
        for(int i = 0 ; i < myPrivateConnections.length ; i++){
              myPrivateConnections[i] = new Collection(i);
        }
     }

     //static method to look at the connection count
     public static int getNbOpenConnections(){
          return iCount;
     }

     //non-static method to get a connection if any are available
     public Connection getNewConnection(){
           if(iCount < myPrivateConnections.length){
                //give an available connection
                return myPrivateConnections[iCount++];
                // ++ set AFTER the variable means it will increase it 
                // by 1 AFTER the current expression
            }
            //if you get here then you skipped the if and theres no
            // available connections so just return null
            return null;
     }
}

I hope there is no offense. The solution to the exercise that you posted DOES NOT make sense really. If you're using a class which you don't really need, Why are you using in the first place? Well, if you didn't have the Connection class, you'd have to make an array of ConnectionManager objects like I previously did.

And the variable iCount has the deafult value which is ZERO. Therefore, your if-statement will evaluate to true all the time, won't it? and also, I can see any calls to the method getNewConnection in your constructor in order for it to change the value of iCount. Please do correct me if I'm mistaken.

public Connection getNewConnection(){
if(iCount < myPrivateConnections.length){  [B][U] <-----<   Here. iCount == 0 [/U][/B]
//give an available connection
return myPrivateConnections[iCount++];

}

return null;
}

I know the solution I came up with before is not dynamic. But, Is there anything wrong with it?

If you're using a class which you don't really need, Why are you using in the first place?

because the assignment says he has to :)

That unfortunately doesn't answer my question in the second last post.

Yeah, I agree with you that the question does ask me to have two classes at least to test the concept of inheritance and making a call to a private constructor through a method. And, that's what I initially had. I had two classes; one of them was ConnectionManager. I made a call to the ConnectionManager in the main method in another class.

well ... I'm currently on a long weekend with my family, but I'll take a closer look when I get back home.

Have great fun with them.

PS: Sorry. That was not about the concept of inheritance; it was more about the concept of composition.

:)

assignement says :

create a class called ConnectionManager that manages a fixed array of Connection objects. The client programmer must not be able to explicitly create Connection objects, but can only get them via a static method in ConnectionManager. When the ConnectionManager runs out of objects, it returns a null reference. Test the classes in main( ).


So you need to understand theres a difference between Connection and ConnectionManager, ConnectionManager is just the class that distributes Connections, it is NOT a Connection , does not have info about a Connection details etc. You could not connect with a ConnectionManager, you can connect with a Connection object. (here the assignement is not to create a working Connection object, thus why i said that the class was "useless" as in "it does not have to work")


As for the

if(iCount<myPrivateConnections.length){...}

always evaluating at true,
you missed the inside code of the if :

return myPrivateConnections[iCount++];

now :

int iMyNumber = 0;

//the 4 next lines do the same result in a diferent way : add 1 to iMyNumber

iMyNumber = iMyNumber + 1;

iMyNumber += 1;

iMyNumber++;

++iMyNumber;

//the thing about the 2 last is they return a diferent value when used in an expression
//to explain this let me demonstrate :

iMyNumber = 5;

System.out.println("the value is : " + iMyNumber++ ); // returns 5
System.out.println("the value is : " + iMyNumber ); // returns 6


iMyNumber = 5;

System.out.println("the value is : " + ++iMyNumber ); // returns 6
System.out.println("the value is : " + iMyNumber ); // returns 6


//you see ++variable increases BEFORE evaluating the expression while variable++ increases it AFTER.

so in the line

return myPrivateConnections[iCount++];

the static method returns the Connection Object, in the array myPrivateConnections, at Position iCount, then increases iCount by 1 , all this on the same line.

it does the same work as :

public static Connection getNewConnection(){
   if(iCount < myPrivateConnections.length){
       //give an available connection
       Connection myTemporaryConnection = myPrivateConnections[iCount];
       iCount = iCount + 1;
       return myTemporaryConnection;
    }
    return null;
}

please note i added static to that method's signature, after revising the assignement i noticed it says ConnectionManager has a "static method" to dispense connections , this means the method will need to be static on top of iCount, but the Connection array also else we won't be able to acess it from the static method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.