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 “foreach” loop
The “foreach” 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
ArrayListofStringvalues. - Returns the longest
Stringword. - If the
ArrayListis 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.
Hint: Have a look at the available String methods in the Java API documentation for String. Can you find a method that helps you determine the length of a String?
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.
<hr class=”thin-hr>
Hints: Have a look at the available String methods in the Java API documentation for String.
- Can you find a method that helps you split a
Stringinto parts? How are these parts returned? - Can you find a method that removes empty spaces from the beginning and end of a
String? - Can you find a method that helps you determine if a
Stringis empty?
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
dayToCheckto another weekday, e.g.WEDNESDAY. Seems fine, right? - Change the value of
dayToCheckto a weekend, e.g.SATURDAY. Uh oh, why couldn’t it findSATURDAY? Looks like theif-elseconditions 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
dayToCheckto23. 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
intdays. - Uncomment the line that declares the
enum Day ... - Change the type of
dayToChecktoDayinstead ofint, and the value ofdayToCheckto something likeDay.MONDAY. - Instead of the complicated
if-else-if..., replace this with an elegantswitchstatement. 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
cases one after another (MONDAYtoFRIDAY), only placing abreakstatement 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
dayToCheckin 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.
