Hello all who read this,
I'm still learning Java but i can't figure out how to put results from another class into an array in the main class and then write a method that compares certain results from the array.
so here is the case:
- I have an App.java which contains the main method
- I use Car.java which contains fields, gets/sets and methods for random cars
- I use constructors to get the results from Car.java and print them.
- I want all the results given from Car.java to be put into an array in App.java
- A method will then read the array and checks which car from the Car.java has the highest maximum speed and accordingly displays it.
public class App
{
public static void main(String[] args)
{
/**************constructor**************/
Auto a = new Auto("AB-01-CD",3,"black","Opel",260,"Astra",2007,5);
Auto b = new Auto("EF-23-GH",5,"Green","Opel",200,"Corsa",2002,3);
Auto c = new Auto("IJ-45-KL",2,"Red","Ferrari",415,"F1",2010,7);
a.print();
b.print();
c.print();
/******here should the results of Auto "a" "b" and "c" be put in an array*****/
array[] = a, b, c;
/*******and here it should print the highest speed of the cars that are in the array*****/
speed(array[]).print();
}
public int speed(int s)
{
/***********some kind of method that calculates the car with the highest speed******/
return s;
}
}
Thank you very much in advance!
Kind Regards, SteelDevil
EDIT:
added this to main method of App.java
public void main
{
Auto[] array = new Auto[] {a, b, c};
System.out.println("The car with the highest speed is: ");
int maxSpeed = getHighestSpeed(array);
System.out.println(maxSpeed);
}
added new method in App.java
public int getHighestSpeed (Auto[] array)
{
int highest = 0;
for (Auto auto : array)
{
if (auto.getSpeed() > highest)
{
highest = auto.getSpeed();
}
}
return highest;
}
getting an error code at the compiler:
App.java:25: error: non-static method getHighestSpeed(Auto[]) cannot be referenced from a static context
int maxSpeed = getHighestSpeed(array);
1 error