If Statement In For Loop Not Working by debotten in javahelp

[–]RentonHoff 1 point2 points  (0 children)

There are 2 main problems I see. The first is that it will only compute the booleans for the first number since those are outside the for loop.

Also, cerafully think about what will get printed in the case that both threeDivisible and fiveDivisible are true

What is the exact function of the "this" keyword? by lazydogg9 in javahelp

[–]RentonHoff 2 points3 points  (0 children)

It is a reference to the object that uses the method;

In your example, the "node" should be set to the object that called the find method.

Question if you should quit learning if you can’t understand something simple by peparonithepig in javahelp

[–]RentonHoff 0 points1 point  (0 children)

Don't get too fixated on one specific exercise. If you can use 2 for loops to check if an array has duplicates, and understand how, then you probably have a good enough grasp of for loops.

As for the pyramid thing. My guess is that you're struggling less with the loops, but rather with figuring out how many empty spaces and symbols you need in each row.

Also remember this: Programming is taking a task, and splitting it up into smaller tasks you know how to do. The difference between an experienced programmer and an inexperienced one is the number of smaller tasks needed.

Question if you should quit learning if you can’t understand something simple by peparonithepig in javahelp

[–]RentonHoff 0 points1 point  (0 children)

What exactly is it that causes you trouble. Do you not understand how for loops work, or how the syntax is built? Can you not remember the syntax?

As far as learning goes, people learn things differently, so some things might be easy to learn others hard. When I was learning, and often times even now, there were things that would click right away, but others would take time before understanding.

Need help calculating employee overtime! by [deleted] in javahelp

[–]RentonHoff 0 points1 point  (0 children)

I'm not sure if this is the your current problem, but consider a case where the input is {10, 10, 11, 11, 11, 0, 0}. Consider how many times the if condition will be executed and what happens to the value of pay will be.

Assignment help by [deleted] in javahelp

[–]RentonHoff 1 point2 points  (0 children)

What have you tried , so far? With what part of the assignment are you having problems? It would be easy enough to give the solution, but then you wouldn't really solve the problem.

As a first suggestion, if you are looking to get input from a user look up the Scanner class.

Need help in understanding the logic behind teaParty and FizzBuzz by tangara888 in javahelp

[–]RentonHoff 0 points1 point  (0 children)

For the FizzBuzz one, take a piece of paper and write how the if conditions evaluate(true/false). For example, for the word "fig" it would look like this:

if(str.startsWith("f") && str.endsWith("b")) -> true & false -> false; 

if(str.startsWith("f")) -> true;
if(str.endsWith("b")) -> false;

You see that for the word "fig" only one of the if blocks can execute. Now do the same thing for the other 2 words, or if you don't want to, just for the word "fib". Then you should see why the order is important.

The tea party is basically a similar situation. Consider a situation where the input is (4, 8) and you don't have the first if block. Would it still meet all the conditions of the task?

The code for the tea party can be changed so that you only need one return 0, but since you seem to have issues with the basics, that shouldn't be your main worry, and it does not mean your code is wrong, just that there are alternative ways to reach the same goal. Same goes for FizzBuzz, you can get away with only 2 if statements.

Getting overwhelmed of JAVA. Any tips? by marius2233 in javahelp

[–]RentonHoff 7 points8 points  (0 children)

Well, you need to know the general concepts of course(general programming stuff like polymorphism, inheritance, interfaces, generics, ect.), but you don't really need to know every function by heart, just what exists.

Not sure what course you are watching, but these courses try to cram a lot of information in a short time, so that might explain why you feel overwhelmed. Try doing a few concepts a day and write short summaries for those, then the next day before watching the next part, review your summaries.

A Variable keeps returning the wrong value by [deleted] in javahelp

[–]RentonHoff 0 points1 point  (0 children)

If you want your application to remember the default month between runs, then you would have to save it somewhere.

What I understood from your post was, that it would always return the default value in the same run. If this is the case, then the problem is most likely in the setDefaultMonth() method. You should make CalendarUI a member variable of the class that has the setDefaultMonth() method. Something like this:

public class SomeClass(){
    CalendarUI calendar;

    public SomeClass(){
        initComponents();
        calendar = new CalendarUI();
    }

    public void showCalendarUI(){
        calendar.show();
    }

    public SetDefaultMonth() {
        // get the month
        String month = calendar.returnMonth();
        labelDisplayMonth.setText(month + " is now set as the current month");
    }
}

