Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 11, 2020 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/2eb7b8a07b2f336636d835875e7fd6b7 to your computer and use it in GitHub Desktop.
Save codecademydev/2eb7b8a07b2f336636d835875e7fd6b7 to your computer and use it in GitHub Desktop.
Codecademy export
package com.company.com;
import java.util.Arrays;
import java.lang.Math;
public class TransitCalculator {
int days;
int rides;
String[] rideTypes = {"Pay-per-ride", "7-day Unlimited", "30-day Unlimited"};
double[] ridePrices = {2.75, 33.00, 127.00};
public TransitCalculator(int daysOnTransit, int individualRides) {
days = daysOnTransit;
rides = individualRides;
}
public double unlimited7Price() {
double sevenDayPayment = Math.round(days / 7);
sevenDayPayment = ((sevenDayPayment * ridePrices[1]) / rides);
return sevenDayPayment;
}
public double[] getRidePrices(){
double[] pricePerRide = {};
pricePerRide[0] = ridePrices[0];
pricePerRide[1] = unlimited7Price();
pricePerRide[2] = (ridePrices[2] / rides);
return pricePerRide;
}
public String getBestFare(){
double[] ridePrices = getRidePrices();
int winningIndex = 0;
for (int i = 0; i < ridePrices.length; i++) {
if (ridePrices[i] < ridePrices[winningIndex]) {
winningIndex = i;
}
}
String bestFare = "You should get the " + rideTypes[winningIndex] + " option at " + ridePrices[winningIndex] + " per ride.";
return bestFare;
}
public static void main(String[] args){
TransitCalculator person = new TransitCalculator(23, 45);
System.out.println(person.getBestFare());
}
}
@StudioKaori
Copy link

I think the problem is this :line 22.
double[] pricePerRide = {};
You declared an array with zero element in it.
You cannot change the number of elements once you declare an array.
Instead of that, try this.
double[] pricePerRide = new double[3]; //You should put the numbers of elements here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment