Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Campbell Ritchie wrote:They have given you some good advice on SO. Parallel arrays are awkward to implement and error‑prone; you are far better creating an object that encapsulates the data you want to anaylse. Then you can put them into an array.
Campbell Ritchie wrote:Another thing: you will have to work out how to sort the array. Start by deciding what you are going to sort by. I don't think your products have a natural ordering, otherwise yo uwould make them implement
Only when you have got Product working should you write any other code. I can't understand your yoyo class; I think you may end up getting rid of all that code.
Why have you got a no‑arguments constructor?
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Well, that bit looks correct apart from the fact that you haven't explained about halving the size. Make a count of how many object there will be, not how many keyboard inputs you need.Louis Müller wrote:. . .
halfSize is half the size of my list of Inputs.
You don't put the data into the array; you put the objects into the array. You are going to have to find some way to write this sort of thing:-or you can shorten that to:-I suggest you should get the array full, with no empty spaces, otherwise you will have trouble with nulls later on.
but it doesnt work. Could you tell me how I can put the data in my object into an array? . . .
Yes, it will ba an array of objects of your Product class. Test that class to make sure it is working before you try any sorting.. . .
This I have to figure out. . . . Will it be an array of objects? More specific of an Object of my class (Which I now renamed project!)Campbell Ritchie wrote:Another thing: you will have to work out how to sort the array. . . .
So far I have not tested my Product class. . . .
You don't want to figure out what is going on. You want to decide what you are going to do.. . . I think I dont have developed the perfect approach on problems. I usually start with getting the input in the main class and from there on I try to figure out whats going on.
That will create a product object with the values (0, 0.0), which you probably don't want. I suggest you should delete that constructor altogether.I made this because i thought I'd need to initialize an Object without parameters first.Why have you got a no‑arguments constructor?
I think that is because you are trying to do too many things simultaneously; you will find it much easier if you sort out one problem at a time. That is why I haven't said much about the yoyo class; I am leaving that till later.I know this is a mess : D
You're doing fine; just taking on an awful lot at once. And don't worry, it's quite normal when you're new to it all.Louis Müller wrote:I know this is a mess : D
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Good advice as usual, but I would probably create the Product objects with numbers in the code first, and only try getting those numbers from the keyboard later.Winston Gutkowski wrote:. . .
How do I get an int/double from the keyboard? How do I create a Product? . . .
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
That sounds correctLouis Müller wrote:. . . . Now I (think I) understand that I have to create an object and then put a number of those objects in an Array. . . .
In that case it should be Comparable<Product>. I thought compareTo() was the only method in that interface; have they added anything?I have to implement the Comparable Interface in my Product class. This enables me to use certain methods of this interface. In my case, the compareTo method is what I will need. . . .
Careful. You don't know how good the example you found is.. . . . I found a example and tried to adjust my own project to it.
Yes, that's right.. . . my compareTo method:
. . . would return 0 for the objects beeing equal . . .
Yes, it means there is no reference to an object; The compareTo() method doesn't have to acept nulls. If you pass null, it will throw an exception, which I think from my reading of compareTo() is appropriate behaviour.2. what is null ? What I understood is that it kind of means that there is no reference to an object. . . .
Careful about the language please.God damnit . . .
Campbell Ritchie wrote:
The bad news is, your assignment appears only to test your knowledge of switch statements.
Didn't I give you a Java™ Tutorials link yesterday? Have you read it?
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
The first part looks goodLouis Müller wrote:. . . the output I'd usually get if I wouldnt use the toString method. Do you know why?
Yes and no. If you use a version of Java ≤ 7, straight yes. In Java8, they introduced this method.. . . a) If I would've used an ArrayList instead of an Array of objects, am I right to assume that I would have to use Collections.sort in that case?
It allows other code to find out the price. As we said before, it is probably better to say price.b) What is the purpose of getMoney() method in my Product-class ? . . .
You need two things to compare; the other thing you are comparing is called o in that bit of code. When you wrote Product there, that was specifying the type of o. The object you are comparing to o is whichever object would be “this” inside that method.c) I'm not entirely sure about this: Product is the Object that is subject to the comparision is that right? And o?
But you have declared it. If you declare o as a parameter of the method, that counts as a declaration.It is new to me that I don't have to declare "o" before using it. . . .
What Winston showed you is actually much cleverer than a constructor; it is a static factory method. I shall let you look it up. Try in Effective Java by Joshua Bloch somewhere in chapter 1.d) Winston proposed
4. Why not add a "constructor" to Product that takes a BufferedReader? That way all your 'creation' logic is contained in in the Product class, . . .
You don't need n=...; anywhere in that switch, nor do you need the money and re local variables. You can write much neater code if you omit those things. Also find out the difference between double and Double.Louis Müller wrote:. . .
Here is my Code
Louis Müller wrote:Here is my Code
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
True, but in the real world "products" usually have an identifying 'name'.Campbell Ritchie wrote:I am still dubious that Product in this case has a natural ordering.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Not yet he hasn't.Winston Gutkowski wrote:. . . in the real world "products" usually have an identifying 'name'.
@Louis - Does yours?
Winston
This is probably easiest to follow:Winston Gutkowski wrote:Will post the correct solution later in a couple of hours.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:
By the way: Java® only supports 1D arrays; what appear to be 2D arrays are really arrays of arrays.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Yes.Campbell Ritchie wrote:...a talk from Devoxx where they recommend you use...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Right now, you can create two Products with the same number, but different prices, but they will compare as equal, simply because their numbers are the same.
and those first two checks are important - especially the second one. And they're needed because equals() takes an Object, not a Product. The first one simply says, 'if o is the same object as me, we must be equal', and bypasses a lot of unnecessary logic. The second one says 'if o is not the same type as me, we cannot be equal', and is absolutely required. The rest is hopefully obvious.
@Louis - Does yours?
the last five lines of which can be reduced to:
return diff > 0.0d ? 1 : diff < 0.0d ? -1 : 0;
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
And you're absolutely right, it is. The signature should have been:Louis Müller wrote:The compareTo method made way more sense to me. But then you corrected it and now it is of return type boolean. I really thought it would be int.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hope you're enjoying the ride. :-)Louis Müller wrote:Alright. You've been giving me a big field to graze on if I may : D
Correct.Louis Müller wrote:So the first test is just to make it work faster in the case of two identical Objects is that correct?
Object with a capital O.Louis Müller wrote:I'm a bit confused about the difference between object and Product.
That's what it does if you don't override it, but it's not what the method is there for.Louis Müller wrote:As far as I know now, the equals method checks if two objects have the same "memory space"?
I don't think you quite understand the idea of an 'order'.Louis Müller wrote:For me this doesnt make sense. If the product number is the same shouldnt we add this.money += that.money in this manner? because in this case we sold more of product number x !
Hmmm. Doesn't mean much does it?Louis Müller wrote:Yes I do think they have an identifying name!
product 1...
Yes, and I really apologise for that. Hopefully I've cleared things up now.Louis Müller wrote:The compareTo method made way more sense to me. But then you corrected it...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Louis Müller wrote:why am I using compare? I thought this is a method of Comparator.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Those methods are designed to get around the problem that using subtraction can cause an overflow error, not to mention that the value NaN (only seen with floating‑point numbers) has some weird behaviour if you use the != == < <= > and >= operators on it. Using those compare() methods gets around those problems.Winston Gutkowski wrote:. . . And all the Number classes have similar methods for their own primitive type.
I suspect they used that name to indicate that the method is "Comparator-like". . . .
It also says that thenComparingDouble works similarly.If this Comparator considers two elements equal, i.e. compare(a, b) == 0, other is used to determine the order.
Winston Gutkowski wrote:and the reason for doing it that way is that then both methods use the same "comparison" code, and therefore must be consistent.
Campbell Ritchie wrote:I am still dubious that Product in this case has a natural ordering. You can order products by price or by ID, so they don't have one natural ordering.
Winston Gutkowski wrote:I don't think you quite understand the idea of an 'order'.
Basically, what compareTo() (or, as Campbell suggested, a Comparator) does is to provide a way of deciding whether one object is "greater than", "less than", or "equal to" another object of the same type. And you do it so that you can sort them in a predictable way.
And usually - not always, but most of the time - it will be closely related to equals(), and involve comparing the same fields.
This is called "making comparison consistent with equals()", and is usually a good thing.
"I would say that an object should implement Comparable if that is the clear natural way to sort the class, and anyone would need to sort the class would generally want to do it that way.
If, however, the sorting was an unusual use of the class, or the sorting only makes sense for a specific use case, then a Comparator is a better option."
Campbell Ritchie wrote:λ
Hope you're enjoying the ride. :-)
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
Yes, if you want to make the two methods consistent.Louis Müller wrote:
In contrary to using a equals method based on the == operator instead of the compare(primitive type) method ?Winston Gutkowski wrote:and the reason for doing it that way is that then both methods use the same "comparison" code, and therefore must be consistent.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Hey, it's your project and you probably know best. And if it IS the case, then I suspect that maybe you don't want the two methods to use the same criteria.Louis Müller wrote:This keeps bothering me because I think you are wrong in this case. ... To back my opinion let me quote Stackoverflow:
"I would say that an object should implement Comparable if that is the clear natural way to sort the class, and anyone would need to sort the class would generally want to do it that way.
If, however, the sorting was an unusual use of the class, or the sorting only makes sense for a specific use case, then a Comparator is a better option."
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Das Filet, wenn der Fleischvergleich erlaubt ist, ist am besten halb durchgebraten.
You would do better to say what you don't understand; maybe somebody else's explanation will be clearer.Louis Müller wrote:I didnt understand all that you have said . . . see you in the next thread!
Don't get me started about those stupid light bulbs. |