Java / Android Coding - Beginners Help

Soldato
Joined
28 Apr 2011
Posts
15,224
Location
Barnet, London
Some of you may have seen my other thread in which I have explained I'm attempting to learn Java, with a goal of then moving on to Android and being able to write my own apps. In my youth I did a lot of programming, but never really progressed out of Basic.

I have bought a couple of video tutorial courses on Udemy and working my way through them.

I'm making this thread for me to ask for a little help in understanding some things. At the moment I don't think it's falling into place just yet. Perhaps because I keep thinking of classes like procedures, when they are not, they are objects.

Now, doing ArrayLists I can't get my head around the structure of calling the variable from within the ArrayList. When to use 'this.' and when it's 'Customer' for the class or 'customer' for the variable and when I link these with a '.'

I'm sure it sounds silly for seasoned programmers, but maybe someone can phrase it in a way it will click with me?

Here's a bit of code for example -

Code:
package com.AndyCr15;

import java.util.ArrayList;

/**
 * Created by AndyCr15 on 01/03/2017.
 */
public class Branch {
    private String branchName;
    private ArrayList<Customer> customers;

    public Branch(String branchName, ArrayList<Customer> customers) {
        this.branchName = branchName;
        this.customers = new ArrayList<Customer>();
    }

    public String getName() {
        return branchName;
    }

    public boolean newCustomer(String customerName, double intialAmount) {
        if (findCustomer(customerName) == null) {
            this.customers.add(new Customer(customerName, intialAmount));
            return true;
        }
        return false;
    }

    public boolean addCustomerTransaction(String customerName, double amount) {
        Customer existingCustomer = findCustomer(customerName);
        if (existingCustomer != null) {
            existingCustomer.addTransaction(amount);
            return true;
        }
        return false;
    }

    private Customer findCustomer(String customerName) {
        for(int i = 0; i<this.customers.size(); i++){
            if (this.customers.get(i).getCustomerName().equals(customerName)) {
                return this.customers.get(i);
            }
        }
        return null;
    }
}

And this is Customer -

Code:
package com.AndyCr15;

import java.util.ArrayList;

/**
 * Created by AndyCr15 on 01/03/2017.
 */
public class Customer {
    private String customerName;
    private ArrayList<Double> transactions;


    public String getCustomerName() {
        return customerName;
    }

    public ArrayList<Double> getTransactions() {
        return transactions;
    }

    public Customer(String customerName, double initialAmount) {
        this.customerName = customerName;
        this.transactions = new ArrayList<Double>();
        addTransaction(initialAmount);
    }

    public void addTransaction(double amount) {
        this.transactions.add(amount);
    }

}

So the line I still look at and think 'I wouldn't have been able to recall that' is -

Code:
if (this.customers.get(i).getCustomerName().equals(customerName)) {
                return this.customers.get(i);
            }

Can anyone help me get my head round how it works?

I guess it's the whole thing about Branch in itself is a list of branches, how do I link customer lists to each branch? When do I use this. rather than just the ArrayList name. How/why do I keep just using . to link different commands together? I get confused as to when am I getting customer as a 'card of information' and when am I getting a piece of information from that card.

I suppose the weird thing is, actually all the code makes sense as I read it through, I know what each bit is doing, but I struggle to know it well enough to recall how I should use it, unless I look things up. As I said, I'm looking for it to 'click'.

Any help, or I should just keep cracking on and it will come in time? (I know in the other thread I was told to crack on, but I guess I worry I should be learning quicker.)

(Long post, thanks for taking the time to read)
 
Code:
if (this.customers.get(i).getCustomerName().equals(customerName)) {
    return this.customers.get(i);
}

To break it down a bit

Code:
this

Means 'this' instance of the class Branch

Code:
this.customers

The 'customers' ArrayList in 'this' instance of the class Branch

Code:
this.customers.get(i)

Get the Customer instance at index i from the customers ArrayList in this instance of the class Branch

Code:
this.customers.get(i).getCustomerName()

Using the Customer instance at index i from the customers ArrayList in this instance of the class Branch, get the name of the customer

Code:
this.customers.get(i).getCustomerName().equals(customerName)

Does the customer name equal that of the customer name passed into the method findCustomer true/false.

Incidentally the way you're iterating through each instance in a list to find a specific customer means you're using the wrong data structure. A Map would be more apt, but I won't complicate things with any more detail on that for now ;)
 
Thanks for the help. It all makes sense. I just can't recall and write it myself. I guess I can learn through repetition of doing it over and over. I started one like the above, but for a shoe shop, with a shoe class made of style, colour, size and price. I guess just keep making things like this and I will get the hang of it.

I've not got as far as maps yet. I'm only 22% of the way into the course. The first 15% flew by too, it's really slowed down now... so I could be doing this for some time!
 
So... I think I'm getting it as I just debugged and got working this code -

Customer is made of -

private String customerName;
private ArrayList<Double> transactions;

