Louis Müller

Greenhorn
+ Follow
since Feb 04, 2019
Louis likes ...
IntelliJ IDE Java
Studied chemistry. Learning Java for fun. German. Knee deep into dancing and all that is acrobatic, mostly Breakdance Hip Hop and Parkour. I play guitar and croak to it. Liberal but not all too happy with some of the current developments.
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
1
Total given
23
Given in last 30 days
23
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Louis Müller

Now, here comes the code..



2 days ago
Alright. I finally got all them sweet points from the assignment. They are only virtual, but the satisfaction is real.

However there are still a few things left for me to understand. (I added the whole code down below even though I assume you wont be keen on reading it. It wont be necessary to answer my questions.)


1.
Since I had to find a way to deal with the case of the same product number occuring twice in the input. I included this condition in my ReadInputGetPrice class


I'm not happy having all that code in that class. I'd much prefer to create a separate method getIndex to include the for loop. But I couldnt figure out how to access the namesAndPrices Arraylist from outside of the method. Is it because Im using it in the following for loop? I thought it would be defined as public by default and therefore be accessible.

2.

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.  


In contrary to using a equals method based on the == operator instead of the compare(primitive type) method ?

3.
I didnt need the equals method at all. This is not to say that you gave bad advice. I assume this method usually is extremely helpful, only in this assignment for noobs, it wasn't needed. We agreed that one would use the equals method to safe the program some unnecessary work. But I feel like in this assignment, due to the way the problem is articulated and the kind of problem we're dealing with I think there cannot be the same object twice. Also I dont think there can be an object other than the ProductsAndSellingPrices (former Product) object.

4.

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.



This keeps bothering me because I think you are wrong in this case. Considering my imense lack of knowledge, this is a bold assumption and I'm not about to insist on that But, after researching on natural ordering I came to the conclusion that my products do have a one. The are ordered by the productNumber. Thats what I wanted to achieve the whole time. Since I dont want to order my list based on any other property than on the product number I think comparable is the best option. 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."




Campbell Ritchie wrote:λ


I will look into this (and your last post in general) tomorow because I'm too worn out right now to grasp yet another concept. I tried to understand λ already when trying to figure out streams but failed (with both). I will try it tomorrow but I might skip it if I'll face the same confusion as last time. In this case I will just wait till I feel comfortable with all those classes and objects before tackling the lambdas again. (I prefer to primarily associate them with wavelenght for a while too)


Finally:

Hope you're enjoying the ride. :-)


Hell Yeah I did. I had the best time these days learning all of that with your help. This forum is great, you two are great. No need to apologise for any mistakes, as long as you correct them there is even more for me to learn !!!


2 days ago


I wrote my current problem in the comments. The reading helped a lot but I think apart from this I'm slowly seeing through this.
3 days ago
I will read a chapter in "think Java" and maybe "programming java" about this and then come back. No need for you to explain it all to me. After all, this is not a college. Safe yourself some time, I'll be back.
5 days ago
Alright. You've been giving me a big field to graze on if I may : D

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.


This is correct and comprehensible. But what would that mean for my (old) code? It will return a 0 to the sort method and next? Also I took the task for something like the final calculation after a year of selling product 1,2,3,4,5 therefore I assumed the same product number not to occur twice. But I submitted it at the universitys-page and got an error in both test cases even tho the given examples worked fine in IntelliJ. So probably, I was mistaken.

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.


So the first test is just to safe my programm some work is that correct? Just to make it work faster in the case of two identical Objects? I'm a bit confused about the difference between object and Product. As far as I know now, the equals method checks if two objects have the same "memory space"? In other words whether I've used only one keyword "new" to invoke both of them. Is that correct?
I shamefully admit that the rest, also, is not obvious.

First we checked whether or not the objects are equal. Then whether they are not of the same type. If the first of those checks is false and the second true (doesnt return false) then we proceed to assigning product that to o? Where is product that coming from and why is it product o? Then we're returning the value of productNumber this & that which are the same as we've just figured out. Same with money. 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 !

@Louis - Does yours?  



Yes I do think they have an identifying name!
product 1, $2.98;
product 2, $4.50;
product 3, $9.98;
product 4, $4.49;
product 5, $6.87.

they are called product 1,2,3,4,5.




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.


