We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › class for appending a new object to an array
Page Index Toggle Pages: 1
class for appending a new object to an array (Read 720 times)
class for appending a new object to an array
Feb 4th, 2006, 10:01pm
 
I wrote a class to append a new object to an array with objects created from the class Ball.

So here comes my question: Is there a way to pass the name of the class(Ball) to the method app of the class App?
That would be cool because I wouldn't have to manually change the name of the class (in this case Ball) when I want to use the class App in other projects.

Pretty hard explaining, I hope someone can find out what I want ;-)

Here's the code:

class App {  
 Ball[] app(Ball[] b) {
   
   // assign l the length of the array
   int l = b.length;
   
   // save content of b temporarily to c
   Ball[] c = new Ball[l];
   for (int i = 0; i<l; i++) {
     c[i] = b[i];
   }
   
   // create the expanded array and add new object to end
   b = new Ball[l+1];
   b[l] = new Ball(23);
   
   // fill the rest of the array with old content
   for (int i = 0; i<l; i++) {
     b[i] = c[i];
   }
   
   return b;
 }
}
Re: class for appending a new object to an array
Reply #1 - Feb 5th, 2006, 9:38am
 
I'm not sure your experience level with object-oriented programming, but the very, very short answer to your question is yes, you would use polymorphism, which could be handled through inheritance or with Interfaces (you can also combine the two.) I'll briefly discuss the interface option, as I generally think it's a better way to go.  

classes and interfaces define types. So a class that implements an interface is of its class type as well as the interface type(every object in Java is also, at least, of type Object-through inheritance.) Types control pretty much everything in OOP, especially the connection of an instance to its properties/methods. When objects of different types, or classes, need to work together, as in your example, you need a way of passing object references. In your solution, you buried an object reference in another class. This works, but I think you see the problem with scaling that approach. As you add more objects, you need to add more references and do more checking in the constructor, perhaps using the instanceof operator-not a good solution. However, Java has a very cool feature called dynamic binding, which makes polymorphism possible providing a much better solution to your problem.

Dynamic binding allows the linking of an instance and its type to occur at runtime. In other words, an instance type can be left somwhat anonymous at compilation and, later at runtime, more specifically defined. To understand how this might help in your program, here's a Pizza example (sorry I'm hungry.)

Let's say you wanted to create a virtual Pizza, with 20 different toppings, each a separate class. Using your initial solution you'd have to bury references to each of the 20 toppings in the Pizza class. Then if you ever wanted add more toppings, you'd need to go back into the class...not good. A better solution is to create an interface, which I'll call ITastyToppings. To keep things simple, we'll just give the interface a single create() method. Interfaces don't implement their methods, but rather just include a method declaration, for example, here's our really simple  interface:

Code:

interface ITastyToppings {
void create();
}


Next we'd create our 20 toppings classes and each class would implement the ITastyToppings interface. By implementing the interface, each instance of a respective toppings class will also be of type ITastyToppings. Each class that implements the interface is required to implement the create() method. This will allow each class to implement the method in their own way. So Meatballs can be slow-cooked in sauce all day, while mushroooms can be sauteed, etc. The Pizza class will include reference variables of type ITastyToppings. When the Pizza is instantiated, the Toppings object references can be passed to the Pizza constructor and caught by parameters of type ITastyToppings, since each of the different Toppings instances are also of the common type ITastyToppings.

Here's the really cool part: when each of the ITastyToppings references calls the create() method, Java automatically figures out which respective version of the method to call, based on the specific Toppings class.

The solution is scalable now. You can go hogwild and add as many new toppings as you want, as long as each new toppings class implements the ITastyToppings interface.

It's now 3:30 am, and I think it's time to sleep.
Re: class for appending a new object to an array
Reply #2 - Feb 5th, 2006, 2:30pm
 
wow, thanks for the detailed answer. I'm hungry now...;-)

I tried to recreate the pizza-case from your explanations.
But there's one thing I don't understand. How can I pass the type of the Topping I want to be created by Pizza to the constructor of Pizza?



void setup() {
 size(400, 400);
 background(255,255,255);

 p = new Pizza();
}

Pizza p;

void draw() {

 p.show();

}

class Pizza {
 Toppings a;
 Pizza() {
   a = new Salami(); // How can I replace Salami with a value that has been passed to the Constructor of Pizza?
 }
 void show() {
   a.tell();
 }
}

class Salami implements Toppings{
 void tell() {
   println("I'm a Salami");
 }
}

class Mushroom implements Toppings{
 void tell() {
   println("I'm a Mushroom");
 }
}

class Onion implements Toppings{
 void tell() {
   println("I'm an Onion");
 }
}

interface Toppings {
 void tell();
}
Re: class for appending a new object to an array
Reply #3 - Feb 5th, 2006, 5:07pm
 
Wow, that was a fast (and delicious) implementation! Here are some fully prepared pizzas. Notice I added a Spinach class (a personal favorite) and multiple constructors.

Code:

void setup() {
size(400, 400);
background(255,255,255);
Pizza p1 = new Pizza(new Mushroom(), new Onion(), new
Salami());
p1.show();

Pizza p2 = new Pizza(new Mushroom(), new Onion());
p2.show();

Pizza p3 = new Pizza(new Spinach());
p3.show();

Toppings[]tops = {
new Mushroom(), new Onion(), new Salami(), new Spinach() };
Pizza p4 = new Pizza(tops);
p4.show();
}

class Pizza {
Toppings[]tops;

//multiple constructors
Pizza(Toppings t1, Toppings t2, Toppings t3) {
tops = new Toppings[] { t1, t2, t3 };
}

Pizza(Toppings t1, Toppings t2) {
tops = new Toppings[] { t1, t2 };
}

Pizza(Toppings t1) {
tops = new Toppings[] { t1 };
}

Pizza(Toppings[]tops) {
this.tops=tops;
}

void show() {
for (int i=0; i<tops.length; i++){
tops[i].tell();
}
println("\n");
}
}

class Salami implements Toppings{
void tell() {
println("I'm a Salami");
}
}

class Mushroom implements Toppings{
void tell() {
println("I'm a Mushroom");
}
}

class Onion implements Toppings{
void tell() {
println("I'm an Onion");
}
}

class Spinach implements Toppings{
void tell() {
println("I'm Spinach");
}
}

interface Toppings {
void tell();
}


Page Index Toggle Pages: 1