Good Coding Principles:
1. mark your variables private, provide accessor and mutator methods (aka. getters and setters for access). For example:
public class Foo{
private String name;
public String getName(){return name;}
public void setName(String name){ this.name=name;}
}
2. Naming Conventions:
The Java convention is:
* Constants are named in all capital letters:
e.g. final static String UNIVERSITY_NAME;
* Classes are named with capital first letter: e.g BankAccount, not bankAccount
* Methods start with a lower case first letter, subsequent words are capitalized: e.g. getBankAccount();
* Variable names start with lower case letters:
Common conventions are:
o int bankAccountNumber
o int bank_account_number
o int nBankAccountNumber
3. Testing: For each class, provide a static method that calls all methods implemented in that class—this provides an easy way for you and other developers to verify the integrity of your code
142 review:
Loops: for, while, do-while…
Arrays: 0-indexed; maximum accessible item is at index size-1, where size is the array size
arrays vs ArrayList:
ArrayList: provides multiple methods. Takes items of type object, so it is possible to store different types in it. Adding items on to the end of the ArrayList has low cost. Downside: when retrieving items from the ArrayList, we get out Object instances. To use them as a concrete class, we need to explicitly downcast:
ArrayList myArrayList=new ArrayList();
Student s=new Student(“Yana”);
myArrayList.add(s);
String studentName=((Student) myArrayList.get(0)).getName();
Downcasting is risky, especially if you have items of different types in the ArrayList.
Arrays: Only contain items of the specified array type (or its subclasses—we’ll revisit this point). Requires no downcasting. Could be used with built-in Java algorithms for sorting.