design question & using arrays

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
 
N

Neomorph

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top