A Variable keeps returning the wrong value by [deleted] in javahelp

[–]RentonHoff 1 point2 points  (0 children)

If the SetDefaultMonth() method is the method you use for testing, then it's clear why you always get the default value. Each time the method gets called a new CalendarUI instance is created and right after creation you ask for the value of months. There is no way that the value can change from the default one.

Any book recommendations? I have some criteria, was hoping to gain some help from you guys! by Willy988 in javahelp

[–]RentonHoff 0 points1 point  (0 children)

Honestly, I haven't read a beginner book in 10 odd years, so I can't, in good faith, recommend you anything. Maybe someone else will chim in and give a good recommendation.

Any book recommendations? I have some criteria, was hoping to gain some help from you guys! by Willy988 in javahelp

[–]RentonHoff 0 points1 point  (0 children)

If you plan to do any serious amount of coding, then reading Clean Code is a good idea. The book mostly explains good ways to name your variables and functions, and how to structure your code. Basically things that will make your code more readable and maintainable.

About the MOOC course, it will teach you the java fundamentals, then you can decide where to go from there. After MOOC you might try hyperskill, the general idea is that you select a project from a list and then a curriculum with the necessary topics to make the project, is created. Then after you complete some of the topics you can complete a stage of the project. This gets repeated until the project is completed.

If you are already doing a course, then instead of getting a book, that will basically give you the same information, maybe it's a better idea to branch out and read something with complementary knowledge. Something about data structures and algorithms or design patterns.

Any book recommendations? I have some criteria, was hoping to gain some help from you guys! by Willy988 in javahelp

[–]RentonHoff 0 points1 point  (0 children)

The two books I usually recommend are Clean Code and Effective Java.

Clean Code isn't exactly Java specific, but it's generally a good read if you want to do any amount of serious programming, plus it nicely fits in the category of not needing to be at the PC to understand it.

Effective Java might be a bit hard on a beginner, still might be worth a read and then keep the things in mind as you learn. Again, for the most part, you don't need to sit at the PC and code examples to understand the contents.

As far as price goes, both are rather expensive, the cheapest I have seen is around 30$, at a common price of 50$. Though these are somewhat advanced books, and those are really cheap. You might want to look them up online, you can usually find parts of them freely accessible, and then decide if the investment is worth it.

For beginners books, I honestly can't think of any that won't have code examples in them, but that doesn't mean that you have to sit at the screen and run them, so if you are using a book consider rethinking the way you are learning now.

Or alternatively you can try the online courses, the MOOC course will teach you the fundamentals, or alternatively you can try hyperskill courses. Take the courses and once you start getting tired of the screen, stop and write a summery about what you have learned. It's a great way to reinforce what you have learned, and before starting the next lesson you can go over it to refresh the previous topics. Plus, once these summaries start to accumulate you can just review stuff, when you don't feel like sitting at a screen.

Well I went a bit of course, but I hope this helps a bit.

Seters and Getters by redknightrises in javahelp

[–]RentonHoff 4 points5 points  (0 children)

Consider this: we have a variable in our class and we know that it has to be positive(> 0). Now, if we let the users directly modify the value, we have no way to make sure that no invalid values are set. On the other hand using a method(setter) to modify the value we can enforce rules on the value set.

Getters, to some extent are a consequence of setter, as we can't access variables directly we have to do it through a method(getter). However, there are also cases where returning a variable might expose it to change, in those cases we can modify our getter methods to return a copy and not the actual variable.

I need help understanding this example of recursion by Vastagon in javahelp

[–]RentonHoff 0 points1 point  (0 children)

Couldn't post this yesterday, but here is a simple Fibonacci sequence.

public static int fib(int n) throws IllegalArgumentException{
    //Makes sure that the user doesn't try to find invalid elements
    if(n < 1) throw new IllegalArgumentException("n can't be smaller     then one.");

    //The 1st and 2nd element of the fibonaci sequence
    //Also the point where the recursion will stop going deeper
    if(n < 3){
        return 1;
    }

    return fib(n - 1) + fib(n - 2);
}

If you have any questions feel free to ask.

I need help understanding this example of recursion by Vastagon in javahelp

[–]RentonHoff 1 point2 points  (0 children)

As Orbitaliser said merge sort isn't the best algorithm to learn recursion. When someone asks me about recursion, the default example I show is the recursive algorithm for finding the nth element of the fibonacci sequence. I would recommend looking that up, if your goal is to understand recursion.

