简体   繁体   中英

Constructing my own classes?

I'm having trouble with learning how to write and use my own classes. For example;

import java.text.NumberFormat;

public class BikeCommute {

private String route;
private double distanceTraveled;
private double timeRequired;
private String dateTraveled;
private String mode;
private double gallonsSaved;
final private int mpg = 25;


public BikeCommute(String mode, String dateTraveled, String route, 
        double distanceTraveled, double timeRequired)
{
    mode = this.mode;
    route = this.route;
    distanceTraveled = this.distanceTraveled;
    timeRequired = this.timeRequired;
    dateTraveled = this.dateTraveled;
}

public double gallonsCalculated(){
    gallonsSaved = distanceTraveled/mpg;
    return gallonsSaved;

}

public double getGallons(){
    return gallonsSaved;
}

public String toString(){

    return mode + route + distanceTraveled + timeRequired + dateTraveled + gallonsSaved;
}
}

I'm trying to write a class called BikeCommute that will read in the route, mode of transportation, distance traveled, time required, and date that the trip was traveled, calculate how many gallons of gas were saved by dividing the distance traveled by the miles per gallon, and output the route, mode, distance traveled, time required, date traveled, and gallons saved. This, however, returns nullnull0.0null0.0 when I run it. Any thoughts?

Assignment is from right to left. Replace

mode = this.mode;

with

this.mode = mode

Same for the other field variables in BikeCommute

You have these the wrong way around:

mode = this.mode;

Should be:

this.mode = mode;

Your question is about a specific problem in this specific class; however, you should consider the general problem too.

If you have problems constructing or using your own classes, it's probably because you have confused the building of the class with the using of the class. Actually, you can get much better results if you "use the class" before you "build the class". I know that this sounds like nonsense, but it is the driving idea behind test driven development, which is one of the more effective ways to write robust usable programs.

Consider looking into JUnit and learn how to setup a "test" directory. I also recommend you minimally learn "just enough" maven to automate this. Don't start off trying to be a JUnit or Maven virtuoso. Cut-and-paste is fine for beginning in this area.

Then you can write your test first:

public void testBikeCommute() {
  BikeCommute commute = new BikeCommute(...);
  Assert.assertEquals(5, commute.getGallons());
}

Now you can see that your design is pretty odd, as most of my commutes don't have Gallons. As you attempt to "use" your software before you write it, you might find that your "commute" is really a comparison between two theoretical commutes, one in a car, and one on a bike.

public void testCommuteSavings() {
  BikeCommute bike = new BikeCommute(...);
  CarCommute car = new CarCommute(...);
  SavingsCalculator calculator = new Calculator(car, bike);
  Assert.assertEquals(5, calcuator.getSavedGallons());
}

This "use first" technique can allow you to develop a much cleaner representation of your problem, and with a toolkit like JUnit, can even become an automated testing strategy. Also it makes some items pretty easy to test, like

public void testBikeCommuteUsesZeroGallons() {
  BikeCommute bike = new BikeCommute();
  Assert.assertEquals(0, bike.getGallonsUsed());
}

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM