Head First Java: Chapter-5 summary
Extra-Strength methods
Developing a Class
Before building a class, we need to consider the following things.
- Figure out what the class is supposed to do.
- List the instance variables and methods.
- Write prep code for the methods.
- Write test code for the methods.
- Implement the class.
- Test the methods.
- Debug and reimplement as needed.
The following things should be written for each class.
- prep code
The prep code is the pseudo-code. It will represent the logic without stressing about syntax. Prepcode should describe what to do, not how to do it. Implementation comes later. Also, the prep code will help to develop the test code.
- test code
The test code is the code for checking whether the real code is working correctly or not.
- real code
The real code is the actual implementation of the class.
Extreme Programming (XP)
XP is a set of proven practices designed to work together. Followings are the XP rules.
- Write the test code first.
- No killer schedules; work regular hours.
- Refactor (improve the code) whenever and wherever you notice the opportunity.
- Don’t release anything until it passes all the tests.
- Set realistic schedules based on small releases.
- Keep it simple.
- Program in pairs, and move people around so that everybody knows almost everything about the code.
Test code
You need to determine what testing matters for the class. We need to identify the possibilities for all cases. So following steps should follow the writing the test cases.
Converting a string into an int.
Integer.parseInt() can be used to convert string ino integer.
The break statement
The “break” statement will get out of the loop immediately.
Generating random numbers.
To generate random numbers, the random() method of the Math class can be used.
For loop.
It is a kind of a loop. Ti is used to go through an array and do something iteratively.
1 Initialization (int I = 0)
Declare and initialize a variable. Its scope goes through the body of the loop. More variables can be initialized.
2 boolean test (i<100)
Until this test is false, code inside the loop will run.
3 Iteration expression (i++)
This operation is done after each run of the loop.
Difference between while and for loops.
While loop has only boolean test only. it is used when you don’t know how many times to loop and want to keep going while some condition is true.
Pre and Post Increment/Decrement Operator
The increment will increase the value by one, and the decrement will decrease the value by one. Post increments will reduce one after the read of that variable value. (After the operation). Pre increments will reduce one before the read of that variable value. (Before the operation)
For each loop.
The For foreach loop is another kind of loop used to do some tasks iteratively.
Compiler involving the for-each loops as follows.
- Create a String variable called name and set it to null.
- Assign the first value in nameArray to the name.
- Run the loop’s body (the code block bounded by curly braces).
- Assign the next value in nameArray to the name.
- Repeat while there are still elements in the array.
Iteration variable declaration
The type of this variable must be compatible with the elements in the array.
The actual collection
This part refers to an array or other collection.
Casting primitives
In chapter 3, I have mentioned that we can not directly assign large types to small types.
But we can force to convert larger size types into smaller types. It is called type casting.
What will happen behind the scene when casting
When converting a larger type into a small, then bits on the left side of the variable were cut off.