Code:
public void listCustomerTransactions(String name) {
        Customer currentCustomer = findCustomer(name);
        ArrayList<Double> transactions = currentCustomer.getTransactions();
        int totalTransactions = transactions.size();
        for (int i = 0; i < totalTransactions; i++) {
            double amount = transactions.get(i);
            System.out.println("Transaction " + (i+1) + " was " + amount);
        }
    }

    private Customer findCustomer(String customerName) {
        for (int i = 0; i < this.customers.size(); i++) {
            if (this.customers.get(i).getCustomerName().equals(customerName)) {
                return this.customers.get(i);
            }
        }
        return null;
    }

To visualise it, I have a rolodex of Customers. Each card in the rolodex has a customer name written on and any transactions they have made listed.

When I "Customer currentCustomer = findCustomer(name);" it effectively finds the correct card, pulls it from the rolodexand photocopies it*. I label this photocopy currentCustomer.

Then when I "ArrayList<Double> transactions = currentCustomer.getTransactions();" I've copied down the transactions from the photocopy onto a piece of paper in front of me.

When I "double amount = transactions.get(i);" I've counted through this handwritten list (starting at zero) to the i'th item on the list and made that be my variable 'amount'.

Am I visualising it right?

* does it do this? Do the transactions info come, can I access the transactions without the writing them down on paper that comes next? Or do I just get a reference of where the card is? More like a little label is stuck to the card in the rolodex with currentCustomer written on it? I still need to take the name or transactions of the card if I want to use them?
 
* does it do this? Do the transactions info come, can I access the transactions without the writing them down on paper that comes next? Or do I just get a reference of where the card is? More like a little label is stuck to the card in the rolodex with currentCustomer written on it? I still need to take the name or transactions of the card if I want to use them?

Yep, this is pretty much correct. You can reference the transactions directly without making a copy of the cutomer. For example, I can rewrite the listCustomerTransactions method to not make these copies. It means you have to go back to the rolodex and search through the whole thing again every time you want to find some data for the customer (unlike a real rolodex, it wont sit open at the right position for next time) This makes it very inefficient:

Code:
public void listCustomerTransactions(String name) {
        for (int i = 0; i < findCustomer(name).getTransactions().size(); i++) {
            System.out.println("Transaction " + (i+1) + " was " + findCustomer(name).getTransactions().get(i));
        }
    }

It may not be easy to follow what i've done. Take this line first:
Customer currentCustomer = findCustomer(name);
"currentCustomer" and "findCustomer(name)" are the same thing, so you can go through and replace all occurances of currentCustomer with findCustomer(name)
Do the same for all the other variables and that's how I got the code above.

It depends on the context, sometimes doing it this way will be quicker sometimes not.
 
Yeah, that makes sense, although I would have thought finding the customer just the once and then using that reference would be more efficient, not keep going through the rolodex each time? But yes, maybe no need to reference the transactions into a new Array. (I realise here it's not copying the transaction list of the rolodex card, it's sticking a little post-it saying here are the transactions.)

The problem I'm finding, a lot of the time people that explain Java use a lot of Java terminology when explaining it. I prefer the 'rolodex' type of explanation... (or relating it to physical actions at least)

Thanks for the help. Moving on to Inner and Abstract Classes & Interfaces now! :)
 
Yeah, that makes sense, although I would have thought finding the customer just the once and then using that reference would be more efficient, not keep going through the rolodex each time? But yes, maybe no need to reference the transactions into a new Array. (I realise here it's not copying the transaction list of the rolodex card, it's sticking a little post-it saying here are the transactions.)

The problem I'm finding, a lot of the time people that explain Java use a lot of Java terminology when explaining it. I prefer the 'rolodex' type of explanation... (or relating it to physical actions at least)

Thanks for the help. Moving on to Inner and Abstract Classes & Interfaces now! :)
Thats some good progress you're making! Interesting reading what you're coming across, ever thought of keeping this thread alive as a little mini blog type thing? Not sure if that is against forum rules.
 
Yeah, that makes sense, although I would have thought finding the customer just the once and then using that reference would be more efficient, not keep going through the rolodex each time?

Yep, in this case it is much better to store the customer in a variable but there are some cases where it might not be. For example, if you wanted to look up the customer's phone number.
Storing customer in a variable:
Code:
public void getCustomerPhoneNumber(String name) {
       Customer currentCustomer = findCustomer(name);
            System.out.println(currentCustomer.PhoneNumber);
}
Not storing customer:
Code:
public void getCustomerPhoneNumber(String name) {
            System.out.println(findCustomer(name).PhoneNumber);
}

Both do the same thing but it's probably slightly more efficient to use the second version*

*possibly, compilers are very clever these days and both of these bits of code might actually get compiled down into the same thing behind the scenes - i'm not sure how java does it.
 
Thats some good progress you're making!

Thanks. I have a very logical mind, which I think helps. I honestly wonder sometimes if I'm clever enough though! We'll see I guess :)

