design question & using arrays

Discussion in 'Java' started by gerrymcc@indigo.ie, Aug 1, 2003.

  1. Guest

    I'm trying to write a program that allows students to practice written
    work for their music exams. I want to create an Answer class that will
    contain a sequence of bars (Measure [] bar). Each Measure contains two
    NoteSpecies2 objects, except for the last, which contains a single
    NoteSpecies2 object. I've tried the following design unsuccessfully:

    class Answer extends Panel{
    public Measure [] bar;

    public Answer(Applet a, int [] part){
    bar = new Measure[ part.length];
    for( int i = 0; i<part.length; i++){
    if( i==0)
    bar = new EditableMeasureType2( a, 1); // ie first bar
    else if( i==part.length-1)
    bar = new EditableMeasureType1( a, 2); // last bar
    else
    bar = new EditableMeasureType2( a); // other bars
    add( bar);
    }
    }
    }

    class Measure extends Canvas{
    public NoteSpecies2 aNote = null;
    . ...
    }

    class EditableMeasureType1 extends Measure{
    public NoteSpecies2 [] aNote = new NoteSpecies2 [1];
    . ...
    }

    class EditableMeasureType2 extends Measure{
    public NoteSpecies2 [] aNote = new NoteSpecies2[2];
    . ...
    }

    Answer studentWork;

    for( int i=0; i<studentWork.bar.length; i++)
    for( int j=0; j<2; j++){
    if( studentWork.bar.aNote[j] == null) ****PROBLEM LINE****
    System.out.println( "all bars must be complete");

    I can see that the loop above will try to access an incorrect index,
    that is, aNote[1] doesn't exist for EditableMeasureType1. However, I
    also get the following compilation error:
    array required but NoteSpecies2 found.

    Is there a way that I can populate the bar[] array with the two
    EditableMeasureTypeX subclasses of Measure, and iterate through the
    array? I was wondering if I'd be better off using Vector or ArrayList
    for the job; though an ordinary array seems easier to use (if it's
    possible to use it at all).

    Thanks for any help,
    Gerard
     
    , Aug 1, 2003
    #1
    1. Advertising

  2. Neomorph Guest

    On Fri, 01 Aug 2003 12:37:35 GMT, two-finger typed:

    >I'm trying to write a program that allows students to practice written
    >work for their music exams. I want to create an Answer class that will
    >contain a sequence of bars (Measure [] bar). Each Measure contains two
    >NoteSpecies2 objects, except for the last, which contains a single
    >NoteSpecies2 object. I've tried the following design unsuccessfully:
    >
    >class Answer extends Panel{
    > public Measure [] bar;
    >
    > public Answer(Applet a, int [] part){
    > bar = new Measure[ part.length];
    > for( int i = 0; i<part.length; i++){
    > if( i==0)
    > bar = new EditableMeasureType2( a, 1); // ie first bar
    > else if( i==part.length-1)
    > bar = new EditableMeasureType1( a, 2); // last bar
    > else
    > bar = new EditableMeasureType2( a); // other bars
    > add( bar);
    > }
    > }
    >}
    >
    >class Measure extends Canvas{
    > public NoteSpecies2 aNote = null;
    > ...
    >}
    >
    >class EditableMeasureType1 extends Measure{
    > public NoteSpecies2 [] aNote = new NoteSpecies2 [1];


    You are now overriding a variable that was a different type.
    I don't think that works.

    Change the Measure class to have:
    public NoteSpecies2 [ ] aNote = new NoteSpecies2 [0];

    That fixed your boundary problem below as well...
    > ...
    >}
    >
    >class EditableMeasureType2 extends Measure{
    > public NoteSpecies2 [] aNote = new NoteSpecies2[2];
    > ...
    >}
    >
    >Answer studentWork;
    >
    >for( int i=0; i<studentWork.bar.length; i++)
    > for( int j=0; j<2; j++){


    Change this to:
    for(int j=0;j<studentWork.bar.aNote.length;j++){

    > if( studentWork.bar.aNote[j] == null) ****PROBLEM LINE****
    > System.out.println( "all bars must be complete");
    >
    >I can see that the loop above will try to access an incorrect index,
    >that is, aNote[1] doesn't exist for EditableMeasureType1. However, I
    >also get the following compilation error:
    > array required but NoteSpecies2 found.
    >
    >Is there a way that I can populate the bar[] array with the two
    >EditableMeasureTypeX subclasses of Measure, and iterate through the
    >array? I was wondering if I'd be better off using Vector or ArrayList
    >for the job; though an ordinary array seems easier to use (if it's
    >possible to use it at all).
    >
    >Thanks for any help,
    >Gerard


    Cheers.
     
    Neomorph, Aug 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Treseler
    Replies:
    3
    Views:
    605
    Mike Treseler
    Dec 19, 2005
  2. Alexandra Stehman
    Replies:
    5
    Views:
    30,893
    Chris Smith
    Jun 17, 2004
  3. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
  4. shashi
    Replies:
    17
    Views:
    472
    Guest
    Apr 13, 2006
  5. Replies:
    11
    Views:
    744
    Seungbeom Kim
    Sep 19, 2012
Loading...

Share This Page