Fare Calculator

So I worked through the fair calculator and finally got the 7dayunlimited to return the correct value. However I am confused on how the logic works. My question is as i looked into Math.ceil method I was under the impression it rounded the given input up which would create the correct number of weeks to multiple the weekly pass price by. However in order to get the code to return the correct value I had to add the plus one. Just wondering why the +1 was necessary. Seems like it was rounding down not up. Any light you can shed on this is always appreciated.

<public double unlimited7Price(){

double weeksNeeded = days/7;
double totalWeeks = Math.ceil(weeksNeeded)+1;
double unlimited7Day = (totalWeeks*33)/rides;

return unlimited7Day;

Math.ceil will round any decimal point up to the next highest integer (so 6.2 would become 7.

However the rounding down happens here: double weeksNeeded = days/7;

Example:

public class Example {
    public static void main(String[] args) {
    	double number = 7/4;
        System.out.println(number); 
        // this prints out 1.0 (the value for number), not 1.75
    }
}

still kind of confused. will it always round and operation like that when being assigned to variable like that?

So i am completely mixed up. I have no idea what iv have done wrong but cant getBestFair method to work for the life of me. Any help is appreciated

class TransitCalculator{

int days;

int rides;

public TransitCalculator(int numOfDays, int numOfRides){

days = numOfDays;

rides = numOfRides;

String[] pass = {"Pay-per-ride", "7 day unlimited", "30 day unlimited"};

double[] price = {2.75, 33, 127};

}

public double unlimited7Price(){

 double weeksNeeded = days/7;

 double totalWeeks = Math.ceil(weeksNeeded)+1;

 double unlimited7Day = (totalWeeks*33)/rides;

 return unlimited7Day;

}

public double getRidePrices(){

 double unlimited7 = unlimited7Price();

 double payPer = 2.75;

 double thirtyDay = 127/rides;

 double[] ridePrices = {payPer, unlimited7, thirtyDay};

 return ridePrices;

} 

public String getBestFare(){

double[] ridePrices = getRidePrices();

int winningIndex = 0;

for(int i = 0; i < ridePrices.length; i++){

  if(ridePrices[i] < ridePrices[winningIndex])

  {

    winningIndex = i;}}

    return "you should get the " + pass[winningIndex] + " option at $" +ridePrices2[winningIndex] + " per ride.";

  }

public static void main(String[] args){

TransitCalculator stevesRides = new TransitCalculator(22,50);

TransitCalculator katiesRides = new TransitCalculator(18, 83);

System.out.println(katiesRides.unlimited7Price());

System.out.println(stevesRides.unlimited7Price());

stevesRides.getBestFare();

}

}

Hey, no problem, we’ll see what we can do.
Can you try editing and press </> before pasting?
Formatting just makes it easier to tell what’s happening.

I should’ve clarified: integer division will always floor its answer. Double division will give you a double.

public class DoubleDivision {
    public static void main(String[] args) {
    	double number = 7.0/4.0;
        System.out.println(number);
        // 1.75
    }
}

Thanks just formatted the code for ya. You guys rock

3 issues I see

public double getRidePrices(){
 double unlimited7 = unlimited7Price();
 double payPer = 2.75;
 double thirtyDay = 127/rides;
 double[] ridePrices = {payPer, unlimited7, thirtyDay};
 return ridePrices;
} 

^^ in the above method, you declare that it will return a double but it returns a double[]. So if you want to return a double[] the declaration should reflect that.

There’s a typo in your return statement of getBastFare(): ridePrices2[winningIndex]. I don’t think you meant for the 2 to be there.

The other one is you should test the initialization of your classes. It’s been a minute since I’ve written in java but it seems like since your String[] pass and double[] price are constants they don’t need to be initialized in the constructor (and so should go outside of it next to your int rides; declaration. As it is, pass is not accessible.

Thanks. This is very helpful. Ill keep at it. One day I will (hopefully) figure this stuff out