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