Best Fare Calculator - Part 2

I got myself into another mess:

import java.util.Arrays;
public class TransitCalculator{
  int days;
  int rides;
  String name;
  double price;
  public TransitCalculator(String transitName, double moneyRequired) {
    name = transitName;
    price = moneyRequired;
  }
  public double unlimited7Price(int day, int ride){
    int week;
    if(day % 7 > 0){
      week = day / 7 + 1;
    }
    week = day / 7;
    price = week * 33.00 / ride;
    return price;
  }
  public double[] getRidePrices(int days, double[] priceList){
    double[] dollars = new double[3];
    int weeks;
    int months;
    dollars[0] = days * priceList[0];
    if(days % 7 > 0){
      weeks = days / 7 + 1;
    }else{
      weeks = days / 7;
    }
    dollars[1] = weeks * priceList[1];
    if(days % 30 > 0){
      months = days / 30 + 1;
    }else{
      months = days / 30;
    }
      dollars[2] = months * priceList[2];
    }
  }
  public static void main(String[] args){
    double[2] ridePrices = {2.75, 33.00, 127.00};
    TransitCalculator payPerRide = new TransitCalculator("Pay-Per-Ride", 2.75);
    getRidePrices(5, ridePrices[]);
  }
}

Here is the error:

Usage: javac <options> <source files>
use --help for a list of possible options
$ javac TransitCalculator.java
TransitCalculator.java:39: error: class, interface, or enum expected
  public static void main(String[] args){
                ^
TransitCalculator.java:41: error: class, interface, or enum expected
    TransitCalculator payPerRide = new TransitCalculator("Pay-Per-Ride", 2.75);
    ^
TransitCalculator.java:42: error: class, interface, or enum expected
    getRidePrices(5, ridePrices[]);
    ^
TransitCalculator.java:43: error: class, interface, or enum expected
  }

Can someone help?

I think I see one too many curly brackets in getRidePrices(), but you’ll get more errors after that.

@yimoliu5061522555

Remember, all methods have to explicitly state what they return, your getRidePrices() promises to return a double[], but no double[] is returned.


Then here:

public static void main(String[] args){
    double[2] ridePrices = {2.75, 33.00, 127.00};
    TransitCalculator payPerRide = new TransitCalculator("Pay-Per-Ride", 2.75);
    getRidePrices(5, ridePrices[]);
  }

I think this line might need revision, in particular, ridePrices: getRidePrices(5, ridePrices[]);

Also your array declaration double[2] ridePrices might not work.

Arrays in java are hard!

This is my code now:

import java.util.Arrays;
public class TransitCalculator{
  int days;
  int rides;
  String name;
  double price;
  public TransitCalculator(String transitName, double moneyRequired) {
    name = transitName;
    price = moneyRequired;
  }
  public double unlimited7Price(int day, int ride){
    int week;
    if(day % 7 > 0){
      week = day / 7 + 1;
    }
    week = day / 7;
    price = week * 33.00 / ride;
    return price;
  }
  public double[] getRidePrices(int days, double[] priceList){
    double[] dollars = new double[3];
    int weeks;
    int months;
    dollars[0] = days * priceList[0];
    if(days % 7 > 0){
      weeks = days / 7 + 1;
    }else{
      weeks = days / 7;
    }
    dollars[1] = weeks * priceList[1];
    if(days % 30 > 0){
      months = days / 30 + 1;
    }else{
      months = days / 30;
      dollars[2] = months * priceList[2];
    }
    return dollars[];
  }
  public static void main(String[] args){
    double[] ridePrices = new double[2];
    TransitCalculator payPerRide = new TransitCalculator("Pay-Per-Ride", 2.75);
    getRidePrices(5, ridePrices[]);
  }
}

and here is the errors:

TransitCalculator.java:37: error: '.class' expected
    return dollars[];
                    ^
TransitCalculator.java:42: error: '.class' expected
    getRidePrices(5, ridePrices[]);
                                 ^
2 errors

The variable names are dollars, ridePrices, I don’t think you need []

I did that and now here is the error:

TransitCalculator.java:45: error: illegal start of expression
    System.out.println(getRidePrices(5, ridePrices[));
                                                   ^
1 error

sorry about the random [, I now have that fixed.

TransitCalculator.java:45: error: non-static method getRidePrices(int,double[]) cannot be referenced from a static context
    System.out.println(getRidePrices(5, ridePrices));
                       ^
1 error

Then try making the method static, something like: public static double[] getRidePrices as your method declaration.

You might want to read a thread like this one for more on when to use static method.

I have that fixed now, but when I run my code there is this problem:

Error: Could not find or load main class TransitCalculator.java
Caused by: java.lang.ClassNotFoundException: TransitCalculator.java

Can you re-post your code?

Did you save the file as TransitCalculator.java? Are you using other files in conjunction?

here is the code:

import java.util.Arrays;
public class TransitCalculator{
  int days;
  int rides;
  String name;
  double price;
  public TransitCalculator(String transitName, double moneyRequired) {
    name = transitName;
    price = moneyRequired;
  }
  public double unlimited7Price(int day, int ride){
    int week;
    if(day % 7 > 0){
      week = day / 7 + 1;
    }
    week = day / 7;
    price = week * 33.00 / ride;
    return price;
  }
  public static double[] getRidePrices(int days, double[] priceList){
    double[] dollars = new double[3];
    int weeks;
    int months;
    dollars[0] = days * priceList[0];
    if(days % 7 > 0){
      weeks = days / 7 + 1;
    }else{
      weeks = days / 7;
    }
    dollars[1] = weeks * priceList[1];
    if(days % 30 > 0){
      months = days / 30 + 1;
    }else{
      months = days / 30;
      dollars[2] = months * priceList[2];
    }
    return dollars;
  }
  public String getBestFare(daysRiding, rideAmount){
    
  }
  public static void main(String[] args){
    double[] ridePrices = new double[2];
    ridePrices[0] = 2.75;
    ridePrices[1] = 33.00;
    ridePrices[2] = 127.00;
    TransitCalculator payPerRide = new TransitCalculator("Pay-Per-Ride", 2.75);
    System.out.println(getRidePrices(5, ridePrices));
  }
}

I get a different error with this code (saving it as TransitCalculator.java)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
	at TransitCalculator.main(TransitCalculator.java:46)

Do you know what that error means?

That your index is out of bounds. Check your arrays and what indexes you’re calling from them.

I see no problems within it?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
	at TransitCalculator.main(TransitCalculator.java:46)

If we look at the error message it says index-2 is out of bounds for length 2.
It means that because your array is of length 2, it can’t have any index outside those 2 numbers (0 and 1).

The code with the error is this one, make the length 3 and it should work:

double[] ridePrices = new double[2];
ridePrices[0] = 2.75;
ridePrices[1] = 33.00;
ridePrices[2] = 127.00;

I changed it and it still doesn’t work!

Also, can you use a method from another method?

Also, when I do Javac TransitCalculator.java, the error is this:

TransitCalculator.java:37: error: '.class' expected
    return dollars[];
                    ^
1 error

Return dollars instead of dollars[].
A way to think about this is that you have have to return a valid object.

Consider: can you println( dollars[])? If you can’t, then it’s probably not going to be a valid type of return.

can you use a method from another method?

I’m not quite sure what you mean by this. Can you give an example?

For example, in my code, I have a getRidePrices() method. If I were to use it in getBestFare(), would it still work?

Yes. I thought you meant use a method defined inside a method in another:
(If method A has a method inside it B, if an unrelated method C can use B).

But using getRidePrices() inside getBestFare() should be no problem.

1 Like