Dawid Smith

Ranch Hand
+ Follow
since Dec 17, 2018
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 Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dawid Smith

If I understand correctly, on line 46 you are trying to create an array of Employee type of object, however I don't see Employee class being declared anywhere so you can't create an instance of it.
1 day ago
Hello Debra. I think the problem is that you are trying to declare a method inside a method - inputinfo() inside the main() to be precise. In general, your approach to main method seems wrong to me. There is too much code in there. Instead, consider creating more classes which will contain methods and then create instance of those classes in main() and call your methods. I think it will be very beneficial for you to read this : https://coderanch.com/wiki/660020/Main-Pain

Please note, I am not an expert here but definitely check out the link I gave you. The article is written by someone experienced and it's about problems you are having.
1 day ago

Campbell Ritchie wrote:

Dawid Smith wrote:. . . But.. where would the fun be in that? . . .

A $40,000 starting salary?

Oh, that Still, there is so much to learn I haven't even started thinking about salaries yet
1 day ago

Liutauras Vilda wrote:@OP

Very nicely indented and formatted code. Have a cow for disciplined approach on that.

Thank you. I am trying to make it easy to read.

Campbell Ritchie wrote:Once you have passed all your exams and got a job, you will sort your array like this

But.. where would the fun be in that?

I get your point though, there are ready-made solutions for common problems.
2 days ago

Winston Gutkowski wrote:

Dawid Smith wrote:Still, what do you think about writing your own version just for practice?

Nothing wrong with it at all; just don't expect too much in the way of enlightenment unless you repeat the process at least 10,000 times, or work with arrays involving millions of objects, which probably isn't very practical.

I am not sure if I understand your point. Do you mean that getting better by writing your own code takes crazy amount of time?

Winston Gutkowski wrote:I'm with Junilu - right now, you might get more mileage out of looking at sorting algorithms and working out why you might use them; but don't ignore the simple ones simply because they're supposedly "inefficient" (O(n²) or worse).

Thank you for the advice. Right now, my studying is heavily skewed towards writing my own stuff instead of looking up what's already out there. Maybe that's a mistake.
2 days ago

Junilu Lacar wrote:If it's your first try, then it's a pretty good try. This algorithm, however, is quite inefficient. If I'm not mistaken, it is best case and worst case O(n²) which means that for large arrays, it takes a very long time, even if it's already sorted. You're basically looking through the entire list NxN times to find the largest unsorted element, including the elements that you've already looked at before and determined to be larger than all others.

Sorting algorithms are a basic area of study in computer science so I suggest you take a look at the various ones already existing and commonly used and compare them with yours to see what kind of improvements you might make.

Now I can see the efficiancy problem with my method. Not that I expected it to be anywhere close in terms of performance to already existing solutions. Thank you
2 days ago

Junilu Lacar wrote:Instead of writing your own printer() method, you could just use java.util.Arrays.toString() to display an int[]. To display nested arrays, you can use Arrays.deepToString()

Thank you for the suggestion. I didn't know that such methods exist. Still, what do you think about writing your own version just for practice? I imagine it seems supereasy for someone experienced but I had to think how to print those results
4 days ago

Carey Brown wrote:It would be interesting to see some performance numbers for very large arrays comparing your method to a simple bubble sort.
Your method has these nested loops
A bubble sort has these
Note the inner loop gets shorter and shorter as the outer loop progresses.

How would you go about comparing performances of 2 methods?
4 days ago
Hello. I've given myself a task to come up with a way to sort an array of integers. It seems to be working fine but I would love to hear some feedback on it from you guys.

The solution works in the following way :
The nested loop compares each value of the array to every other value of the array.  It skips the comparison between values of the same index. There is a counter declared at the beginning of the nested loop which will increase every time a value being compared is found to be either smaller or equal to the other value.The value of the counter is then used to determine a relative position of the tested value and the value is assigned to an appriopriate index position to the newly created array. So, for example, if after all the comparisons the counter remains 0, then we know that the value being tested is the biggest one and it goes at the last index position of the newly created array.

Aside of a sorting method, there is also a printer method for testing purposes. It prints all the values of the arrays after the sorting is done. Other than that the methods can't handle exceptions and printer() could print the data in a nicer way but I think it would be best to try getting some feedback before putting more work into it.

Here is the code :

4 days ago

Liutauras Vilda wrote:Dawid Smith,

CowGratulations, your topic has been published in our CodeRanch Journal August Edition.

A copy of journal you could find here -> https://coderanch.com/wiki/715405/CodeRanch-Journal-August
Other than that, you, as everybody else supposed to get one emailed to you

That's good to hear. The responses in the topic proved to be valuable resource for my studying. I hope other people will find it beneficial as well.
5 days ago

Winston Gutkowski wrote:

Dawid Smith wrote:Hello. I've attempted to code a recursive version of a binary search algorithm. It seems to be working, but I would love to hear feedback from you guys.

Oddly enough, your program contains the same mistake as almost all binary chops ever written, so don't worry, you're in good company. :-)

The problem is with the expression 'half = (a + z) / 2'.
It may look right, but it contains a dangerous flaw, which you'll likely never reproduce because all the lists you deal with will be too small.

The reason is that it will not always produce the correct result because of integer overflow.
If the sum of a and z is >= 2^31, the result will be a negative number, which the compiler will happily divide by 2, and screw up your lovely calculation.

