• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

MIT OpenCourseWare Assignment 3

 
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I attempted answering a question from MIT Open courseware....
This is the link to the question
MIT OpenCourseware

This is my implementation of the assignment, I want it to be critiqued to make necessary changes and learn in the process too.
 
 
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You’re not following standard naming conventions. Java convention is to use camelcase for variable names and method names, starting with a lowercase letter. No underscores.

2. You’re not following instructions. The second method should use the first method to find the fastest time. Your second method doesn’t call the first method. Instead, you’ve designed it to take the index of the fastest time as a parameter. Those are NOT the instructions you were given.
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, converting an int[] into an Integer[] is pointless. Why do you think that was necessary?
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote:
and

When choosing names, choose names that help tell a story about WHAT the program is doing. Compare your code to this:
 
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method 'public static int bestRunner(Integer[] time) {...' is very cumbersome. Why not have a second field 'indexOfFastestTime' that you update whenever you encounter a faster time? You can then retrn that value.

In howfar is that 'Direction' an obligation? Working with parallel arrays is something to avoid like the plague. If you have a Pair class, with fields name and time, (JavaFX has such a class, or you can make one for youraelf), then it is just one line to create a List<Pair>, sorted on time.

And last: you do not take into account that more than one person may have the same fastest time. What would you do in that case?
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the comments so far, am making the necessary changes and updates, I will post them when am done...
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:You wrote:
and

When choosing names, choose names that help tell a story about WHAT the program is doing. Compare your code to this:



Why should I use to
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, converting an int[] into an Integer[] is pointless. Why do you think that was necessary?



The return methods were return Integer objects so I needed to change the array type from int to Integer..

But now, when I avoided converting the array type to Integer I run into ArrayIndexOutOfBounds Exception.
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:The return methods were return Integer objects so I needed to change the array type from int to Integer..


I see nothing in the instructions that say the return type has to be Integer. Even if it were so, Java has autoboxing so conversion from int to Integer (and vice versa) happens behind the scenes in many cases, particularly with a return statement.

But now, when I avoided converting the array type to Integer I run into ArrayIndexOutOfBounds Exception.


That error most probably has nothing to do with dealing with int values vs. Integer values. Check your logic again.
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Your method 'public static int bestRunner(Integer[] time) {...' is very cumbersome. Why not have a second field 'indexOfFastestTime' that you update whenever you encounter a faster time? You can then retrn that value.


Wouldn’t that require you to call the method that finds the fastest time first? I think the solution suggested by the instructions are better: make the method that finds the second fastest time call the method that finds the fastest time. That way you won’t need a field and the sequence you call the methods won’t matter.
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Piet Souris wrote:Your method 'public static int bestRunner(Integer[] time) {...' is very cumbersome. Why not have a second field 'indexOfFastestTime' that you update whenever you encounter a faster time? You can then retrn that value.


Wouldn’t that require you to call the method that finds the fastest time first? I think the solution suggested by the instructions are better: make the method that finds the second fastest time call the method that finds the fastest time. That way you won’t need a field and the sequence you call the methods won’t matter.



Thank you, I will work on that
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Kelvin Okornoe wrote:The return methods were return Integer objects so I needed to change the array type from int to Integer..


I see nothing in the instructions that say the return type has to be Integer. Even if it were so, Java has autoboxing so conversion from int to Integer (and vice versa) happens behind the scenes in many cases, particularly with a return statement.

But now, when I avoided converting the array type to Integer I run into ArrayIndexOutOfBounds Exception.


That error most probably has nothing to do with dealing with int values vs. Integer values. Check your logic again.



Okay, I will go through the logic and figure it out...

Thanks soo much for the comments
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu

Have you read that method? It finds the fastest time, then it creates a List of all the times (while the time array is already present) and finally it returns the index of that fastest time. I say that that is very cumbersome.
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:It finds the fastest time, then it creates a List of all the times (while the time array is already present) and finally it returns the index of that fastest time. I say that that is very cumbersome.


I wasn’t questioning that. Yes, the return statement in that method is a very roundabout way. I was talking about the suggestion to use a second “field” to track the index. I probably misunderstood what you meant by “field” though. You probably meant “local variable,” in which case I would agree, that would be the way to go.
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh, sorry for using the word 'field', indeed I meant 'local variable'.
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am working on making the secondBestRunner() call the bestRunner(),
but I can not pass the the time array as an argument into the bestRunner() been called inside the secondBestRunner() because the time array is a local variable.
Will it be okay to make the time array a private class variable or there is another way out?
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think you can’t pass it as a parameter? Post your code so we know exactly what you’re trying to do.
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what I have done
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not able to pass int[] time to the bestRunner() method because int[] and Integer[] are two different types. Autoboxing does not apply to int[]. Like I said before, converting int[] times to Integer[] new_times is pointless. I would change bestRunner() to take an int[] instead of an Integer[].
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another tip: I would change bestRunner() slightly so that I'm just keeping track of the index of the fastest time found so far. There's really no need to track what the actual time value is since you're only looking for the index. You can still compare values since both the index variable and the fastest variable can be used to access elements of the times array.
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done the necessary changes, this what my code looks like.

 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am now trying to fix ArrayIndexOutOfBoundsException
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't why the index of the fastestRunner is not been returned...
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must absolutely skip that 'return Arrays.asList(time).indexOf(fastest);'. If you have, for instance, two fastest indices (two people with the same fastest time), then you will miss that other index (why?). And even if you do find a seond fastest index, that Arrays.as... will always return the first index found.


