For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2006 > Array to subclass









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Array to subclass
Mike

2006-07-29, 4:01 am

I am new to this and i don't understand how to transfer my arrays to a
subclass, alter the data and then send it back to the superclass. I
don't want to jump right in, I do some manipulating on the arrays in
the superclass using several methods. I want to pick the point in the
supercalss whent it sends to the subclass and I would like the altyered
data to drop back to the superclass right after the call to subclass.

My superclass: Inventory is passing data to my subclass: ProductXtras.

My supercalss has the following arrays which U want to send to the
subclass and retrieve them after I make changes to them in the
subclass.

final int Array_Length = 50;
// Constant array size
String DVDID[] = new String[Array_Length]; // DVD
identification code
String DVDScreen[] = new String[Array_Length]; // Screen
format 4:3 or 16:9
String DVDName[] = new String[Array_Length]; // DVD title
Double DVDQty[] = new Double[Array_Length]; // Number of
DVDs per title
Double DVDEach[] = new Double[Array_Length]; // Price of each
DVD
Double DVDRestock[] = new Double[Array_Length]; // Restock Price
of each DVD
Double DVDTotal[] = new Double[Array_Length]; // Total value
of DVDs of 1 title
int index;
// Number of array elements used

Bart Cremers

2006-07-29, 4:01 am


Mike wrote:
> I am new to this and i don't understand how to transfer my arrays to a
> subclass, alter the data and then send it back to the superclass. I
> don't want to jump right in, I do some manipulating on the arrays in
> the superclass using several methods. I want to pick the point in the
> supercalss whent it sends to the subclass and I would like the altyered
> data to drop back to the superclass right after the call to subclass.
>


I'm not sure how the classes are structured, but it's easy to do this
if your superclass is abstract.

public abstract class Super {

public void process(Object[] array) {
// Do a lot of work on the array here
subProcess(array);
// Do even more processing here
}

protected abstract void subProcess(Object[] array);
}

public class Sub extends Super {
protected abstract void subProcess(Object[] array) {
// Do sub processing here
}
}

The abstract makes sure the Sub class has to implement a certain method
for it to compile. You can easily create different subclasses which
perform different processing:

public class OtherSub extends Super {
protected abstract void subProcess(Object[] array) {
// Do other sub processing here
}
}

public class Tester {
public static void main(String[] args) {
String[] myArray = new String[] { "a", "b", "c"};
Super one = new Sub();
Super two = new OtherSub();

one.process(myArray.clone());
two.process(myArray.clone());
}
}

I hope the examples show the power of using an abstract superclass.

Regards,

Bart

Greg R. Broderick

2006-07-29, 7:00 pm

"Mike" <mparham1@austin.rr.com> wrote in news:1154146031.584543.305470
@i3g2000cwc.googlegroups.com:

> final int Array_Length = 50;
> // Constant array size
> String DVDID[] = new String[Array_Length]; // DVD
> identification code
> String DVDScreen[] = new String[Array_Length]; // Screen
> format 4:3 or 16:9
> String DVDName[] = new String[Array_Length]; // DVD title
> Double DVDQty[] = new Double[Array_Length]; // Number of
> DVDs per title
> Double DVDEach[] = new Double[Array_Length]; // Price of each
> DVD
> Double DVDRestock[] = new Double[Array_Length]; // Restock Price
> of each DVD
> Double DVDTotal[] = new Double[Array_Length]; // Total value
> of DVDs of 1 title
> int index;
> // Number of array elements used
>


How about turning each of these arrays into an object, e.g.

public class DVD
{
private string name;
private long quantity;
private BigDecimal price;
private BigDecimal restock;

// add getter and setter methods as appropriate
}

then you could have a single array (or better, use a List - a list can grow
or shrink) of these objects to pass back and forth.

DVD dvds[] = new DVD[ARRAY_LENGTH];

or

List dvds = new List();


Cheers
GRB

--
---------------------------------------------------------------------
Greg R. Broderick gregb.usenet200606@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
Hendrik Maryns

2006-07-31, 8:01 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike schreef:
> I am new to this and i don't understand how to transfer my arrays to a
> subclass, alter the data and then send it back to the superclass. I
> don't want to jump right in, I do some manipulating on the arrays in
> the superclass using several methods. I want to pick the point in the
> supercalss whent it sends to the subclass and I would like the altyered
> data to drop back to the superclass right after the call to subclass.
>
> My superclass: Inventory is passing data to my subclass: ProductXtras.


Is ProductXtras a special case of Inventory? I don’t think so. Then do
not make it inherit from it. Rather pass the arrays explicitly to an
instance of it.

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEzeLGe+7xMGD3itQRAl5DAJ0amxXJ+ltj
b2BssLuxPrs/1SKgYQCaA+z/
CoFwyyiR5y4SHjoHBLeRyxE=
=YPOJ
-----END PGP SIGNATURE-----
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com