Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Computer Science AP question  RSS feed

 
Tim Hoang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a empty case. The arrays named flights have a problem under eclipse and I hope someone could help/correct my lame errors.
I would like a little tip on how I should perform these tasks and so far I was tampering around method getDuration ONLY for these...
What I need is:
+1 handle empty case
+1/2 check if flights is empty
+1/2 return 0 if empty
+1 access start time
+1/2 access flights.get(0)
+1/2 correctly call getDepartureTime on a flight
+1 access end time
+1/2 access flights.get(flights.size()-1)
+1/2 correctly call getArrivalTime on a flight


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a member variable "flights" in line 19 of your code.

Then, in line 23, you create a local variable called "flights" (that hides the member variable!). In line 24 you loop over the elements of the local ArrayList.

Note that this loop is never going to do anything, because the local ArrayList from line 23 has no elements. So the loop isn't even going to execute once.

Remove the local variable in line 23.
 
Jessid Leon Velez Gutierrez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I think there is a mistake in line 17. The () are incorrect syntax when defining a class... Am I wrong?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jessid, you are right, those () do not belong there.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getDuration() method doesn't get the duration of anything. It creates an empty list, prints 0 for whatever isn't there (ie prints nothing) and then vanishes so the compiler can say "doesn't return an int".
 
Tim Hoang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for catching my errors guys, but how should I get my departure times from empty (values not shown) classes? Is it from the array? If so how should i extract the multiple data from one thing.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you are trying to get departure times from an empty List, you are better off throwing an Exception than messing around trying to produce fake times. How to do it with a full List? Don't know. You will have to resort to pencil and paper and write down what you want and how you intend to achieve it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!