Introduction
Break and Continue Statements
Break statements are useful tools when manipulating the order of execution within loops. The keyword break causes an immediate exit of the loop.
Continue statements cause an immediate jump to the loop condition check. If in a for loop, it will jump to the next iteration. If in a while loop, it will jump to the condition and check whether true.
Nested For Loops
Nested For Loops are loops that appear in the body of another loop.
1
2
3
4
5
for(int i = 0; i < 3; i++){ // This is the outer loop
for(int j = 0; j < 5; j++){ // This is the inner loop
System.out.print(i + "." + j + " ");
}
}
For example, the above code would output:
Notice that in one iteration of the outer loop, the inner loop completes all iterations. In other words, the inner loop runs all the way through each time the outer loop runs through.
Nested for loops come in handy if you are counting multiple variables at once, as well as with other types of data that you’ll see later in this course, like arrays.
Do-While Loops
Do-while loops are similar to while loops, but the order in which the condition is checked differs.
In while loops, the condition is checked before entering the loop. In do-while loops, the code within the loop executes once before the condition is checked.
For some examples of do-while loops, you can check out the Java Docs explanation here, or these examples from an outside resource.
For Today’s Lab
In this lab, you will working with various methods that handle different operations with numbers, as well as user and file input.
Each method you write has an associated test method already written for you. After completing each method, please call the test method from main to ensure your methods are working properly.
Step 1: getAreaCode(String phoneNumber)
Review
Last lab, you worked with branching and common classes.
To review this, you will write a method that, given a phone number, will return the three-digit area code. Given a String with a phone number in the format “***-***-****” or “***-****”. Phone numbers are typically represented as seven digit numbers, with or without a three-digit area code. Between those digits are dashes.
For this method, you will check to see if the string given has an area code or not (is seven digits plus the dash, or 10 with dashes).
If the area code is present, return it using the relevant String and Integer class methods.
If the area code is not present, return the default, which for Fort Collins, is 970.
You will write the method signature from scratch, but here are a few specifications:
- The method is self-contained and doesn’t rely on an instance of the class. What keyword should you use?
- The method returns an int.
- The method is called getAreaCode.
- The method takes one parameter, a string, for the phone number.
Step 2 : multiplicationTable(int rows, int columns)
Multiplication Using Nested For Loops
Write a method that returns a multiplication table based on two input values that specify what two ranges of numbers to multiply together. For example, if the method were given 3 and 4 as input, it would return a string that when printed, would look like this:
Output Requirements:
- Each number must be followed by a tab character.
- Each line must be followed by a new line character (including the last row).
- The columns and rows should range from 1 to the input number.
The method signature should look as follows:
Hint: Review string formatting, as it may be helpful in returning the correct string from the method.
Call the testMT() method from main after completing this method to ensure it is working as expected.
Step 3: bruteForceEquationSolver(int one, int two, int three, int constant)
Brute Force Equation Solver using Nested For Loops
Brute force algorithms are a method of finding a solution to try all possibilities until a solution is found. They do not consider the most efficient path, but systematically test possibilities until one works. For more information about brute force algorithms, check out this outside resource.
In this lab, you will be writing your own method using the brute force algorithm, to solve a second degree polynomial.
This method will take in four parameters, three coefficients, and one constant, and it will have the following signature:
The equation you are trying to solve is of the following format, given the parameters stated above:
X, Y, and Z are variables that yoo are finding solutions for.
For example, if you were to call the method with parameters (2, 3, 4, 42), you would be finding the solution for the equation 2x + 3y + 4z = 42.
Some specifications for the method:
- Your method should try every possibility within the range (1-10) for each variable. Check possibilities using nested for loops, starting with x, then y, then z.
-
Your method should return if a solution is found, and return a string with the solutions in the following format:
x: xSolution y: ySolution z: zSolution
There should be a space between the solution and the next variable, and each variable letter should be followed by a colon and a space.
- If no solution is found, return the string “Solution not found.”.
Call the testBFES() method from main after completing this method to ensure it is working as expected.