• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Yet another Java problem

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Dreamstalker

Member
Joined
Nov 13, 2004
Location
Brookline, MA
The current project we have is essentially developing an AI; it uses a random number generator to give stats about a car (does it start, how is it turning/accelerating, etc). A large portion is severely flawed as given to us, but the instructor says it is correct (basically a fairly important function that is based on data that we do not have, were not given and cannot get from any other methods).

I've written the bit of code for starting the engine, but the compiler is throwing a fit with the boolean return statements, and no matter what I do to fix it I get the same or worse errors.

Code:
package carapp;

import java.util.Random;
import java.lang.Object;

public class CarApp
{

    double num;
    boolean StartEngine;
   {
        num = (double)(Math.random());
        if (num > 0.75)
        return True;
        System.out.println("Engine successfully started!");
        if (num < 0.25)
        return False;
        System.out.println("Engine start failed.");
        
    }
The errors it's giving me are at the boolean return statements: "return outside method". That seems to be the only thing preventing this from working, except I can't figure out what it wants me to do (maybe I need to go to bed; been working on this all day).

Any ideas?

*grumbles: my school is too cheap to hire tutors for this stuff*
 
first as it looks you didnt close your class brackets ({}), so that may be part of the problem. other then that theres not much i can tell thats wrong. you may want to consider indenting thoug, makes it easyer to read (;

if that wasent the problem, pleese reply with the exact compiler output and on what line the error is occuring.
 
First of all, you have some bracket issues. Also, why are you trying to return true or false? You can't return a boolean unless it's inside a boolean-returning method. Do you want the boolean variable "StartEngine" to be set to true or false instead. In that case, it'd look like this:

Code:
package carapp;

import java.util.Random;
import java.lang.Object;

public class CarApp
{

    double num;
    boolean StartEngine;
    num = (double)(Math.random());
    if (num > 0.75){
        StartEngine = true;
        System.out.println("Engine successfully started!");
    }
    if (num < 0.25){
        StartEngine = false;
        System.out.println("Engine start failed.");
    }
        
}

However, what if num is between .25 and .75?
 
The boolean value can return either true or false; if it returns false the car does not start, and if it returns true it does. It is based on "engine efficiency" which is between 0.0 (dead) and 1.0 (perfect). There is a special condition for a return value between .25 and .75, but I haven't figured out what it is supposed to be.

Here are the two errors I'm getting. If I fix them, it comes back with another error, and if I fix that I get thrown back to the first error.
Code:
C:\Program Files\Java\CarApp\src\carapp\Main.java:24: return outside method
        return True;
C:\Program Files\Java\CarApp\src\carapp\Main.java:27: return outside method
        return False;
 
Try this:
package carapp;

import java.util.Random;
import java.lang.Object;

public class CarApp
{

double num;
/*you needed brackets to make StartEngine a function*/
boolean StartEngine() //no semicolon here
{
num = (double)(Math.random());
if (num > 0.75)
{
System.out.println("Engine successfully started!");
return True;
/*System.out.println wont run after return(thats the exit of the function*/
}
if (num < 0.25)
{
System.out.println("Engine start failed.");
return False;
}

}
}//closes class brackets
 
Back