Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Disclaimer: I'm a beginner so feel free to point stuff out...

I have a superclass composed by an array of int with 8 values, now i want to create a subclass to randomly pick 4 items in the array and store them in another Object.

Superclass:

public class SideDeck{
    public static final int MaxValue = 6;
    public static final int MinValue = -6;
    public static final int MaxArrayValue = 8;
    public final int[] sidecards = new int[MaxArrayValue];


    public SideDeck(){
        for(int i=0;i<MaxArrayValue;i++){
            sidecards[i]=0;
        }
    }

public SideDeck(int sidecards1,int sidecards2,int sidecards3,int sidecards4,int sidecards5,int sidecards6, int sidecards7, int sidecards8){
    sidecards[0]=sidecards1;
    sidecards[1]=sidecards2;
    sidecards[2]=sidecards3;
    sidecards[3]=sidecards4;
    sidecards[4]=sidecards5;
    sidecards[5]=sidecards6;
    sidecards[6]=sidecards7;
    sidecards[7]=sidecards8;
    }

    public boolean ValidSidedeck(){
        int check=0;
        if (sidecards[0]!=0) {
            for(int i=0;i<MaxArrayValue;i++){
                if ((sidecards[i] > MinValue) && (sidecards[i] < MaxValue)){
                    check=1;
                } else{
                    check=0;
                    break;
                }
            }
        } else {
            check=0;
        }

        if (check==1){
            return true;
        } else {
            return false;
        }
    }

    public String toString(){
        String s="";
        for(int i=0;i<MaxArrayValue;i++){
                s+=(" || Card n° " + (i+1) + " = " + sidecards[i]);
                }
        return s;
    }


    public void ResetSidedeck(){
        if (sidecards[0]!=0) {//why check it? what if we just run it?
            for(int i=0;i<MaxArrayValue;i++){
                sidecards[i]=0;
            }
        }

}
}

Subclass: (Not really sure what to do here… ) Basically it should pick 4 random positions from the .super and store them here, just that i have no clue how to create the object this way. And passing the super as constructor doesn't seem right since it's gonna pass the Object and not the array(and i don't need the full array anyway). Main thing is that i wanna keep the superclss like that, maybe just adding a method there so extract the 4 values..and passing them as arguments…?

import java.lang.Math;

public final class PlayableSideDeck extends SideDeck{
    private final static int MaxCArrayValue=4;
    public final int[] sidecardsPlay = new int[MaxCArrayValue];
    public PlayableSideDeck(SideDeck sidecards){
/*      sidecardsPlay[0]=0;
        sidecardsPlay[1]=0;
        sidecardsPlay[2]=0;
        sidecardsPlay[3]=0;*/
    //  SetDeck();//<-Can i call a private method in the constructor
    }




    public void SetDeck(){
/*          for(int j=0;j<4;j++){
                int position=(super.sidecards[PickDeck()]);//<--this is the main problem.. since it's gonna call the object i guess. 
                sidecards[j]=position;
                System.out.println(/*"i= " + i + *//* " ||| j= " + j + "|||| new sidecard= " + sidecards[j] + " |||| old sidecard=" + super.sidecards[PickDeck()]);
            }*/
                for(int j=0;j<MaxCArrayValue;j++){
                sidecardsPlay[j]=(super.sidecards[PickDeck()]);
                System.out.println(/*"i= " + i + */ " ||| j= " + j + "|||| new sidecard= " + sidecardsPlay[j] + " |||| old sidecard=" + super.sidecards[PickDeck()] + "|| random= " + PickDeck());
            }

    }

    public int PickDeck(){
        return ((int)(Math.random() * 8));
    }

    public String toString(){
    String s="";
    for(int i=0;i<MaxCArrayValue;i++){
            s+=(" || Card n° " + (i+1) + " = " + sidecards[i]);
            }
    return s;
    }
}

Thanks.

share|improve this question

closed as unclear what you're asking by Brian Roach, Dukeling, Raedwald, Padma Kumar, Alex Apr 4 at 14:02

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Convert it to a list using Arrays.asList() and now you'll be able to use the entirety of List including the Collections.shuffle method. –  hd1 Feb 16 at 0:36

1 Answer 1

up vote 0 down vote accepted

I'm not sure how you plan to use PlayableSideDeck, so I'll answer answer this two ways and you can pick the most fitting answer.

First, as the book Effective Java (by Josh Bloch) points out, you should favor composition over inheritance. By using composition you have your answer to the question of whether you should pass an instance of SideDeck to the constructor of PlayableSideDeck - you will have to since you won't be inheriting any access to SideDeck. Anyway, I'd recommend reading Item 16 in the book (google for it, there are copies available online) and see if composition doesn't better fit your needs.

Second, if you decide to go with inheritance, you don't need to pass an instance of SideDeck to the constructor of PlayableSideDeck. This is because when you create an instance of PlayableSideDeck you are automatically creating an instance of SideDeck along with it. All constructors in Java will implicitly call super() (which is the superclasses's default constructor) if you don't explicitly provide another such call yourself. For example, you could prevent the implicit call to super() like so:

public class BaseClass {
    protected String strValue;

    public BaseClass () {
        strValue = "";
    }

    public BaseClass (String str) {
        strValue = str;
    }
}

public class SubClass extends BaseClass {
    private int intValue;

    SubClass (String str, int i) {
        super (str);
        intValue = i;

        // note that since strValue is protected, SubClass can access directly
        System.out.println ("strValue = " + strValue);
    }
}

In this example, if you call new SubClass ("foobar") then you will see strValue = foobar printed on the console.

If BaseClass didn't have a zero argument constructor you would, in fact, be required to call super(str) since the compiler wouldn't be able to figure out how to do it for you.

Also, since you asked, here are a few other tips and pointers:

In the constructor SideDeck() you explicitly initialize all values of the array to 0, which isn't necessary. They will already all be 0. If you needed to init them to 0 then you'd be better off avoiding code duplication by calling ResetSideDeck. Speaking of which, you can shorten that code to Arrays.fill (sidecards, 0); (be sure to import java.util.Arrays).

Yes, you can call private methods from a constructor - but only private methods that are part of the local class, not any of the superclasses (you can, however, call protected methods of superclasses).

You're right about not checking sidecards[0] == 0 since there's little efficiency to be gained unless MaxArrayValue becomes very large.

Your class member variables such as sidecards should be private (or maybe protected if you need to access them from a subclass). Use getter/setter methods to access them.

Lastly, Java naming conventions would tell you to use a lower-case letter for method names (e.g. setDeck, pickDeck, resetDeck, etc.), and for even more idiomatic Java you could rename ValidaDeck to isValidDeck (since it returns a boolean). For the constants such as MaxArrayValue the convention is to use all upper-case with underscores between words, e.g. MAX_ARRAY_VALUE.

Hope this all helps!

share|improve this answer
    
Thanks both for the tips and the answer, i'll read that passage of the book and try to implement your suggestions. –  valentino Feb 16 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.