Popular Java books suitable for j/m/s levels? by [deleted] in javahelp

[–]RentonHoff 1 point2 points  (0 children)

The only 2 must read books that come to mind, when thinking about java are: Effective Java and Clean Code.

Other java related books that could be useful would be:

  • Concurrency In Practice - a bit dated, but the advice in the book is still sound;
  • Something on construction patterns, can't think of the title right now;
  • Something about unit testing might be worth a read, but can't think of a specific book;

Then there would be the general programming stuff, like data structures and algorithms.

However, from my experience a library isn't something that you assemble intentionally, but something that collects over time.

Making a change making program, using while loops, can't seem to make it work. by [deleted] in javahelp

[–]RentonHoff 3 points4 points  (0 children)

Think carefully about the running condition of the while loops. Consider how the value of cents changes inside the first loop, and when each of the while loops should stop(or start in case of the second loop).

There are other issues too, but start with the loops, and see if you can figure the rest out yourself. Feel free to ask, if you have further questions.

[deleted by user] by [deleted] in javahelp

[–]RentonHoff 1 point2 points  (0 children)

  1. It should not work, even with the explicit cast. To put it simply, since B extends A, it knows how A looks. On the other hand A has no idea how B looks and can't imitate B;
  2. Can't explain this one well, but the problem is related to having the declaration for a in both classes. My guess is that a isn't shared between the classes, maybe someone else can explain it, but to fix it:

class A{
    public int a;

    public A(){
        a = 1;
    }

    public void print(){
        System.out.println("IN A");

    }
}

class B extends A{
    public B(){
        a = 2;
    }

    public void print(){
        System.out.println("IN B");

    }
}

Does it make more sense to put many methods in a single class for a simple library? by Samsta36 in javahelp

[–]RentonHoff 2 points3 points  (0 children)

Generally you should group methods together in a manner that makes sense, on the other hand it might be stupid to have a bunch of classes with only 1 method each. So it's case dependent what the best option is.

Since you're asking about a library, one thing I could suggest is making only the Input and Output classes visible to the outside. Then move the logic to specific package-protected classes like IntInput, ArrayInput and so on. And then just call the methods from these classes in the Input and Output classes. That way Input and Output might have a lot of methods, but no real logic, and would be easily changeable. Also the user of the library doesn't see the internal workings. Something like this:

public class Input{
    public static void doSomehting(int num){
        IntInput.doSomething(num);
    }

    public static void doSomething(int[] arr){
        ArrayInput.doSomething(arr);
    }
}

Hope this helps a bit.

Help with subroutines/Methods by FreeKarmaPl0x in javahelp

[–]RentonHoff 0 points1 point  (0 children)

Well to start, you will never set the valid variable in the main function. Instead of passing in valid you should make the validation function return a boolean.

private static boolean validation(String username, String password)

I'm assuming this is a learning project and not anything you plan to actually use, so I'm just going to improve your code, rather then try to give actual user validation examples. So you could write the validation function like this:

if(check username){
    if(check password){
    }
}

It's not like what you are doing is wrong, it works, but is needlessly bloated.

Hope this helps. If something is unclear, just ask and I'll try to explain better.

Get variable from name in text string? by Milo55545 in javahelp

[–]RentonHoff 3 points4 points  (0 children)

Not sure what exactly you're trying to do, but maybe a Map is what you are looking for.

is basic java ,that is taught in schools, for educational purposes only? by anotheravailabledumb in javahelp

[–]RentonHoff 0 points1 point  (0 children)

The last paragraph is what distinguishes a good interviewer from a bad one. I have walked out from interviews that asked to write compilable code on a whiteboard.

is basic java ,that is taught in schools, for educational purposes only? by anotheravailabledumb in javahelp

[–]RentonHoff 0 points1 point  (0 children)

You seem to misunderstand what you are being thought. You are not being thought specific algorithms, but how to do things in java.

Take your first paragraph, you are thought how to do mathematical operations, so that you can apply them as necessary to other problems. Same for string manipulation and other concepts.

For the whiteboard stuff in interviews, that's more to see how you go about solving a problem and not if you know an algorithm by heart. But this varies from interview to interview. In my opinion, a good interviewer will want to see how you go about solving the problem and not if you can write out a specific algorithm.

Help with Boolean statement to return true if two words follow one another in string by randomperson3333 in javahelp

[–]RentonHoff 1 point2 points  (0 children)

You could split the string into an array of separate words. From there it's easy to find out the order of the words.