Merak_Dubhe Merak_Dubhe - 2 months ago 21
Java Question

My return type for method is missing?

My method

public get_speed() //should i make this s capitol?
{
return speed; //or should i make the s capitol?

}


is giving me the return type for method is missing error is missing and i font know why.

Here is my car class

import java.util.Calendar;
public class Car {

private int yearModel;
private String make;
private int speed;

public Car()
{
yearModel = 0;
make = " ";
speed = 0;
}
public Car(int modelYear, String carMake)
{
//int currentYear = Calender.getInstance().get(Calender.YEAR);
yearModel = modelYear;
speed = 0;
make = carMake;
}
public int get_yearModel()
{
return yearModel;
}
public String get_make()
{
return make;
}
public get_speed()
{
return speed;
}
public void accelerate()
{
speed = speed + 5; // speed += 5
}
public void brake()
{
speed = speed - 5; // speed -= 5
}

}


Here is my main
Another issue i have been getting in the eclipse ide is
"The method print(boolean) in the type PrintStream is not applicable for the argument(void)"
Does this mean i am using the wrong System.out.print(); or is there a specific one that needs to be used?

public class CarMain {

/**
* @param args
*/
public static void main(String[] args) {
Car theCar = new Car();

/*
* here we will accelerate the vehicle five times
*/
theCar.accelerate();
System.out.println(theCar.get_speed());

theCar.accelerate();
System.out.println(theCar.get_speed());

theCar.accelerate();
System.out.println(theCar.get_speed());

theCar.accelerate();
System.out.println(theCar.get_speed());

theCar.accelerate();
System.out.println(theCar.get_speed());

/*
* here we will brake the car five times
*/
theCar.brake();
System.out.println(theCar.get_speed());

theCar.brake();
System.out.println(theCar.get_speed());

theCar.brake();
System.out.println(theCar.get_speed());

theCar.brake();
System.out.println(theCar.get_speed());

theCar.brake();
System.out.println(theCar.get_speed());

theCar.brake();
System.out.print(theCar.get_speed());




}

}

Answer

You are indeed missing the return type which is int as speed has the type int. Also in java the the naming convention is camelCase starting with lower case.

so the correct solution is:

public int getSpeed() {
    return speed;
}

The error regarding the print should go away once you add the type to the method getSpeed (at least according to the print usages you have in the code posted). You cannot invoke the print method with a method that has void return type, so this will not work: System.out.println(theCar.accelerate()); as void states that the method method does not return anything, so you would want to pass this 'nothing' into a the print method that expects a parameter that it will print.