Hi, I read a few post on passing arrays from one class to another but i can't understand. I tried doing it and i'm stuck with it, would really appreaciate it if you can help correct the following code:

//This is my super class
public class Transport
{
    protected String type;
    protected int wheels;
    protected double engine;
    
   Transport(String type, int wheels, double engine)
   {
       setTransport(type, wheels, engine);
    }
    
   void setTransport(String type, int wheels, double engine)
   {
       this.type = type;
       this.wheels = wheels;
       this.engine = engine;
    }
    
   String setType()
   {  return type;  }
   
   int setWheels()
   {  return wheels; }
   
   double setEngine()
   {  return engine;  }
   
   public String toString()
   {
       return "Type: " +setType()+ "\nNumber of wheels: " +setWheels()+ "\nEngine cc: " +setEngine();
    }
}


//This is my subclass
public class Bus extends Transport
{
    Bus(String type, int wheels, double engine)
    {
        super(type, wheels, engine);
    }
//I try re-creating the array in the subclass to sort it but i have no idea what i'm doing actually, i try receiving the array list from main but to no avail. Please help  
    public void Sorting()
    {
        Bus metro[]=new Bus[3];
        for (int i=0;i<3;i++)
         metro[i] = new Bus(type,wheels,engine);
     for (int i=0;i<2;i++)
        for (int j=1+i;j<3;j++)
        {
            if (metro[i].setWheels()>metro[j].setWheels()){
                Bus temp = metro[i];
                metro[i] = metro[j];
                metro[j] =temp;
        }  }
    }
}

//This is my client
import java.util.Scanner;
public class Client
{
   public static void main(String argsp[])
   {
       int i;
       Scanner input = new Scanner(System.in);
        Bus metro[] = new Bus[3];
       
        for(i=0;i<3;i++)
        {
            String type = input.nextLine();
            int wheels = input.nextInt();
            double engine = input.nextDouble();
            input.nextLine();
            metro[i] = new Bus(type,wheels,engine);
        }
       //Calling the sorting method from subclass
       for ( i=0;i<3;i++)
       {
           metro[i].Sorting();
        }
        
       for (i=0;i<3;i++) 
       System.out.println(metro[i]);
    }
   
}

Note: Basically i want to know how to pass array from one class to another for other operations eg. sorting

Recommended Answers

All 5 Replies

You can pass an array as a parameter, ie declare the method as
public void Sort(Transport[] arrayOfTransportsToBeSorted)
then in the method sort that array.
and call it like this
Sort(metro);

Thank you for your reply but I have a few questions,

public void Sort(Transport[] arrayOfTransportsToBeSorted)
The line above is written in the subclass is it? so it'll be like:

public void Sorting(metro[])    
{
.........

Sort(metro);
The code above is written in the client is it?
Sorry but I still don't really get what you mean.If you have the time, would appreaciate it if you can correct my code instead; easier for me to understand. thank you.

Yes, that's a replacement for public void Sorting().
I've changed the names so you have to read and understand it, you can't just copy/paste. (That's to help you learn better.)
The important thing is that I've added the parameter in brackets - ie it wants a parameter that's an array of Transports.
In you code that you posted lines 46-48 shouldn't be there - they try to create a new array, but now the array is being passed in as a parameter.
In lines 52-55 instead of "metro" you should have the name that you used for the parameter (arrayOfTransportsToBeSorted in my example).
Finally, you should put this method in the Transports class because it can sort any kind of Transports, nit just buses.

Then, when you call it, you pass an array of Transports (eg Bus's) to be sorted - eg sort(metro);. (this replaces lines 79-82)

If this is still unclear, spend a few minutes revising methods and parameters.

Yay i got it! Thank you so much. but one question though, you say when calling the method, use "sort(metro)", i tried it but cannot. Then i try

for (int i=0;i<3;i++)
metro[i].Sorting(metro);

and it works. Is the method you use another way to call it? or is it just a brief code for me to understand?

Yup, all my code is just brief code for you to understand. I'm here to help you learn, and that's the best way I know to do it.
Calling the method: That depends on where it's defined and where you're calling it from.
public void Sort(Transport[] arrayOfTransportsToBeSorted) { ...
declares an instance method, that needs an instance to execute with. Because metro. is an instance of the right class that works.
For a utility method like this you can declare it as static, so it doesn't need an instance. (I personally would do that, but some people don't like that. Either way it's valid Java).
public static void Sort(Transport[] arrayOfTransportsToBeSorted) { ...
Now you can just call sort(...) anywhere in that class or its subclasses. You can also call it from any other class by using its class name:
Transport.sort(...);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.