user3296193
user3296193

Reputation: 49

classes returning a method value

public class car
{
private int model;
private String make;
private double speed;
/**
 * *  My constructor
 */
public car()
{
    model = 2000;
    make= "Ferrari";
    speed= 50;

}

public int getYear()
{
    return model;
}

public String getMake()
{
    return make;
}

public double getSpeed()
{
    return speed;
}

public double accerlate ()
{
    double accerlate = speed++;
    return accerlate;
}

public void output(double accerlate)
{
    System.out.println("Year: " +model);
    System.out.println("Make: " + make);
    System.out.println("Speed: " + speed);
    System.out.println(accerlate);
}
}

connect to this class

public class RaceTrack
{
public static void main(String args[]){
   double speed = 50;
   car mycar= new car();
    mycar.getYear();
    mycar.getMake();
    mycar.getSpeed();
    mycar.output(speed);
    mycar.accerlate();
}
}

Anyways, I try to get an output and the accerlate method doesn't work the way I want. It should be add +1 and it is not working. Why isn't it working? It just prints the original speed.

Upvotes: 1

Views: 136

Answers (7)

Premier
Premier

Reputation: 786

ok, the var double speed = 50; is local in the method, and this will be global

public class car
{
private int model;
private String make;
private double speed;
private double accerlate // you need make the getter and setter
/**
 * *  My constructor
 */
public car()
{
    model = 2000;
    make= "Ferrari";
    speed= 50;

}

public int getYear()
{
    return model;
}

public String getMake()
{
    return make;
}

public double getAccelerate()
{
 return accelerate;
 }
public double setAccelerate(double accelerate)
{
  this.accelerate = accelerate;
}

public double getSpeed()
{
    return speed;
}

public double accerlate ()
{
    accerlate = speed++;
    return accerlate;
}

public void output(double accerlate)
{
    System.out.println("Year: " +model);
    System.out.println("Make: " + make);
    System.out.println("Speed: " + speed);
    System.out.println(accerlate);
}
}

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 76482

This would solve your problem:

public double accerlate ()
{
    return ++speed;
}

Also, I have noticed that you have created a speed variable in your main method. I am not sure you need that one.

Upvotes: 0

Zied R.
Zied R.

Reputation: 4964

double accerlate = speed++; ==> accerlate =speed then speed =speed+1

double accerlate = ++speed; ==> accerlate =speed+1

For your code : your output will be :

Year: 2000
Make: Ferrari
Speed: 50.0
50.0

this is the output of mycar.output(speed);the other method hasn't output in you main

double speed = 50;
car mycar= new car();
 mycar.getYear(); //getYear return an int (model)=2000 so store it if you want
mycar.getMake(); //getMake return a string 
mycar.getSpeed();  //getSpeed return a double =50

if you want to see +1 of accelerate , you should get the return of the method then print it :

   Double ouputSpeed=mycar.accerlate();
   System.out.println(ouputSpeed.toString()); // print 51.0 

To return :

public double accerlate ()
{
    double accerlate = ++speed;
    return accerlate;
} 

==> this method return a double (accerlate= spped+1 =51)

==> to get it in you main , declare a double : Double ouputSpeed=mycar.accerlate(); then print it .

Upvotes: 2

Lorenzo_g
Lorenzo_g

Reputation: 311

One of the things you have to be careful with when writing a method in Java is storing values to variables and then returning those variables. Returning variables can cause your data to become stale. In other words java thinks that something like int or double has not value. Anyway to prevent this especially when increasing or decreasing these values is to make sure you initialize these variables with a value such as zero. Which is something you don't do in your code. That may be why

Upvotes: 0

StickyCube
StickyCube

Reputation: 1721

As mentioned, use ++accelerate instead.

However, the program as it stands will always print the speed 50 because of this

double speed = 50;
mycar.output(speed);

then the argument of output() is passed straight to the last System.out.println() in that method. Furthermore, the calls to getYear(), getMake() and getSpeed() in main arent actually doing anything.

Upvotes: 0

mangusta
mangusta

Reputation: 3544

this is enough

public double accerlate ()
{
return ++speed;
}

Upvotes: 0

Lain
Lain

Reputation: 2216

public double accerlate ()
{
    double accerlate = speed+1;
    return accerlate;
}

++ is different than +1, ++ means the operation happens AFTER the assigning of the value, however +1 will happen before hand, could also do ++speed.

Upvotes: -1

Related Questions