the last five lines of which can be reduced to:
return diff > 0.0d ? 1 : diff < 0.0d ? -1 : 0;  



Yesssss I know this one : D

Well yeah I might still need some time with this problem !


5 days ago
First of all: the improved version of my code so far. I hope this will make it easier to talk about it.



5 days ago
Alright!

First of all 2 comments concerning the following monolog:
I) I would've changed the class name "yoyo" but I was worried it would cause me trouble. Be assured that I will choose better names for all of my future classes.
II) I will also write another version using Scanner. Scanner is actually how I got introduced to Java by a friend but I couldnt figure out how to use it in this situation. I will but that is not what this thread is (or became) about.
III) Since nobody will check whether I have completed this assignment or not, all I care about is to get the sweet old knowledge out of this. Therefore I wrote a summary of what I did. I'd love if one of you would give it a glimpse to check if I'm about to get wrong information in my brain.

Summary
1. I made the class "Product" this class consists of the integer productNumber and the double money. I used a Constructor in which I used the keyword this to make clear which one of the variables is the one of the Object accessing it.

2. In the Product class implemented is the Comparable Interface. Together with a new (through @Override) compareTo (Product o) method, this enables me to compare 2 Objects of the type Product based on their ID (In my case this is the productNumber). I could verify this by creating 2 Objects of the type Product and use the compareTo method on them. It worked.

3. In the main method, I created an Array of Product-objects. I asigned the values of productNumber & money to it. I got money through using a Switch statement.

4. I used Arrays.sort to sort my Array of Objects. the sort method used my own compareTo method to figure out how to sort my objects. Since i told it to use productNumber of the two Objects in comparision it did exactly that. When printing it out, even tho I'm using toString I still get the output I'd usually get if I wouldnt use the toString method. Do you know why?


Open Questions:

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?
b) What is the purpose of getMoney() method in my Product-class ? Again Am I right to think that I would only need this if I were to sort my Array by he double value?
c) I'm not entirely sure about this:
Product is the Object that is subject to the comparision is that right? And o? It is new to me that I don't have to declare "o" before using it. Is it because this is just part of the method and I dont have to understand it ? So just accept it as a fact like the if statement?
d) Winston proposed
I will try and do this. Are there anymore advises on how I could make my code more efficient or learn more from this example?

Here is my Code

1 week ago

Campbell Ritchie wrote:
The bad news is, your assignment appears only to test your knowledge of switch statements.


I have to think about this but you might be right. Anyways, I found those problems on the webpage of a danish university. Since I only do them for learning java I'm going to stick to my complicated way of solving this. Out of curiosity, stubborness and lack of ideas how to solve it using only the switch statement. I came up with all of this because as output, I have to print out how much money I've earned with each product in increasing order. I couldnt come up with an easier solution. If I'd only have to print them out I think there would be no problem.

Didn't I give you a Java™ Tutorials link yesterday? Have you read it?


I opened it and then forgot it between the many tabs i had opened. Sorry for that I will educate myself and then come back.
1 week ago
Thanks for all the advice. I advanced a bit in my code. Before your explanations, I somehow thought I'd have to create an Object that behaves like an Array. Now I (think I) understand that I have to create an object and then put a number of those objects in an Array. Way easier to handle I guess.
So now I'm dealing with the problem of sorting my Product.
This is what I understood so far:
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. I now have to write an implemantation to tell the sort() method that I will use later by what it should sort ! Is that correct?
In order to do this I wrote the following code:



From the extremely high level of not-understanding this code you'll be probably able to tell that I didnt come up with this myself. I found a example and tried to adjust my own project to it.

I have yet to try if this works but I try to follow your advice and break it into tasks and Im not really sure how to test my Product without writing the rest of the code.
But even if it were to work, I'd still ask the following questions:

1. Will the sort method I'll use later just use the compareTo on all (or better: on as many as necessary to figure out the order) my objects?

To my compareTo method:
I thought this would return 0 for the objects beeing equal on the attribute I'm investigating them. If that is right:
2. what is null ? What I understood is that it kind of means that there is no reference to an object. That doesnt make sense to me. Why do they both have to be null in order to return null? Since getProductNumber will the method the product number I thought this would compare the integer productNumber.
Same question applies on the other 2 if's as well. When this.productNumber represents the productNumber of the object the method is currently "talking about", why does it have to be "null"? I would have expected something like Object m > this.Object?
3. I thought with the 3 if's I would have covered all the cases. Why is there suddenly a compareTo method in my own compareTo method : D

