G
gerrymcc
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
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