Array to subclass

JAVA

    Sponsored Links

    Next

  • 1. Java Editor with Auto-complete.
    I use GNU/Linux. I had programmed in Java writing source code in Vim. But the huge number of classes and methods is really daunting, and auto-complete feature really comes in handy. I learned about the IDE's like Netbeans and Eclipse, but they are too much ``feature-rich'' for my use. Moreover, they ask me to write in the code they want, not the way I want it. I found that for Windows there is an editor which is JBuilder, which auto-indents, auto-completes and highlights too. Is there any such editor available for GNU/Linux?
  • 2. very simple question about naming objects
    I have a class called Tenant and I loop over the creation of 10 of these objects. But I want to name them Tenant1 Tenant2 Tenant3 etc How do I do that? In the code here they are all being named tenant rather than tenant1 etc: for (int i=0; i<10; i++) { Tenant tenant = new Tenant(i); } I thought maybe converting the int to a string like Integer number = i; String numberstring = number.toString(); But how would I then add numberstring onto the name of the object?

Array to subclass

Postby Mike » Sun, 30 Jul 2006 13:07:11 GMT

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


Re: Array to subclass

Postby Bart Cremers » Sun, 30 Jul 2006 16:19:56 GMT




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


Re: Array to subclass

Postby Greg R. Broderick » Sun, 30 Jul 2006 23:15:22 GMT


@i3g2000cwc.googlegroups.com:


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             XXXX@XXXXX.COM 

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

Re: Array to subclass

Postby Hendrik Maryns » Tue, 01 Aug 2006 20:00:22 GMT

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

Mike schreef:

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

H.

- --
Hendrik Maryns

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

iD8DBQFEzeLGe+7xMGD3itQRAl5DAJ0amxXJ+ltjb2BssLuxPrs/1SKgYQCaA+z/
CoFwyyiR5y4SHjoHBLeRyxE=
=YPOJ
-----END PGP SIGNATURE-----

Similar Threads:

1.subclass has a variable that is subclass of same superclass

I have been running this scenario in my head for a while.  In the
surface it seems fine, but I keep thinking that somehow it will cause
problems.


class A {
}

class B extends A {
}

class C extends A {
    B myB;
}

Class B is a A

Class C is also a A but also has a B which is in turn a A.


Is there anything wrong with having a variable in a class of a type
that inherits from the same parent that the class itself inherits from?


Thanks.

2.Multidimensional arrays and arrays of arrays

Hello
How can I distinguish if an array is multidimensional of if it just
contains an array? Or put it another way how can I tell at runtime
whether a/b in the example below is an array containing an array, or
if it is a multidimensional array.
If I know beforehand exactly which type it is, I can use instanceof
(see example), but I don't (especially the number of dimensions, I
don't know).
Else I can call getClass().getName() on the object and see if it
starts with "[[" (pretty ugly IMHO).
Is there another, cleaner method to do this?

public static void main(String[] args) {
  Object[] a = new Object[1];
  a[0] = new float[12];

  Object b = new float[1][12];

  if(a instanceof float[][]){
    System.out.println("a is float[][]");
  }
  if(b instanceof float[][]){
    System.out.println("b is float[][]");
  }
}

prints "b is float[][]"  (but not for a) as expected.

Phil

3.If a superclass is static and its subclass isn't, will the subclass be static?

If a superclass is static and its subclass isn't, will the subclass be 
static?

public static class A {
}

public class A1 extends A {
}

4.Subclassing, Inheritance, RE subclass "constructor" and inherited members

// Referring to JavaScript the Definitive Guide: 9.5 through 9.5.2

    // the base and sub classes are perfect. the following code is an
    // example case, regarding WHY delete base members from a subclass
    // AND what is wrong with the loops ONLY returning -> prototype :/

// Ex 01:
print(subInstance.hasOwnProperty("baseMember"));            // false
  for (var p in SubClass){
    print(p);
  }                                                         //
prototype


// Ex 02:
delete SubClass.prototype.baseMember;  // Don't need to customize,
delete?
  print(subInstance.hasOwnProperty("baseMember"));          // false
    for (var p in SubClass){
      print(p);
    }                                                       //
prototype

// Why delete the members in subclass if I inherit them from
baseclass? is it
// the same reason you might want to put constants & methods in the
prototype?
// Is it to avoid duplication and memory hogging? e.g., I don't
actually need
// to override the base members so should I really delete them from
subclass?

// Why iterating over the properties in the subclass yield ONLY ->
prototype?
// shouldn't I at least get all properties back defined in ALL classes
(Ex 01)
// AND after deleting those inherited from base on sub, shouldn't I at
least
// get those properties of sub (Ex 02)? Both Examples, have several
members!

5.how subclass native class?

How can I subclass a native class to get my own class with all the
function of the native clas, plus correct instanceof behaviour?

The scenario I'm thinking of is something like

function MyArray() {
  Array.apply( this, arguments );
}
MyArray.prototype = new Array();
MyArray.prototype.constructor = MyArray;

var marr = new MyArray();
marr[0] = "abc";
marr.length == 1; // true
marr instanceof MyArray; // true

But this seems impossible due to the Array constructor returning a new
object even when called with my own "this" in the apply() above. Can
this be solved in any way?

Best regards
Mike

6. trouble passing parameters of a subclass constructor through to it's superclass constructor

7. Date subclass

8. Cast Java Superclass into Subclass in Matlab



Return to JAVA

 

Who is online

Users browsing this forum: No registered users and 66 guest