God damnit I hope you can fight your way through this jungle.
1 week ago

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.



This is where I dont know what to do. How can I put them into an array? I think I already created an object that encapsulates the data didnt I?
I tried this:

halfSize is half the size of my list of Inputs.
but it doesnt work. Could you tell me how I can put the data in my object into an array? Or am I generally barking up the wrong tree here ?

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  



This I have to figure out. I still dont really know how my array will look in the end. Will it be an array of objects? More specific of an Object of my class (Which I now renamed project!)

So far I have not tested my Product class.

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.


Since this is the first Object I ever created I dont really know whats going on in Product. I was hoping I could use it to get my program work and understand it fully when seeing it work.
My yoyo class (the name is stupid, no doubt) so far takes the Input and seperates it so I can extract the productNumber and quantity from it.
The Input looks (for example like this):

1 5  
5 10  
2 0  
0

The first number each line ist the product number, whilst the second represents the quantity of sold items.
I hope this clarifies it. Also since I just started Java a few months ago 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.

Why have you got a no‑arguments constructor?  


I made this because i thought I'd need to initialize an Object without parameters first.

I know this is a mess : D
1 week ago
Hi,

I have a number of doubles (money (calculated by product price * quantity)) and integers (the product number ) which I want to save in a list. After saving them I want to sort them in a way that the integer-list is in ascending order and the corresponding double is still at the right place. This might look like this:
productnumber       money                                       and after sorting like this    productnumber   money

4                           3.8                                                                                    1                     4.6                    
1                           4.6                                                                                    2                      5.2
2                            5.2                                                                                   4                       3.8

First I wanted to use two Arrays and somehow reflect the changes on the indexes of the integer list on the double list
My research revealed this to be a stupid idea:
https://stackoverflow.com/questions/12824423/sort-array-and-reflect-the-changes-in-another-array

next i thought about making a mixed 2 D array.
but this post
https://stackoverflow.com/questions/5809486/java-easiest-way-to-store-mixed-data-types-in-a-multidimensional-array
told me it would be smarter to make my own object and "make it a single dimensional array".
What a chance to learn about objects! After countless youtube tutorials I now understand the concept of classes, constructors and Objects. But I cant seem to get the last bit working. Here is my Class:



In my main method I tried differrent ways of getting my object to be an array. I also tried to declare an array in my instance variables in my class "MyOwnList". It failed because I could not use one of the variables (int size) of my main code which I would need to tell Java in MyOwnList how long my Array should be.
here is the rest of my (still unfinished) code.




Just in case you are interested: Here is the task I want to solve:

A mail-order house sells five products whose retail prices are as follows:

product 1, $2.98;
product 2, $4.50;
product 3, $9.98;
product 4, $4.49;
product 5, $6.87.
Write an application that reads a series of pairs of numbers as follows: <product number> <quantity sold>. Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to stop looping when reading 0 as product number and display the final results.


Thanks in advance !
Louis

1 week ago
Alright!
I will just use Sublime and open the file with my browser to check if it all works. Thanks again, this has been a pleasure.
1 week ago
PHP
Thank you!
I think I'm going to use Sublime then. It's just that the Ebook im using (Learning PHP MySQL & JavaScript) advised me to use an IDE. But I've already wasted too much time I could've spent learning things with getting frustrated : D.
I started learning Java a 2 months ago and I really like it. Nevertheless I want to learn how to design websites and it bothered me to only use Wordpress and not understand the underlying techniques. Thats why I want to add PHP CSS to my budding coding skills.
I really like the tone of this Forum, thanks again.
1 week ago
PHP
Hello fellow and former greenhorns.
I tried Eclipse but when I tried to run my hello world program, i got the error "Selection cannot be run on any server".
I tried NETBEANS but even after repeated installations, there was no BIN folder where and I wasnt able to find the netbeans.exe file
Now I tried PHP Storm but it also didnt work.
I bought a nice ebook about PHP and I would love to do the exercises. I spent 2 days working on this and Im frustrated. I would greatly appreciate any help or useful links. Obviously I dont understand the whole concept of this IDE/PHP/Server connection.
Thanks in advance
1 week ago
PHP