More Java Fundamentals
In the previous lesson, we looked at the most basic programming building blocks.
To do cool things, we need a few more powerful features to support us.
Remember: even though we are using Java here, a lot of these ideas exist in other programming languages.
Videos to watch
Aim to watch all of these, they’re pretty important skills to have under your belt (especially the ones tagged with and ).
Understanding the break
statement
The break
statement sometimes comes in handy, used inside for
loops, while
loops, and switch case
statements.
Understanding the continue
statement
The continue
statement is less common, used inside for
loops and while
loops.
Feel free to ignore this one if you can’t be bothered watching it (but it’s worth knowing it’s there).
Understanding the switch case
statement
Instead of if else if else if ...
statements, this is often a cleaner way to write code.
Don’t worry about it if you’re feeling overwhelmed, you can always come back to this one later once you’re more comfortable with the other concepts.
Understanding String
methods
There are a lot of very useful built-in String
methods available for us to use:
- Java API documentation for
String
- Don’t worry, you don’t have to memorise them!
- You will get to know a lot of them as you progress in the course.
- What’s most important, is feeling comfortable to read such documentation to look things up as you need them.
Understanding the return
statement
The return
statement is used a lot in programming, and is an important concept to understand.
Understanding the ArrayList
The ArrayList
is an improvement over arrays, and is an essential tool in programming.
You’ll use this a lot in your programming, to the extent that you’ll want to memorise the syntax and key steps to using them (especially by the time your Test comes around).
It contains a lot of very useful methods:
- Java API documentation for
ArrayList
- Again, you don’t have to memorise them! Just know how to read the documentation, and look things up as you need them.
- Remember: Whenever we use an ArrayList, we need to add the statement
import java.util.ArrayList;
at the top of our source file.
Understanding the “for
each” loop
The “for
each” loop is an improvement over the traditional “for
” loop.
This one is very useful, and you will use it a lot in your programming.
Understanding enum
An enum
is an elegant way of representing some group of values.
You’ll see this quite a bit in the course.
Get your hands dirty!
Edit the code below:
- Complete the
longestWord()
method, which:- Has a parameter that is an
ArrayList
ofString
s. - Returns the longest
String
word. - If the
ArrayList
is empty, it returnsnull
; - If there are ties for the longest word, the first of the ties is returned.
- Has a parameter that is an
Test your code by using different content inside the ArrayList
, to make sure you account for all the scenarios.
Get your hands dirty!
Edit the code below:
- Complete the
countKeywords()
method, which:- Has a parameter that is a
String
.- The parameter has zero or more semi-colons (
;
).
- The parameter has zero or more semi-colons (
- Returns the number of keywords, as separated by
;
.- Keywords must have at least one non-empty character.
- Has a parameter that is a
Test your code by using different content for the input String
, to make sure you account for all the scenarios.
Get your hands dirty!
Consider the code below.
We are currently using int
constants to represent the 7 days of the week. Constants are variables that are declared as final
, so their values don’t change. But we have so many problems with the code written in this way!
- Try to get the gist of what the code is trying to do. Run it, and observe the output.
- Change the value of
dayToCheck
to another weekday, e.g.WEDNESDAY
. Seems fine, right? - Change the value of
dayToCheck
to a weekend, e.g.SATURDAY
. Uh oh, why couldn’t it findSATURDAY
? Looks like theif-else
conditions have an error from copy-pasting! - Fix the code so it works to print out weekends. Great, it’s working now for all 7 days of the week.
- Now set
dayToCheck
to23
. There is no 23rd day of the week! It’s a bit troubling that our program even allows us to enter invalid values for the day!
Bring in the enum
!
- Delete the line that currently declares the
int
days. - Uncomment the line that declares the
enum Day ...
- Change the type of
dayToCheck
toDay
instead ofint
, and the value ofdayToCheck
to something likeDay.MONDAY
. - Instead of the complicated
if-else-if...
, replace this with an elegantswitch
statement. You can make it even more elegant with only twoSystem.out.println()
statements in total! One for weekday, and one for weekend.- You can pile the
case
s one after another (MONDAY
toFRIDAY
), only placing abreak
statement after processingFRIDAY
. This way, all 5 weekday values get treated by the same piece of code. Then do the same for the weekend days. - You might need to refer to the values as
Day.MONDAY
,Day.TUESDAY
, etc. - You can print the actual day, by including
dayToCheck
in the print statement.
- You can pile the
- Now try to set some invalid value to
dayToCheck
. It won’t let you!
Other excellent online Java tutorials
Each of the following break down a lot of the coding into digestible sections. Pick and match what you need.