Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
2  
Start here... You might want to use Lists instead of arrays. –  home Nov 29 '13 at 12:56
    
create an array with Auto's like this : Auto[] a = new Auto[] {a, b, c}; –  Fabian Nov 29 '13 at 13:05

3 Answers 3

up vote 0 down vote accepted

To put Auto instances a, b and c into an array:

Auto[] array = new Auto[] {a, b, c};

To write a method to find the highest speed of all the Autos in an array, the method will require a parameter of type Auto[] (Auto array) as input, and will return an integer (int) as output.

So the signature will be:

public int getHighestSpeed(Auto[] autos)

What will the method need to do?

It will get the speed from each Auto in the array, and keep track od the highest speed found so far. This should do the trick:

public int getHighestSpeed(Auto[] autos) {

  int highest = 0;  //use a variable to hold the highest speed found so far.

  for (Auto auto : autos) {  //loop through all Auto in the array
    if (auto.getSpeed() > highest) {
      highest = auto.getSpeed();  //this Auto is fastest so far
    }
  }

  //how we have the highest speed, return it
  return highest;
}

You don't supply your code for Auto, but it will need a method called getSpeed() for the getHighestSpeed() method to work.

share|improve this answer
    
yeah correct i didn't put Auto in because it's quite large since it has some specifics in it which i don't want anyone to bother with but it basically has the public void setSpeed(){maxSpeed = Speed } and the public int getSpeed(int s) {return s} in it. i will try your solution out later this day and i'll comment back ASAP if it worked or not thanks for so far –  SteelDevil Nov 29 '13 at 15:35
    
hmmm so i tried using your code and let it print the results from the getHighestSpeed method and got an compiler error foo trying to put the array in the getHighestSpeed method in a println function. maybe i'm doing something wrong again... System.out.println(getHighestSpeed(array)); error: non-static method getHighestSpeed(Auto[]) cannot be referanced from a static context –  SteelDevil Nov 30 '13 at 11:08
    
nevermind figured out the error. it needed to be public "static" int getHighestSpeed(Auto[] autos) –  SteelDevil Nov 30 '13 at 13:05
    
OK - non-static methods need an instance to act on. Glad you figured that out. –  NickJ Nov 30 '13 at 18:13

The correct approach from a design perspective (and overall I think) would be to create a List of Cars and to compare them using their getter method (a getSpeed-method should be provided by the Car-class).

Example:

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);

List<Auto> autos = new ArrayList<>();
autos.add(a);
autos.add(b);
autos.add(c);

int getMaxSpeed(List<Auto> list) {
    int maxSpeed = 0;

    for(Auto act : list) {
        if(act.getSpeed() > maxSpeed)
            maxSpeed = act.getSpeed();
    }

    return maxSpeed;
}

int maxSpeed = getMaxSpeed(autos);

By the way if you write Car in your question and Auto in your code that could confuse non-germans quite a lot ;)

EDIT: You could also write a Comparator for your Car class, but that would be a bit too much for such a little problem, just mentioning that it exists and that there are a lot of comfortable functions out there that do compare things like getting a maximum using Comparator

share|improve this answer
    
+1 for simple, clean code –  Melquiades Nov 29 '13 at 13:09
    
hmmm i'm not very familiar with the List function. at least not that i've been tought to do. but i will try it out and let you know how it went. thanks for so far helping me out –  SteelDevil Nov 29 '13 at 15:38
    
C:\Java\Periode2\Weekopdracht1>javac App.java App.java:84: error: cannot find symbol public int getMaxSpeed(List<Auto> list) ^ symbol: class List location: class App App.java:21: error: cannot find symbol List<Auto> autos = new ArrayList<>(); ^ symbol: class List location: class App App.java:21: error: cannot find symbol List<Auto> autos = new ArrayList<>(); ^ symbol: class ArrayList location: class App 3 errors –  SteelDevil Nov 30 '13 at 11:19
ArrayList<Auto> cars = new ArrayList<Auto>();
cars.add(a);
cars.add(b);
cars.add(c);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.