473,699 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design question & using arrays

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[i] = new EditableMeasure Type2( a, 1); // ie first bar
else if( i==part.length-1)
bar[i] = new EditableMeasure Type1( a, 2); // last bar
else
bar[i] = new EditableMeasure Type2( a); // other bars
add( bar[i]);
}
}
}

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

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

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

Answer studentWork;

for( int i=0; i<studentWork.b ar.length; i++)
for( int j=0; j<2; j++){
if( studentWork.bar[i].aNote[j] == null) ****PROBLEM LINE****
System.out.prin tln( "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 EditableMeasure Type1. 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
EditableMeasure TypeX 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

Jul 17 '05 #1
1 1755
On Fri, 01 Aug 2003 12:37:35 GMT, ge******@indigo .ie 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[i] = new EditableMeasure Type2( a, 1); // ie first bar
else if( i==part.length-1)
bar[i] = new EditableMeasure Type1( a, 2); // last bar
else
bar[i] = new EditableMeasure Type2( a); // other bars
add( bar[i]);
}
}
}

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

class EditableMeasure Type1 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 EditableMeasure Type2 extends Measure{
public NoteSpecies2 [] aNote = new NoteSpecies2[2];
...
}

Answer studentWork;

for( int i=0; i<studentWork.b ar.length; i++)
for( int j=0; j<2; j++){
Change this to:
for(int j=0;j<studentWo rk.bar[i].aNote.length;j ++){
if( studentWork.bar[i].aNote[j] == null) ****PROBLEM LINE****
System.out.prin tln( "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 EditableMeasure Type1. 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
EditableMeasur eTypeX 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.
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
6852
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
2
2029
by: Mark Hannon | last post by:
I am trying to wrap my brain around storing form elements inside variables & arrays before I move on to a more complicated project. I created this simple example to experiment and as far as I can tell, it should work but it doesn't. Can someone tell me where I went wrong? <html><head> <title>Form Test</title> <script language="JavaScript> <!-- function copy(){
5
3974
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
16
1676
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
4
7292
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
2
7052
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
0
2506
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
8
4758
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
0
1292
by: David Thompson | last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com> wrote: I think you've got the idea, but: - I would be careful about using 'equal'. Pointers and arrays are different things, but they only way in C to determine equality of two things is an expression using the == operator, and _in an expression_ an array turns into a pointer which when compared to another pointer can indeed be equal. Since your sig refers to...
0
8706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8630
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7786
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.