Interesting reading what you're coming across, ever thought of keeping this thread alive as a little mini blog type thing? Not sure if that is against forum rules.

I don't see why not. Do you mean just for interest, or maybe so it might help others setting off on the same path as me?

I've actually just bought another Udemy Course. Related to the comment above about how a tutor explains something, I thought I'd try someone else. I watched one preview video and liked how this guy explained things. Quite surprising it's only 12 hours, but 165 videos... We'll see how I get on with that one as well as the first I bought.
 
I don't see why not. Do you mean just for interest, or maybe so it might help others setting off on the same path as me?

I've actually just bought another Udemy Course. Related to the comment above about how a tutor explains something, I thought I'd try someone else. I watched one preview video and liked how this guy explained things. Quite surprising it's only 12 hours, but 165 videos... We'll see how I get on with that one as well as the first I bought.

Yea I just meant interesting to follow for the rest of us, to see your progression, might also be good motivation for you to see how you're getting on :).

I've never used Udemy, if it works for you then glad to hear it! Have you checked out coursera though? They have some great courses for free!
 
I've just signed up, but pretty much every Java course I click is £39 a month?
Ah I'm very sorry, just had a search and it seems that they've changed their payment model earlier in 2016, previously every course it had seemed to be free unless you wanted to pay a small amount for an official 'certificate' which doesn't seem the case any more with the pay wall for some of the courses.
 
I did ask in the other thread and was told don't bother with certificates. I've seen this one mentioned on sites though - Oracle Certified Associate, Java SE 8 Programmer. Any worth in working towards it?
Yea I would never advise getting one of those myself. Most people in the industry I know have degrees however so I guess if you didn't have a degree and a super small portfolio, then it could be worth it. I'd still rather advise making good side projects or real world experience over that type of cert.
 
I'm happy to work up my own portfolio, but I guess that will take a fair bit of time, so I think the qualification is better to have for me. I see it's multiple choice too, which will help me as I think I can read and understand the code a lot better than I can write it at the moment! I assume there are some mock versions I can try before, to see if I'm good enough (not now, but in time). I also note it's only 65% for a pass? Doesn't sound hard at this point?

The first course I started had me install Intellij IDEA to code with, which does seem pretty good, but the second course the guy uses Eclipse, which is the one I've heard Devs talk about in the past. I know it's rather subjective, but which is 'better' to work with?

Also, really finding this second (much shorter) course much better. It's odd, the first course I've written lots of code, worked through various challenges. The second, I've not written any code, but understanding things so much better. For example, I've just done a lesson on Static variables and methods. I had no idea what Static meant before. All makes sense now!

I think perhaps it's good to do both. On the one hand, writing the code itself is a great thing to be doing, but there are plenty of times I've not really understood why I was using the code I did. I think I'll get ahead on this second course and then 'keep up' with the first, writing code and putting into practise the things I've learnt.
 
The first course I started had me install Intellij IDEA to code with, which does seem pretty good, but the second course the guy uses Eclipse, which is the one I've heard Devs talk about in the past. I know it's rather subjective, but which is 'better' to work with?
Yea you're right that it's subjective and it's what normally people learnt with that they stick with.

If you're looking to move into Android later down the line, Android Studio which is what the large majority of android devs work with, supported by google etc. is based on Intellij IDEA so may be good for you to be comfortable in that environment, in saying that, it's not a huge leap to move from Eclipse to it later down the line if you like Eclipse.
 
Yea I would never advise getting one of those myself. Most people in the industry I know have degrees however so I guess if you didn't have a degree and a super small portfolio, then it could be worth it. I'd still rather advise making good side projects or real world experience over that type of cert.

Whilst certification may not be widely appreciated in this country I certainly wouldn't advise against it. If you have certification and experience that's certainly not going to reflect badly on you. Plus I have noticed job interview technical tests use similar questions to those seen in the certification you listed.
 
Whilst certification may not be widely appreciated in this country I certainly wouldn't advise against it. If you have certification and experience that's certainly not going to reflect badly on you. Plus I have noticed job interview technical tests use similar questions to those seen in the certification you listed.
Yea definitely agree it won't reflect badly on you, if you have no commercial experience and little side projects it can definitely be a foot in the door. Personally I'd recommend building a good portfolio over spending money on certificates specifically in development, if you're going to technical interviews then hacker rank or leet code are the best question prep (imo).

This is all in my experience/views I definitely am not saying this is the only/right way to do things, just from what I've experienced.
 
Thanks for the comments guys. Hacker Rank looks fun. Just did one of the ijntroduction tests and found it quit easy. I think that will work quite well in conjunction with my course that doesn't have me programming much. Thanks.

**EDIT** The challenges often say this -

/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

I've not come across this yet though? StdIn and StdOut?

**EDIT** So StdIn and StdOut aren't commands or variables? Just to use scanner and then system.out.println is apparently what it's after...
 
Last edited:
Back
Top Bottom