Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!

Junilu Lacar

+ Follow
since Feb 26, 2001
Junilu likes ...
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
Columbus OH
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt Green check
expand Ranch Hand Scavenger Hunt Green check
expand Greenhorn Scavenger Hunt

Recent posts by Junilu Lacar

Kory Perry wrote:
The output is:
Inventory: Wrath Flight
Gold: 500

I set the gold to 300 just to test the setter() method.  I expected the output to be:
Inventory: Wrath Flight
Gold: 300

My setter() method doesn't remain after I call the amazonInventory() method.  The gold just goes back to 500.

These are the relevant lines that cause the behavior you're seeing:

If you can reconcile that code with where you think you're setting the value of gold to 300, then you'll understand where your problem is.
8 hours ago

Marti Nob wrote:Question : The amount of money dropped into the machine by the customer will always be equal to or greater
than the cost of the item. ?  Can anyone help me to the above question

That's not a question, that's an assumption that was given with the problem. Are you not sure what it means?

If you're not sure what that means, all it is saying is that if you have an item with a price of 100, the cash amount given is guaranteed to be at least 100, and possibly more. That is, the cash given will never be less than the price of the item being paid for.
1 day ago
Bumping to top so our authors can see it.

Junilu Lacar wrote:Ok, I did that and got it working:

And I compulsively composed the method:

2 days ago
And my toString() is very similar:
2 days ago

Carey Brown wrote:As Les mentioned, if head and tail are Node's instead of Node references the above method works and is very succinct.

Ok, I did that and got it working:
2 days ago
Here's the insert() method I came up with:

I have it returning a reference to the current list because that allows chaining of multiple calls to insert(), like so:
3 days ago
This part of your insert method has a number of flaws:

The break statements on lines 41, 49, and 57 are wrong. Since these three if-statements cover every possible outcome of compareTo(), you end up comparing the new node to just the head node. You never actually go through the list.

Another thing that's wrong is line 59 - you're clobbering the head node. You shouldn't change the value of head unless you're inserting the new node before the current head node. All your other list operation methods use a current variable, why did you get away from that in your insert() method?

Line 56 is also redundant for the same reason I pointed out in my last reply: it's already done by the Node constructor.
4 days ago

William Golovlev wrote:

Al Hobbs wrote:minimize repeating lines. ("DRY") This increases readability and makes it easier to fix things.

What specifically are you referring to? If it is something outside of the insert method, I am not worrying about that as of now.

DRY is not just about minimizing mechanical character for character duplication; it's about avoiding logical duplication. Here's an example:

Lines 29 and 30 are redundant because you have this constructor:

Line 29 does what line 11 already did and line 30 does what line 13 already did.

And technically, lines 12 and 13 are redundant because reference fields are already initialized to null by default.
4 days ago

brian leung wrote:When I run my program, it doesn't show the output. I'm not surprised. Would you be able to point me in the direction of my mistake?

The biggest problem is that what you've written is NOT even remotely close to being an implementation of the bubble sort algorithm.

Here's what Wikipedia has:

Notice that there's only one loop, not two nested loops. Note the swapped variable, which you don't have. Also note that the pseudocode assumes an array data structure and as I said, your use of an ArrayList instead just complicates things for yourself since you're confused by the List API.

EDIT: restoring deleted part to add context to Carey's response below.
5 days ago

Campbell Ritchie wrote:Also the sorted() method probably uses merge sort (or a special kind of merge sort), so it wouldn't use bubble sort at all.

I followed the white rabbit and ended up in Arrays.sort() which, at least in the Java source code I looked at, makes a call to DualPivotQuickSort, whatever that is.
5 days ago

brian leung wrote:i'm a bit lost with the streaming input/output.

Ignore that stuff if it confuses you then. All that's about is getting the values to sort collected in a List. You can do that just the way you originally showed it. People around here are just a little enamored with streams right now so that's why you're seeing stream-based suggestions for how to do things.

But if your objective was to just sort the values, you could easily do that by adding sorted() to what I suggested earlier:

But simply sorting the values is not the objective; specifically, you want to implement the bubble sort algorithm so doing the above is cheating.
5 days ago

brian leung wrote:
I've tried to read through the remaining posts on this thread, however i'm a bit lost with the streaming input/output. I'm simply trying to write a program where I can sort an arraylist of whatever size and elements of my choosing.

The problems are:

1. You are confused about how to use the List API - you are, in my opinion, making things unnecessarily harder for yourself by using an ArrayList instead of just a plain old array of int. The get() method is used to read a value from the ArrayList, the set() method is used to save a value in the ArrayList. Somehow, you're confused as to when to use one or the other.

2. As I said before, since you are prompting for the number of elements to sort, you can use an int[] of the appropriate size and do simple assignment with the = operator and comparisons with the == operator. You are also somehow confused as to when to use equals() as evidenced by this line:

This line of code has no effect whatsoever in your program, basically a do-nothing statement, but you seem to think it assigns the value of temp to temp1. It doesn't.
5 days ago
Here's my take on why many of the problems we're seeing take the first input as the size of the dataset.

The solutions are tested programmatically and input redirected from some static source through and output redirected to another file, something like this:

This is why using and System.out still works even when the checking is done programmatically.

The first line in the data file will be the size of the data set, for example:

$ cat bubblesort-data.txt
11 12 13 14 15
6 days ago
1. Many of the problem statements require the program to accept the number of elements to sort as the first input, so ignoring that may not be an option.
2. Assuming #1 is true, then you don't need to use a List. You already know how many elements, so you can just straight up use an array.
3. Also assuming #1, you can use limit() on the Scanner token stream and then just use mapToInt() and toArray()

Edited: IntStream.toArray() returns an int[] not an Integer[]

You can even cheat a little with a prompt:

Lines 6 and 8 is the cheat in the above code. A little clunky but it works if you don't mind the extra prompt at the end.

6 days ago