Luckily, there is more than 1 solution:
1. half = a + ((z - a) / 2) - hopefully fairly obvious.
2. half = (a + z) >>> 1 - as fast as it gets, and guaranteed to work provided a and z are >= 0, but a bit "mystical", and not directly translatable to other languages.

For more info check out this.

Thank you for your input. I've read the blog you linked and I found it very interesting. Since the int range is quite large, I wouldn't even think that it may not be enough. Solution #1 you presented make sense. What about just using long instead of int though? Wouldn't that solve the problem as well?

Regarding implementation of binary search, I've found surprising story that comes from Programming Pearls by Jon Louis Bentley. To cut it short, back in the day, Bentley did an experiment which challenged professional programers at Bell Labs and IBM to implement bug-free binary search. The result was that only 10% of the participants succeded. You can read more about it here : https://reprog.wordpress.com/2010/04/19/are-you-one-of-the-10-percent/    
I am very curious as to what was meant by bug-free implementation. If most participants of the experiment made the same mistake as I did(potential for Integer overflow)then it isn't that interesting since as far as I understand, to reproduce this bug you need very large databases and they hadn't had them in the past. Therefore, it makes sense that they wouldn't have considered a problem like that. However, if so many participants failed the task because of some other logical mistakes, then it would be very surprising to me.

1 month ago

Stephan van Hulst wrote:You've already heard good suggestions from my colleagues. I'd like to add that method/class names should not contains hints about their implementation. It doesn't matter whether the search is performed through recursion or by looking in a crystal ball, the important part is that it searches. This restriction does not hold for private methods, as private methods are used solely for implementation.

Validate inputs to non-private methods.

Avoid reassigning values to method parameters, especially if the method is a "pure function" (a static method whose outcome depends only on the arguments).

Perform early returns to avoid the "arrow anti-pattern", where large code blocks are scoped deeply.

Finally, don't use magic values for special results. Use an appropriate type instead. In this particular case, I would use OptionalInt as the return type:

I must admit  that this is code is quite overwhelming for me now. I need to get familiar with throwing exceptions,var type,OptionalInt class and arrow anti-pattern to try to grasp it. As of now though, I would like to address 2 things. Firstly, there are lots of detailed comments in your code. Is this how professionally written code should look like or you did it to help beginner programmers understand what is going on there?
Secondly, by saying "don't use magical values for special results" I assume you refer to my code returning -1 if the desired value is not found. Is my approach inherently bad there? Anyways, thank you very much for such a detailed answer. I need to study it more.
1 month ago

Liutauras Vilda wrote:

Dawid Smith wrote:

I am pleasantly surprised you wrote the comment for private constructor to clarify your intention to your code reader - very well done on that! However, comment is slightly imprecise. Isn't really clear what you mean by empty, whether no parameters specified, or body empty - in anyway, non of these are the reasons which prevent object from instantiation. What prevents is its privatness. So comment probably better would read as: "intentional private constructor to prevent class's instantiation" or similar along those lines.

So you renamed your class. Alright. To ArraysSortingMethods. >? Why? I see one method inside, which performs search within the array. What does it have to do with sorting? If your plans are to have also function(s) which sort array, then give to class some broader name, which covers both. In fact Stephan already suggested seem to be not a bad name for your needs.

Again. You have variables: arr, ar. First they are some sort of abbreviations (personally to me, abbreviations are most annoying things, but that's not the reason to improve). Secondly, they describe implementation details. You don't need to do that, int[] <-- this is what denotes that it is an array, and in particular '[]'. Why you don't want to call them simply "numbers"? If you decide at some point to change data structure they are in, you wouldn't need to rename variable, but again, that's not the main reason to change it, main reason is the clarity, what this array holds...

Try to avoid placing array type definition after variable name. That is unconventional. Plus if you read it out loud it doesn't make sense, because this parameter defines "an array of strings", not an array of args. One less known thing, if you ever decide to change it to variable arguments (so called varargs), and you would have it defined as 'String args...' - you'd get compilation error, while 'String... args' would work just fine.

! Your indentation is off again. Lines 4, 5, 22, 26, 27, 32, 33 and 36 this time (indentation level differs). Plus your braces placing style is different in TestingClass as well as in ArraysSortingMethods class - consistency is important.

It seems that everyone here is on the same page regarding my way of naming things. Point taken. I need to put effort into making it as clear as possible. Also, incosistent way of using brackets and incorrect indentation.
I've got a question regarding private constructor in my class. I understand that ArraysSortingMethods(bad name, I know) is like utility type of class so it should be static and it shouldn't be instantiated so we create private constructor to prevent it. It may be a silly question but..why do we that? Shouldn't it be obvious for the user of the class that instantiation doesn't make sense?

Liutauras Vilda wrote:Note: don't get annoyed by the things we point out (I know it can be frustrating), but the sooner you become more disciplined, the more better and less chaos in your code will be.

No worries. I am grateful for feedback I get from all of you

Note : I am addressing specific comments now but obviously everyone is more than welcomed to chime in
1 month ago

Ryan McGuire wrote:
Yes, it's perfectly valid to  leave the braces out.  If you scoff at my suggestion, continue to omit braces in such cases and ever admit to getting bitten by something like...

... I will just point and laugh.  Ok, maybe that's a bit harsh.  I'll probably have some sympathy for you, but you see my point.

You have a point. Very recently I wasted like 30mins trying to find a logical error in my code. The mistake was similiar to your code example
1 month ago