user2984143 user2984143 - 3 months ago 21
Java Question

Feet to Seconds - Can't increment seconds Java

I am creating a program that will show user how many feet they have travel while driving if they were texting.

So far this is what I got,
If the user is driving at 60 MPH, one second of distraction will travel 88ft.
If the user is driving at 20MPH, one second of distraction will travel 29ft. ETC.

The problem that I am having trouble figuring out is that if I increment the seconds on this method c.SetHourstoSeconds(1). My program starts dividing, instead of multiplying. I have created new methods but so far I am completely lossed of how to make it work. Thanks in advance.

public class App {

public App() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Conversions c = new Conversions();



c.SetMilestoFeet(60);

c.SetHourstoSeconds(1);


System.out.println("Total feet travel " + c.Total() + "ft");
}
}


public class Conversions {

static int Milestofeet =0;
static int MinutestoSeconds =0;

public Conversions() {

}



public static void SetMilestoFeet(int x)
{

Milestofeet = x * 5280;




}

public static void SetHourstoSeconds(int hr)
{
int hourstoMinutes = hr * 60;
MinutestoSeconds = hourstoMinutes * 60;

System.out.println("***********" + hr);


}

public static int Sec()
{

return MinutestoSeconds;
}

public static int Feet()
{

return Milestofeet;
}


public int Total()
{
System.out.print(Milestofeet + " ___" + MinutestoSeconds + "______=" );

return (Feet()/Sec());

}


}

Answer

You're making this much too hard:

/**
 * Calculate distance traveled per second of distraction
 * User: mduffy
 * Date: 8/25/2016
 * Time: 2:40 PM
 * @link http://stackoverflow.com/questions/39152475/feet-to-distractionSeconds-cant-increment-distractionSeconds-java
 */
public class DistractionDistanceCalculator {

    public static final int FEET_PER_MILE = 5280;
    public static final int SECONDS_PER_HOUR = 3600;

    public static void main(String[] args) {
        double mph = (args.length > 0) ? Double.parseDouble(args[0]) : 60.0;
        double distractionSeconds = (args.length > 1) ? Double.parseDouble(args[1]) : 1.0;
        double distance = calculateDistractionDistance(mph, distractionSeconds);
        System.out.println(String.format("You will travel %10.3f feet if you are distracted for %10.3f distractionSeconds at %10.3f mph", distance, distractionSeconds, mph));
    }

    private DistractionDistanceCalculator() {
    }

    public static double calculateDistractionDistance(double mph, double distractionSeconds) {
        if (mph < 0.0) throw new IllegalArgumentException("Speed must be positive");
        if (distractionSeconds < 0.0) throw new IllegalArgumentException("Distraction seconds must be positive");
        return mph*FEET_PER_MILE/SECONDS_PER_HOUR*distractionSeconds;
    }
}
Comments