Edit: this reply was sent before I saw your latest code.
Edit 2: in your latest 'bestRunner' the parameter is int[] time, but in Arrays.as... you use times. That works, because you have times as static variable.
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:You must absolutely skip that 'return Arrays.asList(time).indexOf(fastest);'. If you have, for instance, two fastest indices (two people with the same fastest time), then you will miss that other index (why?). And even if you do find a seond fastest index, that Arrays.as... will always return the first index found.


Edit: this reply was sent before I saw your latest code.
Edit 2: in your latest 'bestRunner' the parameter is int[] time, but in Arrays.as... you use times. That works, because you have times as static variable.



Thank you for @Piet for the comments, my current code does not even the index of the fastest time, I am struggling to resolve that then I will go ahead and try implementing your suggestion.
Thank you
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kelvin,

I overlooked a very nasty error, that is very easy to make, and that is the reason why your method returns -1.  If x is an int[], then Arrays.asList(x) does NOT create a List of the elements of x. Do you use an IDE? If so, do this at the end of the method:
I use the java 10 'var' here, but hover the mouse over 'var' and let the IDE change it to the real type. What do you notice?
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually let people work out problems themselves but I think it might help you understand where you're going wrong if you see how other people would solve this. Would you like to keep trying to get your code to work or would you like to see how Piet and/or I (or others, if there's anyone else lurking around here) would solve this?
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:We usually let people work out problems themselves but I think it might help you understand where you're going wrong if you see how other people would solve this. Would you like to keep trying to get your code to work or would you like to see how Piet and/or I (or others, if there's anyone else lurking around here) would solve this?



My idea is to learn and seek guidance when am stack trying to solve a problem
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bedtime in Holland! Goodnight and I'm very curious to see where we are tomorrow morning.
 
Junilu Lacar
Marshal
Posts: 14501
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kelvin Okornoe wrote:My idea is to learn and seek guidance when am stack trying to solve a problem


Your perseverance is certainly commendable.

Once you get your program to work, I think it would still be a good learning exercise to see other solutions. This is the approach taken by the authors of the book, "Java by Comparison," who were college professors who wanted to show their students how the programming problems they were given as homework would be solved by professional developers. This is something I do as well when I'm trying to learn a new language; I write my solution and then compare what I did to what other more experienced developers in that language would do. It's quite informative.
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Bedtime in Holland! Goodnight and I'm very curious to see where we are tomorrow morning.



Thank you soo much Piet, I do appreciate your time and effort....

Still working on it, am also in Ghana
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Kelvin Okornoe wrote:My idea is to learn and seek guidance when am stack trying to solve a problem


Your perseverance is certainly commendable.

Once you get your program to work, I think it would still be a good learning exercise to see other solutions. This is the approach taken by the authors of the book, "Java by Comparison," who were college professors who wanted to show their students how the programming problems they were given as homework would be solved by professional developers. This is something I do as well when I'm trying to learn a new language; I write my solution and then compare what I did to what other more experienced developers in that language would do. It's quite informative.



Thank you Junilu for your time and words of encouragement, I will continue working hard...
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Kelvin,

I overlooked a very nasty error, that is very easy to make, and that is the reason why your method returns -1.  If x is an int[], then Arrays.asList(x) does NOT create a List of the elements of x. Do you use an IDE? If so, do this at the end of the method:
I use the java 10 'var' here, but hover the mouse over 'var' and let the IDE change it to the real type. What do you notice?



I have implemented your code, but still the same issue, x returns an object in memory, fastest returns the fastest time and i returns -1

Am thinking what could be wrong?
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Kelvin,

I overlooked a very nasty error, that is very easy to make, and that is the reason why your method returns -1.  If x is an int[], then Arrays.asList(x) does NOT create a List of the elements of x. Do you use an IDE? If so, do this at the end of the method:
I use the java 10 'var' here, but hover the mouse over 'var' and let the IDE change it to the real type. What do you notice?



I just checks the documentation, x returns a List of Integer array.... var is the same as List<Integer[]>,
that was the reason why I converted the int[] time to Integer[] time...
 
Piet Souris
Bartender
Posts: 3668
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, and that is why the method always returns -1. It is looking for an Integer in a List of Integer[]. And since an Integer never equals an Integer[], you get that -1.

So, as I said earlier, the 'return Arrays.asList(time).indexOf(fastest) doesn't work. Now, lets for the time being maintain the idea of turning the time array into a List and find the index of fastest. Can you come up with a way to properly convert the time array to a List<Integer>?
 
Kelvin Okornoe
Ranch Hand
Posts: 98
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Exactly, and that is why the method always returns -1. It is looking for an Integer in a List of Integer[]. And since an Integer never equals an Integer[], you get that -1.

So, as I said earlier, the 'return Arrays.asList(time).indexOf(fastest) doesn't work. Now, lets for the time being maintain the idea of turning the time array into a List and find the index of fastest. Can you come up with a way to properly convert the time array to a List<Integer>?



Am thinking of declaring an array of type Integer and copy the items from int[] times into it
 
Marshal
Posts: 66575
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would work, but why do you want a List in the first place?
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!