Quick Java Question

Man of Honour
Joined
15 Mar 2004
Posts
28,140
Location
Liverpool
How do I find the smallest float number (out of 4)?

I want to put this line:

Winning Time = public static float findMin(float TimeG, TimeW, TimeCP, TimeS);

Where:

Winning Time = the quickest race time
Time'X' = a character's individual race time.

I keep getting really weird errors though, and I can't figure out if it's anything to do with the the fact that I've either defined it wrong (it's not 'findMin' or I haven't defined it correctly above where this line sits? :confused:

Thanks for any help offered. :)
 
Been a while since I did any Java, but have you tried:

Code:
findMin(float TimeG, float TimeW, float TimeCP, float TimeS);
 
Code:
Winning Time = public static float findMin(float TimeG, TimeW, TimeCP, TimeS);

I'm not sure what you're trying to do here. Has the method already been defined and implemented somewhere else in your class?

If you simply want to call the method you would use;
Code:
float winningTime = findMin(TimeA, TimeB, TimeC, TimeD);

Elsewhere in your class you would have
Code:
public static float findMin(float TimeG, float TimeW, float TimeCP, float TimeS)
{
    //implementation code goes here
}

Can you post the specific error messages you get like in this thread. It really helps :)
 
It's essentially to find the winner of the race - I have defined the distance each thing needs to cover, and it's speed (in other classes). I now call these to work out a time (just by using speed=distance/time => time=distance/speed). Then I'm trying to find the shortest one of these times.

Code:
import java.util.Scanner;
import java.util.*;

public class Creature {
    static Grasshopper z = new Grasshopper();
    static Worm z = new Worm();//etc etc
    static CP z = new CP();
    static Slug z = new Slug();
    
    //static Driver a = new Driver();//New instance of Driver.java class to be read from Creature.java (here)
    
    public double GD = z.fetchGD();
    public double WD = z.fetchWD(); //- will need these distances from creature to leaf and the two below
    public double CPD = z.fetchCPD();
    public double SD = z.fetchSD();
    
    public static int speedG = z.fetchspeedG();//creature's individual speeds and three below
    public static int speedW = z.fetchspeedW();
    public static int speedCP = z.fetchspeedCP();
    public static int speedS = z.fetchspeedS();
    
    public float TimeG;
    public float TimeW;
    public float TimeCP;
    public float TimeS;
    
    public float WinningTime ();
   
    public Creature() {
    }
    {float TimeG = (GD / speedG);
    }
    {float TimeW = (GW / speedW);
    }
    {float TimeCP = (GCP / speedCP);
    }
    {float TimeS = (GS / speedS);
    }
}    
    {float WinningTime = findMin( TimeG,  TimeW,  TimeCP, TimeS)
}
 
When you compile the class what error messages are you getting?

The first thing I notice is
Code:
    static Grasshopper z = new Grasshopper();
    static Worm z = new Worm();//etc etc
    static CP z = new CP();
    static Slug z = new Slug();
You should be getting duplicate errors like this:
Code:
markm@tsunami:~/javatest$ cat DuplicateTest.java
public class DuplicateTest 
{
        public static void main(String[] args) 
        {
                int i = 1;
                int i = 2;
        }

}
markm@tsunami:~/javatest$ javac DuplicateTest.java 
DuplicateTest.java:6: i is already defined in main(java.lang.String[])
                int i = 2;
                    ^
1 error
markm@tsunami:~/javatest$

In fact when I look at it further it seems you are unsure about quite a bit of the syntax. I suggest you read up on classes and variables then try creating a couple of simple classes to check your knowledge.

For example variables only need prefixing with their type when declaring them not every time they are used.

Did you want the following performed in the constructor ie when you call "Creature daveTheRabbit = new Creature();"
Code:
    public Creature() {
    }
    {float TimeG = (GD / speedG);
    }
    {float TimeW = (GW / speedW);
    }
    {float TimeCP = (GCP / speedCP);
    }
    {float TimeS = (GS / speedS);
    }

It should be
Code:
    public Creature() {
        TimeG = (GD / speedG);
        TimeW = (GW / speedW);
        TimeCP = (GCP / speedCP);
        TimeS = (GS / speedS);
    }

Have a look at this example of how things are laid out.
Code:
public class Box {
	
	//member variables to hold the box dimensions
	private int width;
	private int length;
	private int height;

	public Box(int w, int l, int h) {
		//constructor for our class

		width = w;
		length = l;
		height = h;
	}

	public int getVolume() {
		//this method returns the volume of our box.
		return width * length * height;
	}
	
}

To use a Box
Code:
public class BoxTest {

	public static void main(String[] args) {
		Box myBox;
		int volume;

		myBox = new Box(10,10,20);
		volume = myBox.getVolume();

		System.out.println("The box's volume is: " + volume);
	}

}
 
As fourstar said your syntax seem to be wrong. The class box the he made is a good template to base a class on.

As for finding the smalliest of 4 varibles i think it may be quicker to store the results into an array/arraylist as this makes finding the smallest value a lot quicker/easier by elimintating the use of repetative if statements and replacing them with a for loop.
 
Back
Top Bottom