[HW] Bit a help with java and making many objects

Cheetoz

[H]ard|Gawd
Joined
Mar 3, 2003
Messages
1,972
I have to create a lot of objects in my test class, and I can't seem to find a way to do it. So far it would have to be done as so

Code:
Object a;
    private void creatorA()
        {
            switch (generator.nextInt(2) )
            {
                case 0: a = new TypeA();
                break;
                case 1: a = new TypeB();
                break;
                case 2: a = new TypeC();
                break;
            }
        }

Object b;
    private void creatorB()
        {
            switch (generator.nextInt(2) )
            {
                case 0: b = new TypeA();
                break;
                case 1: b = new TypeB();
                break;
                case 2: b = new TypeC();
                break;
            }
        }

but I would rather have the switch in a method with parameters, so I can do

Code:
    private void create(String name)
        {
            switch (generator.nextInt(2) )
            {
                case 0: Object name = new TypeA();
                break;
                case 1: Object name = new TypeB();
                break;
                case 2: Object name = new TypeC();
                break;
            }
        }

and just use create(String name) for each object.

Any hints in the right direction?
 
Umm, lets see. There is a parent class Cars, and it has three child classes Toyota, Ford, and Chevy. I am using the switch and nextInt() so I can create 15 random cars of these 3 different types of objects.

The issue being that I am copying & pasting that switch loop for each 15 cars.

I tried this, but get an error that cars are not initializied.

Code:
        static void create(Car name, String id)
        {
            int number = generator.nextInt(3);
            
            if(number==0)
                    name = new Toyota(id, (double)generator.nextInt(150000)+25000);
            else if ( number==1 )
                    name = new Ford(id, (double)generator.nextInt(30)+7);
            else
                    name = new Chevy(id, (double)generator.nextInt(30)+7);
        }

    public static void main(String[] arg)
    {
        Car car1; 
create(car1, "Jake"); 
        Car car2; 
create(car2, "Joe");
        Car car3; 
create(car3, "Sally");
     }


** variable car1 might not have been initialized

doing Car car1=null; doesnt work, as it stays null. so the create method isn't creating anything
 
You can't assign a new object to a parameter in a method because object references are pass-by-value in Java. You should be creating the object in the method and returning it as a Car.

Code:
 static Car create( String id)
        {
            int number = generator.nextInt(3);
            Car name;

            if(number==0){
                    name = new Toyota(id, (double)generator.nextInt(150000)+25000);
            }
            else if ( number==1 ){
                    name = new Ford(id, (double)generator.nextInt(30)+7);
            }
            else{
                    name = new Chevy(id, (double)generator.nextInt(30)+7);
            }
            return name;
        }


Looks like you are trying to make use of the Factory pattern, I suggest you read up on it.

On an unrelated note, its good form to always brace your blocks even if the braces would be implied.
 
Back
Top